From 64e4cc7aa87f60efad062d76491ec78d9d96ca1e Mon Sep 17 00:00:00 2001 From: quietsy Date: Thu, 13 Jan 2022 11:38:20 +0200 Subject: [PATCH] Add fail2ban to the dashboard --- root/dashboard/swag-f2b.py | 10 ++ .../{swag-status.py => swag-proxies.py} | 4 +- root/dashboard/www/index.php | 111 ++++++++++++------ 3 files changed, 86 insertions(+), 39 deletions(-) create mode 100644 root/dashboard/swag-f2b.py rename root/dashboard/{swag-status.py => swag-proxies.py} (93%) diff --git a/root/dashboard/swag-f2b.py b/root/dashboard/swag-f2b.py new file mode 100644 index 0000000..92b6349 --- /dev/null +++ b/root/dashboard/swag-f2b.py @@ -0,0 +1,10 @@ +import json +import sqlite3 + + +con = sqlite3.connect("/config/fail2ban/fail2ban.sqlite3") +cur = con.cursor() +results = cur.execute("SELECT jails.name, COUNT(bans.ip) AS bans FROM jails LEFT JOIN bans ON jails.name=bans.jail GROUP BY jails.name").fetchall() +con.close() +output = json.dumps({k:v for (k,v) in results}, sort_keys=True) +print(output) diff --git a/root/dashboard/swag-status.py b/root/dashboard/swag-proxies.py similarity index 93% rename from root/dashboard/swag-status.py rename to root/dashboard/swag-proxies.py index b25dff2..f5604fb 100644 --- a/root/dashboard/swag-status.py +++ b/root/dashboard/swag-proxies.py @@ -19,10 +19,10 @@ def find_apps(): results = re.finditer(r"(\s+)set \$upstream_app (?P\S+?);.*\n(\s+)set \$upstream_port (?P\d+);.*\n(\s+)set \$upstream_proto (?P\w+);.*", content) for result in results: params = result.groupdict() - app = f"{params['proto']}://{params['name']}:{params['port']}" + app = f"{params['proto']}://{params['name']}:{params['port']}/" if app not in apps: apps[app] = set() - if not file_path.endswith(".sample"): + if file_path.startswith("/config/nginx/site-confs/") or file_path.endswith(".conf"): apps[app].add(file_path) return apps diff --git a/root/dashboard/www/index.php b/root/dashboard/www/index.php index 5d7c30c..dbd551d 100644 --- a/root/dashboard/www/index.php +++ b/root/dashboard/www/index.php @@ -1,32 +1,12 @@ $data){ - $tr_class = ($index % 2 == 0) ? 'shaded' : ''; - $status .= ''.$result.''; - if ($data->status == 1) { - $status .= ''; - } else { - $status .= ''; - } - $status .= ''; - if (!empty($data->locations)) { - $locations = $data->locations; - $location = implode(",", $locations); - $status .= ''.$location.''; - } else { - $status .= ''; - } - $status .= ''; - $index++; - } + function GetHeader() { return << .status-div { display: inline-block; + padding: 20px; + text-align: center; + vertical-align: top; } .status-text { font-size: 15px; @@ -55,34 +35,91 @@

A webserver and reverse proxy solution brought to you by linuxserver.io with php support and a built-in Certbot client.

We have an article on how to use swag here: docs.linuxserver.io

For help and support, please visit: linuxserver.io/support

+ HTML; + } + + function GetProxies() { + $output = shell_exec("python3 /dashboard/swag-proxies.py"); + $results = json_decode($output); + $status = ""; + $index = 0; + foreach($results as $result => $data){ + $tr_class = ($index % 2 == 0) ? 'shaded' : ''; + $status .= ''.$result.''; + if ($data->status == 1) { + $status .= ''; + } else { + $status .= ''; + } + $status .= ''; + if (!empty($data->locations)) { + $locations = $data->locations; + $location = implode(",", $locations); + $status .= ''.$location.''; + } else { + $status .= ''; + } + $status .= ''; + $index++; + } + return <<
+

Proxies

- - - - - - - - - - + + + + + + - {$status} + {$status}

Application

Available

Proxied

Location

Application

Available

Proxied

Location



-
HTML; } + + function GetF2B() { + $output = exec("python3 /dashboard/swag-f2b.py"); + $jails = json_decode($output, true); + $status = ""; + $index = 0; + foreach($jails as $jail=>$bans){ + $tr_class = ($index % 2 == 0) ? 'shaded' : ''; + $status .= ''.$jail.''.$bans.''; + $index++; + } + return << +
+

Fail2Ban

+ + + + + + + + + {$status} + +

Jail

Bans

+
+
+
+
+ HTML; + } + $geodb = file_exists('/config/geoip2db/GeoLite2-City.mmdb') ? '--geoip-database=/config/geoip2db/GeoLite2-City.mmdb' : ''; $access = shell_exec("cat /config/log/nginx/access.log | goaccess -a -o html --html-prefs='{\"theme\":\"darkGray\"}' --log-format COMBINED ".$geodb." -"); - $status = GetStatus(); + $status = GetHeader() . GetProxies() . GetF2B() . '
'; echo str_replace("
", $status, $access); ?>