Clash.Meta/component/dialer/bind_linux.go

54 lines
1.1 KiB
Go
Raw Normal View History

package dialer
import (
"net"
2022-04-20 01:52:51 +08:00
"net/netip"
"syscall"
"golang.org/x/sys/unix"
)
type controlFn = func(network, address string, c syscall.RawConn) error
func bindControl(ifaceName string, chain controlFn) controlFn {
return func(network, address string, c syscall.RawConn) (err error) {
defer func() {
if err == nil && chain != nil {
err = chain(network, address, c)
}
}()
2022-04-20 01:52:51 +08:00
addrPort, err := netip.ParseAddrPort(address)
if err == nil && !addrPort.Addr().IsGlobalUnicast() {
return
}
2021-11-17 16:03:47 +08:00
var innerErr error
err = c.Control(func(fd uintptr) {
innerErr = unix.BindToDevice(int(fd), ifaceName)
})
2021-11-17 16:03:47 +08:00
if innerErr != nil {
err = innerErr
}
return
}
}
2022-04-20 01:52:51 +08:00
func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, _ string, _ netip.Addr) error {
dialer.Control = bindControl(ifaceName, dialer.Control)
return nil
}
func bindIfaceToListenConfig(ifaceName string, lc *net.ListenConfig, _, address string) (string, error) {
lc.Control = bindControl(ifaceName, lc.Control)
return address, nil
}
2023-02-13 10:14:59 +08:00
func ParseNetwork(network string, addr netip.Addr) string {
return network
}