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
|
2023-10-28 15:00:13 +00:00
|
|
|
sockaddr = "/var/run/yggdrasil/yggdrasil.sock"
|
2021-05-23 03:47:16 +00:00
|
|
|
|
2023-11-04 18:52:07 +00:00
|
|
|
def readall(sock):
|
|
|
|
data = []
|
|
|
|
sock.settimeout(1)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
frag = sock.recv(4096)
|
|
|
|
data.append(frag)
|
|
|
|
except:
|
|
|
|
break
|
|
|
|
return "".join(data)
|
|
|
|
|
2021-05-23 03:47:16 +00:00
|
|
|
def getPeersRequest(key):
|
2022-12-03 22:13:55 +00:00
|
|
|
return '{{"keepalive":true, "request":"debug_remoteGetPeers", "arguments": {{"key":"{}"}}}}'.format(key)
|
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)
|
2023-11-04 18:52:07 +00:00
|
|
|
data = json.loads(readall(ygg))
|
2021-05-23 03:47:16 +00:00
|
|
|
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
|
2021-05-23 03:47:16 +00:00
|
|
|
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
|
2021-05-23 03:47:16 +00:00
|
|
|
|
|
|
|
# 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
|
2021-05-23 03:47:16 +00:00
|
|
|
|
|
|
|
# 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)))
|
2021-05-23 03:47:16 +00:00
|
|
|
break
|
|
|
|
rumored.remove(k)
|
|
|
|
#End
|
|
|
|
|
|
|
|
# TODO do something with the results
|
|
|
|
|
|
|
|
#print visited
|
|
|
|
#print timedout
|