From 06d786737839b4e0ae7c65b0446564b562f297ba Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 31 Aug 2022 17:05:47 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++- src/fn.py | 5 +++ src/html_fn.py | 60 +++++++++++++++++++++++++ src/index.py | 104 ++++---------------------------------------- src/msg.py | 17 ++++++++ src/notification.py | 19 ++++++++ 6 files changed, 113 insertions(+), 97 deletions(-) create mode 100644 src/fn.py create mode 100644 src/html_fn.py create mode 100644 src/msg.py create mode 100644 src/notification.py diff --git a/.gitignore b/.gitignore index ed26aac..254e6f9 100644 --- a/.gitignore +++ b/.gitignore @@ -134,4 +134,7 @@ cython_debug/ /config/ /db/ .vscode -/data \ No newline at end of file +/data + +# Cached Files +/src/static \ No newline at end of file diff --git a/src/fn.py b/src/fn.py new file mode 100644 index 0000000..f9bfc0f --- /dev/null +++ b/src/fn.py @@ -0,0 +1,5 @@ +import flask + + +def is_onion(req: flask.globals.request) -> bool: + return req.host.endswith("onion") diff --git a/src/html_fn.py b/src/html_fn.py new file mode 100644 index 0000000..ff7e761 --- /dev/null +++ b/src/html_fn.py @@ -0,0 +1,60 @@ +import requests +import os +from htmlpy import * + +# Wrapper for Base HTML +def buildSite(content, title=None): + return Document( + head=Head( + [ + Title(title), + Meta( + name="viewport", + content="user-scalable=no, width=device-width, initial-scale=1.0", + ), + BOOTSTRAP, + ] + ), + body=Body( + content, + global_attr=GlobalAttributes( + css_class="bg-dark text-white justify-content-center text-center" + ), + ), + ) + + +def download_file(url, file): + os.makedirs("/app/static/", exist_ok=True) + r = requests.get(url, allow_redirects=True) + open(file, "wb").write(r.content) + + +# Downloads all bootstrap files to flasks static directory +def cache_bootstrap(): + download_file( + "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css", + "/app/static/bootstrap.min.css", + ) + download_file( + "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css", + "/app/static/bootstrap-icons.css", + ) + download_file( + "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js", + "/app/static/bootstrap.bundle.min.js", + ) + + +# Bootstrap CSS +BOOTSTRAP = [ + Reference( + "/static/bootstrap.min.css", + "stylesheet", + ), + Reference( + "/static/bootstrap-icons.css", + "stylesheet", + ), + Script(src="/static/bootstrap.bundle.min.js"), +] diff --git a/src/index.py b/src/index.py index bdb617d..4936f29 100644 --- a/src/index.py +++ b/src/index.py @@ -1,83 +1,17 @@ -from flask import request, session, Blueprint, Response, redirect, escape -from htmlpy import * -from config import CONFIG from os.path import exists -import os -import datetime -import requests + import gnupg +from flask import request, Blueprint, Response, redirect +from htmlpy import * + +from config import CONFIG +from html_fn import buildSite +from msg import encrypt, save_message +from notification import notify main_pages = Blueprint("main", __name__) -def download_file(url, file): - os.makedirs("/app/static/", exist_ok=True) - r = requests.get(url, allow_redirects=True) - open(file, "wb").write(r.content) - - -# Downloads all bootstrap files to flasks static directory -def cache_bootstrap(): - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css", - "/app/static/bootstrap.min.css", - ) - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css", - "/app/static/bootstrap-icons.css", - ) - download_file( - "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js", - "/app/static/bootstrap.bundle.min.js", - ) - - -# Bootstrap CSS -BOOTSTRAP = [ - Reference( - "/static/bootstrap.min.css", - "stylesheet", - ), - Reference( - "/static/bootstrap-icons.css", - "stylesheet", - ), - Script(src="/static/bootstrap.bundle.min.js"), -] - -# Encrypt msg with GPG -def encrypt(msg): - pgp = gnupg.GPG() - pgp.import_keys(open("/config/pub.key").read()) - return str(pgp.encrypt(msg, pgp.list_keys()[0]["fingerprint"])) - - -# Save msg in `/data/messages` -def save_message(msg, name=""): - os.makedirs("/data/messages", exist_ok=True) - dt = datetime.datetime.now().strftime("%Y-%m-%d.%H-%M") - f = open(f"/data/messages/{name}-{dt}.asc", "w") - f.write(msg) - - -# Send Notification to all handlers -def notify(msg, title=None): - try: - gotify_notification(msg, title) - except: - pass - - -# Gotify Notification Handler -def gotify_notification(msg, title=None): - token = CONFIG["notify"]["gotify"]["token"] - url = CONFIG["notify"]["gotify"]["host"] - requests.post( - f"https://{url}/message?token={token}", - {"title": title, "message": msg, "priority": "5"}, - ) - - # Message Sending Page @main_pages.route("/message", methods=["GET", "POST"]) def send_message(): @@ -139,28 +73,6 @@ def send_message(): ).to_code() -# Wrapper for Base HTML -def buildSite(content, title=None): - return Document( - head=Head( - [ - Title(title), - Meta( - name="viewport", - content="user-scalable=no, width=device-width, initial-scale=1.0", - ), - BOOTSTRAP, - ] - ), - body=Body( - content, - global_attr=GlobalAttributes( - css_class="bg-dark text-white justify-content-center text-center" - ), - ), - ) - - # Public Key Page @main_pages.route("/public_key", methods=["GET"]) def public_key(): diff --git a/src/msg.py b/src/msg.py new file mode 100644 index 0000000..e9eabed --- /dev/null +++ b/src/msg.py @@ -0,0 +1,17 @@ +import datetime +import os +import gnupg + +# Encrypt msg with GPG +def encrypt(msg): + pgp = gnupg.GPG() + pgp.import_keys(open("/config/pub.key").read()) + return str(pgp.encrypt(msg, pgp.list_keys()[0]["fingerprint"])) + + +# Save msg in `/data/messages` +def save_message(msg, name=""): + os.makedirs("/data/messages", exist_ok=True) + dt = datetime.datetime.now().strftime("%Y-%m-%d.%H-%M") + f = open(f"/data/messages/{name}-{dt}.asc", "w") + f.write(msg) diff --git a/src/notification.py b/src/notification.py new file mode 100644 index 0000000..03b06a5 --- /dev/null +++ b/src/notification.py @@ -0,0 +1,19 @@ +from config import CONFIG +import requests + +# Send Notification to all handlers +def notify(msg, title=None): + try: + gotify_notification(msg, title) + except: + pass + + +# Gotify Notification Handler +def gotify_notification(msg, title=None): + token = CONFIG["notify"]["gotify"]["token"] + url = CONFIG["notify"]["gotify"]["host"] + requests.post( + f"https://{url}/message?token={token}", + {"title": title, "message": msg, "priority": "5"}, + )