5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-12-28 14:05:42 +00:00
yggdrasil-map/scripts/getcurrent.py

58 lines
1.4 KiB
Python
Raw Normal View History

2023-10-28 13:54:34 +00:00
import json
import socket
import sys
import time
#gives the option to get data from an external server instead and send that
#if no options given it will default to localhost instead
if len(sys.argv) == 3:
socktype = socket.AF_INET
sockaddr = (sys.argv[1], int(sys.argv[2]))
elif len(sys.argv) == 2:
socktype = socket.AF_UNIX
sockaddr = sys.argv[1]
else:
socktype = socket.AF_UNIX
2023-10-28 15:00:13 +00:00
sockaddr = "/var/run/yggdrasil/yggdrasil.sock"
2023-10-28 13:54:34 +00:00
def readall(sock):
data = []
sock.settimeout(1)
while True:
try:
frag = sock.recv(4096)
data.append(frag)
except:
break
return "".join(data)
2023-10-28 13:54:34 +00:00
def doRequest(req):
try:
ygg = socket.socket(socktype, socket.SOCK_STREAM)
ygg.connect(sockaddr)
ygg.send(req)
data = json.loads(readall(ygg))
2023-10-28 13:54:34 +00:00
return data
except:
return None
known = doRequest('{"keepalive":true, "request":"lookups"}')
infos = dict()
for node in known['response']['infos']:
coords = json.dumps(node["path"]).replace(",", " ")
v = {"address": node["addr"], "coords": coords, "time": node["time"]}
infos[node["key"]] = v
# Initialize dicts of visited/rumored nodes
#for k,v in selfInfo['response']['self'].iteritems(): rumored[k] = v
# Loop over rumored nodes and ping them, adding to visited if they respond
print json.dumps({"yggnodes": infos})
# TODO do something with the results
#print visited
#print timedout