5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-12-30 19:55:39 +00:00
yggdrasil-map/scripts/crawl.py

78 lines
2.0 KiB
Python
Raw Normal View History

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"
def getPeersRequest(key):
2022-12-03 22:13:55 +00:00
return '{{"keepalive":true, "request":"debug_remoteGetPeers", "arguments": {{"key":"{}"}}}}'.format(key)
def doRequest(req):
try:
ygg = socket.socket(socktype, socket.SOCK_STREAM)
ygg.connect(sockaddr)
ygg.send(req)
2022-12-03 22:13:55 +00:00
data = json.loads(ygg.recv(1048576))
return data
except:
return None
visited = set() # Add nodes after a successful lookup response
rumored = set() # Add rumors about nodes to ping
timedout = set()
2023-10-28 13:54:34 +00:00
def handleGetPeersResponse(publicKey, data):
global vistied
global rumored
global timedout
2021-05-23 17:41:27 +00:00
if publicKey in visited: return
2023-10-28 13:54:34 +00:00
#visited.add(publicKey)
try:
ks = data['response'].values()[0]['keys']
for k in ks:
if k in visited: continue
if k in timedout: continue
rumored.add(k)
visited.add(publicKey)
except:
pass
# Get self info
selfInfo = doRequest('{"keepalive":true, "request":"getSelf"}')
2023-10-28 13:54:34 +00:00
#rumored.add(selfInfo['response']['key'])
visited.add(selfInfo['response']['key'])
try:
peers = doRequest('{"keepalive":true, "request":"getPeers"}')
for p in peers['response']['peers']:
rumored.add(p['key'])
except:
pass
# 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
while len(rumored) > 0:
for k in rumored:
2023-10-28 13:54:34 +00:00
print "DEBUG tested:", len(visited), "remaining:", len(rumored)
handleGetPeersResponse(k, doRequest(getPeersRequest(k)))
break
rumored.remove(k)
#End
# TODO do something with the results
#print visited
#print timedout