diff --git a/cmd/syncthing/addresslister.go b/cmd/syncthing/addresslister.go index 5f85c6e93..89b48c6a2 100644 --- a/cmd/syncthing/addresslister.go +++ b/cmd/syncthing/addresslister.go @@ -63,13 +63,13 @@ func (e *addressLister) addresses(includePrivateIPV4 bool) []string { if addr.IP == nil || addr.IP.IsUnspecified() { // Address like 0.0.0.0:22000 or [::]:22000 or :22000; include as is. - addrs = append(addrs, "tcp://"+addr.String()) + addrs = append(addrs, tcpAddr(addr.String())) } else if isPublicIPv4(addr.IP) || isPublicIPv6(addr.IP) { // A public address; include as is. - addrs = append(addrs, "tcp://"+addr.String()) + addrs = append(addrs, tcpAddr(addr.String())) } else if includePrivateIPV4 && addr.IP.To4().IsGlobalUnicast() { // A private IPv4 address. - addrs = append(addrs, "tcp://"+addr.String()) + addrs = append(addrs, tcpAddr(addr.String())) } } @@ -117,3 +117,11 @@ func isPublicIPv6(ip net.IP) bool { return ip.IsGlobalUnicast() } + +func tcpAddr(host string) string { + u := url.URL{ + Scheme: "tcp", + Host: host, + } + return u.String() +} diff --git a/lib/config/config.go b/lib/config/config.go index a8e48f4ac..452021075 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -9,9 +9,9 @@ package config import ( "encoding/xml" - "fmt" "io" "math/rand" + "net/url" "os" "path/filepath" "reflect" @@ -293,14 +293,14 @@ func convertV11V12(cfg *Configuration) { // Change listen address schema for i, addr := range cfg.Options.ListenAddress { if len(addr) > 0 && !strings.HasPrefix(addr, "tcp://") { - cfg.Options.ListenAddress[i] = fmt.Sprintf("tcp://%s", addr) + cfg.Options.ListenAddress[i] = tcpAddr(addr) } } for i, device := range cfg.Devices { for j, addr := range device.Addresses { if addr != "dynamic" && addr != "" { - cfg.Devices[i].Addresses[j] = fmt.Sprintf("tcp://%s", addr) + cfg.Devices[i].Addresses[j] = tcpAddr(addr) } } } @@ -499,3 +499,11 @@ func randomString(l int) string { } return string(bs) } + +func tcpAddr(host string) string { + u := url.URL{ + Scheme: "tcp", + Host: host, + } + return u.String() +}