Browse Source

Expose discovery cache over rest interface

Jakob Borg 11 years ago
parent
commit
532b576fd5
2 changed files with 17 additions and 0 deletions
  1. 5 0
      cmd/syncthing/gui.go
  2. 12 0
      discover/discover.go

+ 5 - 0
cmd/syncthing/gui.go

@@ -49,6 +49,7 @@ func startGUI(cfg GUIConfiguration, m *Model) error {
 	router.Get("/rest/config/sync", restGetConfigInSync)
 	router.Get("/rest/system", restGetSystem)
 	router.Get("/rest/errors", restGetErrors)
+	router.Get("/rest/discovery", restGetDiscovery)
 
 	router.Post("/rest/config", restPostConfig)
 	router.Post("/rest/restart", restPostRestart)
@@ -243,6 +244,10 @@ func restPostDiscoveryHint(r *http.Request) {
 	}
 }
 
+func restGetDiscovery(w http.ResponseWriter) {
+	json.NewEncoder(w).Encode(discoverer.All())
+}
+
 func basic(username string, passhash string) http.HandlerFunc {
 	return func(res http.ResponseWriter, req *http.Request) {
 		error := func() {

+ 12 - 0
discover/discover.go

@@ -98,6 +98,18 @@ func (d *Discoverer) Hint(node string, addrs []string) {
 	})
 }
 
+func (d *Discoverer) All() map[string][]string {
+	d.registryLock.RLock()
+	nodes := make(map[string][]string, len(d.registry))
+	for node, addrs := range d.registry {
+		addrsCopy := make([]string, len(addrs))
+		copy(addrsCopy, addrs)
+		nodes[node] = addrsCopy
+	}
+	d.registryLock.RUnlock()
+	return nodes
+}
+
 func (d *Discoverer) announcementPkt() []byte {
 	var addrs []Address
 	for _, astr := range d.listenAddrs {