瀏覽代碼

Create a correct URL is more difficult than just slapping on a scheme (fixes #2316)

Jakob Borg 10 年之前
父節點
當前提交
08c8d679ac
共有 2 個文件被更改,包括 22 次插入6 次删除
  1. 11 3
      cmd/syncthing/addresslister.go
  2. 11 3
      lib/config/config.go

+ 11 - 3
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()
+}

+ 11 - 3
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()
+}