2012-05-11 18:39:26 +02:00
|
|
|
import json
|
2012-06-11 23:53:45 +02:00
|
|
|
from functools import reduce
|
|
|
|
from collections import defaultdict
|
|
|
|
from node import Node, Interface
|
|
|
|
from link import Link, LinkConnector
|
2012-05-12 14:58:16 +02:00
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
class NodeDB:
|
2014-06-29 22:47:29 +02:00
|
|
|
def __init__(self, time=0):
|
|
|
|
self.time = time
|
2012-05-11 18:39:26 +02:00
|
|
|
self._nodes = []
|
2012-06-04 20:58:17 +02:00
|
|
|
self._links = []
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
# fetch list of links
|
|
|
|
def get_links(self):
|
2012-06-12 02:09:44 +02:00
|
|
|
self.update_vpn_links()
|
2012-06-11 23:53:45 +02:00
|
|
|
return self.reduce_links()
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
# fetch list of nodes
|
|
|
|
def get_nodes(self):
|
|
|
|
return self._nodes
|
|
|
|
|
2014-06-29 22:47:29 +02:00
|
|
|
# remove all offlines nodes with lastseen < timestamp
|
|
|
|
def prune_offline(self, timestamp):
|
|
|
|
self._nodes = list(filter(lambda x: x.lastseen >= timestamp, self._nodes))
|
|
|
|
|
|
|
|
# write persistent state to file
|
|
|
|
def dump_state(self, filename):
|
|
|
|
obj = []
|
|
|
|
|
|
|
|
for node in self._nodes:
|
|
|
|
if node.flags['client']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
obj.append({ 'id': node.id
|
|
|
|
, 'name': node.name
|
|
|
|
, 'lastseen': node.lastseen
|
|
|
|
, 'geo': node.gps
|
|
|
|
})
|
|
|
|
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
json.dump(obj, f)
|
|
|
|
|
|
|
|
# load persistent state from file
|
|
|
|
def load_state(self, filename):
|
|
|
|
try:
|
|
|
|
with open(filename, "r") as f:
|
|
|
|
obj = json.load(f)
|
|
|
|
for n in obj:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_id(n['id'])
|
|
|
|
except:
|
|
|
|
node = Node()
|
|
|
|
node.id = n['id']
|
|
|
|
node.name = n['name']
|
|
|
|
node.lastseen = n['lastseen']
|
|
|
|
node.gps = n['geo']
|
|
|
|
self._nodes.append(node)
|
|
|
|
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
def maybe_node_by_mac(self, macs):
|
|
|
|
for node in self._nodes:
|
|
|
|
for mac in macs:
|
2012-05-12 16:44:27 +02:00
|
|
|
if mac.lower() in node.macs:
|
2012-05-11 18:39:26 +02:00
|
|
|
return node
|
|
|
|
|
2013-12-29 20:20:33 +01:00
|
|
|
raise KeyError
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2013-12-29 04:14:07 +01:00
|
|
|
def maybe_node_by_id(self, mac):
|
|
|
|
for node in self._nodes:
|
|
|
|
if mac.lower() == node.id:
|
|
|
|
return node
|
|
|
|
|
2013-12-29 20:20:33 +01:00
|
|
|
raise KeyError
|
2013-12-29 04:14:07 +01:00
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
def parse_vis_data(self,vis_data):
|
|
|
|
for x in vis_data:
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
if 'of' in x:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_mac((x['of'], x['secondary']))
|
|
|
|
except:
|
|
|
|
node = Node()
|
2014-06-29 22:47:29 +02:00
|
|
|
node.lastseen = self.time
|
2012-06-07 22:21:50 +02:00
|
|
|
node.flags['online'] = True
|
2014-01-30 19:10:48 +01:00
|
|
|
if 'legacy' in x:
|
|
|
|
node.flags['legacy'] = True
|
2012-05-11 18:39:26 +02:00
|
|
|
self._nodes.append(node)
|
|
|
|
|
|
|
|
node.add_mac(x['of'])
|
|
|
|
node.add_mac(x['secondary'])
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
for x in vis_data:
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
if 'router' in x:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_mac((x['router'], ))
|
|
|
|
except:
|
|
|
|
node = Node()
|
2014-06-29 22:47:29 +02:00
|
|
|
node.lastseen = self.time
|
2012-06-07 22:21:50 +02:00
|
|
|
node.flags['online'] = True
|
2014-01-30 19:10:48 +01:00
|
|
|
if 'legacy' in x:
|
|
|
|
node.flags['legacy'] = True
|
2012-05-11 18:39:26 +02:00
|
|
|
node.add_mac(x['router'])
|
|
|
|
self._nodes.append(node)
|
|
|
|
|
2012-06-06 13:57:10 +02:00
|
|
|
if 'gateway' in x and x['label'] == "TT":
|
2014-08-10 09:36:29 +02:00
|
|
|
if x['router'] in node.macs:
|
2012-07-07 23:11:37 +02:00
|
|
|
node.add_mac(x['gateway'])
|
2012-06-06 13:57:10 +02:00
|
|
|
|
2012-07-07 23:11:37 +02:00
|
|
|
# skip processing as regular link
|
|
|
|
continue
|
2012-06-06 13:57:10 +02:00
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
try:
|
2012-06-15 18:23:03 +02:00
|
|
|
if 'neighbor' in x:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_mac((x['neighbor']))
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
if 'gateway' in x:
|
|
|
|
x['neighbor'] = x['gateway']
|
|
|
|
|
|
|
|
node = self.maybe_node_by_mac((x['neighbor'], ))
|
|
|
|
except:
|
|
|
|
node = Node()
|
2014-06-29 22:47:29 +02:00
|
|
|
node.lastseen = self.time
|
2012-06-07 22:21:50 +02:00
|
|
|
node.flags['online'] = True
|
2012-05-11 18:39:26 +02:00
|
|
|
if x['label'] == 'TT':
|
2012-06-07 22:21:50 +02:00
|
|
|
node.flags['client'] = True
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
node.add_mac(x['neighbor'])
|
|
|
|
self._nodes.append(node)
|
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
for x in vis_data:
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
if 'router' in x:
|
|
|
|
try:
|
|
|
|
if 'gateway' in x:
|
|
|
|
x['neighbor'] = x['gateway']
|
|
|
|
|
|
|
|
router = self.maybe_node_by_mac((x['router'], ))
|
|
|
|
neighbor = self.maybe_node_by_mac((x['neighbor'], ))
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2012-06-12 00:32:13 +02:00
|
|
|
# filter TT links merged in previous step
|
|
|
|
if router == neighbor:
|
|
|
|
continue
|
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
link = Link()
|
|
|
|
link.source = LinkConnector()
|
|
|
|
link.source.interface = x['router']
|
|
|
|
link.source.id = self._nodes.index(router)
|
|
|
|
link.target = LinkConnector()
|
|
|
|
link.target.interface = x['neighbor']
|
|
|
|
link.target.id = self._nodes.index(neighbor)
|
|
|
|
link.quality = x['label']
|
|
|
|
link.id = "-".join(sorted((link.source.interface, link.target.interface)))
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
if x['label'] == "TT":
|
|
|
|
link.type = "client"
|
|
|
|
|
|
|
|
self._links.append(link)
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2013-11-12 00:28:11 +01:00
|
|
|
for x in vis_data:
|
2012-06-04 17:33:23 +02:00
|
|
|
|
|
|
|
if 'primary' in x:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_mac((x['primary'], ))
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
node.id = x['primary']
|
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
def reduce_links(self):
|
|
|
|
tmp_links = defaultdict(list)
|
|
|
|
|
|
|
|
for link in self._links:
|
|
|
|
tmp_links[link.id].append(link)
|
|
|
|
|
|
|
|
links = []
|
|
|
|
|
|
|
|
def reduce_link(a, b):
|
|
|
|
a.id = b.id
|
|
|
|
a.source = b.source
|
|
|
|
a.target = b.target
|
|
|
|
a.type = b.type
|
|
|
|
a.quality = ", ".join([x for x in (a.quality, b.quality) if x])
|
|
|
|
|
|
|
|
return a
|
|
|
|
|
|
|
|
for k, v in tmp_links.items():
|
|
|
|
new_link = reduce(reduce_link, v, Link())
|
|
|
|
links.append(new_link)
|
|
|
|
|
|
|
|
return links
|
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
def import_aliases(self, aliases):
|
|
|
|
for mac, alias in aliases.items():
|
|
|
|
try:
|
2014-05-02 04:08:23 +02:00
|
|
|
node = self.maybe_node_by_mac([mac])
|
2012-05-11 18:39:26 +02:00
|
|
|
except:
|
2014-05-02 04:08:23 +02:00
|
|
|
try:
|
2014-08-10 09:36:29 +02:00
|
|
|
node = self.maybe_node_mac(mac)
|
2014-05-02 04:08:23 +02:00
|
|
|
except:
|
|
|
|
# create an offline node
|
|
|
|
node = Node()
|
|
|
|
node.add_mac(mac)
|
|
|
|
self._nodes.append(node)
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2013-02-02 01:27:49 +01:00
|
|
|
if 'name' in alias:
|
|
|
|
node.name = alias['name']
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2014-05-31 14:53:28 +02:00
|
|
|
if 'vpn' in alias and alias['vpn'] and mac and node.interfaces and mac in node.interfaces:
|
2012-06-12 02:26:41 +02:00
|
|
|
node.interfaces[mac].vpn = True
|
2012-06-12 02:09:44 +02:00
|
|
|
|
2012-06-30 16:49:45 +02:00
|
|
|
if 'gps' in alias:
|
|
|
|
node.gps = alias['gps']
|
|
|
|
|
2014-02-02 23:28:09 +01:00
|
|
|
if 'firmware' in alias:
|
|
|
|
node.firmware = alias['firmware']
|
|
|
|
|
2014-05-02 10:49:01 +02:00
|
|
|
if 'id' in alias:
|
|
|
|
node.id = alias['id']
|
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
# list of macs
|
|
|
|
# if options['gateway']:
|
|
|
|
# mark_gateways(options['gateway'])
|
|
|
|
def mark_gateways(self, gateways):
|
|
|
|
for gateway in gateways:
|
|
|
|
try:
|
|
|
|
node = self.maybe_node_by_mac((gateway, ))
|
|
|
|
except:
|
2014-05-31 14:53:28 +02:00
|
|
|
print("WARNING: did not find gateway '",gateway,"' in node list")
|
2012-05-11 18:39:26 +02:00
|
|
|
continue
|
|
|
|
|
2012-06-07 22:21:50 +02:00
|
|
|
node.flags['gateway'] = True
|
2012-06-11 23:53:45 +02:00
|
|
|
|
2012-06-12 02:09:44 +02:00
|
|
|
def update_vpn_links(self):
|
2012-06-11 23:53:45 +02:00
|
|
|
changes = 1
|
|
|
|
while changes > 0:
|
|
|
|
changes = 0
|
|
|
|
for link in self._links:
|
|
|
|
if link.type == "client":
|
|
|
|
continue
|
2012-06-07 22:58:45 +02:00
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
source_interface = self._nodes[link.source.id].interfaces[link.source.interface]
|
|
|
|
target_interface = self._nodes[link.target.id].interfaces[link.target.interface]
|
|
|
|
if source_interface.vpn or target_interface.vpn:
|
|
|
|
source_interface.vpn = True
|
|
|
|
target_interface.vpn = True
|
|
|
|
if link.type != "vpn":
|
|
|
|
changes += 1
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
link.type = "vpn"
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2014-04-10 17:17:11 +02:00
|
|
|
def count_clients(self):
|
|
|
|
for link in self._links:
|
|
|
|
try:
|
|
|
|
a = self.maybe_node_by_id(link.source.interface)
|
|
|
|
b = self.maybe_node_by_id(link.target.interface)
|
|
|
|
|
|
|
|
if a.flags['client']:
|
|
|
|
client = a
|
|
|
|
node = b
|
|
|
|
elif b.flags['client']:
|
|
|
|
client = b
|
|
|
|
node = a
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
node.clientcount += 1
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2013-12-29 13:09:33 +01:00
|
|
|
def obscure_clients(self):
|
|
|
|
|
|
|
|
globalIdCounter = 0
|
|
|
|
nodeCounters = {}
|
|
|
|
clientIds = {}
|
|
|
|
|
|
|
|
for node in self._nodes:
|
|
|
|
if node.flags['client']:
|
|
|
|
node.macs = set()
|
|
|
|
clientIds[node.id] = None
|
|
|
|
|
|
|
|
for link in self._links:
|
|
|
|
ids = link.source.interface
|
|
|
|
idt = link.target.interface
|
2014-01-31 22:14:28 +01:00
|
|
|
|
2013-12-29 13:09:33 +01:00
|
|
|
try:
|
2014-08-10 09:36:29 +02:00
|
|
|
node_source = self.maybe_node_by_mac(ids)
|
|
|
|
node_target = self.maybe_node_by_mac(idt)
|
2014-01-31 21:50:09 +01:00
|
|
|
|
|
|
|
if not node_source.flags['client'] and not node_target.flags['client']:
|
|
|
|
# if none of the nodes associated with this link are clients,
|
|
|
|
# we do not want to obscure
|
|
|
|
continue
|
|
|
|
|
2013-12-29 13:09:33 +01:00
|
|
|
if ids in clientIds and idt in clientIds:
|
|
|
|
# This is for corner cases, when a client
|
|
|
|
# is linked to another client.
|
|
|
|
clientIds[ids] = str(globalIdCounter)
|
2014-01-31 22:14:28 +01:00
|
|
|
ids = str(globalIdCounter)
|
2013-12-29 13:09:33 +01:00
|
|
|
globalIdCounter += 1
|
|
|
|
|
|
|
|
clientIds[idt] = str(globalIdCounter)
|
|
|
|
idt = str(globalIdCounter)
|
|
|
|
globalIdCounter += 1
|
|
|
|
|
|
|
|
elif ids in clientIds:
|
|
|
|
newId = generateId(idt)
|
|
|
|
clientIds[ids] = newId
|
|
|
|
ids = newId
|
|
|
|
|
|
|
|
link.source.interface = ids;
|
|
|
|
node_source.id = ids;
|
|
|
|
|
|
|
|
elif idt in clientIds:
|
|
|
|
newId = generateId(ids,nodeCounters)
|
|
|
|
clientIds[idt] = newId
|
|
|
|
idt = newId
|
|
|
|
|
|
|
|
link.target.interface = idt;
|
|
|
|
node_target.id = idt;
|
|
|
|
|
|
|
|
link.id = ids + "-" + idt
|
|
|
|
|
2014-01-31 22:14:28 +01:00
|
|
|
except KeyError:
|
2013-12-29 20:20:33 +01:00
|
|
|
pass
|
2013-12-29 13:09:33 +01:00
|
|
|
|
|
|
|
# extends node id by incremented node counter
|
|
|
|
def generateId(nodeId,nodeCounters):
|
|
|
|
if nodeId in nodeCounters:
|
|
|
|
n = nodeCounters[nodeId]
|
|
|
|
nodeCounters[nodeId] = n + 1
|
|
|
|
else:
|
|
|
|
nodeCounters[nodeId] = 1
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
return nodeId + "_" + str(n)
|