mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-16 06:32:22 +08:00
Skip bind connection with private destination to interface
This commit is contained in:
parent
b797cdf91e
commit
e839beb73b
19
common/dialer/bind.go
Normal file
19
common/dialer/bind.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package dialer
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/sagernet/sing/common/control"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
func skipIfPrivate(next control.Func) control.Func {
|
||||
return func(network, address string, conn syscall.RawConn) error {
|
||||
destination := M.ParseSocksaddr(address)
|
||||
if !N.IsPublicAddr(destination.Addr) {
|
||||
return nil
|
||||
}
|
||||
return next(network, address, conn)
|
||||
}
|
||||
}
|
|
@ -61,25 +61,25 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
|
|||
var listener net.ListenConfig
|
||||
if options.BindInterface != "" {
|
||||
warnBindInterfaceOnUnsupportedPlatform.Check()
|
||||
bindFunc := control.BindToInterface(router.InterfaceBindManager(), options.BindInterface)
|
||||
bindFunc := skipIfPrivate(control.BindToInterface(router.InterfaceBindManager(), options.BindInterface))
|
||||
dialer.Control = control.Append(dialer.Control, bindFunc)
|
||||
listener.Control = control.Append(listener.Control, bindFunc)
|
||||
} else if router.AutoDetectInterface() {
|
||||
if C.IsWindows {
|
||||
bindFunc := control.BindToInterfaceIndexFunc(func() int {
|
||||
bindFunc := skipIfPrivate(control.BindToInterfaceIndexFunc(func() int {
|
||||
return router.InterfaceMonitor().DefaultInterfaceIndex()
|
||||
})
|
||||
}))
|
||||
dialer.Control = control.Append(dialer.Control, bindFunc)
|
||||
listener.Control = control.Append(listener.Control, bindFunc)
|
||||
} else {
|
||||
bindFunc := control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string {
|
||||
bindFunc := skipIfPrivate(control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string {
|
||||
return router.InterfaceMonitor().DefaultInterfaceName()
|
||||
})
|
||||
}))
|
||||
dialer.Control = control.Append(dialer.Control, bindFunc)
|
||||
listener.Control = control.Append(listener.Control, bindFunc)
|
||||
}
|
||||
} else if router.DefaultInterface() != "" {
|
||||
bindFunc := control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface())
|
||||
bindFunc := skipIfPrivate(control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface()))
|
||||
dialer.Control = control.Append(dialer.Control, bindFunc)
|
||||
listener.Control = control.Append(listener.Control, bindFunc)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user