mihomo/component/dialer/bind_windows.go

102 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-02-13 10:14:59 +08:00
package dialer
import (
"context"
2023-02-13 10:14:59 +08:00
"encoding/binary"
2023-04-20 10:22:51 +08:00
"fmt"
2023-02-13 10:14:59 +08:00
"net"
"net/netip"
"syscall"
"unsafe"
"github.com/Dreamacro/clash/component/iface"
)
const (
IP_UNICAST_IF = 31
IPV6_UNICAST_IF = 31
)
func bind4(handle syscall.Handle, ifaceIdx int) error {
var bytes [4]byte
binary.BigEndian.PutUint32(bytes[:], uint32(ifaceIdx))
idx := *(*uint32)(unsafe.Pointer(&bytes[0]))
2023-04-20 10:22:51 +08:00
err := syscall.SetsockoptInt(handle, syscall.IPPROTO_IP, IP_UNICAST_IF, int(idx))
if err != nil {
err = fmt.Errorf("bind4: %w", err)
}
return err
2023-02-13 10:14:59 +08:00
}
func bind6(handle syscall.Handle, ifaceIdx int) error {
2023-04-20 10:22:51 +08:00
err := syscall.SetsockoptInt(handle, syscall.IPPROTO_IPV6, IPV6_UNICAST_IF, ifaceIdx)
if err != nil {
err = fmt.Errorf("bind6: %w", err)
}
return err
2023-02-13 10:14:59 +08:00
}
func bindControl(ifaceIdx int) controlFn {
return func(ctx context.Context, network, address string, c syscall.RawConn) (err error) {
2023-02-13 10:14:59 +08:00
addrPort, err := netip.ParseAddrPort(address)
if err == nil && !addrPort.Addr().IsGlobalUnicast() {
return
}
var innerErr error
err = c.Control(func(fd uintptr) {
handle := syscall.Handle(fd)
bind6err := bind6(handle, ifaceIdx)
bind4err := bind4(handle, ifaceIdx)
2023-02-13 10:14:59 +08:00
switch network {
case "ip6", "tcp6":
innerErr = bind6err
case "ip4", "tcp4", "udp4":
innerErr = bind4err
case "udp6":
// golang will set network to udp6 when listenUDP on wildcard ip (eg: ":0", "")
if (!addrPort.Addr().IsValid() || addrPort.Addr().IsUnspecified()) && bind6err != nil {
// try bind ipv6, if failed, ignore. it's a workaround for windows disable interface ipv6
if bind4err != nil {
2023-04-20 10:22:51 +08:00
innerErr = fmt.Errorf("%w (%s)", bind6err, bind4err)
} else {
2023-04-20 10:22:51 +08:00
innerErr = nil
}
} else {
innerErr = bind6err
}
2023-02-13 10:14:59 +08:00
}
})
if innerErr != nil {
err = innerErr
}
return
}
}
func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, _ string, _ netip.Addr) error {
ifaceObj, err := iface.ResolveInterface(ifaceName)
if err != nil {
return err
}
addControlToDialer(dialer, bindControl(ifaceObj.Index))
2023-02-13 10:14:59 +08:00
return nil
}
func bindIfaceToListenConfig(ifaceName string, lc *net.ListenConfig, _, address string) (string, error) {
ifaceObj, err := iface.ResolveInterface(ifaceName)
if err != nil {
return "", err
}
addControlToListenConfig(lc, bindControl(ifaceObj.Index))
2023-02-13 10:14:59 +08:00
return address, nil
}
func ParseNetwork(network string, addr netip.Addr) string {
return network
}