fix: dhcp without host interface

This commit is contained in:
Daniel Ding
2024-01-27 20:21:26 +08:00
parent 52e37bdff3
commit 3619f393b5
7 changed files with 55 additions and 52 deletions

1
.gitignore vendored
View File

@@ -13,6 +13,7 @@
# Test binary, build with `go test -c` # Test binary, build with `go test -c`
*.test *.test
*.idea *.idea
*.vscode
*.DS_Store *.DS_Store
coverage.out coverage.out
coverage.html coverage.html

View File

@@ -15,13 +15,17 @@ type Output struct {
} }
func (h Output) Router(router *mux.Router) { func (h Output) Router(router *mux.Router) {
router.HandleFunc("/api/output", h.List).Methods("GET") router.HandleFunc("/api/network/{id}/output", h.Get).Methods("GET")
router.HandleFunc("/api/output/{id}", 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) outputs := make([]schema.Output, 0, 1024)
for l := range cache.Output.List() { for l := range cache.Output.List(name) {
if l == nil { if l == nil {
break break
} }
@@ -30,14 +34,6 @@ func (h Output) List(w http.ResponseWriter, r *http.Request) {
ResponseJson(w, outputs) ResponseJson(w, outputs)
} }
func (h Output) Get(w http.ResponseWriter, r *http.Request) { func (h Output) Post(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) ResponseJson(w, "outputs")
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)
}
} }

4
pkg/cache/output.go vendored
View File

@@ -29,13 +29,15 @@ func (p *output) Del(key string) {
p.outputs.Del(key) 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) c := make(chan *models.Output, 128)
go func() { go func() {
p.outputs.Iter(func(k string, v interface{}) { p.outputs.Iter(func(k string, v interface{}) {
m := v.(*models.Output) m := v.(*models.Output)
if name == "" || m.Network == name {
m.Update() m.Update()
c <- m c <- m
}
}) })
c <- nil //Finish channel by nil. c <- nil //Finish channel by nil.
}() }()

View File

@@ -2,7 +2,7 @@ package config
type Dhcp struct { type Dhcp struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Bridge *Bridge `json:"bridge,omitempty"` Interface string `json:"interface,omitempty"`
Subnet *Subnet `json:"subnet,omitempty"` Subnet *Subnet `json:"subnet,omitempty"`
Hosts []HostLease `json:"hosts,omitempty"` Hosts []HostLease `json:"hosts,omitempty"`
Routes []PrefixRoute `json:"routes,omitempty"` Routes []PrefixRoute `json:"routes,omitempty"`

View File

@@ -80,7 +80,7 @@ dhcp-leasefile=%s
func (d *Dhcp) SaveConf() { func (d *Dhcp) SaveConf() {
cfg := d.cfg cfg := d.cfg
data := fmt.Sprintf(d.Tmpl(), data := fmt.Sprintf(d.Tmpl(),
cfg.Bridge.Name, cfg.Interface,
cfg.Subnet.Start, cfg.Subnet.Start,
cfg.Subnet.End, cfg.Subnet.End,
d.LeaseFile(), d.LeaseFile(),

View File

@@ -307,7 +307,7 @@ func (h *Http) getIndex(body *schema.Index) *schema.Index {
}) })
// display esp state // display esp state
for s := range cache.Output.List() { for s := range cache.Output.List("") {
if s == nil { if s == nil {
break break
} }

View File

@@ -108,14 +108,6 @@ func (w *WorkerImpl) Initialize() {
w.updateVPN() w.updateVPN()
w.createVPN() 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) w.fire = cn.NewFireWallTable(cfg.Name)
if out, err := w.setV.Clear(); err != nil { if out, err := w.setV.Clear(); err != nil {
@@ -130,6 +122,18 @@ func (w *WorkerImpl) Initialize() {
w.ztrust.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.forwardSubnet()
w.forwardVPN() w.forwardVPN()
} }
@@ -204,6 +208,8 @@ func (w *WorkerImpl) AddOutput(bridge string, port *LinuxPort) {
if err := nl.LinkSetUp(link); err != nil { if err := nl.LinkSetUp(link); err != nil {
w.out.Warn("WorkerImpl.AddOutput %s %s", cfg.Remote, err) w.out.Warn("WorkerImpl.AddOutput %s %s", cfg.Remote, err)
} }
if cfg.Segment > 0 {
if port.link == "" { if port.link == "" {
port.link = fmt.Sprintf("%s.%d", cfg.Remote, cfg.Segment) port.link = fmt.Sprintf("%s.%d", cfg.Remote, cfg.Segment)
} }
@@ -218,6 +224,9 @@ func (w *WorkerImpl) AddOutput(bridge string, port *LinuxPort) {
w.out.Error("WorkerImpl.linkAdd %s %s", subLink.Name, err) w.out.Error("WorkerImpl.linkAdd %s %s", subLink.Name, err)
return return
} }
} else {
port.link = cfg.Remote
}
} }
if mtu > 0 { if mtu > 0 {
@@ -342,12 +351,6 @@ func (w *WorkerImpl) Start(v api.Switcher) {
if !(w.dhcp == nil) { if !(w.dhcp == nil) {
w.dhcp.Start() 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) { 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) w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err)
return return
} }
} else { } else if port.cfg.Segment > 0 {
link := &nl.Vlan{ link := &nl.Vlan{
LinkAttrs: nl.LinkAttrs{ LinkAttrs: nl.LinkAttrs{
Name: port.link, Name: port.link,
}, },
} }
if err := nl.LinkDel(link); err != nil { if err := nl.LinkDel(link); err != nil {
w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err) w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err)
return return