2021-06-20 23:12:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-03 01:37:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2021-06-20 23:12:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var waitgroup sync.WaitGroup
|
|
|
|
|
2021-07-07 00:12:06 +00:00
|
|
|
const MAX_RETRY = 3
|
2021-07-31 15:34:52 +00:00
|
|
|
const N_PARALLEL_REQ = 32
|
|
|
|
|
|
|
|
var semaphore chan struct{}
|
|
|
|
|
|
|
|
func init() {
|
2022-04-29 10:14:12 +00:00
|
|
|
semaphore = make(chan struct{}, N_PARALLEL_REQ)
|
2021-07-31 15:34:52 +00:00
|
|
|
}
|
2021-07-07 00:12:06 +00:00
|
|
|
|
2021-06-20 23:12:02 +00:00
|
|
|
func dial() (net.Conn, error) {
|
2021-07-03 01:37:10 +00:00
|
|
|
return net.DialTimeout("unix", "/var/run/yggdrasil.sock", time.Second)
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRequest(key, request string) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
return map[string]interface{}{
|
|
|
|
"keepalive": true,
|
|
|
|
"request": request,
|
2022-12-03 22:13:55 +00:00
|
|
|
"arguments": map[string]interface{}{
|
|
|
|
"key": key,
|
|
|
|
},
|
2021-07-03 01:37:10 +00:00
|
|
|
}
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doRequest(request map[string]interface{}) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
req, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var res map[string]interface{}
|
2021-07-07 00:12:06 +00:00
|
|
|
for idx := 0; idx < MAX_RETRY; idx++ {
|
|
|
|
sock, err := dial()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err = sock.Write(req); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bs := make([]byte, 65535)
|
2022-04-29 10:14:12 +00:00
|
|
|
deadline := time.Now().Add(6 * time.Second)
|
|
|
|
sock.SetReadDeadline(deadline)
|
2021-07-07 00:12:06 +00:00
|
|
|
n, err := sock.Read(bs)
|
2022-04-29 10:14:12 +00:00
|
|
|
sock.Close()
|
2021-07-07 00:12:06 +00:00
|
|
|
if err != nil {
|
2022-04-29 10:14:12 +00:00
|
|
|
continue
|
2021-07-07 00:12:06 +00:00
|
|
|
panic(bs)
|
|
|
|
}
|
|
|
|
bs = bs[:n]
|
|
|
|
if err = json.Unmarshal(bs, &res); err != nil {
|
2022-04-29 10:14:12 +00:00
|
|
|
return nil
|
2021-07-07 00:12:06 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// TODO parse res, check if there's an error
|
|
|
|
if res, ok := res["response"]; ok {
|
2022-12-03 22:23:44 +00:00
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-07 00:12:06 +00:00
|
|
|
if _, isIn := res.(map[string]interface{})["error"]; isIn {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2021-07-03 01:37:10 +00:00
|
|
|
}
|
|
|
|
return res
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNodeInfo(key string) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
return doRequest(getRequest(key, "getNodeInfo"))
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSelf(key string) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
return doRequest(getRequest(key, "debug_remoteGetSelf"))
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPeers(key string) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
return doRequest(getRequest(key, "debug_remoteGetPeers"))
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDHT(key string) map[string]interface{} {
|
2021-07-03 01:37:10 +00:00
|
|
|
return doRequest(getRequest(key, "debug_remoteGetDHT"))
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 13:54:34 +00:00
|
|
|
func getPaths() map[string]interface{} {
|
|
|
|
req := map[string]interface{}{
|
|
|
|
"keepalive": true,
|
|
|
|
"request": "getPaths",
|
|
|
|
}
|
|
|
|
return doRequest(req)
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:12:02 +00:00
|
|
|
type rumorResult struct {
|
2021-07-03 01:37:10 +00:00
|
|
|
key string
|
|
|
|
res map[string]interface{}
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doRumor(key string, out chan rumorResult) {
|
2021-07-03 01:37:10 +00:00
|
|
|
waitgroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer waitgroup.Done()
|
2022-04-29 10:14:12 +00:00
|
|
|
semaphore <- struct{}{}
|
2021-07-31 15:34:52 +00:00
|
|
|
defer func() { <-semaphore }()
|
2021-07-03 01:37:10 +00:00
|
|
|
results := make(map[string]interface{})
|
|
|
|
if res, ok := getNodeInfo(key)["response"]; ok {
|
2022-12-03 22:23:44 +00:00
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 01:37:10 +00:00
|
|
|
for addr, v := range res.(map[string]interface{}) {
|
2021-07-06 23:48:25 +00:00
|
|
|
vm, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 01:37:10 +00:00
|
|
|
results["address"] = addr
|
|
|
|
results["nodeinfo"] = vm
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 13:54:34 +00:00
|
|
|
|
|
|
|
if res, ok := getPaths()["response"]; ok {
|
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
psi := res.(map[string]interface{})["paths"]
|
|
|
|
if _, ok := psi.([]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ps := psi.([]interface{})
|
|
|
|
for _, pi := range ps {
|
|
|
|
if _, ok := pi.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := pi.(map[string]interface{})
|
|
|
|
if _, ok := p["key"]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ki := p["key"]
|
|
|
|
if _, ok := ki.(string); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
k := ki.(string)
|
|
|
|
if k != key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := p["path"]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
results["coords"] = fmt.Sprintf("%v", p["path"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2021-07-03 01:37:10 +00:00
|
|
|
if res, ok := getSelf(key)["response"]; ok {
|
2022-12-03 22:23:44 +00:00
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 01:37:10 +00:00
|
|
|
for _, v := range res.(map[string]interface{}) {
|
|
|
|
vm, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if coords, ok := vm["coords"]; ok {
|
|
|
|
results["coords"] = coords
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 13:54:34 +00:00
|
|
|
*/
|
2021-07-03 01:37:10 +00:00
|
|
|
if res, ok := getPeers(key)["response"]; ok {
|
2022-12-03 22:23:44 +00:00
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 01:37:10 +00:00
|
|
|
for _, v := range res.(map[string]interface{}) {
|
|
|
|
vm, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if keys, ok := vm["keys"]; ok {
|
|
|
|
results["peers"] = keys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 13:54:34 +00:00
|
|
|
/*
|
2021-07-03 01:37:10 +00:00
|
|
|
if res, ok := getDHT(key)["response"]; ok {
|
2022-12-03 22:23:44 +00:00
|
|
|
if _, ok := res.(map[string]interface{}); !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 01:37:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 13:54:34 +00:00
|
|
|
*/
|
2021-07-03 01:37:10 +00:00
|
|
|
if len(results) > 0 {
|
|
|
|
results["time"] = time.Now().Unix()
|
|
|
|
out <- rumorResult{key, results}
|
2022-12-03 22:13:55 +00:00
|
|
|
waitgroup.Add(1)
|
2021-07-03 01:37:10 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func doPrinter() (chan rumorResult, chan struct{}) {
|
2021-07-03 01:37:10 +00:00
|
|
|
results := make(chan rumorResult)
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2022-12-03 22:13:55 +00:00
|
|
|
visited := make(map[string]struct{})
|
2021-07-03 01:37:10 +00:00
|
|
|
fmt.Println("{\"yggnodes\": {")
|
|
|
|
var notFirst bool
|
|
|
|
for result := range results {
|
|
|
|
// TODO correct output
|
2022-12-03 22:13:55 +00:00
|
|
|
if _, isIn := visited[result.key]; isIn {
|
|
|
|
waitgroup.Done()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
visited[result.key] = struct{}{}
|
2021-07-03 01:37:10 +00:00
|
|
|
res, err := json.Marshal(result.res)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if notFirst {
|
|
|
|
fmt.Println(",")
|
|
|
|
}
|
|
|
|
fmt.Printf("\"%s\": %s", result.key, res)
|
|
|
|
notFirst = true
|
2022-12-03 22:13:55 +00:00
|
|
|
toVisit := make(map[string]struct{})
|
|
|
|
if peers, isIn := result.res["peers"]; isIn {
|
|
|
|
if ks, ok := peers.([]interface{}); ok {
|
|
|
|
for _, k := range ks {
|
|
|
|
if key, ok := k.(string); ok {
|
|
|
|
toVisit[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dht, isIn := result.res["dht"]; isIn {
|
|
|
|
if ks, ok := dht.([]interface{}); ok {
|
|
|
|
for _, k := range ks {
|
|
|
|
if key, ok := k.(string); ok {
|
|
|
|
toVisit[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range toVisit {
|
|
|
|
if _, isIn := visited[k]; !isIn {
|
|
|
|
doRumor(k, results)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waitgroup.Done()
|
2021-07-03 01:37:10 +00:00
|
|
|
}
|
|
|
|
fmt.Println("\n}}")
|
|
|
|
}()
|
|
|
|
return results, done
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-07-03 01:37:10 +00:00
|
|
|
self := doRequest(map[string]interface{}{"keepalive": true, "request": "getSelf"})
|
2022-12-03 22:13:55 +00:00
|
|
|
res := self["response"].(map[string]interface{})
|
2021-07-03 01:37:10 +00:00
|
|
|
var key string
|
2022-12-03 22:13:55 +00:00
|
|
|
key = res["key"].(string)
|
2021-07-03 01:37:10 +00:00
|
|
|
results, done := doPrinter()
|
|
|
|
doRumor(key, results)
|
|
|
|
waitgroup.Wait()
|
|
|
|
close(results)
|
|
|
|
<-done
|
2021-06-20 23:12:02 +00:00
|
|
|
}
|