Add android package rules support in tun routing

This commit is contained in:
世界 2022-08-15 11:40:49 +08:00
parent 3157593b6b
commit cd5e7055d2
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
16 changed files with 100 additions and 188 deletions

View file

@ -90,6 +90,7 @@ type Router struct {
defaultMark int
networkMonitor tun.NetworkUpdateMonitor
interfaceMonitor tun.DefaultInterfaceMonitor
packageManager tun.PackageManager
trafficController adapter.TrafficController
processSearcher process.Searcher
}
@ -260,8 +261,22 @@ func NewRouter(ctx context.Context, logger log.ContextLogger, dnsLogger log.Cont
router.interfaceMonitor = interfaceMonitor
}
if hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess {
searcher, err := process.NewSearcher(logger)
needFindProcess := hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess
needPackageManager := C.IsAndroid && (needFindProcess || common.Any(inbounds, func(inbound option.Inbound) bool {
return len(inbound.TunOptions.IncludePackage) > 0 || len(inbound.TunOptions.ExcludePackage) > 0
}))
if needPackageManager {
packageManager, err := tun.NewPackageManager(router)
if err != nil {
return nil, E.Cause(err, "create package manager")
}
router.packageManager = packageManager
}
if needFindProcess {
searcher, err := process.NewSearcher(process.Config{
Logger: logger,
PackageManager: router.packageManager,
})
if err != nil {
if err != os.ErrInvalid {
logger.Warn(E.Cause(err, "create process searcher"))
@ -425,13 +440,10 @@ func (r *Router) Start() error {
return err
}
}
if r.processSearcher != nil {
if starter, isStarter := r.processSearcher.(common.Starter); isStarter {
err := starter.Start()
if err != nil {
r.logger.Error(E.Cause(err, "initialize process searcher"))
r.processSearcher = nil
}
if r.packageManager != nil {
err := r.packageManager.Start()
if err != nil {
return err
}
}
return nil
@ -454,7 +466,7 @@ func (r *Router) Close() error {
common.PtrOrNil(r.geoIPReader),
r.interfaceMonitor,
r.networkMonitor,
r.processSearcher,
r.packageManager,
)
}
@ -679,6 +691,10 @@ func (r *Router) InterfaceMonitor() tun.DefaultInterfaceMonitor {
return r.interfaceMonitor
}
func (r *Router) PackageManager() tun.PackageManager {
return r.packageManager
}
func (r *Router) SetTrafficController(controller adapter.TrafficController) {
r.trafficController = controller
}
@ -912,6 +928,10 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
return err
}
func (r *Router) OnPackagesUpdated(packages int, sharedUsers int) {
r.logger.Info("updated packages list: ", packages, " packages, ", sharedUsers, " shared users")
}
func (r *Router) NewError(ctx context.Context, err error) {
common.Close(err)
if E.IsClosedOrCanceled(err) {