2021-05-23 03:47:16 +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
|
|
|
|
sockaddr = "/var/run/yggdrasil.sock"
|
|
|
|
|
2021-05-23 17:52:59 +00:00
|
|
|
def getNodeInfoRequest(key):
|
|
|
|
return '{{"keepalive":true, "request":"getNodeInfo", "key":"{}"}}'.format(key)
|
|
|
|
|
|
|
|
def getSelfRequest(key):
|
2021-05-23 18:39:45 +00:00
|
|
|
return '{{"keepalive":true, "request":"debug_remoteGetSelf", "key":"{}"}}'.format(key)
|
2021-05-23 17:52:59 +00:00
|
|
|
|
2021-05-23 03:47:16 +00:00
|
|
|
def getPeersRequest(key):
|
2021-05-23 18:39:45 +00:00
|
|
|
return '{{"keepalive":true, "request":"debug_remoteGetPeers", "key":"{}"}}'.format(key)
|
2021-05-23 03:47:16 +00:00
|
|
|
|
2021-05-23 17:52:59 +00:00
|
|
|
def getDHTRequest(key):
|
2021-05-23 18:39:45 +00:00
|
|
|
return '{{"keepalive":true, "request":"debug_remoteGetDHT", "key":"{}"}}'.format(key)
|
2021-05-23 17:52:59 +00:00
|
|
|
|
2021-05-23 03:47:16 +00:00
|
|
|
def doRequest(req):
|
|
|
|
try:
|
|
|
|
ygg = socket.socket(socktype, socket.SOCK_STREAM)
|
|
|
|
ygg.connect(sockaddr)
|
|
|
|
ygg.send(req)
|
|
|
|
data = json.loads(ygg.recv(1024*15))
|
|
|
|
return data
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
visited = set() # Add nodes after a successful lookup response
|
|
|
|
rumored = set() # Add rumors about nodes to ping
|
|
|
|
timedout = set()
|
2021-05-23 17:52:59 +00:00
|
|
|
def handleNodeInfoResponse(publicKey, data):
|
2021-05-23 03:47:16 +00:00
|
|
|
global visited
|
|
|
|
global rumored
|
|
|
|
global timedout
|
2021-05-23 17:41:27 +00:00
|
|
|
if publicKey in visited: return
|
2021-05-23 03:47:16 +00:00
|
|
|
if not data: return
|
|
|
|
if 'response' not in data: return
|
2021-05-23 17:41:27 +00:00
|
|
|
out = dict()
|
|
|
|
for addr,v in data['response'].iteritems():
|
|
|
|
out['address'] = addr
|
2021-05-23 17:52:59 +00:00
|
|
|
out['nodeinfo'] = v
|
|
|
|
selfInfo = doRequest(getSelfRequest(publicKey))
|
2021-05-23 17:41:27 +00:00
|
|
|
if 'response' in selfInfo:
|
|
|
|
for _,v in selfInfo['response'].iteritems():
|
|
|
|
if 'coords' in v:
|
|
|
|
out['coords'] = v['coords']
|
2021-05-23 17:52:59 +00:00
|
|
|
peerInfo = doRequest(getPeersRequest(publicKey))
|
|
|
|
if 'response' in peerInfo:
|
|
|
|
for _,v in peerInfo['response'].iteritems():
|
|
|
|
if 'keys' not in v: continue
|
|
|
|
peers = v['keys']
|
|
|
|
for key in peers:
|
|
|
|
if key in visited: continue
|
|
|
|
if key in timedout: continue
|
|
|
|
rumored.add(key)
|
|
|
|
out['peers'] = peers
|
|
|
|
dhtInfo = doRequest(getDHTRequest(publicKey))
|
2021-05-23 17:41:27 +00:00
|
|
|
if 'response' in dhtInfo:
|
|
|
|
for _,v in dhtInfo['response'].iteritems():
|
|
|
|
if 'keys' in v:
|
2021-05-23 17:44:57 +00:00
|
|
|
dht = v['keys']
|
|
|
|
for key in dht:
|
|
|
|
if key in visited: continue
|
|
|
|
if key in timedout: continue
|
|
|
|
rumored.add(key)
|
2021-05-23 17:52:59 +00:00
|
|
|
out['dht'] = dht
|
2021-05-23 17:41:27 +00:00
|
|
|
out['time'] = time.time()
|
2021-05-23 03:47:16 +00:00
|
|
|
if len(visited) > 0: sys.stdout.write(",\n")
|
2021-05-23 17:41:27 +00:00
|
|
|
sys.stdout.write('"{}": {}'.format(publicKey, json.dumps(out)))
|
2021-05-23 03:47:16 +00:00
|
|
|
sys.stdout.flush()
|
2021-05-23 17:41:27 +00:00
|
|
|
visited.add(publicKey)
|
2021-05-23 03:47:16 +00:00
|
|
|
# End handleResponse
|
|
|
|
|
|
|
|
# Get self info
|
|
|
|
selfInfo = doRequest('{"keepalive":true, "request":"getSelf"}')
|
|
|
|
for k,v in selfInfo['response']['self'].iteritems(): rumored.add(v['key'])
|
|
|
|
|
|
|
|
# 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 '{"yggnodes": {'
|
|
|
|
while len(rumored) > 0:
|
|
|
|
for k in rumored:
|
2021-05-23 19:16:52 +00:00
|
|
|
handleNodeInfoResponse(k, doRequest(getNodeInfoRequest(k)))
|
2021-05-23 03:47:16 +00:00
|
|
|
break
|
|
|
|
rumored.remove(k)
|
|
|
|
print '\n}}'
|
|
|
|
#End
|
|
|
|
|
|
|
|
# TODO do something with the results
|
|
|
|
|
|
|
|
#print visited
|
|
|
|
#print timedout
|