瀏覽代碼

Config option to enable/disable UPnP

Jakob Borg 11 年之前
父節點
當前提交
a08cba9c85
共有 5 個文件被更改,包括 39 次插入26 次删除
  1. 0 0
      auto/gui.files.go
  2. 1 0
      cmd/syncthing/config.go
  3. 3 0
      cmd/syncthing/config_test.go
  4. 34 26
      cmd/syncthing/main.go
  5. 1 0
      gui/app.js

文件差異過大導致無法顯示
+ 0 - 0
auto/gui.files.go


+ 1 - 0
cmd/syncthing/config.go

@@ -51,6 +51,7 @@ type OptionsConfiguration struct {
 	ReconnectIntervalS int      `xml:"reconnectionIntervalS" default:"60"`
 	ReconnectIntervalS int      `xml:"reconnectionIntervalS" default:"60"`
 	MaxChangeKbps      int      `xml:"maxChangeKbps" default:"1000"`
 	MaxChangeKbps      int      `xml:"maxChangeKbps" default:"1000"`
 	StartBrowser       bool     `xml:"startBrowser" default:"true"`
 	StartBrowser       bool     `xml:"startBrowser" default:"true"`
+	UPnPEnabled        bool     `xml:"upnpEnabled" default:"true"`
 
 
 	Deprecated_ReadOnly   bool   `xml:"readOnly,omitempty"`
 	Deprecated_ReadOnly   bool   `xml:"readOnly,omitempty"`
 	Deprecated_GUIEnabled bool   `xml:"guiEnabled,omitempty"`
 	Deprecated_GUIEnabled bool   `xml:"guiEnabled,omitempty"`

+ 3 - 0
cmd/syncthing/config_test.go

@@ -19,6 +19,7 @@ func TestDefaultValues(t *testing.T) {
 		ReconnectIntervalS: 60,
 		ReconnectIntervalS: 60,
 		MaxChangeKbps:      1000,
 		MaxChangeKbps:      1000,
 		StartBrowser:       true,
 		StartBrowser:       true,
+		UPnPEnabled:        true,
 	}
 	}
 
 
 	cfg, err := readConfigXML(bytes.NewReader(nil))
 	cfg, err := readConfigXML(bytes.NewReader(nil))
@@ -149,6 +150,7 @@ func TestOverriddenValues(t *testing.T) {
         <reconnectionIntervalS>6000</reconnectionIntervalS>
         <reconnectionIntervalS>6000</reconnectionIntervalS>
         <maxChangeKbps>2345</maxChangeKbps>
         <maxChangeKbps>2345</maxChangeKbps>
         <startBrowser>false</startBrowser>
         <startBrowser>false</startBrowser>
+        <upnpEnabled>false</upnpEnabled>
     </options>
     </options>
 </configuration>
 </configuration>
 `)
 `)
@@ -164,6 +166,7 @@ func TestOverriddenValues(t *testing.T) {
 		ReconnectIntervalS: 6000,
 		ReconnectIntervalS: 6000,
 		MaxChangeKbps:      2345,
 		MaxChangeKbps:      2345,
 		StartBrowser:       false,
 		StartBrowser:       false,
+		UPnPEnabled:        false,
 	}
 	}
 
 
 	cfg, err := readConfigXML(bytes.NewReader(data))
 	cfg, err := readConfigXML(bytes.NewReader(data))

+ 34 - 26
cmd/syncthing/main.go

@@ -234,32 +234,8 @@ func main() {
 	// UPnP
 	// UPnP
 
 
 	var externalPort = 0
 	var externalPort = 0
-	if len(cfg.Options.ListenAddress) == 1 {
-		_, portStr, err := net.SplitHostPort(cfg.Options.ListenAddress[0])
-		if err != nil {
-			warnln(err)
-		} else {
-			// Set up incoming port forwarding, if necessary and possible
-			port, _ := strconv.Atoi(portStr)
-			igd, err := upnp.Discover()
-			if err == nil {
-				for i := 0; i < 10; i++ {
-					err := igd.AddPortMapping(upnp.TCP, port+i, port, "syncthing", 0)
-					if err == nil {
-						externalPort = port + i
-						infoln("Created UPnP port mapping - external port", externalPort)
-						break
-					}
-				}
-				if externalPort == 0 {
-					warnln("Failed to create UPnP port mapping")
-				}
-			} else {
-				infof("No UPnP IGD device found, no port mapping created (%v)", err)
-			}
-		}
-	} else {
-		warnln("Multiple listening addresses; not attempting UPnP port mapping")
+	if cfg.Options.UPnPEnabled {
+		externalPort = setupUPnP()
 	}
 	}
 
 
 	// Routine to connect out to configured nodes
 	// Routine to connect out to configured nodes
@@ -290,6 +266,38 @@ func main() {
 	<-stop
 	<-stop
 }
 }
 
 
+func setupUPnP() int {
+	var externalPort = 0
+	if len(cfg.Options.ListenAddress) == 1 {
+		_, portStr, err := net.SplitHostPort(cfg.Options.ListenAddress[0])
+		if err != nil {
+			warnln(err)
+		} else {
+			// Set up incoming port forwarding, if necessary and possible
+			port, _ := strconv.Atoi(portStr)
+			igd, err := upnp.Discover()
+			if err == nil {
+				for i := 0; i < 10; i++ {
+					err := igd.AddPortMapping(upnp.TCP, port+i, port, "syncthing", 0)
+					if err == nil {
+						externalPort = port + i
+						infoln("Created UPnP port mapping - external port", externalPort)
+						break
+					}
+				}
+				if externalPort == 0 {
+					warnln("Failed to create UPnP port mapping")
+				}
+			} else {
+				infof("No UPnP IGD device found, no port mapping created (%v)", err)
+			}
+		}
+	} else {
+		warnln("Multiple listening addresses; not attempting UPnP port mapping")
+	}
+	return externalPort
+}
+
 func resetRepositories() {
 func resetRepositories() {
 	suffix := fmt.Sprintf(".syncthing-reset-%d", time.Now().UnixNano())
 	suffix := fmt.Sprintf(".syncthing-reset-%d", time.Now().UnixNano())
 	for _, repo := range cfg.Repositories {
 	for _, repo := range cfg.Repositories {

+ 1 - 0
gui/app.js

@@ -32,6 +32,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
     {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
     {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
     {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
     {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
     {id: 'StartBrowser', descr: 'Start Browser', type: 'bool'},
     {id: 'StartBrowser', descr: 'Start Browser', type: 'bool'},
+    {id: 'UPnPEnabled', descr: 'Enable UPnP', type: 'bool'},
     ];
     ];
 
 
     $scope.guiSettings = [
     $scope.guiSettings = [

部分文件因文件數量過多而無法顯示