fix: output yaml not pretty

This commit is contained in:
Daniel Ding
2022-09-25 00:45:09 +08:00
parent 42dfbe5cb4
commit b099e325f0
21 changed files with 103 additions and 116 deletions

View File

@@ -73,6 +73,7 @@ func OutTable(data interface{}, tmpl string) error {
}
func Out(data interface{}, format string, tmpl string) error {
libol.Debug("Out %s %s", format, tmpl)
switch format {
case "json":
return OutJson(data)

View File

@@ -7,7 +7,6 @@ import (
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/schema"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"path/filepath"
)
@@ -27,14 +26,13 @@ func (u Config) List(c *cli.Context) error {
clt := u.NewHttp(c.String("token"))
cfg := &config.Switch{}
if err := clt.GetJSON(url, cfg); err == nil {
var data []byte
if c.String("format") == "yaml" {
data, _ = yaml.Marshal(cfg)
format := c.String("format")
if format == "yaml" {
cfg.Format()
} else {
data, _ = libol.Marshal(cfg, true)
format = "json"
}
fmt.Println(string(data))
return nil
return u.Out(cfg, format, "")
} else {
return err
}
@@ -165,10 +163,7 @@ func (u Config) Commands(app *api.App) {
Name: "list",
Usage: "Display all configuration",
Aliases: []string{"ls"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "format", Value: "json"},
},
Action: u.List,
Action: u.List,
},
{
Name: "check",

View File

@@ -29,7 +29,7 @@
"auth": "a263d01a96db11eb9",
"crypt": "9b73bc48e3864b3ebc"
},
"policy": [
"policies": [
{
"source": "192.168.1.0/24",
"destination": "192.168.2.0/24"

View File

@@ -3,5 +3,7 @@
"connection": "who.openlan.net",
"username": "hi@default",
"password": "cb2ff088a34d",
"cert": { "insecure": true }
"cert": {
"insecure": true
}
}

View File

@@ -1,12 +1,11 @@
{
"network": "default",
"interface": {
"name": "tap0",
"bridge": "br-default",
"address": "172.32.100.10/24"
},
"connection": "who.openlan.net",
"username": "hi",
"username": "hi@default",
"password": "1f4ee82b5eb6",
"protocol": "tls",
"cert": {

View File

@@ -29,7 +29,7 @@
"ldap": {
"server": "ldap-server.net:389",
"bindDN": "cn=admin,dc=openlan,dc=com",
"password": "your-passowrd",
"bindPass": "your-passowrd",
"baseDN": "dc=openlan,dc=com",
"attribute": "cn",
"filter": "(cn=%s)"

View File

@@ -7,8 +7,8 @@ type Bridge struct {
IPMtu int `json:"mtu,omitempty" yaml:"mtu,omitempty"`
Address string `json:"address,omitempty" yaml:"address,omitempty"`
Provider string `json:"provider,omitempty" yaml:"provider,omitempty"`
Stp string `json:"stp,omitempty" yaml:"stpState,omitempty"`
Delay int `json:"delay,omitempty" yaml:"forwardDelay,omitempty"`
Stp string `json:"stp,omitempty" yaml:"stp,omitempty"`
Delay int `json:"delay,omitempty" yaml:"delay,omitempty"`
Mss int `json:"tcpMss,omitempty" yaml:"tcpMss,omitempty"`
}

View File

@@ -30,11 +30,11 @@ func SetLocalUdp(port string) {
type EspState struct {
Local string `json:"local,omitempty" yaml:"local,omitempty"`
LocalIp net.IP `json:"local_addr" yaml:"local_addr"`
LocalIp net.IP `json:"local_addr" yaml:"localAddr"`
Remote string `json:"remote,omitempty" yaml:"remote,omitempty"`
RemotePort int `json:"remote_port" yaml:"remote_port"`
RemoteIp net.IP `json:"remote_addr" yaml:"remote_addr"`
Encap string `json:"encap,omitempty" yaml:"encapsulation,omitempty"`
RemotePort int `json:"remote_port" yaml:"remotePort"`
RemoteIp net.IP `json:"remote_addr" yaml:"remoteAddr"`
Encap string `json:"encapsulation" yaml:"encapsulation"`
Auth string `json:"auth,omitempty" yaml:"auth,omitempty"`
Crypt string `json:"crypt,omitempty" yaml:"crypt,omitempty"`
}
@@ -90,7 +90,7 @@ func (s *EspState) Correct(obj *EspState) {
type ESPPolicy struct {
Source string `json:"source,omitempty"`
Dest string `json:"destination,omitempty"`
Dest string `json:"destination,omitempty" yaml:"destination"`
Priority int `json:"priority"`
}
@@ -107,7 +107,7 @@ type ESPMember struct {
Peer string `json:"peer"`
Spi int `json:"spi"`
State EspState `json:"state"`
Policies []*ESPPolicy `json:"policies" yaml:"policies,omitempty"`
Policies []*ESPPolicy `json:"policies" yaml:"policies"`
}
func (m *ESPMember) Correct(state *EspState) {
@@ -178,9 +178,9 @@ func (m *ESPMember) RemovePolicy(dest string) bool {
type ESPSpecifies struct {
Name string `json:"name"`
Address string `json:"address,omitempty"`
State EspState `json:"state" yaml:"state,omitempty"`
State EspState `json:"state,omitempty" yaml:"state,omitempty"`
Members []*ESPMember `json:"members"`
Listen string `json:"listen,omitempty"`
Listen string `json:"listen,omitempty" yaml:"listen,omitempty"`
}
func (n *ESPSpecifies) Correct() {

View File

@@ -3,9 +3,9 @@ package config
type FabricSpecifies struct {
Mss int `json:"tcpMss,omitempty"`
Fragment bool `json:"fragment"`
Driver string `json:"driver,omitempty"`
Driver string `json:"driver,omitempty" yaml:"driver,omitempty"`
Name string `json:"name"`
Tunnels []*FabricTunnel `json:"tunnels"`
Tunnels []*FabricTunnel `json:"tunnels" yaml:"tunnels"`
}
func (n *FabricSpecifies) Correct() {
@@ -53,7 +53,7 @@ func (n *FabricSpecifies) DelTunnel(remote string) bool {
}
type FabricTunnel struct {
DstPort uint32 `json:"dport"`
DstPort uint32 `json:"dport" yaml:"destPort"`
Remote string `json:"remote"`
Local string `json:"local,omitempty" yaml:"local,omitempty"`
Mode string `json:"mode,omitempty" yaml:"mode,omitempty"`

View File

@@ -12,7 +12,7 @@ type FlowRule struct {
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Proto string `json:"protocol,omitempty" yaml:"protocol,omitempty"`
Match string `json:"match,omitempty" yaml:"match,omitempty"`
DstPort string `json:"dport,omitempty" yaml:"dstPort,omitempty"`
SrcPort string `json:"sport,omitempty" yaml:"srcPort,omitempty"`
DstPort string `json:"dport,omitempty" yaml:"destPort,omitempty"`
SrcPort string `json:"sport,omitempty" yaml:"sourcePort,omitempty"`
Jump string `json:"jump,omitempty" yaml:"jump,omitempty"` // SNAT/RETURN/MASQUERADE
}

View File

@@ -2,10 +2,10 @@ package config
type LDAP struct {
Server string `json:"server"`
BindDN string `json:"bindDN"`
BindPass string `json:"bindPass"`
BaseDN string `json:"baseDN"`
BindDN string `json:"bindDN" yaml:"bindDN"`
BindPass string `json:"bindPass" yaml:"bindPass"`
BaseDN string `json:"baseDN" yaml:"baseDN"`
Attribute string `json:"attribute"`
Filter string `json:"filter"`
EnableTls bool `json:"enableTLS"`
Tls bool `json:"tLS"`
}

View File

@@ -7,11 +7,11 @@ import (
)
type Network struct {
ConfDir string `json:"-"`
ConfDir string `json:"-" yaml:"-"`
File string `json:"file"`
Alias string `json:"-" yaml:"-"`
Name string `json:"name,omitempty" yaml:"name"`
Provider string `json:"provider,omitempty" yaml:"provider"`
Name string `json:"name" yaml:"name"`
Provider string `json:"provider,omitempty" yaml:"provider,omitempty"`
Bridge *Bridge `json:"bridge,omitempty" yaml:"bridge,omitempty"`
Subnet *IpSubnet `json:"subnet,omitempty" yaml:"subnet,omitempty"`
OpenVPN *OpenVPN `json:"openvpn,omitempty" yaml:"openvpn,omitempty"`
@@ -21,7 +21,7 @@ type Network struct {
Acl string `json:"acl,omitempty" yaml:"acl,omitempty"`
Specifies interface{} `json:"specifies,omitempty" yaml:"specifies,omitempty"`
Dhcp string `json:"dhcp,omitempty" yaml:"dhcp,omitempty"`
Outputs []Output `json:"outputs" yaml:"output,omitempty"`
Outputs []Output `json:"outputs" yaml:"outputs"`
}
func (n *Network) Correct() {

View File

@@ -16,18 +16,17 @@ type OpenVPN struct {
Device string `json:"device"`
Version int `json:"version"`
Auth string `json:"auth"` // xauth or cert.
DhPem string `json:"dhPem"`
DhPem string `json:"dhPem" yaml:"dhPem"`
RootCa string `json:"rootCa"`
ServerCrt string `json:"cert"`
ServerKey string `json:"key"`
TlsAuth string `json:"tlsAuth"`
ServerCrt string `json:"cert" yaml:"cert"`
ServerKey string `json:"key" yaml:"key"`
TlsAuth string `json:"tlsAuth" yaml:"tlsAuth"`
Cipher string `json:"cipher"`
Routes []string `json:"-"`
Renego int `json:"renego,omitempty"`
Script string `json:"-"`
Breed []*OpenVPN `json:"breed,omitempty"`
Push []string `json:"push,omitempty"`
Clients []*OpenVPNClient `json:"clients,omitempty"`
Routes []string `json:"-" yaml:"-"`
Renego int `json:"renego,omitempty" yaml:"renego,omitempty"`
Script string `json:"-" yaml:"-"`
Push []string `json:"push,omitempty" yaml:"push,omitempty"`
Clients []*OpenVPNClient `json:"clients,omitempty" yaml:"clients,omitempty"`
}
type OpenVPNClient struct {
@@ -109,7 +108,4 @@ func (o *OpenVPN) Correct(obj *OpenVPN) {
value, _ := strconv.Atoi(port)
o.Subnet = fmt.Sprintf("%s.%d.0/24", pool, value&0xff)
}
for _, ch := range o.Breed {
ch.Correct(o)
}
}

View File

@@ -1,7 +1,7 @@
package config
type Password struct {
Network string `json:"network,omitempty"`
Network string `json:"network,omitempty" yaml:"network,omitempty"`
Username string `json:"username"`
Password string `json:"password"`
}

View File

@@ -10,7 +10,7 @@ import (
type Interface struct {
Name string `json:"name,omitempty"`
IPMtu int `json:"mtu"`
IPMtu int `json:"mtu" yaml:"mtu"`
Address string `json:"address,omitempty"`
Bridge string `json:"bridge,omitempty"`
Provider string `json:"provider,omitempty"`
@@ -31,14 +31,14 @@ type Point struct {
Http *Http `json:"http,omitempty"`
Crypt *Crypt `json:"crypt,omitempty"`
PProf string `json:"pprof,omitempty"`
RequestAddr bool `json:"requestAddr,omitempty"`
RequestAddr bool `json:"requestAddr,omitempty" yaml:"requestAddr,omitempty"`
ByPass bool `json:"bypass,omitempty"`
SaveFile string `json:"-"`
SaveFile string `json:"-" yaml:"-"`
Queue *Queue `json:"queue,omitempty"`
Terminal string `json:"-"`
Terminal string `json:"-" yaml:"-"`
Cert *Cert `json:"cert,omitempty"`
StatusFile string `json:"status,omitempty"`
PidFile string `json:"pid,omitempty"`
StatusFile string `json:"status,omitempty" yaml:"status"`
PidFile string `json:"pid,omitempty" yaml:"pid,omitempty"`
}
func DefaultPoint() *Point {

View File

@@ -11,7 +11,7 @@ type ShadowProxy struct {
Cipher string `json:"cipher,omitempty"`
Password string `json:"password,omitempty"`
Plugin string `json:"plugin,omitempty"`
PluginOpts string `json:"pluginOpts,omitempty"`
PluginOpts string `json:"pluginOpts,omitempty" yaml:"pluginOpts,omitempty"`
Protocol string `json:"protocol,omitempty"`
}

View File

@@ -1,7 +1,7 @@
package config
type IpSubnet struct {
Network string `json:"network,omitempty"`
Network string `json:"network,omitempty" yaml:"network,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
Netmask string `json:"netmask,omitempty"`
@@ -13,17 +13,17 @@ type MultiPath struct {
}
type PrefixRoute struct {
File string `json:"file,omitempty"`
Network string `json:"network,omitempty"`
File string `json:"-" yaml:"-"`
Network string `json:"network,omitempty" yaml:"network,omitempty"`
Prefix string `json:"prefix"`
NextHop string `json:"nexthop"`
MultiPath []MultiPath `json:"multipath,omitempty"`
Metric int `json:"metric"`
Mode string `json:"mode" yaml:"forwardMode"` // route or snat
Mode string `json:"forward,omitempty" yaml:"forward,omitempty"` // route or snat
}
type HostLease struct {
Network string `json:"network"`
Network string `json:"network,omitempty" yaml:"network,omitempty"`
Hostname string `json:"hostname"`
Address string `json:"address"`
}

View File

@@ -73,14 +73,14 @@ type Switch struct {
Log Log `json:"log"`
Cert *Cert `json:"cert,omitempty"`
Crypt *Crypt `json:"crypt,omitempty"`
Network []*Network `json:"network,omitempty" yaml:"networks"`
Network []*Network `json:"network,omitempty" yaml:"network"`
Acl []*ACL `json:"acl,omitempty" yaml:"acl,omitempty"`
FireWall []FlowRule `json:"firewall,omitempty" yaml:"firewall,omitempty"`
Inspect []string `json:"inspect,omitempty" yaml:"inspect,omitempty"`
Queue Queue `json:"queue" yaml:"queue"`
PassFile string `json:"password" yaml:"passwordFile"`
PassFile string `json:"password" yaml:"password"`
Ldap *LDAP `json:"ldap,omitempty" yaml:"ldap,omitempty"`
AddrPool string `json:"pool,omitempty"`
AddrPool string `json:"pool,omitempty" yaml:"pool,omitempty"`
ConfDir string `json:"-" yaml:"-"`
TokenFile string `json:"-" yaml:"-"`
}
@@ -164,6 +164,29 @@ func (s *Switch) Dir(elem ...string) string {
return filepath.Join(args...)
}
func (s *Switch) Format() {
for _, obj := range s.Network {
libol.Debug("Switch.Format %s", obj)
context := obj.Specifies
switch obj.Provider {
case "esp":
obj.Specifies = &ESPSpecifies{}
case "vxlan":
obj.Specifies = &VxLANSpecifies{}
case "fabric":
obj.Specifies = &FabricSpecifies{}
default:
obj.Specifies = nil
continue
}
if data, err := libol.Marshal(context, true); err == nil {
if err := libol.Unmarshal(obj.Specifies, data); err != nil {
libol.Warn("Switch.Format %s", err)
}
}
}
}
func (s *Switch) LoadNetwork() {
files, err := filepath.Glob(s.Dir("network", "*.json"))
if err != nil {
@@ -181,22 +204,9 @@ func (s *Switch) LoadNetwork() {
}
obj.LoadLink()
obj.LoadRoute()
switch obj.Provider {
case "esp":
obj.Specifies = &ESPSpecifies{}
case "vxlan":
obj.Specifies = &VxLANSpecifies{}
case "fabric":
obj.Specifies = &FabricSpecifies{}
}
if obj.Specifies != nil {
if err := libol.UnmarshalLoad(obj, k); err != nil {
libol.Error("Switch.LoadNetwork %s", err)
continue
}
}
s.Network = append(s.Network, obj)
}
s.Format()
for _, obj := range s.Network {
for _, link := range obj.Links {
link.Default()

View File

@@ -133,6 +133,13 @@ func LoadWithoutAnn(file string) ([]byte, error) {
return ScanAnn(fp)
}
func Unmarshal(v interface{}, contents []byte) error {
if err := json.Unmarshal(contents, v); err != nil {
return NewErr("%s", err)
}
return nil
}
func UnmarshalLoad(v interface{}, file string) error {
if err := FileExist(file); err != nil {
return NewErr("%s %s", file, err)
@@ -141,10 +148,7 @@ func UnmarshalLoad(v interface{}, file string) error {
if err != nil {
return NewErr("%s %s", file, err)
}
if err := json.Unmarshal(contents, v); err != nil {
return NewErr("%s", err)
}
return nil
return Unmarshal(v, contents)
}
func FunName(i interface{}) string {

View File

@@ -25,7 +25,7 @@ type OpenLANWorker struct {
startTime int64
links *Links
bridge network.Bridger
openVPN []*OpenVPN
vpn *OpenVPN
}
func NewOpenLANWorker(c *co.Network) *OpenLANWorker {
@@ -69,18 +69,10 @@ func (w *OpenLANWorker) Initialize() {
}
w.bridge = network.NewBridger(brCfg.Provider, brCfg.Name, brCfg.IPMtu)
vCfg := w.cfg.OpenVPN
if vCfg != nil {
if !(vCfg == nil) {
obj := NewOpenVPN(vCfg)
obj.Initialize()
w.openVPN = append(w.openVPN, obj)
for _, _vCfg := range vCfg.Breed {
if _vCfg == nil {
continue
}
obj := NewOpenVPN(_vCfg)
obj.Initialize()
w.openVPN = append(w.openVPN, obj)
}
w.vpn = obj
}
w.WorkerImpl.Initialize()
}
@@ -230,15 +222,15 @@ func (w *OpenLANWorker) connectPeer(cfg *co.Bridge) {
}
func (w *OpenLANWorker) Start(v api.Switcher) {
w.uuid = v.UUID()
w.startTime = time.Now().Unix()
w.out.Info("OpenLANWorker.Start")
w.UpBridge(w.cfg.Bridge)
w.uuid = v.UUID()
w.LoadLinks()
w.LoadRoutes()
for _, vpn := range w.openVPN {
vpn.Start()
if !(w.vpn == nil) {
w.vpn.Start()
}
w.startTime = time.Now().Unix()
w.WorkerImpl.Start(v)
}
@@ -266,8 +258,8 @@ func (w *OpenLANWorker) closePeer(cfg *co.Bridge) {
func (w *OpenLANWorker) Stop() {
w.out.Info("OpenLANWorker.Close")
w.WorkerImpl.Stop()
for _, vpn := range w.openVPN {
vpn.Stop()
if !(w.vpn == nil) {
w.vpn.Stop()
}
w.UnLoadRoutes()
w.UnLoadLinks()

View File

@@ -212,9 +212,6 @@ func (v *Switch) preWorkerVPN(w Networker, vCfg *co.OpenVPN) {
}
}
vCfg.Routes = routes
for _, _vCfg := range vCfg.Breed {
v.preWorkerVPN(w, _vCfg)
}
}
func (v *Switch) preWorker(w Networker) {
@@ -261,9 +258,6 @@ func (v *Switch) preNetVPN0(nCfg *co.Network, vCfg *co.OpenVPN) {
v.enableFwd(devName, "", vCfg.Subnet, rt)
v.enableMasq(devName, "", vCfg.Subnet, rt)
}
for _, _vCfg := range vCfg.Breed {
v.preNetVPN0(nCfg, _vCfg)
}
}
func (v *Switch) preNetVPN1(bridge, prefix string, vCfg *co.OpenVPN) {
@@ -273,9 +267,6 @@ func (v *Switch) preNetVPN1(bridge, prefix string, vCfg *co.OpenVPN) {
// Enable MASQUERADE, and allowed forward.
v.enableFwd("", bridge, vCfg.Subnet, prefix)
v.enableMasq("", bridge, vCfg.Subnet, prefix)
for _, _vCfg := range vCfg.Breed {
v.preNetVPN1(bridge, prefix, _vCfg)
}
}
func (v *Switch) preNets() {
@@ -383,9 +374,6 @@ func (v *Switch) preAllowVPN(cfg *co.OpenVPN) {
} else {
v.enablePort("tcp", port)
}
for _, _cfg := range cfg.Breed {
v.preAllowVPN(_cfg)
}
}
func (v *Switch) preAllow() {
@@ -445,7 +433,7 @@ func (v *Switch) Initialize() {
BaseDN: ldap.BaseDN,
Attr: ldap.Attribute,
Filter: ldap.Filter,
EnableTls: ldap.EnableTls,
EnableTls: ldap.Tls,
})
}
// Enable cert verify for access