platform: Refactoring libbox to use gRPC-based protocol

This commit is contained in:
世界
2025-10-07 15:40:11 +08:00
parent 743b460e51
commit 5bc0dfa9dd
67 changed files with 6131 additions and 2002 deletions

View File

@@ -17,7 +17,6 @@ import (
"github.com/sagernet/sing-box/common/settings"
"github.com/sagernet/sing-box/common/taskmonitor"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common"
@@ -48,7 +47,7 @@ type NetworkManager struct {
packageManager tun.PackageManager
powerListener winpowrprof.EventListener
pauseManager pause.Manager
platformInterface platform.Interface
platformInterface adapter.PlatformInterface
endpoint adapter.EndpointManager
inbound adapter.InboundManager
outbound adapter.OutboundManager
@@ -90,7 +89,7 @@ func NewNetworkManager(ctx context.Context, logger logger.ContextLogger, routeOp
FallbackDelay: time.Duration(routeOptions.DefaultFallbackDelay),
},
pauseManager: service.FromContext[pause.Manager](ctx),
platformInterface: service.FromContext[platform.Interface](ctx),
platformInterface: service.FromContext[adapter.PlatformInterface](ctx),
endpoint: service.FromContext[adapter.EndpointManager](ctx),
inbound: service.FromContext[adapter.InboundManager](ctx),
outbound: service.FromContext[adapter.OutboundManager](ctx),
@@ -189,7 +188,7 @@ func (r *NetworkManager) Start(stage adapter.StartStage) error {
}
}
case adapter.StartStatePostStart:
if r.needWIFIState && !(r.platformInterface != nil && r.platformInterface.UsePlatformWIFIMonitor()) {
if r.needWIFIState && r.platformInterface == nil {
wifiMonitor, err := settings.NewWIFIMonitor(r.onWIFIStateChanged)
if err != nil {
if err != os.ErrInvalid {
@@ -264,10 +263,10 @@ func (r *NetworkManager) InterfaceFinder() control.InterfaceFinder {
}
func (r *NetworkManager) UpdateInterfaces() error {
if r.platformInterface == nil {
if r.platformInterface == nil || !r.platformInterface.UsePlatformNetworkInterfaces() {
return r.interfaceFinder.Update()
} else {
interfaces, err := r.platformInterface.Interfaces()
interfaces, err := r.platformInterface.NetworkInterfaces()
if err != nil {
return err
}
@@ -440,7 +439,7 @@ func (r *NetworkManager) UpdateWIFIState() {
var state adapter.WIFIState
if r.wifiMonitor != nil {
state = r.wifiMonitor.ReadWIFIState()
} else if r.platformInterface != nil && r.platformInterface.UsePlatformWIFIMonitor() {
} else if r.platformInterface != nil {
state = r.platformInterface.ReadWIFIState()
} else {
return

View File

@@ -0,0 +1,45 @@
package route
import (
"context"
"net/netip"
"syscall"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/process"
N "github.com/sagernet/sing/common/network"
)
type platformSearcher struct {
platform adapter.PlatformInterface
}
func newPlatformSearcher(platform adapter.PlatformInterface) process.Searcher {
return &platformSearcher{platform: platform}
}
func (s *platformSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
if !s.platform.UsePlatformConnectionOwnerFinder() {
return nil, process.ErrNotFound
}
var ipProtocol int32
switch N.NetworkName(network) {
case N.NetworkTCP:
ipProtocol = syscall.IPPROTO_TCP
case N.NetworkUDP:
ipProtocol = syscall.IPPROTO_UDP
default:
return nil, process.ErrNotFound
}
request := &adapter.FindConnectionOwnerRequest{
IpProtocol: ipProtocol,
SourceAddress: source.Addr().String(),
SourcePort: int32(source.Port()),
DestinationAddress: destination.Addr().String(),
DestinationPort: int32(destination.Port()),
}
return s.platform.FindConnectionOwner(request)
}

View File

@@ -382,18 +382,18 @@ func (r *Router) matchRule(
r.logger.InfoContext(ctx, "failed to search process: ", fErr)
} else {
if processInfo.ProcessPath != "" {
if processInfo.User != "" {
r.logger.InfoContext(ctx, "found process path: ", processInfo.ProcessPath, ", user: ", processInfo.User)
if processInfo.UserName != "" {
r.logger.InfoContext(ctx, "found process path: ", processInfo.ProcessPath, ", user: ", processInfo.UserName)
} else if processInfo.UserId != -1 {
r.logger.InfoContext(ctx, "found process path: ", processInfo.ProcessPath, ", user id: ", processInfo.UserId)
} else {
r.logger.InfoContext(ctx, "found process path: ", processInfo.ProcessPath)
}
} else if processInfo.PackageName != "" {
r.logger.InfoContext(ctx, "found package name: ", processInfo.PackageName)
} else if processInfo.AndroidPackageName != "" {
r.logger.InfoContext(ctx, "found package name: ", processInfo.AndroidPackageName)
} else if processInfo.UserId != -1 {
if processInfo.User != "" {
r.logger.InfoContext(ctx, "found user: ", processInfo.User)
if processInfo.UserName != "" {
r.logger.InfoContext(ctx, "found user: ", processInfo.UserName)
} else {
r.logger.InfoContext(ctx, "found user id: ", processInfo.UserId)
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/sagernet/sing-box/common/process"
"github.com/sagernet/sing-box/common/taskmonitor"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
R "github.com/sagernet/sing-box/route/rule"
@@ -37,7 +36,7 @@ type Router struct {
processSearcher process.Searcher
pauseManager pause.Manager
trackers []adapter.ConnectionTracker
platformInterface platform.Interface
platformInterface adapter.PlatformInterface
started bool
}
@@ -55,7 +54,7 @@ func NewRouter(ctx context.Context, logFactory log.Factory, options option.Route
ruleSetMap: make(map[string]adapter.RuleSet),
needFindProcess: hasRule(options.Rules, isProcessRule) || hasDNSRule(dnsOptions.Rules, isProcessDNSRule) || options.FindProcess,
pauseManager: service.FromContext[pause.Manager](ctx),
platformInterface: service.FromContext[platform.Interface](ctx),
platformInterface: service.FromContext[adapter.PlatformInterface](ctx),
}
}
@@ -120,8 +119,8 @@ func (r *Router) Start(stage adapter.StartStage) error {
}
}
if needFindProcess {
if r.platformInterface != nil {
r.processSearcher = r.platformInterface
if r.platformInterface != nil && r.platformInterface.UsePlatformConnectionOwnerFinder() {
r.processSearcher = newPlatformSearcher(r.platformInterface)
} else {
monitor.Start("initialize process searcher")
searcher, err := process.NewSearcher(process.Config{

View File

@@ -25,10 +25,10 @@ func NewPackageNameItem(packageNameList []string) *PackageNameItem {
}
func (r *PackageNameItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.PackageName == "" {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.AndroidPackageName == "" {
return false
}
return r.packageMap[metadata.ProcessInfo.PackageName]
return r.packageMap[metadata.ProcessInfo.AndroidPackageName]
}
func (r *PackageNameItem) String() string {

View File

@@ -26,10 +26,10 @@ func NewUserItem(users []string) *UserItem {
}
func (r *UserItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.User == "" {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.UserName == "" {
return false
}
return r.userMap[metadata.ProcessInfo.User]
return r.userMap[metadata.ProcessInfo.UserName]
}
func (r *UserItem) String() string {