From 325f6cd1f4950174a73a83bdca571cff3d47c687 Mon Sep 17 00:00:00 2001 From: Alexander Dietrich Date: Mon, 10 Jul 2017 21:50:14 +0200 Subject: [PATCH] Support for "bat-ffhh" interface, add generate_aliases_v2.py --- backend.py | 3 +- gateway.json | 89 ++++++-------------------------- generate_aliases_v2.py | 112 +++++++++++++++++++++++++++++++++++++++++ lib/batman.py | 4 -- lib/graph.py | 1 - lib/nodes.py | 15 +++++- mkmap.sh | 3 +- node_number.py | 0 8 files changed, 146 insertions(+), 81 deletions(-) create mode 100755 generate_aliases_v2.py mode change 100644 => 100755 node_number.py diff --git a/backend.py b/backend.py index 74d3e52..8b9d9a6 100755 --- a/backend.py +++ b/backend.py @@ -102,7 +102,6 @@ def main(params): for batman in batman_instances: vd = batman.vis_data() gwl = batman.gateway_list() - mesh_info.append((vd, gwl)) # update nodedb from batman-adv data @@ -131,6 +130,8 @@ def main(params): try: for mac in node["nodeinfo"]["network"]["mesh"]["bat0"]["interfaces"]["tunnel"]: macs.add(mac) + for mac in node["nodeinfo"]["network"]["mesh"]["bat-ffhh"]["interfaces"]["tunnel"]: + macs.add(mac) except KeyError: pass diff --git a/gateway.json b/gateway.json index c7b0966..4a72859 100644 --- a/gateway.json +++ b/gateway.json @@ -1,86 +1,29 @@ [ { - "node_id": "deadbeef0101", - "hostname": "gw01", - "network": { - "mesh": { - "bat0": { - "interfaces": { - "tunnel": [ - "de:ad:be:ff:01:01", - "de:ad:bf:ff:01:01" - ] - } - } - } - } + "node_id": "deadbfff0101", + "hostname": "gw01" }, { "node_id": "deadbeef0505", - "hostname": "gw02", + "hostname": "gw02.hamburg.freifunk.net", "network": { - "mesh": { - "bat0": { - "interfaces": { - "tunnel": [ - "de:ad:be:ff:05:05", - "de:ad:be:ff:05:06", - "de:ad:bf:ff:05:05" - ] - } + "mac": "de:ad:be:ef:05:05", + "mesh": { + "bat0": { + "interfaces": { + "tunnel": [ + "de:ad:be:ff:05:05", + "de:ad:be:fc:05:05", + "de:ad:bf:ff:05:05" + ] + } + } } - } } }, { - "node_id": "deadbeef0303", - "hostname": "gw03", - "network": { - "mesh": { - "bat0": { - "interfaces": { - "tunnel": [ - "de:ad:be:ff:03:03", - "de:ad:bf:ff:03:03" - ] - } - } - } - } - }, - { - "node_id": "deadbfff2222", - "hostname": "gw05", - "network": { - "mesh": { - "bat0": { - "interfaces": { - "tunnel": [ - "de:ad:be:ff:22:22", - "de:ad:be:ff:22:23", - "de:ad:bf:ff:22:22" - ] - } - } - } - } - }, - { - "node_id": "deadbeef8888", - "hostname": "gw08", - "network": { - "mesh": { - "bat0": { - "interfaces": { - "tunnel": [ - "de:ad:be:ff:88:88", - "de:ad:be:ff:88:89", - "de:ad:bf:ff:88:88" - ] - } - } - } - } + "node_id": "00163efb9d8d", + "hostname": "gw03" } ] diff --git a/generate_aliases_v2.py b/generate_aliases_v2.py new file mode 100755 index 0000000..7a04c7c --- /dev/null +++ b/generate_aliases_v2.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python2 + +from __future__ import print_function + +import json +import os +import sys + +if len(sys.argv) != 2: + print('usage: ' + sys.argv[0] + ' /path/to/peers') + sys.exit(1) + +peersDir = sys.argv[1] + +def normalizeMac(mac): + mac = mac.lower() + normalized = '' + + n = 0 + + for c in mac: + if c != ':': + if n > 0 and n % 2 == 0: + normalized = normalized + ':' + normalized = normalized + c + n += 1 + + return normalized + +def toAlias(peer): + alias = {} + + if not (peer.has_key('name') and peer.has_key('mac')): + return None + + name = peer['name'] + mac = peer['mac'] + + alias['node_id'] = mac.replace(':', '') + alias['hostname'] = name + + if peer.has_key('geo'): + geo = peer['geo'] + + location = {} + + if geo.has_key('lon'): location['longitude'] = geo['lon'] + if geo.has_key('lat'): location['latitude'] = geo['lat'] + + alias['location'] = location + + #alias['network'] = {} + #alias['network']['mesh_interfaces'] = [mac] + + return {'nodeinfo':alias} + +aliases = {} + +for filename in os.listdir(peersDir): + if len(filename) == 0 or filename[0] == '.': + continue + + isGateway = False + + absFilename = peersDir + '/' + filename + if os.path.isfile(absFilename): + peerFile = open(absFilename, 'r') + try: + peerLines = peerFile.readlines() + peer = {} + + for line in peerLines: + parts = line.split() + + if len(parts) > 2: + if parts[1] == 'Knotenname:': + peer['name'] = parts[2] + + elif parts[0] == 'remote': + isGateway = True + + elif parts[1] == 'MAC:': + peer['mac'] = normalizeMac(parts[2]) + + elif parts[1] == 'Koordinaten:' and len(parts) > 3: + try: + peer['geo'] = {'lat': float(parts[2]), 'lon': float(parts[3])} + + except ValueError: + print('Error in %s: Invalid coordinates: %s' % (absFilename, parts[2:4]), file = sys.stderr) + + elif len(parts) == 2 and parts[0] == 'key': + keyParts = parts[1].split('"') + if len(keyParts) > 1: + peer['vpn'] = keyParts[1].lower() + + if isGateway: + continue + + alias = toAlias(peer) + if alias: + tmpid = alias['nodeinfo']['node_id'] +# alias['nodeinfo'].pop('node_id') + aliases[tmpid] = alias + + except Exception as e: + print('Error in %s, ignoring peer: %s' % (absFilename, e), file = sys.stderr) + + finally: + peerFile.close() + +print(json.dumps(aliases)) diff --git a/lib/batman.py b/lib/batman.py index ee0a0fe..ee0198b 100644 --- a/lib/batman.py +++ b/lib/batman.py @@ -96,7 +96,3 @@ if __name__ == "__main__": bc = Batman() vd = bc.vis_data() gw = bc.gateway_list() - for x in vd: - print(x) - print(gw) - print(bc.gateway_mode()) diff --git a/lib/graph.py b/lib/graph.py index db1259e..2833e6e 100644 --- a/lib/graph.py +++ b/lib/graph.py @@ -25,7 +25,6 @@ def mark_vpn(graph, vpn_macs): components = map(frozenset, nx.weakly_connected_components(graph)) components = filter(vpn_macs.intersection, components) nodes = reduce(lambda a, b: a | b, components, set()) - for node in nodes: for k, v in graph[node].items(): v['vpn'] = True diff --git a/lib/nodes.py b/lib/nodes.py index f517438..86ce5b9 100644 --- a/lib/nodes.py +++ b/lib/nodes.py @@ -6,6 +6,10 @@ from functools import reduce def build_mac_table(nodes): macs = dict() for node_id, node in nodes.items(): + try: + macs[node['network']['mac']] = node_id + except KeyError: + pass try: for mac in node['nodeinfo']['network']['mesh_interfaces']: macs[mac] = node_id @@ -23,6 +27,11 @@ def build_mac_table(nodes): macs[mac] = node_id except KeyError: pass + try: + for mac in node['nodeinfo']['network']['mesh']['bat-ffhh']['interfaces']['tunnel']: + macs[mac] = node_id + except KeyError: + pass try: for mac in node['nodeinfo']['network']['mesh']['bat0']['interfaces']['other']: @@ -145,6 +154,11 @@ def import_mesh_ifs_vis_data(nodes, vis_data): except KeyError: pass + try: + ifs = ifs.union(set(node['nodeinfo']['network']['mesh']['bat-ffhh']['interfaces']['tunnel'])) + except KeyError: + pass + try: ifs = ifs.union(set(node['nodeinfo']['network']['mesh']['bat0']['interfaces']['other'])) except KeyError: @@ -166,7 +180,6 @@ def import_vis_clientcount(nodes, vis_data): def mark_gateways(nodes, gateways): macs = build_mac_table(nodes) gateways = filter(lambda d: d in macs, gateways) - for node in map(lambda d: nodes[macs[d]], gateways): node['flags']['gateway'] = True diff --git a/mkmap.sh b/mkmap.sh index 2d9b0f6..9423943 100755 --- a/mkmap.sh +++ b/mkmap.sh @@ -3,4 +3,5 @@ FFMAPPATH='/opt/ffmap-backend/' PEERS="/etc/fastd/ffhh-mesh-vpn/peers" python2 $FFMAPPATH/generate_aliases.py $PEERS > $FFMAPPATH/aliases.json -python3 $FFMAPPATH/backend.py -d /var/www/meshviewer/ --aliases $FFMAPPATH/aliases.json $FFMAPPATH/gateway.json -m bat0:/var/run/alfred.sock -p 30 --vpn de:ad:be:ff:01:01 --vpn de:ad:be:ff:05:05 --vpn de:ad:be:ff:05:06 --vpn de:ad:be:ff:03:03 --vpn de:ad:be:ff:22:22 --vpn de:ad:be:ff:22:23 --vpn de:ad:be:ff:88:88 --vpn de:ad:be:ff:88:89 --vpn de:ad:bf:ff:88:88 --vpn de:ad:bf:ff:22:22 --vpn de:ad:bf:ff:03:03 --vpn de:ad:bf:ff:05:05 --vpn de:ad:bf:ff:01:01 --vpn de:ad:be:fc:03:03 --vpn 00:16:3e:53:75:0d --vpn de:ad:be:fc:05:05 --vpn de:ad:be:fc:01:01 --vpn de:ad:be:ef:03:03 --vpn de:ad:be:ef:01:01 --vpn de:ad:be:ef:05:05 +#python3 $FFMAPPATH/backend.py -d /var/www/meshviewer/ --aliases $FFMAPPATH/aliases.json $FFMAPPATH/gateway.json -m bat0:/var/run/alfred.sock -p 30 --vpn de:ad:be:ff:01:01 --vpn de:ad:be:ff:05:05 --vpn de:ad:be:ff:05:06 --vpn de:ad:be:ff:03:03 --vpn de:ad:be:ff:22:22 --vpn de:ad:be:ff:22:23 --vpn de:ad:be:ff:88:88 --vpn de:ad:be:ff:88:89 --vpn de:ad:bf:ff:88:88 --vpn de:ad:bf:ff:22:22 --vpn de:ad:bf:ff:03:03 --vpn de:ad:bf:ff:05:05 --vpn de:ad:bf:ff:01:01 --vpn de:ad:be:fc:03:03 --vpn 00:16:3e:53:75:0d --vpn de:ad:be:fc:05:05 --vpn de:ad:be:fc:01:01 --vpn de:ad:be:ef:03:03 --vpn de:ad:be:ef:01:01 --vpn de:ad:be:ef:05:05 --vpn 00:16:3e:fb:9d:8d --vpn 00:16:3e:fb:9d:9d +python3 $FFMAPPATH/backend.py -d /var/www/meshviewer/ --aliases $FFMAPPATH/aliases.json $FFMAPPATH/gateway.json -m bat0:/var/run/alfred.sock -p 30 --vpn de:ad:be:ff:01:01 de:ad:be:ff:05:05 de:ad:be:ff:05:06 de:ad:be:ff:03:03 de:ad:be:ff:22:22 de:ad:be:ff:22:23 de:ad:be:ff:88:88 de:ad:be:ff:88:89 de:ad:bf:ff:88:88 de:ad:bf:ff:22:22 de:ad:bf:ff:03:03 de:ad:bf:ff:05:05 de:ad:bf:ff:01:01 de:ad:be:fc:03:03 00:16:3e:53:75:0d de:ad:be:fc:05:05 de:ad:be:fc:01:01 de:ad:be:ef:03:03 de:ad:be:ef:01:01 de:ad:be:ef:05:05 00:16:3e:fb:9d:8d 00:16:3e:fb:9d:9d diff --git a/node_number.py b/node_number.py old mode 100644 new mode 100755