mirror of
https://github.com/cwinfo/yggdrasil-map
synced 2024-11-22 14:00:27 +00:00
Validate received graph format
This commit is contained in:
parent
613b5aec09
commit
ddb2a681e4
@ -48,7 +48,7 @@ def main():
|
|||||||
all_edges = []
|
all_edges = []
|
||||||
|
|
||||||
for process in range(0, 1 if cjdns_use_default else cjdns_processes):
|
for process in range(0, 1 if cjdns_use_default else cjdns_processes):
|
||||||
print "Connecting port %d..." % (cjdns_first_port + process),; sys.stdout.flush()
|
print 'Connecting port %d...' % (cjdns_first_port + process),; sys.stdout.flush()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cjdns = cjdns_connect(process)
|
cjdns = cjdns_connect(process)
|
||||||
@ -62,7 +62,7 @@ def main():
|
|||||||
if not e in all_edges:
|
if not e in all_edges:
|
||||||
all_edges.append(e)
|
all_edges.append(e)
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
print "Failed!"
|
print 'Failed!'
|
||||||
print traceback.format_exc()
|
print traceback.format_exc()
|
||||||
|
|
||||||
success = send_graph(all_nodes, all_edges)
|
success = send_graph(all_nodes, all_edges)
|
||||||
@ -71,10 +71,10 @@ def main():
|
|||||||
|
|
||||||
def generate_graph(cjdns):
|
def generate_graph(cjdns):
|
||||||
source_nodes = cjdns_get_node_store(cjdns)
|
source_nodes = cjdns_get_node_store(cjdns)
|
||||||
print " Found %d source nodes." % len(source_nodes)
|
print ' Found %d source nodes.' % len(source_nodes)
|
||||||
|
|
||||||
nodes, edges = cjdns_graph_from_nodes(cjdns, source_nodes)
|
nodes, edges = cjdns_graph_from_nodes(cjdns, source_nodes)
|
||||||
print " Found %d nodes and %d links." % (len(nodes), len(edges))
|
print ' Found %d nodes and %d links.' % (len(nodes), len(edges))
|
||||||
|
|
||||||
return (nodes, edges)
|
return (nodes, edges)
|
||||||
|
|
||||||
@ -98,9 +98,10 @@ def send_graph(nodes, edges):
|
|||||||
|
|
||||||
json_str = json.dumps(graph_data)
|
json_str = json.dumps(graph_data)
|
||||||
|
|
||||||
print "Sending data...",; sys.stdout.flush()
|
print 'Sending data...',; sys.stdout.flush()
|
||||||
success = send_data(json_str)
|
answer = send_data(json_str)
|
||||||
print ("Done!" if success else "Failed!")
|
success = answer == 'OK'
|
||||||
|
print ('Done!' if success else answer)
|
||||||
|
|
||||||
return success
|
return success
|
||||||
|
|
||||||
@ -207,10 +208,9 @@ def send_data(graph_data):
|
|||||||
post_data = urllib.urlencode({'data': graph_data})
|
post_data = urllib.urlencode({'data': graph_data})
|
||||||
req = urllib2.Request(url, post_data)
|
req = urllib2.Request(url, post_data)
|
||||||
response = urllib2.urlopen(req)
|
response = urllib2.urlopen(req)
|
||||||
output = response.read()
|
return response.read()
|
||||||
return output == 'OK'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
25
web/graph.py
25
web/graph.py
@ -1,7 +1,15 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, ip, version=None, label=None):
|
def __init__(self, ip, version=None, label=None):
|
||||||
|
if not valid_cjdns_ip(ip):
|
||||||
|
raise ValueError('Invalid IP address')
|
||||||
|
if not valid_version(version):
|
||||||
|
raise ValueError('Invalid version')
|
||||||
|
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.version = version
|
self.version = int(version)
|
||||||
self.label = ip[-4:] if label == None else label
|
self.label = ip[-4:] if label == None else label
|
||||||
|
|
||||||
def __lt__(self, b):
|
def __lt__(self, b):
|
||||||
@ -24,3 +32,18 @@ class Edge:
|
|||||||
return 'Edge(a.ip="%s", b.ip="%s")' % (
|
return 'Edge(a.ip="%s", b.ip="%s")' % (
|
||||||
self.a.ip,
|
self.a.ip,
|
||||||
self.b.ip)
|
self.b.ip)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_re_cjdns_ip = re.compile(r'^fc[0-9a-f]{2}(:[0-9a-f]{4}){7}$', re.IGNORECASE)
|
||||||
|
|
||||||
|
def valid_cjdns_ip(ip):
|
||||||
|
return _re_cjdns_ip.match(ip) != None
|
||||||
|
|
||||||
|
def valid_version(version):
|
||||||
|
try:
|
||||||
|
return int(version) < 20
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
@ -6,30 +6,37 @@ def insert_graph_data(config, json_str):
|
|||||||
try:
|
try:
|
||||||
graph_data = json.loads(json_str)
|
graph_data = json.loads(json_str)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return 'Invalid JSON'
|
||||||
|
|
||||||
nodes = dict()
|
nodes = dict()
|
||||||
edges = []
|
edges = []
|
||||||
|
|
||||||
if not 'nodes' in graph_data or not 'edges' in graph_data:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for n in graph_data['nodes']:
|
for n in graph_data['nodes']:
|
||||||
|
try:
|
||||||
node = Node(n['ip'], version=n['version'])
|
node = Node(n['ip'], version=n['version'])
|
||||||
nodes[n['ip']] = node
|
nodes[n['ip']] = node
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
for e in graph_data['edges']:
|
for e in graph_data['edges']:
|
||||||
|
try:
|
||||||
edge = Edge(nodes[e['a']], nodes[e['b']])
|
edge = Edge(nodes[e['a']], nodes[e['b']])
|
||||||
edges.append(edge)
|
edges.append(edge)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
return 'Invalid JSON nodes'
|
||||||
|
|
||||||
except TypeError:
|
print "Accepted %d nodes and %d links." % (len(nodes), len(edges))
|
||||||
return False
|
|
||||||
|
|
||||||
print "Received %d nodes and %d links." % (len(nodes), len(edges))
|
if len(nodes) == 0 or len(edges) == 0:
|
||||||
|
return 'No valid nodes or edges'
|
||||||
|
|
||||||
|
try:
|
||||||
with NodeDB(config) as db:
|
with NodeDB(config) as db:
|
||||||
db.insert_graph(nodes, edges)
|
db.insert_graph(nodes, edges)
|
||||||
|
except Exception:
|
||||||
|
return 'Database failure'
|
||||||
|
|
||||||
return True
|
return None
|
||||||
|
@ -6,6 +6,7 @@ import graphPlotter
|
|||||||
|
|
||||||
def generate_graph(time_limit=60*60*3):
|
def generate_graph(time_limit=60*60*3):
|
||||||
nodes, edges = load_graph_from_db(time_limit)
|
nodes, edges = load_graph_from_db(time_limit)
|
||||||
|
print '%d nodes, %d edges' % (len(nodes), len(edges))
|
||||||
|
|
||||||
graph = graphPlotter.position_nodes(nodes, edges)
|
graph = graphPlotter.position_nodes(nodes, edges)
|
||||||
json = graphPlotter.get_graph_json(graph)
|
json = graphPlotter.get_graph_json(graph)
|
||||||
|
@ -25,10 +25,10 @@ def page_sendGraph():
|
|||||||
|
|
||||||
data = request.form['data']
|
data = request.form['data']
|
||||||
ret = insert_graph_data(app.config, data)
|
ret = insert_graph_data(app.config, data)
|
||||||
if ret:
|
if ret == None:
|
||||||
return 'OK'
|
return 'OK'
|
||||||
else:
|
else:
|
||||||
return 'FAIL'
|
return 'Error: %s' % ret
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='::')
|
app.run(host='::')
|
||||||
|
Loading…
Reference in New Issue
Block a user