mihomo/component/dialer/bind_linux.go

47 lines
955 B
Go
Raw Permalink Normal View History

package dialer
import (
"context"
"net"
2022-04-20 01:52:51 +08:00
"net/netip"
"syscall"
"golang.org/x/sys/unix"
)
func bindControl(ifaceName string) controlFn {
return func(ctx context.Context, network, address string, c syscall.RawConn) (err error) {
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 {
addControlToDialer(dialer, bindControl(ifaceName))
return nil
}
func bindIfaceToListenConfig(ifaceName string, lc *net.ListenConfig, _, address string) (string, error) {
addControlToListenConfig(lc, bindControl(ifaceName))
return address, nil
}
2023-02-13 10:14:59 +08:00
func ParseNetwork(network string, addr netip.Addr) string {
return network
}