Browse Source

Allow multiple listenAddresses (fixes #52)

Jakob Borg 11 years ago
parent
commit
3cb7b8f22b
4 changed files with 50 additions and 18 deletions
  1. 0 0
      auto/gui.files.go
  2. 34 14
      config.go
  3. 3 1
      gui/app.js
  4. 13 3
      main.go

File diff suppressed because it is too large
+ 0 - 0
auto/gui.files.go


+ 34 - 14
config.go

@@ -27,20 +27,20 @@ type NodeConfiguration struct {
 }
 
 type OptionsConfiguration struct {
-	ListenAddress      string `xml:"listenAddress" default:":22000" ini:"listen-address"`
-	ReadOnly           bool   `xml:"readOnly" ini:"read-only"`
-	AllowDelete        bool   `xml:"allowDelete" default:"true" ini:"allow-delete"`
-	FollowSymlinks     bool   `xml:"followSymlinks" default:"true" ini:"follow-symlinks"`
-	GUIEnabled         bool   `xml:"guiEnabled" default:"true" ini:"gui-enabled"`
-	GUIAddress         string `xml:"guiAddress" default:"127.0.0.1:8080" ini:"gui-address"`
-	GlobalAnnServer    string `xml:"globalAnnounceServer" default:"syncthing.nym.se:22025" ini:"global-announce-server"`
-	GlobalAnnEnabled   bool   `xml:"globalAnnounceEnabled" default:"true" ini:"global-announce-enabled"`
-	LocalAnnEnabled    bool   `xml:"localAnnounceEnabled" default:"true" ini:"local-announce-enabled"`
-	ParallelRequests   int    `xml:"parallelRequests" default:"16" ini:"parallel-requests"`
-	MaxSendKbps        int    `xml:"maxSendKbps" ini:"max-send-kbps"`
-	RescanIntervalS    int    `xml:"rescanIntervalS" default:"60" ini:"rescan-interval"`
-	ReconnectIntervalS int    `xml:"reconnectionIntervalS" default:"60" ini:"reconnection-interval"`
-	MaxChangeKbps      int    `xml:"maxChangeKbps" default:"1000" ini:"max-change-bw"`
+	ListenAddress      []string `xml:"listenAddress" default:":22000" ini:"listen-address"`
+	ReadOnly           bool     `xml:"readOnly" ini:"read-only"`
+	AllowDelete        bool     `xml:"allowDelete" default:"true" ini:"allow-delete"`
+	FollowSymlinks     bool     `xml:"followSymlinks" default:"true" ini:"follow-symlinks"`
+	GUIEnabled         bool     `xml:"guiEnabled" default:"true" ini:"gui-enabled"`
+	GUIAddress         string   `xml:"guiAddress" default:"127.0.0.1:8080" ini:"gui-address"`
+	GlobalAnnServer    string   `xml:"globalAnnounceServer" default:"syncthing.nym.se:22025" ini:"global-announce-server"`
+	GlobalAnnEnabled   bool     `xml:"globalAnnounceEnabled" default:"true" ini:"global-announce-enabled"`
+	LocalAnnEnabled    bool     `xml:"localAnnounceEnabled" default:"true" ini:"local-announce-enabled"`
+	ParallelRequests   int      `xml:"parallelRequests" default:"16" ini:"parallel-requests"`
+	MaxSendKbps        int      `xml:"maxSendKbps" ini:"max-send-kbps"`
+	RescanIntervalS    int      `xml:"rescanIntervalS" default:"60" ini:"rescan-interval"`
+	ReconnectIntervalS int      `xml:"reconnectionIntervalS" default:"60" ini:"reconnection-interval"`
+	MaxChangeKbps      int      `xml:"maxChangeKbps" default:"1000" ini:"max-change-bw"`
 }
 
 func setDefaults(data interface{}) error {
@@ -57,6 +57,11 @@ func setDefaults(data interface{}) error {
 			case string:
 				f.SetString(v)
 
+			case []string:
+				rv := reflect.MakeSlice(reflect.TypeOf([]string{}), 1, 1)
+				rv.Index(0).SetString(v)
+				f.Set(rv)
+
 			case int:
 				i, err := strconv.ParseInt(v, 10, 64)
 				if err != nil {
@@ -121,6 +126,20 @@ func writeConfigXML(wr io.Writer, cfg Configuration) error {
 	return err
 }
 
+func uniqueStrings(ss []string) []string {
+	var m = make(map[string]bool, len(ss))
+	for _, s := range ss {
+		m[s] = true
+	}
+
+	var us = make([]string, 0, len(m))
+	for k := range m {
+		us = append(us, k)
+	}
+
+	return us
+}
+
 func readConfigXML(rd io.Reader) (Configuration, error) {
 	var cfg Configuration
 
@@ -132,5 +151,6 @@ func readConfigXML(rd io.Reader) (Configuration, error) {
 		err = xml.NewDecoder(rd).Decode(&cfg)
 	}
 
+	cfg.Options.ListenAddress = uniqueStrings(cfg.Options.ListenAddress)
 	return cfg, err
 }

+ 3 - 1
gui/app.js

@@ -11,7 +11,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
 
     // Strings before bools look better
     $scope.settings = [
-        {id: 'ListenAddress', descr:"Sync Protocol Listen Address", type: 'string', restart: true},
+        {id: 'ListenStr', descr:"Sync Protocol Listen Addresses", type: 'string', restart: true},
         {id: 'GUIAddress', descr: "GUI Listen Address", type: 'string', restart: true},
         {id: 'MaxSendKbps', descr: "Outgoing Rate Limit (KBps)", type: 'string', restart: true},
         {id: 'RescanIntervalS', descr: "Rescan Interval (s)", type: 'string', restart: true},
@@ -49,6 +49,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
 
         $http.get("/rest/config").success(function (data) {
             $scope.config = data;
+            $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(", ")
 
             var nodes = $scope.config.Repositories[0].Nodes;
             nodes = nodes.filter(function (x) { return x.NodeID != $scope.myID; });
@@ -170,6 +171,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
     };
 
     $scope.saveSettings = function () {
+        $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
         $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
         $('#settingsTable').collapse('hide');
     };

+ 13 - 3
main.go

@@ -208,13 +208,16 @@ func main() {
 	if verbose {
 		infoln("Listening for incoming connections")
 	}
-	go listen(myID, cfg.Options.ListenAddress, m, tlsCfg)
+	for _, addr := range cfg.Options.ListenAddress {
+		go listen(myID, addr, m, tlsCfg)
+	}
 
 	// Routine to connect out to configured nodes
 	if verbose {
 		infoln("Attempting to connect to other nodes")
 	}
-	go connect(myID, cfg.Options.ListenAddress, m, tlsCfg)
+	disc := discovery(cfg.Options.ListenAddress[0])
+	go connect(myID, disc, m, tlsCfg)
 
 	// Routine to pull blocks from other nodes to synchronize the local
 	// repository. Does not run when we are in read only (publish only) mode.
@@ -318,6 +321,9 @@ func printStatsLoop(m *model.Model) {
 }
 
 func listen(myID string, addr string, m *model.Model, tlsCfg *tls.Config) {
+	if strings.Contains(trace, "connect") {
+		debugln("NET: Listening on", addr)
+	}
 	l, err := tls.Listen("tcp", addr, tlsCfg)
 	fatalErr(err)
 
@@ -369,7 +375,7 @@ listen:
 	}
 }
 
-func connect(myID string, addr string, m *model.Model, tlsCfg *tls.Config) {
+func discovery(addr string) *discover.Discoverer {
 	_, portstr, err := net.SplitHostPort(addr)
 	fatalErr(err)
 	port, _ := strconv.Atoi(portstr)
@@ -392,6 +398,10 @@ func connect(myID string, addr string, m *model.Model, tlsCfg *tls.Config) {
 		warnf("No discovery possible (%v)", err)
 	}
 
+	return disc
+}
+
+func connect(myID string, disc *discover.Discoverer, m *model.Model, tlsCfg *tls.Config) {
 	connOpts := map[string]string{
 		"clientId":      "syncthing",
 		"clientVersion": Version,

Some files were not shown because too many files changed in this diff