lib/connections: Fix and optimize registry (#7996)
Registry.Get used a full sort to get the minimum of a list, and the sort was broken because util.AddressUnspecifiedLess assumed it could find out whether an address is IPv4 or IPv6 from its Network method. However, net.(TCP|UDP)Addr.Network always returns "tcp"/"udp".
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
@@ -61,11 +60,6 @@ func socksDialerFunction(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error)
|
||||
return proxy.SOCKS5("tcp", u.Host, auth, forward)
|
||||
}
|
||||
|
||||
// Sort available addresses, preferring unspecified address.
|
||||
func tcpAddrLess(i interface{}, j interface{}) bool {
|
||||
return util.AddressUnspecifiedLess(i.(*net.TCPAddr), j.(*net.TCPAddr))
|
||||
}
|
||||
|
||||
// dialerConn is needed because proxy dialed connections have RemoteAddr() pointing at the proxy,
|
||||
// which then screws up various things such as IsLAN checks, and "let's populate the relay invitation address from
|
||||
// existing connection" shenanigans.
|
||||
|
||||
@@ -110,7 +110,9 @@ func DialContextReusePort(ctx context.Context, network, addr string) (net.Conn,
|
||||
return DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
localAddrInterface := registry.Get(network, tcpAddrLess)
|
||||
localAddrInterface := registry.Get(network, func(addr interface{}) bool {
|
||||
return addr.(*net.TCPAddr).IP.IsUnspecified()
|
||||
})
|
||||
if localAddrInterface == nil {
|
||||
// Nothing listening, nothing to reuse.
|
||||
return DialContext(ctx, network, addr)
|
||||
|
||||
Reference in New Issue
Block a user