mirror of
https://github.com/luscis/openlan.git
synced 2025-09-26 20:41:29 +08:00
fix: dhcp without host interface
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -13,6 +13,7 @@
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
*.idea
|
||||
*.vscode
|
||||
*.DS_Store
|
||||
coverage.out
|
||||
coverage.html
|
||||
|
@@ -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")
|
||||
}
|
||||
|
8
pkg/cache/output.go
vendored
8
pkg/cache/output.go
vendored
@@ -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.
|
||||
}()
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
@@ -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(),
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user