From 20e71afeb0b7d8b6aac64e4a316d5fa1e15592a9 Mon Sep 17 00:00:00 2001 From: Adrian Schmutzler Date: Mon, 11 Dec 2017 23:16:05 +0100 Subject: [PATCH] Provide possibility to ban routers If routers are supposed to be removed from the Monitoring permanently, they can now be banned based on their MAC address. All admins can do that via the web interface. ATTENTION: This requires a database update! Signed-off-by: Adrian Schmutzler --- ffmap/db/routers.py | 12 ++++++++++++ ffmap/routertools.py | 19 ++++++++++++++++++- ffmap/web/api.py | 5 ++++- ffmap/web/application.py | 14 +++++++++++++- ffmap/web/templates/router.html | 1 + 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ffmap/db/routers.py b/ffmap/db/routers.py index 4132f77..ef95f08 100755 --- a/ffmap/db/routers.py +++ b/ffmap/db/routers.py @@ -8,6 +8,18 @@ from ffmap.mysqltools import FreifunkMySQL mysql = FreifunkMySQL() +mysql.execute(""" + CREATE TABLE banned ( + `mac` varchar(30) COLLATE utf8_unicode_ci NOT NULL, + `added` datetime NOT NULL + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +""") + +mysql.execute(""" + ALTER TABLE `banned` + ADD PRIMARY KEY (`mac`); +""") + mysql.execute(""" CREATE TABLE router ( `id` int(11) NOT NULL, diff --git a/ffmap/routertools.py b/ffmap/routertools.py index 080a008..5f5b210 100644 --- a/ffmap/routertools.py +++ b/ffmap/routertools.py @@ -29,7 +29,18 @@ def delete_router(mysql,dbid): mysql.execute("DELETE FROM router_stats_netif WHERE router = %s",(dbid,)) mysql.commit() -def import_nodewatcher_xml(mysql, mac, xml): +def ban_router(mysql,dbid): + mac = mysql.findone(""" + SELECT mac + FROM router_netif + WHERE router = %s AND netif = 'br-mesh' + """,(dbid,),"mac") + added = mysql.utcnow() + if mac: + mysql.execute("INSERT INTO banned (mac, added) VALUES (%s, %s)",(mac,added,)) + mysql.commit() + +def import_nodewatcher_xml(mysql, mac, xml, banned): global router_rate_limit_list t = utcnow() @@ -51,6 +62,12 @@ def import_nodewatcher_xml(mysql, mac, xml): try: findrouter = mysql.findone("SELECT router FROM router_netif WHERE mac = %s LIMIT 1",(mac.lower(),)) router_update = parse_nodewatcher_xml(xml) + + # cancel if banned mac found + for n in router_update["netifs"]: + if n["mac"] in banned: + return + if router_update["status"] == "wrongpos": router_update["status"] = "unknown" status_comment = "Coordinates are wrong" diff --git a/ffmap/web/api.py b/ffmap/web/api.py index 9c75ce2..1c4341d 100755 --- a/ffmap/web/api.py +++ b/ffmap/web/api.py @@ -83,6 +83,9 @@ def alfred(): #import cProfile, pstats, io #pr = cProfile.Profile() #pr.enable() + banned = mysql.fetchall(""" + SELECT mac FROM banned + """,(),"mac") if request.method == 'POST': alfred_data = request.get_json() @@ -90,7 +93,7 @@ def alfred(): # load router status xml data i = 1 for mac, xml in alfred_data.get("64", {}).items(): - import_nodewatcher_xml(mysql, mac, xml) + import_nodewatcher_xml(mysql, mac, xml, banned) if (i%500 == 0): mysql.commit() i += 1 diff --git a/ffmap/web/application.py b/ffmap/web/application.py index 9e66d12..87cbab7 100755 --- a/ffmap/web/application.py +++ b/ffmap/web/application.py @@ -9,7 +9,7 @@ from ffmap.web.filters import filters from ffmap.mysqltools import FreifunkMySQL from ffmap import stattools from ffmap.usertools import * -from ffmap.routertools import delete_router +from ffmap.routertools import delete_router, ban_router from ffmap.web.helpers import * from ffmap.config import CONFIG from ffmap.misc import writelog, writefulllog @@ -153,6 +153,18 @@ def router_info(dbid): return redirect(url_for("index")) else: flash("You are not authorized to perform this action!", "danger") + elif request.form.get("act") == "ban": + if session.get('admin'): + if mac: + ban_router(mysql,dbid) + delete_router(mysql,dbid) + flash("Router %s banned!" % router["hostname"], "success") + mysql.close() + return redirect(url_for("index")) + else: + flash("Router has no br-mesh and thus cannot be banned!", "danger") + else: + flash("You are not authorized to perform this action!", "danger") mysql.close() else: mysql.close() diff --git a/ffmap/web/templates/router.html b/ffmap/web/templates/router.html index ee704e9..aec9bd0 100644 --- a/ffmap/web/templates/router.html +++ b/ffmap/web/templates/router.html @@ -55,6 +55,7 @@