Browse Source

Use backend service to verify nodeID (fixes #418)

Jakob Borg 11 years ago
parent
commit
3388d5b49c
4 changed files with 27 additions and 10 deletions
  1. 18 0
      cmd/syncthing/gui.go
  2. 8 7
      gui/app.js
  3. 1 1
      gui/index.html
  4. 0 2
      protocol/nodeid.go

+ 18 - 0
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 {

+ 8 - 7
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;
             });

+ 1 - 1
gui/index.html

@@ -398,7 +398,7 @@
                   <span ng-show="!editingExisting">When adding a new node, keep in mind that <em>this node</em> must be added on the other side too.</span>
                 </span>
                 <span ng-if="nodeEditor.nodeID.$error.required && nodeEditor.nodeID.$dirty">The node ID cannot be blank.</span>
-                <span ng-if="nodeEditor.nodeID.$error.validNodeid && nodeEditor.nodeID.$dirty">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.</span>
+                <span ng-if="nodeEditor.nodeID.$error.validNodeid && nodeEditor.nodeID.$dirty">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.</span>
               </p>
             </div>
             <div class="form-group">

+ 0 - 2
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)