5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-09-20 02:32:30 +00:00
yggdrasil-map/mapper/makeGraph.py

233 lines
4.7 KiB
Python
Raw Normal View History

2014-03-19 09:12:47 +00:00
#!/usr/bin/env python
2014-03-30 20:19:29 +00:00
import conf_sh as conf
2014-03-19 09:12:47 +00:00
import sys
2014-03-30 20:19:29 +00:00
sys.path.append(conf.cjdns_path + '/contrib/python/cjdnsadmin/')
2014-03-19 09:12:47 +00:00
import adminTools as admin
from collections import deque
import pygraphviz as pgv
import json
import time
import httplib2
2014-03-30 20:19:29 +00:00
import traceback
2014-03-19 09:12:47 +00:00
2014-03-21 22:55:16 +00:00
class Node:
def __init__(self, ip):
self.ip = ip
self.version = -1
self.label = ip[-4:]
2014-03-19 09:12:47 +00:00
2014-03-21 22:55:16 +00:00
def __lt__(self, b):
return self.ip < b.ip
class Edge:
def __init__(self, a, b):
self.a, self.b = sorted([a, b])
2014-03-30 20:19:29 +00:00
def is_in(self, edges):
for e in edges:
if e.a.ip == self.a.ip and e.b.ip == self.b.ip:
return True
return False
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
def get_network_from_cjdns(ip, port, password):
nodes = dict()
edges = []
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
cjdns = admin.connect(ip, port, password)
me = admin.whoami(cjdns)
my_ip = me['IP']
nodes[my_ip] = Node(my_ip)
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
nodes_to_check = deque()
nodes_to_check.append(my_ip)
2014-03-30 18:03:24 +00:00
2014-03-30 20:19:29 +00:00
while len(nodes_to_check) != 0:
current_ip = nodes_to_check.popleft()
resp = cjdns.NodeStore_nodeForAddr(current_ip)
if not 'result' in resp or not 'linkCount' in resp['result']:
continue
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
result = resp['result']
link_count = result['linkCount']
if 'protocolVersion' in result:
nodes[current_ip].version = result['protocolVersion']
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
for i in range(0, link_count):
result = cjdns.NodeStore_getLink(current_ip, i)['result']
if not 'child' in result:
continue
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
child_ip = result['child']
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
# Add links with one hop only
if result['isOneHop'] != 1:
continue
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
# Add node
if not child_ip in nodes:
nodes[child_ip] = Node(child_ip)
nodes_to_check.append(child_ip)
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
# Add edge
e = Edge(nodes[current_ip], nodes[child_ip])
if not e.is_in(edges):
edges.append(e)
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
return (nodes, edges)
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
def get_full_network():
all_nodes = dict()
all_edges = []
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
for i in range(0, conf.num_of_nodes):
port = conf.rpc_firstport + i
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
print '[%d/%d] Connecting to %s:%d...' % (i + 1, conf.num_of_nodes, conf.rpc_connect, port),
sys.stdout.flush()
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
try:
nodes, edges = get_network_from_cjdns(conf.rpc_connect, port, conf.rpc_pw)
except Exception as ex:
print 'Fail!'
print traceback.format_exc()
continue
2014-03-21 22:55:16 +00:00
2014-03-30 20:19:29 +00:00
print '%d nodes, %d edges' % (len(nodes), len(edges))
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
for ip, n in nodes.iteritems():
all_nodes[ip] = n
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
for e in edges:
if not e.is_in(all_edges):
all_edges.append(e)
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
return (all_nodes, all_edges)
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
def download_names_from_nameinfo():
2014-03-19 09:12:47 +00:00
page = 'http://[fc5d:baa5:61fc:6ffd:9554:67f0:e290:7535]/nodes/list.json'
2014-03-30 20:19:29 +00:00
print 'Downloading names from Mikey\'s nodelist...',
2014-03-30 22:02:34 +00:00
sys.stdout.flush()
2014-03-19 09:12:47 +00:00
ip_dict = dict()
2014-03-30 20:19:29 +00:00
http = httplib2.Http('.cache', timeout=15.0)
r, content = http.request(page, 'GET')
name_and_ip = json.loads(content)['nodes']
for node in name_and_ip:
ip_dict[node['ip']] = node['name']
print 'Done!'
return ip_dict
def set_node_names(nodes):
2014-03-19 09:12:47 +00:00
try:
2014-03-30 20:19:29 +00:00
ip_dict = download_names_from_nameinfo()
except Exception as ex:
print 'Fail!'
# TODO use cache
print traceback.format_exc()
return
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
for ip, node in nodes.iteritems():
if ip in ip_dict:
node.label = ip_dict[ip]
2014-03-19 09:12:47 +00:00
2014-03-30 20:19:29 +00:00
def build_graph(nodes, edges):
G = pgv.AGraph(strict=True, directed=False, size='10!')
for n in nodes.values():
G.add_node(n.ip, label=n.label, version=n.version)
for e in edges:
G.add_edge(e.a.ip, e.b.ip, len=1.0)
G.layout(prog='neato', args='-Gepsilon=0.0001 -Gmaxiter=100000')
return G
2014-03-19 09:12:47 +00:00
def gradient_color(ratio, colors):
jump = 1.0 / (len(colors) - 1)
gap_num = int(ratio / (jump + 0.0000001))
a = colors[gap_num]
b = colors[gap_num + 1]
ratio = (ratio - gap_num * jump) * (len(colors) - 1)
r = a[0] + (b[0] - a[0]) * ratio
g = a[1] + (b[1] - a[1]) * ratio
b = a[2] + (b[2] - a[2]) * ratio
return '#%02x%02x%02x' % (r, g, b)
2014-03-30 20:19:29 +00:00
def get_graph_json(G):
max_neighbors = 1
for n in G.iternodes():
neighbors = len(G.neighbors(n))
if neighbors > max_neighbors:
max_neighbors = neighbors
print 'Max neighbors: %d' % max_neighbors
out_data = {
'created': int(time.time()),
'nodes': [],
'edges': []
}
for n in G.iternodes():
neighbor_ratio = len(G.neighbors(n)) / float(max_neighbors)
pos = n.attr['pos'].split(',', 1)
out_data['nodes'].append({
2014-03-30 20:33:27 +00:00
'id': n.name,
2014-03-30 20:19:29 +00:00
'label': n.attr['label'],
'version': n.attr['version'],
'x': float(pos[0]),
'y': float(pos[1]),
'color': gradient_color(neighbor_ratio, [(100, 100, 100), (0, 0, 0)]),
'size': neighbor_ratio
})
for e in G.iteredges():
out_data['edges'].append({
'sourceID': e[0],
'targetID': e[1]
})
return json.dumps(out_data)
if __name__ == '__main__':
nodes, edges = get_full_network()
print 'Total:'
print '%d nodes, %d edges' % (len(nodes), len(edges))
set_node_names(nodes)
G = build_graph(nodes, edges)
output = get_graph_json(G)
with open(conf.graph_output, 'w') as f:
f.write(output)