diff --git a/.gitignore b/.gitignore index 269846c..9bafbb3 100755 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # Test binary, build with `go test -c` *.test *.idea +*.vscode *.DS_Store coverage.out coverage.html diff --git a/pkg/api/output.go b/pkg/api/output.go index e9492c2..e0a6dba 100755 --- a/pkg/api/output.go +++ b/pkg/api/output.go @@ -15,13 +15,17 @@ type Output struct { } func (h Output) Router(router *mux.Router) { - router.HandleFunc("/api/output", h.List).Methods("GET") - router.HandleFunc("/api/output/{id}", h.Get).Methods("GET") + router.HandleFunc("/api/network/{id}/output", h.Get).Methods("GET") + router.HandleFunc("/api/network/{id}/output", h.Post).Methods("POST") } -func (h Output) List(w http.ResponseWriter, r *http.Request) { +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() { + for l := range cache.Output.List(name) { if l == nil { break } @@ -30,14 +34,6 @@ func (h Output) List(w http.ResponseWriter, r *http.Request) { ResponseJson(w, outputs) } -func (h Output) Get(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - - libol.Debug("Output.Get %s", vars["id"]) - output := cache.Output.Get(vars["id"]) - if output != nil { - ResponseJson(w, models.NewOutputSchema(output)) - } else { - http.Error(w, vars["id"], http.StatusNotFound) - } +func (h Output) Post(w http.ResponseWriter, r *http.Request) { + ResponseJson(w, "outputs") } diff --git a/pkg/cache/output.go b/pkg/cache/output.go index 5d143f7..b097ab9 100755 --- a/pkg/cache/output.go +++ b/pkg/cache/output.go @@ -29,13 +29,15 @@ func (p *output) Del(key string) { p.outputs.Del(key) } -func (p *output) List() <-chan *models.Output { +func (p *output) List(name string) <-chan *models.Output { c := make(chan *models.Output, 128) go func() { p.outputs.Iter(func(k string, v interface{}) { m := v.(*models.Output) - m.Update() - c <- m + if name == "" || m.Network == name { + m.Update() + c <- m + } }) c <- nil //Finish channel by nil. }() diff --git a/pkg/config/dhcp.go b/pkg/config/dhcp.go index bf52bfd..8f5934b 100755 --- a/pkg/config/dhcp.go +++ b/pkg/config/dhcp.go @@ -1,9 +1,9 @@ package config type Dhcp struct { - Name string `json:"name,omitempty"` - Bridge *Bridge `json:"bridge,omitempty"` - Subnet *Subnet `json:"subnet,omitempty"` - Hosts []HostLease `json:"hosts,omitempty"` - Routes []PrefixRoute `json:"routes,omitempty"` + Name string `json:"name,omitempty"` + Interface string `json:"interface,omitempty"` + Subnet *Subnet `json:"subnet,omitempty"` + Hosts []HostLease `json:"hosts,omitempty"` + Routes []PrefixRoute `json:"routes,omitempty"` } diff --git a/pkg/switch/dhcp.go b/pkg/switch/dhcp.go index 73d7311..8f71be1 100755 --- a/pkg/switch/dhcp.go +++ b/pkg/switch/dhcp.go @@ -80,7 +80,7 @@ dhcp-leasefile=%s func (d *Dhcp) SaveConf() { cfg := d.cfg data := fmt.Sprintf(d.Tmpl(), - cfg.Bridge.Name, + cfg.Interface, cfg.Subnet.Start, cfg.Subnet.End, d.LeaseFile(), diff --git a/pkg/switch/http.go b/pkg/switch/http.go index d0aefb2..7c3081f 100755 --- a/pkg/switch/http.go +++ b/pkg/switch/http.go @@ -307,7 +307,7 @@ func (h *Http) getIndex(body *schema.Index) *schema.Index { }) // display esp state - for s := range cache.Output.List() { + for s := range cache.Output.List("") { if s == nil { break } diff --git a/pkg/switch/network.go b/pkg/switch/network.go index 69ed032..5873b07 100755 --- a/pkg/switch/network.go +++ b/pkg/switch/network.go @@ -108,14 +108,6 @@ func (w *WorkerImpl) Initialize() { w.updateVPN() w.createVPN() - if cfg.Dhcp == "enable" { - w.dhcp = NewDhcp(&co.Dhcp{ - Name: cfg.Name, - Subnet: cfg.Subnet, - Bridge: cfg.Bridge, - }) - } - w.fire = cn.NewFireWallTable(cfg.Name) if out, err := w.setV.Clear(); err != nil { @@ -130,6 +122,18 @@ func (w *WorkerImpl) Initialize() { w.ztrust.Initialize() } + if cfg.Dhcp == "enable" { + name := cfg.Bridge.Name + if w.br != nil { + name = w.br.L3Name() + } + w.dhcp = NewDhcp(&co.Dhcp{ + Name: cfg.Name, + Subnet: cfg.Subnet, + Interface: name, + }) + } + w.forwardSubnet() w.forwardVPN() } @@ -204,19 +208,24 @@ func (w *WorkerImpl) AddOutput(bridge string, port *LinuxPort) { if err := nl.LinkSetUp(link); err != nil { w.out.Warn("WorkerImpl.AddOutput %s %s", cfg.Remote, err) } - if port.link == "" { - port.link = fmt.Sprintf("%s.%d", cfg.Remote, cfg.Segment) - } - subLink := &nl.Vlan{ - LinkAttrs: nl.LinkAttrs{ - Name: port.link, - ParentIndex: link.Attrs().Index, - }, - VlanId: cfg.Segment, - } - if err := nl.LinkAdd(subLink); err != nil { - w.out.Error("WorkerImpl.linkAdd %s %s", subLink.Name, err) - return + + if cfg.Segment > 0 { + if port.link == "" { + port.link = fmt.Sprintf("%s.%d", cfg.Remote, cfg.Segment) + } + subLink := &nl.Vlan{ + LinkAttrs: nl.LinkAttrs{ + Name: port.link, + ParentIndex: link.Attrs().Index, + }, + VlanId: cfg.Segment, + } + if err := nl.LinkAdd(subLink); err != nil { + w.out.Error("WorkerImpl.linkAdd %s %s", subLink.Name, err) + return + } + } else { + port.link = cfg.Remote } } @@ -342,12 +351,6 @@ func (w *WorkerImpl) Start(v api.Switcher) { if !(w.dhcp == nil) { w.dhcp.Start() - fire.Nat.Post.AddRule(cn.IPRule{ - Source: cfg.Bridge.Address, - NoDest: cfg.Bridge.Address, - Jump: cn.CMasq, - Comment: "Default Gateway for DHCP", - }) } if !(w.vpn == nil) { @@ -435,12 +438,13 @@ func (w *WorkerImpl) DelOutput(bridge string, port *LinuxPort) { w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err) return } - } else { + } else if port.cfg.Segment > 0 { link := &nl.Vlan{ LinkAttrs: nl.LinkAttrs{ Name: port.link, }, } + if err := nl.LinkDel(link); err != nil { w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err) return