From 8baf593b62454b612c145179560ed1d312637e3e Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 16 Jan 2019 14:52:27 +0000 Subject: [PATCH] Update source address selection when sintf specified --- src/yggdrasil/tcp.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index 1ebf0b69..ca9ea09b 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -230,35 +230,38 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string, sintf string) { ief, err := net.InterfaceByName(sintf) if err != nil { 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 } - addrs, err := ief.Addrs() - if err == nil { - dst, err := net.ResolveTCPAddr("tcp", saddr) + for addrindex, addr := range addrs { + src, _, err := net.ParseCIDR(addr.String()) if err != nil { - return + continue } - for _, addr := range addrs { - src, _, err := net.ParseCIDR(addr.String()) - if err != nil { - continue - } - if (src.To4() != nil) == (dst.IP.To4() != nil) && src.IsGlobalUnicast() { + if (src.To4() != nil) == (dst.IP.To4() != nil) { + if addrindex == len(addrs)-1 || src.IsGlobalUnicast() { dialer.LocalAddr = &net.TCPAddr{ IP: src, Port: 0, + Zone: sintf, } break } } - if dialer.LocalAddr == nil { - return - } + } + if dialer.LocalAddr == nil { + return } } } + conn, err = dialer.Dial("tcp", saddr) if err != nil { return