diff --git a/adapter/router.go b/adapter/router.go index 786a777e..54dc3396 100644 --- a/adapter/router.go +++ b/adapter/router.go @@ -93,7 +93,6 @@ type DNSRule interface { type RuleSet interface { StartContext(ctx context.Context, startContext RuleSetStartContext) error - PostStart() error Metadata() RuleSetMetadata Close() error HeadlessRule diff --git a/route/router.go b/route/router.go index 73845a01..d194f399 100644 --- a/route/router.go +++ b/route/router.go @@ -509,78 +509,6 @@ func (r *Router) Start() error { r.geositeReader = nil } - if len(r.ruleSets) > 0 { - monitor.Start("initialize rule-set") - ruleSetStartContext := NewRuleSetStartContext() - var ruleSetStartGroup task.Group - for i, ruleSet := range r.ruleSets { - ruleSetInPlace := ruleSet - ruleSetStartGroup.Append0(func(ctx context.Context) error { - err := ruleSetInPlace.StartContext(ctx, ruleSetStartContext) - if err != nil { - return E.Cause(err, "initialize rule-set[", i, "]") - } - return nil - }) - } - ruleSetStartGroup.Concurrency(5) - ruleSetStartGroup.FastFail() - err := ruleSetStartGroup.Run(r.ctx) - monitor.Finish() - if err != nil { - return err - } - ruleSetStartContext.Close() - } - var ( - needProcessFromRuleSet bool - needWIFIStateFromRuleSet bool - ) - for _, ruleSet := range r.ruleSets { - metadata := ruleSet.Metadata() - if metadata.ContainsProcessRule { - needProcessFromRuleSet = true - } - if metadata.ContainsWIFIRule { - needWIFIStateFromRuleSet = true - } - } - if needProcessFromRuleSet || r.needFindProcess || r.needPackageManager { - if C.IsAndroid && r.platformInterface == nil { - monitor.Start("initialize package manager") - packageManager, err := tun.NewPackageManager(r) - monitor.Finish() - if err != nil { - return E.Cause(err, "create package manager") - } - monitor.Start("start package manager") - err = packageManager.Start() - monitor.Finish() - if err != nil { - return E.Cause(err, "start package manager") - } - r.packageManager = packageManager - } - - if r.platformInterface != nil { - r.processSearcher = r.platformInterface - } else { - monitor.Start("initialize process searcher") - searcher, err := process.NewSearcher(process.Config{ - Logger: r.logger, - PackageManager: r.packageManager, - }) - monitor.Finish() - if err != nil { - if err != os.ErrInvalid { - r.logger.Warn(E.Cause(err, "create process searcher")) - } - } else { - r.processSearcher = searcher - } - } - } - if runtime.GOOS == "windows" { powerListener, err := winpowrprof.NewEventListener(r.notifyWindowsPowerEvent) if err == nil { @@ -599,25 +527,6 @@ func (r *Router) Start() error { } } - if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil { - monitor.Start("initialize WIFI state") - r.needWIFIState = true - r.interfaceMonitor.RegisterCallback(func(_ int) { - r.updateWIFIState() - }) - r.updateWIFIState() - monitor.Finish() - } - - for i, rule := range r.rules { - monitor.Start("initialize rule[", i, "]") - err := rule.Start() - monitor.Finish() - if err != nil { - return E.Cause(err, "initialize rule[", i, "]") - } - } - monitor.Start("initialize DNS client") r.dnsClient.Start() monitor.Finish() @@ -726,12 +635,93 @@ func (r *Router) Close() error { } func (r *Router) PostStart() error { + monitor := taskmonitor.New(r.logger, C.StopTimeout) if len(r.ruleSets) > 0 { + monitor.Start("initialize rule-set") + ruleSetStartContext := NewRuleSetStartContext() + var ruleSetStartGroup task.Group for i, ruleSet := range r.ruleSets { - err := ruleSet.PostStart() + ruleSetInPlace := ruleSet + ruleSetStartGroup.Append0(func(ctx context.Context) error { + err := ruleSetInPlace.StartContext(ctx, ruleSetStartContext) + if err != nil { + return E.Cause(err, "initialize rule-set[", i, "]") + } + return nil + }) + } + ruleSetStartGroup.Concurrency(5) + ruleSetStartGroup.FastFail() + err := ruleSetStartGroup.Run(r.ctx) + monitor.Finish() + if err != nil { + return err + } + ruleSetStartContext.Close() + } + var ( + needProcessFromRuleSet bool + needWIFIStateFromRuleSet bool + ) + for _, ruleSet := range r.ruleSets { + metadata := ruleSet.Metadata() + if metadata.ContainsProcessRule { + needProcessFromRuleSet = true + } + if metadata.ContainsWIFIRule { + needWIFIStateFromRuleSet = true + } + } + if needProcessFromRuleSet || r.needFindProcess || r.needPackageManager { + if C.IsAndroid && r.platformInterface == nil { + monitor.Start("initialize package manager") + packageManager, err := tun.NewPackageManager(r) + monitor.Finish() if err != nil { - return E.Cause(err, "post start rule-set[", i, "]") + return E.Cause(err, "create package manager") } + monitor.Start("start package manager") + err = packageManager.Start() + monitor.Finish() + if err != nil { + return E.Cause(err, "start package manager") + } + r.packageManager = packageManager + } + + if r.platformInterface != nil { + r.processSearcher = r.platformInterface + } else { + monitor.Start("initialize process searcher") + searcher, err := process.NewSearcher(process.Config{ + Logger: r.logger, + PackageManager: r.packageManager, + }) + monitor.Finish() + if err != nil { + if err != os.ErrInvalid { + r.logger.Warn(E.Cause(err, "create process searcher")) + } + } else { + r.processSearcher = searcher + } + } + } + if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil { + monitor.Start("initialize WIFI state") + r.needWIFIState = true + r.interfaceMonitor.RegisterCallback(func(_ int) { + r.updateWIFIState() + }) + r.updateWIFIState() + monitor.Finish() + } + for i, rule := range r.rules { + monitor.Start("initialize rule[", i, "]") + err := rule.Start() + monitor.Finish() + if err != nil { + return E.Cause(err, "initialize rule[", i, "]") } } r.started = true diff --git a/route/rule_set_local.go b/route/rule_set_local.go index 1fd09246..39458267 100644 --- a/route/rule_set_local.go +++ b/route/rule_set_local.go @@ -78,10 +78,6 @@ func (s *LocalRuleSet) StartContext(ctx context.Context, startContext adapter.Ru return nil } -func (s *LocalRuleSet) PostStart() error { - return nil -} - func (s *LocalRuleSet) Metadata() adapter.RuleSetMetadata { return s.metadata } diff --git a/route/rule_set_remote.go b/route/rule_set_remote.go index a14c6fe5..8389c2f4 100644 --- a/route/rule_set_remote.go +++ b/route/rule_set_remote.go @@ -112,16 +112,6 @@ func (s *RemoteRuleSet) StartContext(ctx context.Context, startContext adapter.R return nil } -func (s *RemoteRuleSet) PostStart() error { - if s.lastUpdated.IsZero() { - err := s.fetchOnce(s.ctx, nil) - if err != nil { - s.logger.Error("fetch rule-set ", s.options.Tag, ": ", err) - } - } - return nil -} - func (s *RemoteRuleSet) Metadata() adapter.RuleSetMetadata { return s.metadata }