From 3388d5b49c6a7f3a82ad3d14150fe118ede925fe Mon Sep 17 00:00:00 2001
From: Jakob Borg
Date: Fri, 18 Jul 2014 10:00:02 +0200
Subject: [PATCH] Use backend service to verify nodeID (fixes #418)
---
cmd/syncthing/gui.go | 18 ++++++++++++++++++
gui/app.js | 15 ++++++++-------
gui/index.html | 2 +-
protocol/nodeid.go | 2 --
4 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go
index ae494b3aa..e8b3d24ad 100644
--- a/cmd/syncthing/gui.go
+++ b/cmd/syncthing/gui.go
@@ -30,6 +30,7 @@ import (
"github.com/calmh/syncthing/events"
"github.com/calmh/syncthing/logger"
"github.com/calmh/syncthing/model"
+ "github.com/calmh/syncthing/protocol"
"github.com/vitrun/qart/qr"
)
@@ -103,6 +104,7 @@ func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) erro
getRestMux.HandleFunc("/rest/report", withModel(m, restGetReport))
getRestMux.HandleFunc("/rest/events", restGetEvents)
getRestMux.HandleFunc("/rest/upgrade", restGetUpgrade)
+ getRestMux.HandleFunc("/rest/nodeid", restGetNodeID)
// The POST handlers
postRestMux := http.NewServeMux()
@@ -444,6 +446,22 @@ func restGetUpgrade(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(res)
}
+func restGetNodeID(w http.ResponseWriter, r *http.Request) {
+ qs := r.URL.Query()
+ idStr := qs.Get("id")
+ id, err := protocol.NodeIDFromString(idStr)
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
+ if err == nil {
+ json.NewEncoder(w).Encode(map[string]string{
+ "id": id.String(),
+ })
+ } else {
+ json.NewEncoder(w).Encode(map[string]string{
+ "error": err.Error(),
+ })
+ }
+}
+
func restPostUpgrade(w http.ResponseWriter, r *http.Request) {
err := upgrade()
if err != nil {
diff --git a/gui/app.js b/gui/app.js
index 2b4cefb14..e8241ac23 100644
--- a/gui/app.js
+++ b/gui/app.js
@@ -865,7 +865,7 @@ syncthing.directive('uniqueRepo', function() {
};
});
-syncthing.directive('validNodeid', function() {
+syncthing.directive('validNodeid', function($http) {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
@@ -874,12 +874,13 @@ syncthing.directive('validNodeid', function() {
// we shouldn't validate
ctrl.$setValidity('validNodeid', true);
} else {
- var cleaned = viewValue.replace(/ /g, '').replace(/-/g, '').toLowerCase().trim();
- if (cleaned.match(/^[a-z2-7]{52}$/)) {
- ctrl.$setValidity('validNodeid', true);
- } else {
- ctrl.$setValidity('validNodeid', false);
- }
+ $http.get(urlbase + '/nodeid?id='+viewValue).success(function (resp) {
+ if (resp.error) {
+ ctrl.$setValidity('validNodeid', false);
+ } else {
+ ctrl.$setValidity('validNodeid', true);
+ }
+ });
}
return viewValue;
});
diff --git a/gui/index.html b/gui/index.html
index 1630f38fc..9eb6a5247 100644
--- a/gui/index.html
+++ b/gui/index.html
@@ -398,7 +398,7 @@
When adding a new node, keep in mind that this node must be added on the other side too.
The node ID cannot be blank.
- The entered node ID does not look valid. It should be a 52 character string consisting of letters and numbers, with spaces and dashes being optional.
+ The entered node ID does not look valid. It should be a 52 or 56 character string consisting of letters and numbers, with spaces and dashes being optional.
diff --git a/protocol/nodeid.go b/protocol/nodeid.go
index f5754efb8..be46392ed 100644
--- a/protocol/nodeid.go
+++ b/protocol/nodeid.go
@@ -6,7 +6,6 @@ import (
"encoding/base32"
"errors"
"fmt"
- "log"
"regexp"
"strings"
@@ -120,7 +119,6 @@ func unluhnify(s string) (string, error) {
return "", err
}
if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] {
- log.Printf("%q; %q", g, s[i*14:(i+1)*14])
return "", errors.New("check digit incorrect")
}
res = append(res, p)