5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-11-22 07:00:28 +00:00

fix bug in name parsing, don't print info about time-out nodes, and gofmt

This commit is contained in:
Arceliar 2021-07-02 20:37:10 -05:00
parent a94e7c070f
commit f094991677
2 changed files with 140 additions and 127 deletions

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
"sync" "sync"
"time" "time"
) )
var waitgroup sync.WaitGroup var waitgroup sync.WaitGroup
@ -13,155 +13,168 @@ var visited sync.Map
var rumored sync.Map var rumored sync.Map
func dial() (net.Conn, error) { func dial() (net.Conn, error) {
return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second) return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second)
} }
func getRequest(key, request string) map[string]interface{} { func getRequest(key, request string) map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"keepalive": true, "keepalive": true,
"request": request, "request": request,
"key": key, "key": key,
} }
} }
func doRequest(request map[string]interface{}) map[string]interface{} { func doRequest(request map[string]interface{}) map[string]interface{} {
req, err := json.Marshal(request) req, err := json.Marshal(request)
if err != nil { if err != nil {
panic(err) panic(err)
} }
sock, err := dial() sock, err := dial()
if err != nil { if err != nil {
panic(err) panic(err)
} }
if _,err = sock.Write(req); err != nil { if _, err = sock.Write(req); err != nil {
panic(err) panic(err)
} }
bs := make([]byte, 65535) bs := make([]byte, 65535)
n, err := sock.Read(bs) n, err := sock.Read(bs)
if err != nil { if err != nil {
panic(bs) panic(bs)
} }
bs = bs[:n] bs = bs[:n]
var res map[string]interface{} var res map[string]interface{}
if err = json.Unmarshal(bs, &res); err != nil { if err = json.Unmarshal(bs, &res); err != nil {
panic(err) panic(err)
} }
return res return res
} }
func getNodeInfo(key string) map[string]interface{} { func getNodeInfo(key string) map[string]interface{} {
return doRequest(getRequest(key, "getNodeInfo")) return doRequest(getRequest(key, "getNodeInfo"))
} }
func getSelf(key string) map[string]interface{} { func getSelf(key string) map[string]interface{} {
return doRequest(getRequest(key, "debug_remoteGetSelf")) return doRequest(getRequest(key, "debug_remoteGetSelf"))
} }
func getPeers(key string) map[string]interface{} { func getPeers(key string) map[string]interface{} {
return doRequest(getRequest(key, "debug_remoteGetPeers")) return doRequest(getRequest(key, "debug_remoteGetPeers"))
} }
func getDHT(key string) map[string]interface{} { func getDHT(key string) map[string]interface{} {
return doRequest(getRequest(key, "debug_remoteGetDHT")) return doRequest(getRequest(key, "debug_remoteGetDHT"))
} }
type rumorResult struct { type rumorResult struct {
key string key string
res map[string]interface{} res map[string]interface{}
} }
func doRumor(key string, out chan rumorResult) { func doRumor(key string, out chan rumorResult) {
waitgroup.Add(1) waitgroup.Add(1)
go func() { go func() {
defer waitgroup.Done() defer waitgroup.Done()
if _, known := rumored.LoadOrStore(key, true); known { if _, known := rumored.LoadOrStore(key, true); known {
return return
} }
defer rumored.Delete(key) defer rumored.Delete(key)
if _, known := visited.Load(key); known { if _, known := visited.Load(key); known {
return return
} }
results := make(map[string]interface{}) results := make(map[string]interface{})
if res, ok := getNodeInfo(key)["response"]; ok { if res, ok := getNodeInfo(key)["response"]; ok {
for addr,v := range res.(map[string]interface{}) { for addr, v := range res.(map[string]interface{}) {
results["address"] = addr vm, ok := v.(map[string]interface{})
results["nodeinfo"] = v if !ok {
} return
}
if res, ok := getSelf(key)["response"]; ok {
for _,v := range res.(map[string]interface{}) {
vm := v.(map[string]interface{})
if coords, ok := vm["coords"]; ok {
results["coords"] = coords
} }
} results["address"] = addr
} results["nodeinfo"] = vm
if res, ok := getPeers(key)["response"]; ok { }
for _,v := range res.(map[string]interface{}) { }
vm := v.(map[string]interface{}) if res, ok := getSelf(key)["response"]; ok {
if keys, ok := vm["keys"]; ok { for _, v := range res.(map[string]interface{}) {
results["peers"] = keys vm, ok := v.(map[string]interface{})
for _,key := range keys.([]interface{}) { if !ok {
doRumor(key.(string), out) return
} }
} if coords, ok := vm["coords"]; ok {
} results["coords"] = coords
} }
if res, ok := getDHT(key)["response"]; ok { }
for _,v := range res.(map[string]interface{}) { }
vm := v.(map[string]interface{}) if res, ok := getPeers(key)["response"]; ok {
if keys, ok := vm["keys"]; ok { for _, v := range res.(map[string]interface{}) {
results["dht"] = keys vm, ok := v.(map[string]interface{})
for _,key := range keys.([]interface{}) { if !ok {
doRumor(key.(string), out) return
} }
} if keys, ok := vm["keys"]; ok {
} results["peers"] = keys
} for _, key := range keys.([]interface{}) {
if len(results) > 0 { doRumor(key.(string), out)
if _, known := visited.LoadOrStore(key, true); known { }
return }
} }
results["time"] = time.Now().Unix() }
out<-rumorResult{key, results} if res, ok := getDHT(key)["response"]; ok {
} for _, v := range res.(map[string]interface{}) {
}() vm, ok := v.(map[string]interface{})
if !ok {
return
}
if keys, ok := vm["keys"]; ok {
results["dht"] = keys
for _, key := range keys.([]interface{}) {
doRumor(key.(string), out)
}
}
}
}
if len(results) > 0 {
if _, known := visited.LoadOrStore(key, true); known {
return
}
results["time"] = time.Now().Unix()
out <- rumorResult{key, results}
}
}()
} }
func doPrinter() (chan rumorResult, chan struct{}) { func doPrinter() (chan rumorResult, chan struct{}) {
results := make(chan rumorResult) results := make(chan rumorResult)
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer close(done) defer close(done)
fmt.Println("{\"yggnodes\": {") fmt.Println("{\"yggnodes\": {")
var notFirst bool var notFirst bool
for result := range results { for result := range results {
// TODO correct output // TODO correct output
res, err := json.Marshal(result.res) res, err := json.Marshal(result.res)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if notFirst { if notFirst {
fmt.Println(",") fmt.Println(",")
} }
fmt.Printf("\"%s\": %s", result.key, res) fmt.Printf("\"%s\": %s", result.key, res)
notFirst = true notFirst = true
} }
fmt.Println("\n}}") fmt.Println("\n}}")
}() }()
return results, done return results, done
} }
func main() { func main() {
self := doRequest(map[string]interface{}{"keepalive": true, "request": "getSelf"}) self := doRequest(map[string]interface{}{"keepalive": true, "request": "getSelf"})
res := self["response"].(map[string]interface{})["self"].(map[string]interface{}) res := self["response"].(map[string]interface{})["self"].(map[string]interface{})
var key string var key string
for _,v := range res { for _, v := range res {
key = v.(map[string]interface{})["key"].(string) key = v.(map[string]interface{})["key"].(string)
} }
results, done := doPrinter() results, done := doPrinter()
doRumor(key, results) doRumor(key, results)
waitgroup.Wait() waitgroup.Wait()
close(results) close(results)
<-done <-done
} }

View File

@ -40,8 +40,8 @@ def generate_graph(time_limit=60*60*3):
info = NodeInfo(ip, coords) info = NodeInfo(ip, coords)
if 'nodeinfo' in data[key]: if 'nodeinfo' in data[key]:
if 'name' in data[key]['nodeinfo']: if 'name' in data[key]['nodeinfo']:
label = data[key]['nodeinfo']['name'] label = str(data[key]['nodeinfo']['name'])
if type(label) == str and len(label) <= 32: if len(label) <= 32:
info.label = label info.label = label
info.label = cgi.escape(info.label) info.label = cgi.escape(info.label)
toAdd.append(info) toAdd.append(info)