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 db67add
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type StackOptions struct {
Router Router
Handler Handler
Logger logger.Logger
UnderPlatform bool
ForwarderBindInterface bool
}

func NewStack(
Expand Down
14 changes: 10 additions & 4 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type System struct {
tcpNat *TCPNat
udpNat *udpnat.Service[netip.AddrPort]
routeMapping *RouteMapping
underPlatform bool
bindInterface bool
}

type Session struct {
Expand All @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit db67add

Please sign in to comment.