From 8bf411334315e7229da5a73ef3694e7e83b3d10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 20 Apr 2023 09:09:11 +0800 Subject: [PATCH] Ignore system bind error --- stack.go | 4 +++- system.go | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/stack.go b/stack.go index 4ba515b..71a183d 100644 --- a/stack.go +++ b/stack.go @@ -4,6 +4,7 @@ import ( "context" "net/netip" + "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/logger" ) @@ -25,7 +26,8 @@ type StackOptions struct { Router Router Handler Handler Logger logger.Logger - UnderPlatform bool + ForwarderBindInterface bool + InterfaceFinder control.InterfaceFinder } func NewStack( diff --git a/system.go b/system.go index ee59908..356c93b 100644 --- a/system.go +++ b/system.go @@ -40,7 +40,8 @@ type System struct { tcpNat *TCPNat udpNat *udpnat.Service[netip.AddrPort] routeMapping *RouteMapping - underPlatform bool + bindInterface bool + interfaceFinder control.InterfaceFinder } type Session struct { @@ -52,17 +53,18 @@ type Session struct { func NewSystem(options StackOptions) (Stack, error) { stack := &System{ - ctx: options.Context, - tun: options.Tun, - tunName: options.Name, - mtu: options.MTU, - udpTimeout: options.UDPTimeout, - router: options.Router, - handler: options.Handler, - logger: options.Logger, - inet4Prefixes: options.Inet4Address, - inet6Prefixes: options.Inet6Address, - underPlatform: options.UnderPlatform, + ctx: options.Context, + tun: options.Tun, + tunName: options.Name, + mtu: options.MTU, + udpTimeout: options.UDPTimeout, + router: options.Router, + handler: options.Handler, + logger: options.Logger, + inet4Prefixes: options.Inet4Address, + inet6Prefixes: options.Inet6Address, + bindInterface: options.ForwarderBindInterface, + interfaceFinder: options.InterfaceFinder, } if stack.router != nil { stack.routeMapping = NewRouteMapping(options.UDPTimeout) @@ -96,8 +98,14 @@ func (s *System) Close() error { func (s *System) Start() error { var listener net.ListenConfig - if s.underPlatform { - listener.Control = control.Append(listener.Control, control.BindToInterface(control.DefaultInterfaceFinder(), s.tunName, -1)) + if s.bindInterface { + listener.Control = control.Append(listener.Control, func(network, address string, conn syscall.RawConn) error { + err := control.BindToInterface(s.interfaceFinder, s.tunName, -1)(network, address, conn) + if err != nil { + s.logger.Warn("bind forwarder to interface: ", err) + } + return nil + }) } if s.inet4Address.IsValid() { tcpListener, err := listener.Listen(s.ctx, "tcp4", net.JoinHostPort(s.inet4ServerAddress.String(), "0"))