Allow node IDs with spaces/dashes in config. Use dashes in GUI. (ref #230)

This commit is contained in:
Jakob Borg 2014-05-18 12:05:17 +02:00
parent 729b0143e1
commit bbefcef53b
5 changed files with 74 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@ import (
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings"
"code.google.com/p/go.crypto/bcrypt" "code.google.com/p/go.crypto/bcrypt"
"github.com/calmh/syncthing/logger" "github.com/calmh/syncthing/logger"
@ -181,6 +182,14 @@ func Load(rd io.Reader, myID string) (Configuration, error) {
cfg.Repositories = []RepositoryConfiguration{} cfg.Repositories = []RepositoryConfiguration{}
} }
// Sanitize node IDs
for i := range cfg.Nodes {
node := &cfg.Nodes[i]
// Strip spaces and dashes
node.NodeID = strings.Replace(node.NodeID, "-", "", -1)
node.NodeID = strings.Replace(node.NodeID, " ", "", -1)
}
// Check for missing, bad or duplicate repository ID:s // Check for missing, bad or duplicate repository ID:s
var seenRepos = map[string]*RepositoryConfiguration{} var seenRepos = map[string]*RepositoryConfiguration{}
var uniqueCounter int var uniqueCounter int
@ -196,6 +205,13 @@ func Load(rd io.Reader, myID string) (Configuration, error) {
repo.ID = "default" repo.ID = "default"
} }
for i := range repo.Nodes {
node := &repo.Nodes[i]
// Strip spaces and dashes
node.NodeID = strings.Replace(node.NodeID, "-", "", -1)
node.NodeID = strings.Replace(node.NodeID, " ", "", -1)
}
if seen, ok := seenRepos[repo.ID]; ok { if seen, ok := seenRepos[repo.ID]; ok {
l.Warnf("Multiple repositories with ID %q; disabling", repo.ID) l.Warnf("Multiple repositories with ID %q; disabling", repo.ID)

View File

@ -224,3 +224,53 @@ func TestNodeAddresses(t *testing.T) {
t.Errorf("Nodes differ;\n E: %#v\n A: %#v", expected, cfg.Nodes) t.Errorf("Nodes differ;\n E: %#v\n A: %#v", expected, cfg.Nodes)
} }
} }
func TestStripNodeIs(t *testing.T) {
data := []byte(`
<configuration version="2">
<node id="AAAA-BBBB-CCCC">
<address>dynamic</address>
</node>
<node id="AAAA BBBB DDDD">
<address></address>
</node>
<node id="AAAABBBBEEEE">
<address></address>
</node>
<repository directory="~/Sync">
<node id="AAA ABBB-BCC CC" name=""></node>
<node id="AA-AAB BBBD-DDD" name=""></node>
<node id="AAA AB-BBB EEE-E" name=""></node>
</repository>
</configuration>
`)
expected := []NodeConfiguration{
{
NodeID: "AAAABBBBCCCC",
Addresses: []string{"dynamic"},
},
{
NodeID: "AAAABBBBDDDD",
Addresses: []string{"dynamic"},
},
{
NodeID: "AAAABBBBEEEE",
Addresses: []string{"dynamic"},
},
}
cfg, err := Load(bytes.NewReader(data), "n4")
if err != nil {
t.Error(err)
}
for i := range expected {
if !reflect.DeepEqual(cfg.Nodes[i], expected[i]) {
t.Errorf("Nodes[%d] differ;\n E: %#v\n A: %#v", i, expected[i], cfg.Nodes[i])
}
if cfg.Repositories[0].Nodes[i].NodeID != expected[i].NodeID {
t.Errorf("Repo nodes[%d] differ;\n E: %#v\n A: %#v", i, expected[i].NodeID, cfg.Repositories[0].Nodes[i].NodeID)
}
}
}

View File

@ -460,7 +460,7 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
$scope.init(); $scope.init();
setInterval($scope.refresh, refreshInterval); setInterval($scope.refresh, refreshInterval);
setInterval(heartbeat, 450); setInterval(heartbeat, 650);
}); });
function nodeCompare(a, b) { function nodeCompare(a, b) {
@ -581,7 +581,7 @@ syncthing.filter('chunkID', function () {
var parts = input.match(/.{1,6}/g); var parts = input.match(/.{1,6}/g);
if (!parts) if (!parts)
return ""; return "";
return parts.join(' '); return parts.join('-');
} }
}); });

View File

@ -368,10 +368,10 @@
<form role="form" name="nodeEditor"> <form role="form" name="nodeEditor">
<div class="form-group" ng-class="{'has-error': nodeEditor.nodeID.$invalid && nodeEditor.nodeID.$dirty}"> <div class="form-group" ng-class="{'has-error': nodeEditor.nodeID.$invalid && nodeEditor.nodeID.$dirty}">
<label for="nodeID">Node ID</label> <label for="nodeID">Node ID</label>
<input ng-if="!editingExisting" name="nodeID" id="nodeID" class="form-control" type="text" ng-model="currentNode.NodeID" required></input> <input ng-if="!editingExisting" name="nodeID" id="nodeID" class="form-control text-monospace" type="text" ng-model="currentNode.NodeID" required></input>
<div ng-if="editingExisting" class="well well-sm">{{currentNode.NodeID | chunkID}}</div> <div ng-if="editingExisting" class="well well-sm text-monospace">{{currentNode.NodeID | chunkID}}</div>
<p class="help-block"> <p class="help-block">
<span ng-if="nodeEditor.nodeID.$valid || nodeEditor.nodeID.$pristine">The node ID to enter here can be found in the "Add Node" dialog on the other node. Spaces are ignored.</span> <span ng-if="nodeEditor.nodeID.$valid || nodeEditor.nodeID.$pristine">The node ID to enter here can be found in the "Add Node" dialog on the other node. Spaces and dashes are optional (ignored).</span>
<span ng-if="nodeEditor.nodeID.$error.required && nodeEditor.nodeID.$dirty">The node ID cannot be blank.</span> <span ng-if="nodeEditor.nodeID.$error.required && nodeEditor.nodeID.$dirty">The node ID cannot be blank.</span>
</p> </p>
</div> </div>
@ -388,7 +388,7 @@
</form> </form>
<div ng-show="!editingExisting"> <div ng-show="!editingExisting">
When adding a new node, keep in mind that <em>this node</em> must be added on the other side too. The Node ID of this node is: When adding a new node, keep in mind that <em>this node</em> must be added on the other side too. The Node ID of this node is:
<pre>{{myID | chunkID}}</pre> <div class="well well-sm text-monospace">{{myID | chunkID}}</div>
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">