Fix crash on Android when using process rules

This commit is contained in:
世界 2024-08-06 20:38:04 +08:00
parent baecfc7778
commit 07c678fb85
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -131,7 +131,7 @@ func NewRouter(
pauseManager: service.FromContext[pause.Manager](ctx), pauseManager: service.FromContext[pause.Manager](ctx),
platformInterface: platformInterface, platformInterface: platformInterface,
needWIFIState: hasRule(options.Rules, isWIFIRule) || hasDNSRule(dnsOptions.Rules, isWIFIDNSRule), needWIFIState: hasRule(options.Rules, isWIFIRule) || hasDNSRule(dnsOptions.Rules, isWIFIDNSRule),
needPackageManager: C.IsAndroid && platformInterface == nil && common.Any(inbounds, func(inbound option.Inbound) bool { needPackageManager: common.Any(inbounds, func(inbound option.Inbound) bool {
return len(inbound.TunOptions.IncludePackage) > 0 || len(inbound.TunOptions.ExcludePackage) > 0 return len(inbound.TunOptions.IncludePackage) > 0 || len(inbound.TunOptions.ExcludePackage) > 0
}), }),
} }
@ -531,18 +531,20 @@ func (r *Router) Start() error {
r.dnsClient.Start() r.dnsClient.Start()
monitor.Finish() monitor.Finish()
if r.needPackageManager && r.platformInterface == nil { if C.IsAndroid && r.platformInterface == nil {
monitor.Start("initialize package manager") monitor.Start("initialize package manager")
packageManager, err := tun.NewPackageManager(r) packageManager, err := tun.NewPackageManager(r)
monitor.Finish() monitor.Finish()
if err != nil { if err != nil {
return E.Cause(err, "create package manager") return E.Cause(err, "create package manager")
} }
monitor.Start("start package manager") if r.needPackageManager {
err = packageManager.Start() monitor.Start("start package manager")
monitor.Finish() err = packageManager.Start()
if err != nil { monitor.Finish()
return E.Cause(err, "start package manager") if err != nil {
return E.Cause(err, "start package manager")
}
} }
r.packageManager = packageManager r.packageManager = packageManager
} }
@ -675,20 +677,30 @@ func (r *Router) PostStart() error {
} }
ruleSetStartContext.Close() ruleSetStartContext.Close()
} }
var ( needFindProcess := r.needFindProcess
needProcessFromRuleSet bool needWIFIState := r.needWIFIState
needWIFIStateFromRuleSet bool
)
for _, ruleSet := range r.ruleSets { for _, ruleSet := range r.ruleSets {
metadata := ruleSet.Metadata() metadata := ruleSet.Metadata()
if metadata.ContainsProcessRule { if metadata.ContainsProcessRule {
needProcessFromRuleSet = true needFindProcess = true
} }
if metadata.ContainsWIFIRule { if metadata.ContainsWIFIRule {
needWIFIStateFromRuleSet = true needWIFIState = true
} }
} }
if needProcessFromRuleSet || r.needFindProcess { if C.IsAndroid && r.platformInterface == nil && !r.needPackageManager {
if needFindProcess {
monitor.Start("start package manager")
err := r.packageManager.Start()
monitor.Finish()
if err != nil {
return E.Cause(err, "start package manager")
}
} else {
r.packageManager = nil
}
}
if needFindProcess {
if r.platformInterface != nil { if r.platformInterface != nil {
r.processSearcher = r.platformInterface r.processSearcher = r.platformInterface
} else { } else {
@ -707,7 +719,7 @@ func (r *Router) PostStart() error {
} }
} }
} }
if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil { if needWIFIState && r.platformInterface != nil {
monitor.Start("initialize WIFI state") monitor.Start("initialize WIFI state")
r.needWIFIState = true r.needWIFIState = true
r.interfaceMonitor.RegisterCallback(func(_ int) { r.interfaceMonitor.RegisterCallback(func(_ int) {