router: update system routes for linux

This commit is contained in:
ginuerzh 2023-11-30 19:39:37 +08:00
parent 8ef341dc88
commit b1390dda1c
8 changed files with 55 additions and 12 deletions

View File

@ -145,6 +145,8 @@ func (p *authenticator) reload(ctx context.Context) (err error) {
kvs[k] = v
}
p.options.logger.Debugf("load items %d", len(m))
p.mu.Lock()
defer p.mu.Unlock()
@ -206,7 +208,6 @@ func (p *authenticator) load(ctx context.Context) (m map[string]string, err erro
}
}
p.options.logger.Debugf("load items %d", len(m))
return
}

View File

@ -130,6 +130,7 @@ func (bp *localBypass) reload(ctx context.Context) error {
return err
}
patterns := append(bp.options.matchers, v...)
bp.options.logger.Debugf("load items %d", len(patterns))
var addrs []string
var inets []*net.IPNet
@ -205,7 +206,6 @@ func (bp *localBypass) load(ctx context.Context) (patterns []string, err error)
}
}
bp.options.logger.Debugf("load items %d", len(patterns))
return
}

View File

@ -252,6 +252,8 @@ func (p *chainHop) reload(ctx context.Context) (err error) {
nodes = append(nodes, nl...)
p.options.logger.Debugf("load items %d", len(nodes))
p.mu.Lock()
defer p.mu.Unlock()
@ -291,7 +293,6 @@ func (p *chainHop) load(ctx context.Context) (nodes []*chain.Node, err error) {
}
}
p.options.logger.Debugf("load items %d", len(nodes))
return
}

View File

@ -215,6 +215,8 @@ func (h *hostMapper) reload(ctx context.Context) (err error) {
mapf(m[i].Hostname, m[i].IP)
}
h.options.logger.Debugf("load items %d", len(mappings))
h.mu.Lock()
defer h.mu.Unlock()
@ -271,7 +273,6 @@ func (h *hostMapper) load(ctx context.Context) (mappings []Mapping, err error) {
}
}
h.options.logger.Debugf("load items %d", len(mappings))
return
}

View File

@ -139,6 +139,8 @@ func (ing *localIngress) reload(ctx context.Context) error {
fn(rule)
}
ing.options.logger.Debugf("load items %d", len(rules))
ing.mu.Lock()
defer ing.mu.Unlock()
@ -194,7 +196,6 @@ func (ing *localIngress) load(ctx context.Context) (rules []*ingress.Rule, err e
rules = append(rules, v...)
}
ing.options.logger.Debugf("load items %d", len(rules))
return
}

View File

@ -113,19 +113,25 @@ func (p *localRouter) periodReload(ctx context.Context) error {
}
}
func (r *localRouter) reload(ctx context.Context) error {
routes := r.options.routes
func (p *localRouter) reload(ctx context.Context) error {
routes := p.options.routes
v, err := r.load(ctx)
v, err := p.load(ctx)
if err != nil {
return err
}
routes = append(routes, v...)
r.mu.Lock()
defer r.mu.Unlock()
p.options.logger.Debugf("load items %d", len(routes))
r.routes = routes
if err := p.setSysRoutes(routes...); err != nil {
p.options.logger.Error(err)
}
p.mu.Lock()
defer p.mu.Unlock()
p.routes = routes
return nil
}
@ -177,7 +183,6 @@ func (p *localRouter) load(ctx context.Context) (routes []*router.Route, err err
routes = append(routes, v...)
}
p.options.logger.Debugf("load items %d", len(routes))
return
}

23
router/router_linux.go Normal file
View File

@ -0,0 +1,23 @@
package router
import (
"fmt"
"github.com/go-gost/core/router"
"github.com/vishvananda/netlink"
)
func (p *localRouter) setSysRoutes(routes ...*router.Route) error {
for _, route := range routes {
p.options.logger.Debugf("ip route replace %s via %s", route.Net, route.Gateway)
r := netlink.Route{
Dst: route.Net,
Gw: route.Gateway,
}
if err := netlink.RouteReplace(&r); err != nil {
return fmt.Errorf("set route %v %v: %v", r.Dst, r.Gw, err)
}
}
return nil
}

11
router/router_other.go Normal file
View File

@ -0,0 +1,11 @@
//go:build !linux
package router
import (
"github.com/go-gost/core/router"
)
func (*localRouter) setSysRoutes(routes ...*router.Route) error {
return nil
}