From a0abc6819bb72e3b9dd6f9d8ecf4b99bff1aa2b5 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Wed, 17 May 2023 19:05:59 -0400 Subject: [PATCH 01/53] Initial webhook integration --- app/classes/shared/server.py | 13 +++++++++++++ app/classes/web/webhook_handler.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 app/classes/web/webhook_handler.py diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index fa68eb62..9cecc6ce 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -31,6 +31,7 @@ from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.null_writer import NullWriter +from app.classes.web.webhook_handler import WebhookHandler with redirect_stderr(NullWriter()): import psutil @@ -512,6 +513,9 @@ class ServerInstance: if self.process.poll() is None: logger.info(f"Server {self.name} running with PID {self.process.pid}") Console.info(f"Server {self.name} running with PID {self.process.pid}") + WebhookHandler.send_discord_webhook( + "Crafty Controller", f"{self.name} started!", 65354 + ) self.is_crashed = False self.stats_helper.server_crash_reset() self.record_server_stats() @@ -807,6 +811,9 @@ class ServerInstance: logger.info(f"Stopped Server {server_name} with PID {server_pid}") Console.info(f"Stopped Server {server_name} with PID {server_pid}") + WebhookHandler.send_discord_webhook( + "Crafty Controller", f"{self.name} was stopped!", 16748076 + ) # massive resetting of variables self.cleanup_server_object() @@ -890,6 +897,12 @@ class ServerInstance: f"The server {name} has crashed and will be restarted. " f"Restarting server" ) + + WebhookHandler.send_discord_webhook( + "Crafty Controller", + f"{self.name} crashed! Crafty Controller is attempting to start it back up!", + 16711680, + ) self.run_threaded_server(None) return True logger.critical( diff --git a/app/classes/web/webhook_handler.py b/app/classes/web/webhook_handler.py new file mode 100644 index 00000000..c972cff1 --- /dev/null +++ b/app/classes/web/webhook_handler.py @@ -0,0 +1,30 @@ +import json +import logging +import requests + +logger = logging.getLogger(__name__) + + +class WebhookHandler: + @staticmethod + def send_discord_webhook(title, message, color): + dataset = { + "username": "Crafty Webhooks", + "avatar_url": "https://docs.craftycontrol.com/img/favicon.ico", + "embeds": [ + { + "title": title, + "description": message, + "color": color, + } + ], + } + + logger.debug( + "Webhook response: " + + requests.post( + "https://discord.com/api/webhooks/1107017140004995081/leFCJ4g_Uw6ZwxaZXTLmi-L7njIFwVvFbf3JEHnAvUJAd90PoMknlivel0rosfnFed77", + data=json.dumps(dataset), + headers={"Content-type": "application/json"}, + ), + ) From 03cfdc0aa11159392f09ff7189f42ac6044d6748 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 3 Jun 2023 12:00:03 -0400 Subject: [PATCH 02/53] Add perms for webhooks --- app/classes/web/panel_handler.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index b7219ac6..cb6fd9cc 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -576,6 +576,7 @@ class PanelHandler(BaseHandler): "Files": EnumPermissionsServer.FILES, "Config": EnumPermissionsServer.CONFIG, "Players": EnumPermissionsServer.PLAYERS, + "Webhooks": EnumPermissionsServer.WEBHOOKS, } page_data[ "user_permissions" @@ -749,6 +750,17 @@ class PanelHandler(BaseHandler): page_data["history_stats"] = self.controller.servers.get_history_stats( server_id, days ) + if subpage == "webhooks": + if ( + not page_data["permissions"]["Webhooks"] + in page_data["user_permissions"] + ): + if not superuser: + self.redirect( + "/panel/error?error=Unauthorized access to Webhooks Config" + ) + return + page_data[""] def get_banned_players_html(): banned_players = self.controller.servers.get_banned_players(server_id) From e228f04345d6846151cfe0e8fd4c78aa8fb47b37 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 3 Jun 2023 12:18:07 -0400 Subject: [PATCH 03/53] Remove permissions work --- app/classes/web/panel_handler.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 4297d698..692bcff4 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -576,7 +576,6 @@ class PanelHandler(BaseHandler): "Files": EnumPermissionsServer.FILES, "Config": EnumPermissionsServer.CONFIG, "Players": EnumPermissionsServer.PLAYERS, - "Webhooks": EnumPermissionsServer.WEBHOOKS, } page_data[ "user_permissions" @@ -752,7 +751,7 @@ class PanelHandler(BaseHandler): ) if subpage == "webhooks": if ( - not page_data["permissions"]["Webhooks"] + not page_data["permissions"]["Config"] in page_data["user_permissions"] ): if not superuser: @@ -760,7 +759,6 @@ class PanelHandler(BaseHandler): "/panel/error?error=Unauthorized access to Webhooks Config" ) return - page_data[""] def get_banned_players_html(): banned_players = self.controller.servers.get_banned_players(server_id) From b15477ca4ca894171885f4f26b476515d3d705ae Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 3 Jun 2023 12:19:05 -0400 Subject: [PATCH 04/53] Add providers --- app/classes/web/webhook_handler.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/classes/web/webhook_handler.py b/app/classes/web/webhook_handler.py index c972cff1..15b48b34 100644 --- a/app/classes/web/webhook_handler.py +++ b/app/classes/web/webhook_handler.py @@ -6,6 +6,22 @@ logger = logging.getLogger(__name__) class WebhookHandler: + @staticmethod + def get_providers(): + return [ + "Discord", + "Home Assistant", + "Mattermost", + "Opsgenie", + "Signal", + "Slack", + "SMTP", + "Splunk", + "Teams", + "Telegram", + "Custom", + ] + @staticmethod def send_discord_webhook(title, message, color): dataset = { @@ -23,7 +39,7 @@ class WebhookHandler: logger.debug( "Webhook response: " + requests.post( - "https://discord.com/api/webhooks/1107017140004995081/leFCJ4g_Uw6ZwxaZXTLmi-L7njIFwVvFbf3JEHnAvUJAd90PoMknlivel0rosfnFed77", + "", data=json.dumps(dataset), headers={"Content-type": "application/json"}, ), From 7c72dfc241aba6724c8501a7a15f74550e8e6812 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 3 Jun 2023 12:21:23 -0400 Subject: [PATCH 05/53] Add webhook front end --- .../panel/parts/m_server_controls_list.html | 3 + .../panel/parts/server_controls_list.html | 6 + .../templates/panel/server_webhooks.html | 280 ++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 app/frontend/templates/panel/server_webhooks.html diff --git a/app/frontend/templates/panel/parts/m_server_controls_list.html b/app/frontend/templates/panel/parts/m_server_controls_list.html index 63c6b98a..6c645881 100644 --- a/app/frontend/templates/panel/parts/m_server_controls_list.html +++ b/app/frontend/templates/panel/parts/m_server_controls_list.html @@ -31,6 +31,9 @@ {{ translate('serverDetails', 'playerControls', data['lang']) }} {% end %} {{ translate('serverDetails', 'metrics', data['lang']) }} + {% if data['permissions']['Config'] in data['user_permissions'] %} + Webhooks + {% end %} \ No newline at end of file diff --git a/app/frontend/templates/panel/parts/server_controls_list.html b/app/frontend/templates/panel/parts/server_controls_list.html index 4f5bb6b5..09c9b213 100644 --- a/app/frontend/templates/panel/parts/server_controls_list.html +++ b/app/frontend/templates/panel/parts/server_controls_list.html @@ -53,4 +53,10 @@ {{ translate('serverDetails', 'metrics', data['lang']) }} + {% if data['permissions']['Webhooks'] in data['user_permissions'] %} + + {% end %} \ No newline at end of file diff --git a/app/frontend/templates/panel/server_webhooks.html b/app/frontend/templates/panel/server_webhooks.html new file mode 100644 index 00000000..b8552401 --- /dev/null +++ b/app/frontend/templates/panel/server_webhooks.html @@ -0,0 +1,280 @@ +{% extends ../base.html %} + +{% block meta %} +{% end %} + +{% block title %}Crafty Controller - {{ translate('serverDetails', 'serverDetails', data['lang']) }}{% end %} + +{% block content %} + +
+ + +
+
+ +
+ +
+ + + {% include "parts/details_stats.html" %} + +
+ +
+
+
+ + + {% include "parts/server_controls_list.html" %} + + + {% include "parts/m_server_controls_list.html" %} + + +
+
+
+
+

{{ translate('serverSchedules', + 'scheduledTasks', data['lang']) }}

+ {% if data['user_data']['hints'] %} + + {% end %} +
+
+
+
+
+
+
+ +
+
+
+
+ + + + +
+ + + +{% end %} + +{% block js %} + + + +{% end %} \ No newline at end of file From fb2bcd7e4eebf02d0a1dc702c5eeda34e20faf53 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sat, 3 Jun 2023 12:22:10 -0400 Subject: [PATCH 06/53] Fix permissions on webhooks nav --- app/frontend/templates/panel/parts/server_controls_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/frontend/templates/panel/parts/server_controls_list.html b/app/frontend/templates/panel/parts/server_controls_list.html index 09c9b213..39360f72 100644 --- a/app/frontend/templates/panel/parts/server_controls_list.html +++ b/app/frontend/templates/panel/parts/server_controls_list.html @@ -53,7 +53,7 @@ {{ translate('serverDetails', 'metrics', data['lang']) }} - {% if data['permissions']['Webhooks'] in data['user_permissions'] %} + {% if data['permissions']['Config'] in data['user_permissions'] %}