5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-01 21:51:36 +00:00

Update source address selection when sintf specified

This commit is contained in:
Neil Alexander 2019-01-16 14:52:27 +00:00
parent c3bd3bd140
commit 8baf593b62
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -230,35 +230,38 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string, sintf string) {
ief, err := net.InterfaceByName(sintf) ief, err := net.InterfaceByName(sintf)
if err != nil { if err != nil {
return return
} else { }
if ief.Flags&net.FlagUp == 0 { if ief.Flags&net.FlagUp == 0 {
return
}
addrs, err := ief.Addrs()
if err == nil {
dst, err := net.ResolveTCPAddr("tcp", saddr)
if err != nil {
return return
} }
addrs, err := ief.Addrs() for addrindex, addr := range addrs {
if err == nil { src, _, err := net.ParseCIDR(addr.String())
dst, err := net.ResolveTCPAddr("tcp", saddr)
if err != nil { if err != nil {
return continue
} }
for _, addr := range addrs { if (src.To4() != nil) == (dst.IP.To4() != nil) {
src, _, err := net.ParseCIDR(addr.String()) if addrindex == len(addrs)-1 || src.IsGlobalUnicast() {
if err != nil {
continue
}
if (src.To4() != nil) == (dst.IP.To4() != nil) && src.IsGlobalUnicast() {
dialer.LocalAddr = &net.TCPAddr{ dialer.LocalAddr = &net.TCPAddr{
IP: src, IP: src,
Port: 0, Port: 0,
Zone: sintf,
} }
break break
} }
} }
if dialer.LocalAddr == nil { }
return if dialer.LocalAddr == nil {
} return
} }
} }
} }
conn, err = dialer.Dial("tcp", saddr) conn, err = dialer.Dial("tcp", saddr)
if err != nil { if err != nil {
return return