diff --git a/stack.go b/stack.go index 4ba515b..bd50e90 100644 --- a/stack.go +++ b/stack.go @@ -25,7 +25,7 @@ type StackOptions struct { Router Router Handler Handler Logger logger.Logger - UnderPlatform bool + ForwarderBindInterface bool } func NewStack( diff --git a/system.go b/system.go index ee59908..7943dc6 100644 --- a/system.go +++ b/system.go @@ -40,7 +40,7 @@ type System struct { tcpNat *TCPNat udpNat *udpnat.Service[netip.AddrPort] routeMapping *RouteMapping - underPlatform bool + bindInterface bool } type Session struct { @@ -62,7 +62,7 @@ func NewSystem(options StackOptions) (Stack, error) { logger: options.Logger, inet4Prefixes: options.Inet4Address, inet6Prefixes: options.Inet6Address, - underPlatform: options.UnderPlatform, + bindInterface: options.ForwarderBindInterface, } if stack.router != nil { stack.routeMapping = NewRouteMapping(options.UDPTimeout) @@ -96,8 +96,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(nil, 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"))