From c7af02776f84ee3b650d9216676b3a64e0b53f53 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 21 Jan 2015 21:03:16 +0000 Subject: [PATCH 1/4] Obtain status info in JSON format --- router/json.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ weaver/main.go | 5 ++++ 2 files changed, 83 insertions(+) create mode 100644 router/json.go diff --git a/router/json.go b/router/json.go new file mode 100644 index 00000000..12382272 --- /dev/null +++ b/router/json.go @@ -0,0 +1,78 @@ +package router + +import ( + "encoding/json" + "fmt" + "time" +) + +func (router *Router) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Name, Interface string + Macs *MacCache + Peers *Peers + Routes *Routes + }{router.Ourself.Name.String(), fmt.Sprintf("%v", router.Iface), router.Macs, router.Peers, router.Routes}) + // leaving out ConectionMaker due to async complexities +} + +func (cache *MacCache) MarshalJSON() ([]byte, error) { + type cacheEntry struct { + Mac string + PeerName PeerName + LastSeen time.Time + } + entries := make([]*cacheEntry, 0) + for key, entry := range cache.table { + entries = append(entries, &cacheEntry{intmac(key).String(), entry.peer.Name, entry.lastSeen}) + } + return json.Marshal(entries) +} + +func (peers *Peers) MarshalJSON() ([]byte, error) { + ps := make([]*Peer, 0) + peers.ForEach(func(_ PeerName, peer *Peer) { ps = append(ps, peer) }) + return json.Marshal(ps) +} + +func (routes *Routes) MarshalJSON() ([]byte, error) { + routes.RLock() + defer routes.RUnlock() + type uni struct { + Dest, Via PeerName + } + type broad struct { + Source PeerName + Via []PeerName + } + var r struct { + Unicast []*uni + Broadcast []*broad + } + for name, hop := range routes.unicast { + r.Unicast = append(r.Unicast, &uni{name, hop}) + } + for name, hops := range routes.broadcast { + r.Broadcast = append(r.Broadcast, &broad{name, hops}) + } + return json.Marshal(r) +} + +func (peer *Peer) MarshalJSON() ([]byte, error) { + conns := make([]Connection, 0) + peer.ForEachConnection(func(remoteName PeerName, conn Connection) { + conns = append(conns, conn) + }) + return json.Marshal(struct { + Name string + Connections []Connection + }{peer.Name.String(), conns}) +} + +func (conn *RemoteConnection) MarshalJSON() ([]byte, error) { + return json.Marshal(struct{ RemoteName, TcpAddr string }{conn.Remote().Name.String(), conn.RemoteTCPAddr()}) +} + +func (name PeerName) MarshalJSON() ([]byte, error) { + return json.Marshal(name.String()) +} diff --git a/weaver/main.go b/weaver/main.go index de62f30b..ce0a8290 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -145,6 +145,11 @@ func handleHttp(router *weave.Router) { io.WriteString(w, fmt.Sprintln("Encryption", encryption)) io.WriteString(w, router.Status()) }) + http.HandleFunc("/status-json", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + json, _ := router.MarshalJSON() + w.Write(json) + }) http.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) { peer := r.FormValue("peer") if addr, err := net.ResolveTCPAddr("tcp4", weave.NormalisePeerAddr(peer)); err == nil { From 42dfd5142ecde59240c7e63533ab21c3407ffd6b Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 2 Feb 2015 14:36:56 +0000 Subject: [PATCH 2/4] Remove Access-Control hack --- weaver/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/weaver/main.go b/weaver/main.go index ce0a8290..5a6b3075 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -146,7 +146,6 @@ func handleHttp(router *weave.Router) { io.WriteString(w, router.Status()) }) http.HandleFunc("/status-json", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Access-Control-Allow-Origin", "*") json, _ := router.MarshalJSON() w.Write(json) }) From e600816d5f31d43f2956b76b5fd7929ea9d7618c Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 2 Feb 2015 14:41:49 +0000 Subject: [PATCH 3/4] Add UID and version into JSON for each Peer --- router/json.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/router/json.go b/router/json.go index 12382272..68abacc7 100644 --- a/router/json.go +++ b/router/json.go @@ -65,8 +65,10 @@ func (peer *Peer) MarshalJSON() ([]byte, error) { }) return json.Marshal(struct { Name string + UID uint64 + Version uint64 Connections []Connection - }{peer.Name.String(), conns}) + }{peer.Name.String(), peer.UID, peer.version, conns}) } func (conn *RemoteConnection) MarshalJSON() ([]byte, error) { From 410972906bc0a6a8058578b6698b575a6ca53197 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 6 Feb 2015 17:35:39 +0000 Subject: [PATCH 4/4] Add version and encryption status into the JSON output --- router/json.go | 6 ++++-- weaver/main.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/router/json.go b/router/json.go index 68abacc7..e3091b24 100644 --- a/router/json.go +++ b/router/json.go @@ -6,13 +6,15 @@ import ( "time" ) -func (router *Router) MarshalJSON() ([]byte, error) { +func (router *Router) GenerateStatusJSON(version, encryption string) ([]byte, error) { return json.Marshal(struct { + Version string + Encryption string Name, Interface string Macs *MacCache Peers *Peers Routes *Routes - }{router.Ourself.Name.String(), fmt.Sprintf("%v", router.Iface), router.Macs, router.Peers, router.Routes}) + }{version, encryption, router.Ourself.Name.String(), fmt.Sprintf("%v", router.Iface), router.Macs, router.Peers, router.Routes}) // leaving out ConectionMaker due to async complexities } diff --git a/weaver/main.go b/weaver/main.go index 5a6b3075..7d3aae93 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -146,7 +146,7 @@ func handleHttp(router *weave.Router) { io.WriteString(w, router.Status()) }) http.HandleFunc("/status-json", func(w http.ResponseWriter, r *http.Request) { - json, _ := router.MarshalJSON() + json, _ := router.GenerateStatusJSON(version, encryption) w.Write(json) }) http.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {