Moved ClientController to a separate file.

This commit is contained in:
Marat Fayzullin 2023-11-16 00:14:31 -05:00
parent 6757247596
commit 5ba68d346a
4 changed files with 100 additions and 52 deletions

21
htdocs/clients.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html>
<head>
<title>OpenWebRX+ Clients</title>
<link rel="shortcut icon" type="image/x-icon" href="static/favicon.ico" />
<link rel="stylesheet" href="static/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="static/css/admin.css" />
<script src="compiled/settings.js"></script>
<meta charset="utf-8">
</head>
<body>
${header}
<div class="container">
<div class="row">
<h1 class="col-12">Clients</h1>
</div>
<div class="row mt-3 client-list">
${clients}
</div>
</div>
</body>

View File

@ -0,0 +1,71 @@
from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController
from owrx.breadcrumb import Breadcrumb, BreadcrumbItem, BreadcrumbMixin
from owrx.websocket import WebSocketConnection
import re
import logging
logger = logging.getLogger(__name__)
class ClientController(AuthorizationMixin, WebpageController):
def indexAction(self):
self.serve_template("clients.html", **self.template_variables())
def template_variables(self):
variables = super().template_variables()
variables["clients"] = self.renderClients()
return variables
@staticmethod
def renderClients():
return """
<table class='table'>
<tr>
<th>IP Address</th>
<th>SDR Profile</th>
<th>Local Time</th>
<th>Actions</th>
</tr>
{clients}
</table>
""".format(
clients="".join(ClientController.renderClient(c) for c in WebSocketConnection.listAll())
)
@staticmethod
def renderClient(c):
return "<tr><td>{0}</td><td>{1}</td><td>{2} {3}</td><td>{4}</td></tr>".format(
ClientController.renderIp(c["ip"]),
"banned" if c["ban"] else c["sdr"] + " " + c["band"] if "sdr" in c else "n/a",
"until" if c["ban"] else "since",
c["ts"].strftime('%H:%M:%S'),
ClientController.renderButtons(c)
)
@staticmethod
def renderIp(ip):
ip = re.sub("^::ffff:", "", ip)
return """
<a href="https://www.geolocation.com/en_us?ip={0}#ipresult" target="_blank">{1}</a>
""".format(ip, ip)
@staticmethod
def renderButtons(c):
action = "unban" if c["ban"] else "ban"
return """
<button type="button" class="btn btn-sm btn-danger client-{0}" value="{1}">{2}</button>
""".format(action, c["ip"], action)
def ban(self):
ip = self.request.matches.group(1)
logger.info("Banning {0} for {1} minutes".format(ip, 15))
WebSocketConnection.banIp(ip, 15)
self.send_response("{}", content_type="application/json", code=200)
def unban(self):
ip = self.request.matches.group(1)
logger.info("Unbanning {0}".format(ip))
WebSocketConnection.unbanIp(ip)
self.send_response("{}", content_type="application/json", code=200)

View File

@ -1,11 +1,11 @@
from owrx.config import Config
from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController
from owrx.controllers.clients import ClientController
from owrx.breadcrumb import Breadcrumb, BreadcrumbItem, BreadcrumbMixin
from owrx.websocket import WebSocketConnection
from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs
import re
import logging
@ -18,57 +18,9 @@ class SettingsController(AuthorizationMixin, WebpageController):
def template_variables(self):
variables = super().template_variables()
variables["clients"] = self.renderClients()
variables["clients"] = ClientController.renderClients()
return variables
def renderClients(self):
return """
<table class='table'>
<tr>
<th>IP Address</th>
<th>SDR Profile</th>
<th>Local Time</th>
<th>Actions</th>
</tr>
{clients}
</table>
""".format(
clients="".join(self.renderClient(c) for c in WebSocketConnection.listAll())
)
def renderClient(self, c):
return "<tr><td>{0}</td><td>{1}</td><td>{2} {3}</td><td>{4}</td></tr>".format(
self.renderIp(c["ip"]),
"banned" if c["ban"] else c["sdr"] + " " + c["band"] if "sdr" in c else "n/a",
"until" if c["ban"] else "since",
c["ts"].strftime('%H:%M:%S'),
self.renderButtons(c)
)
def renderIp(self, ip):
ip = re.sub("^::ffff:", "", ip)
return """
<a href="https://www.geolocation.com/en_us?ip={0}#ipresult" target="_blank">{1}</a>
""".format(ip, ip)
def renderButtons(self, c):
action = "unban" if c["ban"] else "ban"
return """
<button type="button" class="btn btn-sm btn-danger client-{0}" value="{1}">{2}</button>
""".format(action, c["ip"], action)
def ban(self):
ip = self.request.matches.group(1)
logger.info("Banning {0} for {1} minutes".format(ip, 15))
WebSocketConnection.banIp(ip, 15)
self.send_response("{}", content_type="application/json", code=200)
def unban(self):
ip = self.request.matches.group(1)
logger.info("Unbanning {0}".format(ip))
WebSocketConnection.unbanIp(ip)
self.send_response("{}", content_type="application/json", code=200)
class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageController, metaclass=ABCMeta):
def __init__(self, handler, request, options):

View File

@ -6,6 +6,7 @@ from owrx.controllers.assets import OwrxAssetsController, AprsSymbolsController,
from owrx.controllers.websocket import WebSocketController
from owrx.controllers.api import ApiController
from owrx.controllers.metrics import MetricsController
from owrx.controllers.clients import ClientController
from owrx.controllers.settings import SettingsController
from owrx.controllers.settings.general import GeneralSettingsController
from owrx.controllers.settings.sdr import (
@ -106,8 +107,11 @@ class Router(object):
StaticRoute("/metrics", MetricsController, options={"action": "prometheusAction"}),
StaticRoute("/metrics.json", MetricsController),
StaticRoute("/settings", SettingsController),
RegexRoute("^/settings/ban/(.+)$", SettingsController, options={"action": "ban"}),
RegexRoute("^/settings/unban/(.+)$", SettingsController, options={"action": "unban"}),
StaticRoute("/clients", ClientController),
RegexRoute("^/clients/ban/(.+)$", ClientController, options={"action": "ban"}),
RegexRoute("^/clients/unban/(.+)$", ClientController, options={"action": "unban"}),
RegexRoute("^/settings/ban/(.+)$", ClientController, options={"action": "ban"}),
RegexRoute("^/settings/unban/(.+)$", ClientController, options={"action": "unban"}),
StaticRoute("/settings/general", GeneralSettingsController),
StaticRoute(
"/settings/general", GeneralSettingsController, method="POST", options={"action": "processFormData"}