♻️ refactor
This commit is contained in:
parent
ecb01dfc75
commit
06d7867378
6 changed files with 113 additions and 97 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -135,3 +135,6 @@ cython_debug/
|
|||
/db/
|
||||
.vscode
|
||||
/data
|
||||
|
||||
# Cached Files
|
||||
/src/static
|
5
src/fn.py
Normal file
5
src/fn.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
import flask
|
||||
|
||||
|
||||
def is_onion(req: flask.globals.request) -> bool:
|
||||
return req.host.endswith("onion")
|
60
src/html_fn.py
Normal file
60
src/html_fn.py
Normal file
|
@ -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"),
|
||||
]
|
104
src/index.py
104
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():
|
||||
|
|
17
src/msg.py
Normal file
17
src/msg.py
Normal file
|
@ -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)
|
19
src/notification.py
Normal file
19
src/notification.py
Normal file
|
@ -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"},
|
||||
)
|
Loading…
Add table
Reference in a new issue