Skip to content

Commit 8bf4113

Browse files
committed
Ignore system bind error
1 parent 9c6e70b commit 8bf4113

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

stack.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/netip"
66

7+
"github.com/sagernet/sing/common/control"
78
E "github.com/sagernet/sing/common/exceptions"
89
"github.com/sagernet/sing/common/logger"
910
)
@@ -25,7 +26,8 @@ type StackOptions struct {
2526
Router Router
2627
Handler Handler
2728
Logger logger.Logger
28-
UnderPlatform bool
29+
ForwarderBindInterface bool
30+
InterfaceFinder control.InterfaceFinder
2931
}
3032

3133
func NewStack(

system.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type System struct {
4040
tcpNat *TCPNat
4141
udpNat *udpnat.Service[netip.AddrPort]
4242
routeMapping *RouteMapping
43-
underPlatform bool
43+
bindInterface bool
44+
interfaceFinder control.InterfaceFinder
4445
}
4546

4647
type Session struct {
@@ -52,17 +53,18 @@ type Session struct {
5253

5354
func NewSystem(options StackOptions) (Stack, error) {
5455
stack := &System{
55-
ctx: options.Context,
56-
tun: options.Tun,
57-
tunName: options.Name,
58-
mtu: options.MTU,
59-
udpTimeout: options.UDPTimeout,
60-
router: options.Router,
61-
handler: options.Handler,
62-
logger: options.Logger,
63-
inet4Prefixes: options.Inet4Address,
64-
inet6Prefixes: options.Inet6Address,
65-
underPlatform: options.UnderPlatform,
56+
ctx: options.Context,
57+
tun: options.Tun,
58+
tunName: options.Name,
59+
mtu: options.MTU,
60+
udpTimeout: options.UDPTimeout,
61+
router: options.Router,
62+
handler: options.Handler,
63+
logger: options.Logger,
64+
inet4Prefixes: options.Inet4Address,
65+
inet6Prefixes: options.Inet6Address,
66+
bindInterface: options.ForwarderBindInterface,
67+
interfaceFinder: options.InterfaceFinder,
6668
}
6769
if stack.router != nil {
6870
stack.routeMapping = NewRouteMapping(options.UDPTimeout)
@@ -96,8 +98,14 @@ func (s *System) Close() error {
9698

9799
func (s *System) Start() error {
98100
var listener net.ListenConfig
99-
if s.underPlatform {
100-
listener.Control = control.Append(listener.Control, control.BindToInterface(control.DefaultInterfaceFinder(), s.tunName, -1))
101+
if s.bindInterface {
102+
listener.Control = control.Append(listener.Control, func(network, address string, conn syscall.RawConn) error {
103+
err := control.BindToInterface(s.interfaceFinder, s.tunName, -1)(network, address, conn)
104+
if err != nil {
105+
s.logger.Warn("bind forwarder to interface: ", err)
106+
}
107+
return nil
108+
})
101109
}
102110
if s.inet4Address.IsValid() {
103111
tcpListener, err := listener.Listen(s.ctx, "tcp4", net.JoinHostPort(s.inet4ServerAddress.String(), "0"))

0 commit comments

Comments
 (0)