diff --git a/dist/rootfs/var/openlan/script/switch.sh b/dist/rootfs/var/openlan/script/switch.sh index b82baa3..d5d9721 100755 --- a/dist/rootfs/var/openlan/script/switch.sh +++ b/dist/rootfs/var/openlan/script/switch.sh @@ -14,7 +14,7 @@ sysctl -p /etc/sysctl.d/90-openlan.conf ## END ## START: prepare external dir. -for dir in network acl findhop link output route qos dnat; do +for dir in network acl findhop output route qos dnat; do [ -e "$cs_dir/$dir" ] || mkdir -p "$cs_dir/$dir" done ## END diff --git a/pkg/api/output.go b/pkg/api/output.go index 0b4494e..3aacbc8 100755 --- a/pkg/api/output.go +++ b/pkg/api/output.go @@ -5,7 +5,6 @@ import ( "github.com/gorilla/mux" "github.com/luscis/openlan/pkg/cache" - "github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/models" "github.com/luscis/openlan/pkg/schema" ) @@ -25,7 +24,6 @@ func (h Output) Get(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) name := vars["id"] - libol.Debug("Output.Get %s") outputs := make([]schema.Output, 0, 1024) for l := range cache.Output.List(name) { if l == nil { diff --git a/pkg/cache/output.go b/pkg/cache/output.go old mode 100755 new mode 100644 diff --git a/pkg/config/cert.go b/pkg/config/cert.go index fa2afc4..1b35092 100755 --- a/pkg/config/cert.go +++ b/pkg/config/cert.go @@ -24,6 +24,10 @@ func (c *Crypt) Correct() { } } +func (c *Crypt) Short() string { + return c.Algo + ":" + c.Secret +} + type Cert struct { Dir string `json:"directory" yaml:"directory"` CrtFile string `json:"cert" yaml:"cert"` diff --git a/pkg/config/network.go b/pkg/config/network.go index 779170d..377d43e 100755 --- a/pkg/config/network.go +++ b/pkg/config/network.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "path/filepath" + "strings" "github.com/luscis/openlan/pkg/libol" ) @@ -131,7 +132,6 @@ func (n *Network) Correct(sw *Switch) { } CorrectRoutes(n.Routes, ipAddr) - if n.OpenVPN != nil { n.OpenVPN.Correct(sw.AddrPool, n.Name) } @@ -140,6 +140,9 @@ func (n *Network) Correct(sw *Switch) { value.Correct() n.FindHop[key] = value } + for _, value := range n.Outputs { + value.Correct() + } } func (n *Network) Dir(module string) string { @@ -159,7 +162,6 @@ func (n *Network) IsYaml() bool { } func (n *Network) Load() { - n.LoadLink() n.LoadRoute() n.LoadOutput() n.LoadFindHop() @@ -180,11 +182,32 @@ func (n *Network) LoadRoute() { } } +func UserShort(value string) string { + return strings.SplitN(value, "@", 2)[0] +} + func (n *Network) LoadOutput() { file := n.Dir("output") if err := libol.UnmarshalLoad(&n.Outputs, file); err != nil { libol.Error("Network.LoadOutput... %n", err) } + + n.LoadLink() + // Clone link to outputs. + for _, link := range n.Links { + link.Correct() + username := UserShort(link.Username) + value := &Output{ + Protocol: link.Protocol, + Remote: link.Connection, + Secret: username + ":" + link.Password, + Crypt: link.Crypt.Short(), + } + if _, index := n.FindOutput(value); index == -1 { + n.Outputs = append(n.Outputs, value) + } + } + n.Links = nil } func (n *Network) LoadFindHop() { @@ -203,19 +226,14 @@ func (n *Network) LoadDnat() { func (n *Network) Save() { obj := *n - obj.Routes = nil - obj.Links = nil obj.Outputs = nil obj.Dnat = nil - obj.FindHop = nil - + obj.FindHop = nil // Clear sub dirs. if err := libol.MarshalSave(&obj, obj.File, true); err != nil { libol.Error("Network.Save %s %s", obj.Name, err) } - n.SaveRoute() - n.SaveLink() n.SaveOutput() n.SaveFindHop() n.SaveDnat() @@ -228,13 +246,6 @@ func (n *Network) SaveRoute() { } } -func (n *Network) SaveLink() { - file := n.Dir("link") - if err := libol.MarshalSave(n.Links, file, true); err != nil { - libol.Error("Network.SaveLink %s %s", n.Name, err) - } -} - func (n *Network) SaveOutput() { file := n.Dir("output") if err := libol.MarshalSave(n.Outputs, file, true); err != nil { @@ -318,6 +329,12 @@ func (n *Network) DelOutput(value *Output) (*Output, bool) { return obj, index != -1 } +func (n *Network) ListOutput(call func(value Output)) { + for _, obj := range n.Outputs { + call(*obj) + } +} + func (n *Network) FindFindHop(value *FindHop) *FindHop { if n.FindHop == nil { n.FindHop = make(map[string]*FindHop) diff --git a/pkg/config/output.go b/pkg/config/output.go index 7d9a4ea..b8e5ea2 100755 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -22,7 +22,7 @@ func (o *Output) Id() string { return fmt.Sprintf("%s-%s-%d", o.Protocol, o.Remote, o.Segment) } -func (o *Output) GenName() { +func (o *Output) Correct() { switch o.Protocol { case "gre": o.Link = fmt.Sprintf("%s%d", "gei", o.Segment) diff --git a/pkg/switch/network_linux.go b/pkg/switch/network_linux.go index ce0d42d..0fc9711 100755 --- a/pkg/switch/network_linux.go +++ b/pkg/switch/network_linux.go @@ -601,7 +601,6 @@ func (w *WorkerImpl) Start(v api.SwitchApi) { if cfg.Bridge != nil { w.toACL(cfg.Bridge.Name) for _, output := range cfg.Outputs { - output.GenName() w.addOutput(cfg.Bridge.Name, output) } } @@ -1248,7 +1247,7 @@ func (w *WorkerImpl) AddOutput(data schema.Output) { w.out.Info("WorkerImple.AddOutput %s already existed", output.Id()) return } - output.GenName() + output.Correct() w.addOutput(w.cfg.Bridge.Name, output) }