diff --git a/auth/auth.go b/auth/auth.go index 60c6e98..62e9b89 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -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 } diff --git a/bypass/bypass.go b/bypass/bypass.go index ecebdb5..ea4f131 100644 --- a/bypass/bypass.go +++ b/bypass/bypass.go @@ -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 } diff --git a/hop/hop.go b/hop/hop.go index 9ca780b..c228d2b 100644 --- a/hop/hop.go +++ b/hop/hop.go @@ -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 } diff --git a/hosts/hosts.go b/hosts/hosts.go index 0d48978..a7093f4 100644 --- a/hosts/hosts.go +++ b/hosts/hosts.go @@ -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 } diff --git a/ingress/ingress.go b/ingress/ingress.go index 6e50350..96027dc 100644 --- a/ingress/ingress.go +++ b/ingress/ingress.go @@ -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 } diff --git a/router/router.go b/router/router.go index 50dc125..5c738c7 100644 --- a/router/router.go +++ b/router/router.go @@ -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 } diff --git a/router/router_linux.go b/router/router_linux.go new file mode 100644 index 0000000..4a99bae --- /dev/null +++ b/router/router_linux.go @@ -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 +} diff --git a/router/router_other.go b/router/router_other.go new file mode 100644 index 0000000..76beede --- /dev/null +++ b/router/router_other.go @@ -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 +}