@@ -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
4647type Session struct {
@@ -52,17 +53,18 @@ type Session struct {
5253
5354func 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
9799func (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