diff --git a/generate_aliases.py b/generate_aliases.py new file mode 100755 index 0000000..98fe407 --- /dev/null +++ b/generate_aliases.py @@ -0,0 +1,110 @@ +#!/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 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: + aliases.append(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/mkmap.sh b/mkmap.sh new file mode 100755 index 0000000..cf85166 --- /dev/null +++ b/mkmap.sh @@ -0,0 +1,6 @@ +#!/bin/bash +PATH='/opt/ffmap-backend/' +PEERS="/etc/fastd/ffhh-mesh-vpn/peers" + +python2 $PATH/generate_aliases.py $PEERS > $PATH/aliases.json +python3 $PATH/backend.py -d /var/www/meshviewer/data/ -a $PATH/aliases.json --vpn de:ad:be:ff:01:01