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),
platformInterface: platformInterface,
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
}),
}
@ -531,18 +531,20 @@ func (r *Router) Start() error {
r.dnsClient.Start()
monitor.Finish()
if r.needPackageManager && r.platformInterface == nil {
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")
if r.needPackageManager {
monitor.Start("start package manager")
err = packageManager.Start()
monitor.Finish()
if err != nil {
return E.Cause(err, "start package manager")
}
}
r.packageManager = packageManager
}
@ -675,20 +677,30 @@ func (r *Router) PostStart() error {
}
ruleSetStartContext.Close()
}
var (
needProcessFromRuleSet bool
needWIFIStateFromRuleSet bool
)
needFindProcess := r.needFindProcess
needWIFIState := r.needWIFIState
for _, ruleSet := range r.ruleSets {
metadata := ruleSet.Metadata()
if metadata.ContainsProcessRule {
needProcessFromRuleSet = true
needFindProcess = true
}
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 {
r.processSearcher = r.platformInterface
} 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")
r.needWIFIState = true
r.interfaceMonitor.RegisterCallback(func(_ int) {