mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Track process rules in updated rule-sets
This commit is contained in:
@@ -36,7 +36,7 @@ func (r *Router) findProcessInfoCached(ctx context.Context, network string, sour
|
||||
}
|
||||
|
||||
func (r *Router) searchProcessInfo(ctx context.Context, metadata *adapter.InboundContext) {
|
||||
if r.processSearcher == nil || metadata.ProcessInfo != nil || !r.isLocalSource(metadata.Source.Addr) {
|
||||
if r.processSearcher == nil || metadata.ProcessInfo != nil || !r.NeedFindProcess() || !r.isLocalSource(metadata.Source.Addr) {
|
||||
return
|
||||
}
|
||||
var originDestination netip.AddrPort
|
||||
|
||||
+48
-35
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
@@ -25,31 +28,35 @@ import (
|
||||
var _ adapter.Router = (*Router)(nil)
|
||||
|
||||
type Router struct {
|
||||
ctx context.Context
|
||||
logger log.ContextLogger
|
||||
inbound adapter.InboundManager
|
||||
outbound adapter.OutboundManager
|
||||
dns adapter.DNSRouter
|
||||
dnsTransport adapter.DNSTransportManager
|
||||
connection adapter.ConnectionManager
|
||||
network adapter.NetworkManager
|
||||
httpClientManager adapter.HTTPClientManager
|
||||
rules []adapter.Rule
|
||||
needFindProcess bool
|
||||
needFindNeighbor bool
|
||||
leaseFiles []string
|
||||
ruleSets []adapter.RuleSet
|
||||
ruleSetMap map[string]adapter.RuleSet
|
||||
ruleSetUpdater *R.RuleSetUpdater
|
||||
processSearcher process.Searcher
|
||||
processCache *freelru.Cache[processCacheKey, processCacheEntry]
|
||||
neighborResolver adapter.NeighborResolver
|
||||
pauseManager pause.Manager
|
||||
trackers []adapter.ConnectionTracker
|
||||
platformInterface adapter.PlatformInterface
|
||||
ctx context.Context
|
||||
logger log.ContextLogger
|
||||
inbound adapter.InboundManager
|
||||
outbound adapter.OutboundManager
|
||||
dns adapter.DNSRouter
|
||||
dnsTransport adapter.DNSTransportManager
|
||||
connection adapter.ConnectionManager
|
||||
network adapter.NetworkManager
|
||||
httpClientManager adapter.HTTPClientManager
|
||||
rules []adapter.Rule
|
||||
needFindProcess bool
|
||||
ruleSetNeedFindProcess atomic.Bool
|
||||
ruleSetNeedFindProcessAccess sync.Mutex
|
||||
hasUpdatableRuleSet bool
|
||||
needFindNeighbor bool
|
||||
leaseFiles []string
|
||||
ruleSets []adapter.RuleSet
|
||||
ruleSetMap map[string]adapter.RuleSet
|
||||
ruleSetUpdater *R.RuleSetUpdater
|
||||
processSearcher process.Searcher
|
||||
processCache *freelru.Cache[processCacheKey, processCacheEntry]
|
||||
neighborResolver adapter.NeighborResolver
|
||||
pauseManager pause.Manager
|
||||
trackers []adapter.ConnectionTracker
|
||||
platformInterface adapter.PlatformInterface
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, logFactory log.Factory, options option.RouteOptions, dnsOptions option.DNSOptions) *Router {
|
||||
platformInterface := service.FromContext[adapter.PlatformInterface](ctx)
|
||||
return &Router{
|
||||
ctx: ctx,
|
||||
logger: logFactory.NewLogger("router"),
|
||||
@@ -62,11 +69,11 @@ func NewRouter(ctx context.Context, logFactory log.Factory, options option.Route
|
||||
httpClientManager: service.FromContext[adapter.HTTPClientManager](ctx),
|
||||
rules: make([]adapter.Rule, 0, len(options.Rules)),
|
||||
ruleSetMap: make(map[string]adapter.RuleSet),
|
||||
needFindProcess: hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess,
|
||||
needFindProcess: hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess || C.IsAndroid && platformInterface != nil,
|
||||
needFindNeighbor: hasRule(options.Rules, isNeighborRule) || hasDNSRule(dnsOptions.Rules, isNeighborDNSRule) || hasLocalNeighborDNSServer(dnsOptions.Servers) || options.FindNeighbor,
|
||||
leaseFiles: options.DHCPLeaseFiles,
|
||||
pauseManager: service.FromContext[pause.Manager](ctx),
|
||||
platformInterface: service.FromContext[adapter.PlatformInterface](ctx),
|
||||
platformInterface: platformInterface,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +100,9 @@ func (r *Router) Initialize(rules []option.Rule, ruleSets []option.RuleSet) erro
|
||||
}
|
||||
r.ruleSets = append(r.ruleSets, ruleSet)
|
||||
r.ruleSetMap[tag] = ruleSet
|
||||
if options.Type != C.RuleSetTypeInline {
|
||||
r.hasUpdatableRuleSet = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -168,18 +178,13 @@ func (r *Router) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
||||
scope.Add(r.ruleSetUpdater.Close)
|
||||
}
|
||||
r.network.Initialize(r.ruleSets)
|
||||
needFindProcess := r.needFindProcess
|
||||
for _, ruleSet := range r.ruleSets {
|
||||
metadata := ruleSet.Metadata()
|
||||
if metadata.ContainsProcessRule {
|
||||
needFindProcess = true
|
||||
}
|
||||
ruleSet.RegisterCallback(func(adapter.RuleSet) {
|
||||
r.updateRuleSetNeedFindProcess()
|
||||
})
|
||||
}
|
||||
if C.IsAndroid && r.platformInterface != nil {
|
||||
needFindProcess = true
|
||||
}
|
||||
r.needFindProcess = needFindProcess
|
||||
if needFindProcess {
|
||||
r.updateRuleSetNeedFindProcess()
|
||||
if r.NeedFindProcess() || r.hasUpdatableRuleSet {
|
||||
if r.platformInterface != nil && r.platformInterface.UsePlatformConnectionOwnerFinder() {
|
||||
r.processSearcher = newPlatformSearcher(r.platformInterface)
|
||||
} else {
|
||||
@@ -241,7 +246,15 @@ func (r *Router) AppendTracker(tracker adapter.ConnectionTracker) {
|
||||
}
|
||||
|
||||
func (r *Router) NeedFindProcess() bool {
|
||||
return r.needFindProcess
|
||||
return r.needFindProcess || r.ruleSetNeedFindProcess.Load()
|
||||
}
|
||||
|
||||
func (r *Router) updateRuleSetNeedFindProcess() {
|
||||
r.ruleSetNeedFindProcessAccess.Lock()
|
||||
defer r.ruleSetNeedFindProcessAccess.Unlock()
|
||||
r.ruleSetNeedFindProcess.Store(slices.ContainsFunc(r.ruleSets, func(ruleSet adapter.RuleSet) bool {
|
||||
return ruleSet.Metadata().ContainsProcessRule
|
||||
}))
|
||||
}
|
||||
|
||||
func (r *Router) NeedFindNeighbor() bool {
|
||||
|
||||
Reference in New Issue
Block a user