github.com/kelleygo/clashcore@v1.0.2/component/dialer/bind_linux.go (about) 1 package dialer 2 3 import ( 4 "context" 5 "net" 6 "net/netip" 7 "syscall" 8 9 "golang.org/x/sys/unix" 10 ) 11 12 func bindControl(ifaceName string) controlFn { 13 return func(ctx context.Context, network, address string, c syscall.RawConn) (err error) { 14 addrPort, err := netip.ParseAddrPort(address) 15 if err == nil && !addrPort.Addr().IsGlobalUnicast() { 16 return 17 } 18 19 var innerErr error 20 err = c.Control(func(fd uintptr) { 21 innerErr = unix.BindToDevice(int(fd), ifaceName) 22 }) 23 24 if innerErr != nil { 25 err = innerErr 26 } 27 28 return 29 } 30 } 31 32 func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, _ string, _ netip.Addr) error { 33 addControlToDialer(dialer, bindControl(ifaceName)) 34 35 return nil 36 } 37 38 func bindIfaceToListenConfig(ifaceName string, lc *net.ListenConfig, _, address string, rAddrPort netip.AddrPort) (string, error) { 39 addControlToListenConfig(lc, bindControl(ifaceName)) 40 41 return address, nil 42 } 43 44 func ParseNetwork(network string, addr netip.Addr) string { 45 return network 46 }