Skip to content

Commit

Permalink
Ignore system bind error
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Apr 20, 2023
1 parent 9c6e70b commit 8bf4113
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
4 changes: 3 additions & 1 deletion stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -25,7 +26,8 @@ type StackOptions struct {
Router Router
Handler Handler
Logger logger.Logger
UnderPlatform bool
ForwarderBindInterface bool
InterfaceFinder control.InterfaceFinder
}

func NewStack(
Expand Down
36 changes: 22 additions & 14 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit 8bf4113

Please sign in to comment.