From b3a0131b63f3471d43e20c1008272d1aa549fca7 Mon Sep 17 00:00:00 2001 From: Dominik Heidler Date: Mon, 29 Feb 2016 19:16:49 +0100 Subject: [PATCH] Add alfred_leagcy_provider It gets a list of pre-0.5.1 routers from netmon mysql db, crawls them and sends the results to a (patched) alfred master. --- contrib/alfred_legacy_provider/.gitignore | 1 + ...low_setting_source_mac_via_unix_sock.patch | 29 +++++++++++ contrib/alfred_legacy_provider/crawl.py | 49 +++++++++++++++++++ contrib/alfred_legacy_provider/crawl_all.sh | 6 +++ contrib/alfred_legacy_provider/get_macs.sh | 3 ++ contrib/alfred_legacy_provider/list_old.sh | 4 ++ contrib/alfred_legacy_provider/run.sh | 4 ++ ffmap/web/templates/index.html | 4 +- 8 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 contrib/alfred_legacy_provider/.gitignore create mode 100644 contrib/alfred_legacy_provider/alfred_allow_setting_source_mac_via_unix_sock.patch create mode 100755 contrib/alfred_legacy_provider/crawl.py create mode 100755 contrib/alfred_legacy_provider/crawl_all.sh create mode 100755 contrib/alfred_legacy_provider/get_macs.sh create mode 100755 contrib/alfred_legacy_provider/list_old.sh create mode 100755 contrib/alfred_legacy_provider/run.sh diff --git a/contrib/alfred_legacy_provider/.gitignore b/contrib/alfred_legacy_provider/.gitignore new file mode 100644 index 0000000..f0a4400 --- /dev/null +++ b/contrib/alfred_legacy_provider/.gitignore @@ -0,0 +1 @@ +macs.txt diff --git a/contrib/alfred_legacy_provider/alfred_allow_setting_source_mac_via_unix_sock.patch b/contrib/alfred_legacy_provider/alfred_allow_setting_source_mac_via_unix_sock.patch new file mode 100644 index 0000000..27fff5b --- /dev/null +++ b/contrib/alfred_legacy_provider/alfred_allow_setting_source_mac_via_unix_sock.patch @@ -0,0 +1,29 @@ +From: Dominik Heidler +Date: Mon, 29 Feb 2016 19:07:00 +0100 +Subject: [PATCH] Allow setting the source mac via unix sock + +The server will only overwrite the mac if it is zero. +The alfred client sets the mac to zero by default +so this shouldn't break existing behaviour. +--- + unix_sock.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/unix_sock.c b/unix_sock.c +index 3c7e583..12a10e6 100644 +--- a/unix_sock.c ++++ b/unix_sock.c +@@ -119,7 +119,9 @@ static int unix_sock_add_data(struct globals *globals, + + data = push->data; + data_len = ntohs(data->header.length); +- memcpy(data->source, &interface->hwaddr, sizeof(interface->hwaddr)); ++ static const char zero[ETH_ALEN] = { 0 }; ++ if (!memcmp(zero, data->source, sizeof(data->source))) ++ memcpy(data->source, &interface->hwaddr, sizeof(interface->hwaddr)); + + if ((int)(data_len + sizeof(*data)) > len) + goto err; +-- +1.7.10.4 + diff --git a/contrib/alfred_legacy_provider/crawl.py b/contrib/alfred_legacy_provider/crawl.py new file mode 100755 index 0000000..1b58f93 --- /dev/null +++ b/contrib/alfred_legacy_provider/crawl.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 + +import sys +import subprocess +import re +import pyalfred + + + +CONFIG = { + #"crawl_outgoing_netif": "br-mesh", + # this old system sucks + "crawl_outgoing_netif": "25%s" % open("/sys/class/net/br-mesh/ifindex").read().strip(), +} + +def mac_to_ipv6_linklocal(mac): + # Remove the most common delimiters; dots, dashes, etc. + mac_bare = re.sub('[%s]+' % re.escape(' .:-'), '', mac) + mac_value = int(mac_bare, 16) + + # Split out the bytes that slot into the IPv6 address + # XOR the most significant byte with 0x02, inverting the + # Universal / Local bit + high2 = mac_value >> 32 & 0xffff ^ 0x0200 + high1 = mac_value >> 24 & 0xff + low1 = mac_value >> 16 & 0xff + low2 = mac_value & 0xffff + + return 'fe80::{:04x}:{:02x}ff:fe{:02x}:{:04x}'.format(high2, high1, low1, low2) + +mac = sys.argv[1] +fe80_ip = mac_to_ipv6_linklocal(mac) + +node_data = subprocess.check_output(["curl", "-s", "--max-time", "5", "-g", "http://[%s%%%s]/node.data" % ( + fe80_ip, + CONFIG["crawl_outgoing_netif"] +)]) +try: + node_data = gzip.decompress(node_data) +except: + pass + +assert "404" not in str(node_data).upper() + + +#print(node_data) + +ac = pyalfred.AlfredConnection() +ac.send(64, node_data.decode("UTF-8", errors="replace"), mac, gzip_data=True) diff --git a/contrib/alfred_legacy_provider/crawl_all.sh b/contrib/alfred_legacy_provider/crawl_all.sh new file mode 100755 index 0000000..cfea987 --- /dev/null +++ b/contrib/alfred_legacy_provider/crawl_all.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for mac in `cat /home/dominik/alfred_legacy_provider/macs.txt` ; do + echo "Crawling $mac" + /home/dominik/alfred_legacy_provider/crawl.py $mac +done diff --git a/contrib/alfred_legacy_provider/get_macs.sh b/contrib/alfred_legacy_provider/get_macs.sh new file mode 100755 index 0000000..29e69b3 --- /dev/null +++ b/contrib/alfred_legacy_provider/get_macs.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +/home/dominik/alfred_legacy_provider/list_old.sh 2>/dev/null | grep br-mesh | awk -F ' ' '{print $NF}' > /home/dominik/alfred_legacy_provider/macs.txt diff --git a/contrib/alfred_legacy_provider/list_old.sh b/contrib/alfred_legacy_provider/list_old.sh new file mode 100755 index 0000000..ed37268 --- /dev/null +++ b/contrib/alfred_legacy_provider/list_old.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +MYSQL_PWD="`grep mysql_password /var/www/netmon/config/config.local.inc.php | cut -d '"' -f 2`" +mysql -u netmon -"p$MYSQL_PWD" netmon -e 'SELECT r.id, r.hostname, c.firmware_version, i.name, i.mac_addr FROM routers r JOIN crawl_routers c ON c.id = (SELECT MAX(id) FROM crawl_routers WHERE router_id = r.id) LEFT JOIN crawl_interfaces i ON i.id = (SELECT MAX(id) FROM crawl_interfaces WHERE router_id = r.id AND name = "br-mesh") WHERE c.status = "online" AND c.firmware_version <= "0.5.0" ORDER BY r.id;'; diff --git a/contrib/alfred_legacy_provider/run.sh b/contrib/alfred_legacy_provider/run.sh new file mode 100755 index 0000000..22f208d --- /dev/null +++ b/contrib/alfred_legacy_provider/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +./get_macs.sh +./crawl_all.sh diff --git a/ffmap/web/templates/index.html b/ffmap/web/templates/index.html index 088034e..00fdc16 100644 --- a/ffmap/web/templates/index.html +++ b/ffmap/web/templates/index.html @@ -9,7 +9,9 @@ </div> <div class="col-xs-8 col-sm-8"><p> Monitoring für das Freifunk Franken Funknetzwerk.<br /> - Hier werden nur Daten von Routern ab Firmware 0.5.1 angezeigt.<br /> + Hier werden Daten von Routern ab Firmware 0.5.1 direkt angezeigt.<br /> + Die alten Router werden über den <a href="https://github.com/asdil12/fff-monitoring/tree/master/contrib/alfred_legacy_provider">Alfred Legacy Provider</a> abgefragt,<br /> + sodass sie ebenfalls hier angezeigt werden können.<br /> Der Quellcode für diese Webanwendung steht unter der GPL und kann <a href="https://github.com/asdil12/fff-monitoring">hier</a> heruntergeladen werden. </p></div>