diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 7a4adc403..97a65fa98 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -34,6 +34,7 @@ import ( "code.google.com/p/go.crypto/bcrypt" "github.com/syncthing/syncthing/internal/auto" "github.com/syncthing/syncthing/internal/config" + "github.com/syncthing/syncthing/internal/discover" "github.com/syncthing/syncthing/internal/events" "github.com/syncthing/syncthing/internal/logger" "github.com/syncthing/syncthing/internal/model" @@ -451,7 +452,17 @@ func restPostDiscoveryHint(w http.ResponseWriter, r *http.Request) { } func restGetDiscovery(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(discoverer.All()) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + registry := discoverer.All() + + // Device ids can't be marshalled as keys so we need to manually + // rebuild this map using strings. + devices := make(map[string][]discover.CacheEntry, len(registry)) + for device, _ := range registry { + devices[device.String()] = registry[device] + } + + json.NewEncoder(w).Encode(devices) } func restGetReport(m *model.Model, w http.ResponseWriter, r *http.Request) { diff --git a/gui/app.js b/gui/app.js index 333a5342c..6932edc7d 100644 --- a/gui/app.js +++ b/gui/app.js @@ -666,15 +666,21 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http, $translate, $loca }; $scope.addDevice = function () { - $scope.currentDevice = { - AddressesStr: 'dynamic', - Compression: true, - Introducer: false - }; - $scope.editingExisting = false; - $scope.editingSelf = false; - $scope.deviceEditor.$setPristine(); - $('#editDevice').modal(); + $http.get(urlbase + '/discovery') + .success(function (registry) { + $scope.discovery = registry; + }) + .then(function () { + $scope.currentDevice = { + AddressesStr: 'dynamic', + Compression: true, + Introducer: false + }; + $scope.editingExisting = false; + $scope.editingSelf = false; + $scope.deviceEditor.$setPristine(); + $('#editDevice').modal(); + }); }; $scope.deleteDevice = function () { diff --git a/gui/index.html b/gui/index.html index cb24de487..30b57b3e2 100644 --- a/gui/index.html +++ b/gui/index.html @@ -383,7 +383,10 @@
- + + +
{{currentDevice.DeviceID}}

The device ID to enter here can be found in the "Edit > Show ID" dialog on the other device. Spaces and dashes are optional (ignored). diff --git a/internal/discover/discover.go b/internal/discover/discover.go index dc2f6620b..39365bbc6 100644 --- a/internal/discover/discover.go +++ b/internal/discover/discover.go @@ -39,7 +39,7 @@ type Discoverer struct { cacheLifetime time.Duration broadcastBeacon beacon.Interface multicastBeacon beacon.Interface - registry map[protocol.DeviceID][]cacheEntry + registry map[protocol.DeviceID][]CacheEntry registryLock sync.RWMutex extServer string extPort uint16 @@ -51,9 +51,9 @@ type Discoverer struct { extAnnounceOKmut sync.Mutex } -type cacheEntry struct { - addr string - seen time.Time +type CacheEntry struct { + Address string + Seen time.Time } var ( @@ -68,7 +68,7 @@ func NewDiscoverer(id protocol.DeviceID, addresses []string) *Discoverer { globalBcastIntv: 1800 * time.Second, errorRetryIntv: 60 * time.Second, cacheLifetime: 5 * time.Minute, - registry: make(map[protocol.DeviceID][]cacheEntry), + registry: make(map[protocol.DeviceID][]CacheEntry), } } @@ -139,16 +139,16 @@ func (d *Discoverer) Lookup(device protocol.DeviceID) []string { if len(cached) > 0 { addrs := make([]string, len(cached)) for i := range cached { - addrs[i] = cached[i].addr + addrs[i] = cached[i].Address } return addrs } else if len(d.extServer) != 0 { addrs := d.externalLookup(device) - cached = make([]cacheEntry, len(addrs)) + cached = make([]CacheEntry, len(addrs)) for i := range addrs { - cached[i] = cacheEntry{ - addr: addrs[i], - seen: time.Now(), + cached[i] = CacheEntry{ + Address: addrs[i], + Seen: time.Now(), } } @@ -169,11 +169,11 @@ func (d *Discoverer) Hint(device string, addrs []string) { }) } -func (d *Discoverer) All() map[protocol.DeviceID][]cacheEntry { +func (d *Discoverer) All() map[protocol.DeviceID][]CacheEntry { d.registryLock.RLock() - devices := make(map[protocol.DeviceID][]cacheEntry, len(d.registry)) + devices := make(map[protocol.DeviceID][]CacheEntry, len(d.registry)) for device, addrs := range d.registry { - addrsCopy := make([]cacheEntry, len(addrs)) + addrsCopy := make([]CacheEntry, len(addrs)) copy(addrsCopy, addrs) devices[device] = addrsCopy } @@ -365,14 +365,14 @@ func (d *Discoverer) registerDevice(addr net.Addr, device Device) bool { deviceAddr = ua.String() } for i := range current { - if current[i].addr == deviceAddr { - current[i].seen = time.Now() + if current[i].Address == deviceAddr { + current[i].Seen = time.Now() goto done } } - current = append(current, cacheEntry{ - addr: deviceAddr, - seen: time.Now(), + current = append(current, CacheEntry{ + Address: deviceAddr, + Seen: time.Now(), }) done: } @@ -388,7 +388,7 @@ func (d *Discoverer) registerDevice(addr net.Addr, device Device) bool { if len(current) > len(orig) { addrs := make([]string, len(current)) for i := range current { - addrs[i] = current[i].addr + addrs[i] = current[i].Address } events.Default.Log(events.DeviceDiscovered, map[string]interface{}{ "device": id.String(), @@ -468,11 +468,11 @@ func (d *Discoverer) externalLookup(device protocol.DeviceID) []string { return addrs } -func (d *Discoverer) filterCached(c []cacheEntry) []cacheEntry { +func (d *Discoverer) filterCached(c []CacheEntry) []CacheEntry { for i := 0; i < len(c); { - if ago := time.Since(c[i].seen); ago > d.cacheLifetime { + if ago := time.Since(c[i].Seen); ago > d.cacheLifetime { if debug { - l.Debugf("removing cached address %s: seen %v ago", c[i].addr, ago) + l.Debugf("removing cached address %s: seen %v ago", c[i].Address, ago) } c[i] = c[len(c)-1] c = c[:len(c)-1]