fix(dialer): skip SO_BINDTODEVICE when interface is specified as IP address

When the user specifies an IP address as the interface (e.g.
interface=10.1.1.1), GOST resolved it to its hosting interface and
called SO_BINDTODEVICE on it — breaking outbound connectivity for
IP aliases on loopback or dummy interfaces (go-gost/gost#785).

The initial fix (caa82f2) only skipped SO_BINDTODEVICE for loopback
interfaces, missing non-loopback dummy interfaces like ip-aliases.

This change adds an isIP return value to ParseInterfaceAddr so callers
can distinguish 'user specified an IP' (intent: source IP binding for
policy routing — skip SO_BINDTODEVICE) from 'user specified an
interface name' (intent: force traffic through that NIC — keep
SO_BINDTODEVICE). The dialOnce method now only calls bindDevice when
bindToDevice is true AND the input was an interface name (!isIP).

The loopback-specific guard in bindDevice is reverted — superseded by
the broader isIP-based fix that covers all interface types.
This commit is contained in:
ginuerzh
2026-06-23 21:43:54 +08:00
parent a32ffd5608
commit c06ba17916
4 changed files with 31 additions and 20 deletions
+7 -1
View File
@@ -14,7 +14,11 @@ import (
// addresses assigned to that interface. The network parameter determines the address
// type (TCPAddr for "tcp"/"tcp4"/"tcp6", UDPAddr for "udp"/"udp4"/"udp6", IPAddr
// otherwise).
func ParseInterfaceAddr(ifceName, network string) (ifce string, addr []net.Addr, err error) {
//
// isIP reports whether ifceName was parsed as an IP address (true) rather than an
// interface name (false). When isIP is true, callers should skip SO_BINDTODEVICE —
// the user's intent is source IP binding for policy routing, not device binding.
func ParseInterfaceAddr(ifceName, network string) (ifce string, addr []net.Addr, isIP bool, err error) {
if ifceName == "" {
addr = append(addr, nil)
return
@@ -43,6 +47,8 @@ func ParseInterfaceAddr(ifceName, network string) (ifce string, addr []net.Addr,
}
}
} else {
// ifceName is an IP address — skip SO_BINDTODEVICE, use LocalAddr only
isIP = true
ifce, err = findInterfaceByIP(ip)
if err != nil {
return