lib: Factor out getting IP address from net.Addr (#8538)

... and add fast paths for common cases.
This commit is contained in:
greatroar
2022-09-14 08:44:46 +02:00
committed by GitHub
parent 6e768a8387
commit 8065cf7e97
7 changed files with 26 additions and 29 deletions
+2 -9
View File
@@ -12,6 +12,7 @@ import (
"time"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/osutil"
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/relay/protocol"
)
@@ -66,7 +67,7 @@ func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingproto
l.Debugln("Received invitation", msg, "via", conn.LocalAddr())
ip := net.IP(msg.Address)
if len(ip) == 0 || ip.IsUnspecified() {
msg.Address = remoteIPBytes(conn)
msg.Address, _ = osutil.IPFromAddr(conn.RemoteAddr())
}
return msg, nil
default:
@@ -163,11 +164,3 @@ func configForCerts(certs []tls.Certificate) *tls.Config {
},
}
}
func remoteIPBytes(conn net.Conn) []byte {
addr := conn.RemoteAddr().String()
if host, _, err := net.SplitHostPort(addr); err == nil {
addr = host
}
return net.ParseIP(addr)[:]
}