diff --git a/adapter/router.go b/adapter/router.go index 5828ab35..9d8bcb3e 100644 --- a/adapter/router.go +++ b/adapter/router.go @@ -33,6 +33,8 @@ type Router interface { RuleSet(tag string) (RuleSet, bool) + NeedWIFIState() bool + Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index b30ca22f..a516c4c5 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -77,6 +77,10 @@ func (s *BoxService) Close() error { return s.instance.Close() } +func (s *BoxService) NeedWIFIState() bool { + return s.instance.Router().NeedWIFIState() +} + var ( _ platform.Interface = (*platformInterfaceWrapper)(nil) _ log.PlatformWriter = (*platformInterfaceWrapper)(nil) diff --git a/route/router.go b/route/router.go index 7a7c535a..df1a54a0 100644 --- a/route/router.go +++ b/route/router.go @@ -560,13 +560,12 @@ func (r *Router) Start() error { } } } - if needWIFIStateFromRuleSet || r.needWIFIState { + if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil { monitor.Start("initialize WIFI state") - if r.platformInterface != nil && r.interfaceMonitor != nil { - r.interfaceMonitor.RegisterCallback(func(_ int) { - r.updateWIFIState() - }) - } + r.needWIFIState = true + r.interfaceMonitor.RegisterCallback(func(_ int) { + r.updateWIFIState() + }) r.updateWIFIState() monitor.Finish() } @@ -716,6 +715,10 @@ func (r *Router) RuleSet(tag string) (adapter.RuleSet, bool) { return ruleSet, loaded } +func (r *Router) NeedWIFIState() bool { + return r.needWIFIState +} + func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { if r.pauseManager.IsDevicePaused() { return E.New("reject connection to ", metadata.Destination, " while device paused")