import subprocess from pathlib import Path import cherrypy from mako.template import Template import config import ovenapi @cherrypy.tools.register("on_end_request") def restart_server() -> None: subprocess.call(["sudo", "/usr/bin/systemctl", "restart", "ovenmediaengine"]) class Management: def __init__(self): self.page_template = Path("template/management.mako").read_text(encoding="utf-8") self.redirect_template = Path("template/message.mako").read_text(encoding="utf-8") self.api = ovenapi.OvenAPI(config.API_USER, config.API_PASS) def __message_and_redirect(self, message: str) -> bytes | str: return Template(self.redirect_template).render(message=message) @cherrypy.expose @cherrypy.tools.restart_server() def restart(self) -> bytes | str: # Blank our stream list because we're about to DC everyone config.LAST_STREAM_LIST = [] # Compile and dispatch our response return self.__message_and_redirect("Restart command dispatched") @cherrypy.expose def disconnect(self, target): vhost, app, stream = target.split(":") self.api.disconnect_key(vhost, app, stream) return self.__message_and_redirect(f"Disconnected {target}") @cherrypy.expose def ban(self, target): vhost, app, stream = target.split(":") ip = self.api.get_stream_ip(vhost, app, stream) if ip: config.BLOCKED_IPS.append(ip) self.disconnect(target) return self.__message_and_redirect(f"Banned {ip}") return self.__message_and_redirect("No stream found at that location or other error") @cherrypy.expose def unban(self, target): if target in config.BLOCKED_IPS: config.BLOCKED_IPS.remove(target) return self.__message_and_redirect(f"Unbanned {target}") return self.__message_and_redirect(f"{target} not in ban list") @cherrypy.expose def disable(self, target): config.DISABLED_KEYS.append(target) self.disconnect(target) return self.__message_and_redirect(f"Disabled key {target}") @cherrypy.expose def enable(self, target): if target in config.DISABLED_KEYS: config.DISABLED_KEYS.remove(target) return self.__message_and_redirect(f"Re-enabled {target}") return self.__message_and_redirect(f"{target} not in disabled key list") @cherrypy.expose def stop(self): config.DISABLED = True self.api.disconnect_all() return self.__message_and_redirect("Server disabled") @cherrypy.expose def start(self): config.DISABLED = False return self.__message_and_redirect("Server re-enabled") @cherrypy.expose def default(self) -> bytes | str: if not (config.API_USER and config.API_PASS): cherrypy.response.status = 503 return "Remote management is disabled on this node." data = self.api.get_all_stream_info() return Template(self.page_template).render( DISABLED=config.DISABLED, BLOCKED_IPS=config.BLOCKED_IPS, DISABLED_KEYS=config.DISABLED_KEYS, data=data, )