BDServer/modules/Run.py

160 lines
4.6 KiB
Python
Raw Normal View History

2024-11-19 13:47:45 +02:00
# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )
from http.server import BaseHTTPRequestHandler, HTTPServer
from subprocess import *
import json
import os
import time
import random
import datetime
import hashlib
import urllib.request
import urllib.parse
import subprocess
import os
from modules import Set
from modules import Render
from modules.Common import *
class handler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
if "?" in self.path:
self.path = self.path[:self.path.find("?")]
2024-11-25 23:17:19 +02:00
q = clr["tbbl"]
b = clr["tbbl"]
if self.newview:
b = clr["bold"]+clr["tbrd"]
for i in self.parsed:
if i != "password":
q = q + "[ "+i+" : "+self.parsed[i][0]+" ] "
else:
q = q + "[ "+i+" : ******* ] "
2024-11-25 23:17:19 +02:00
toprint = consoleForm(self.cookie)+" "+b+"[ "+str(datetime.datetime.now())+" ] "+self.path+" "+q
print(toprint)
folder = Set.Folder()
filename = folder+"/logs/"+datetime.datetime.now().strftime('%Y-%m-%d')+".log"
logfile = open(filename, "ab")
logfile.write((toprint+"\n").encode('utf-8'))
logfile.close()
2024-11-19 13:47:45 +02:00
def do_GET(self):
self.path = self.path.replace("/..", "/")
self.path = self.path.replace("%27", "'")
parsed_url = urllib.parse.urlparse(self.path)
self.parsed = urllib.parse.parse_qs(parsed_url.query)
self.cookie = self.headers.get("Cookie")
2024-11-25 00:04:12 +02:00
if self.cookie: self.cookie = self.cookie[-50:]
2024-11-25 23:17:19 +02:00
self.newview = False
categories = list(Render.tabs().keys())
if self.path[:self.path.find("?")] == "/" or self.path == "/":
2024-11-19 13:47:45 +02:00
Render.MainPage(self)
elif self.path[1:] in categories or self.path[1:self.path.find("?")] in categories:
url = self.path[1:]
if "?" in url: url = url[:url.find("?")]
Render.ListPage(self, url)
elif self.path[1:].startswith(tuple(categories)):
url = self.path[1:]
if "?" in url: url = url[:url.find("?")]
Render.ArticlePage(self, url)
elif self.path[1:].startswith("account"):
url = self.path[9:]
if "?" in url: url = url[:url.find("?")]
Render.AccountPage(self, url)
2024-11-23 22:34:11 +02:00
elif self.path[1:].startswith("login"):
Render.LoginPage(self)
2024-11-24 23:28:19 +02:00
elif self.path[1:].startswith("register"):
Render.RegisterPage(self)
2024-11-24 16:13:01 +02:00
elif self.path[1:].startswith("settings"):
Render.SettingsPage(self)
2024-11-23 22:34:11 +02:00
elif self.path[1:].startswith("comment"):
Render.DoComment(self)
2024-11-24 02:53:16 +02:00
elif self.path[1:].startswith("delete_comment"):
Render.DeleteComment(self)
2024-11-24 16:13:01 +02:00
elif self.path[1:].startswith("update_account"):
Render.UpdateAccount(self)
2024-11-24 23:28:19 +02:00
elif self.path[1:].startswith("create_invite"):
Render.CreateInvite(self)
elif self.path[1:].startswith("cancel_invite"):
Render.CancelInvite(self)
elif self.path[1:].startswith("log_out"):
Render.LogOut(self)
2024-11-23 22:34:11 +02:00
elif self.path[1:].startswith("do_login"):
Render.Login(self)
2024-11-24 23:28:19 +02:00
elif self.path[1:].startswith("do_register"):
Render.Register(self)
2024-11-25 23:01:02 +02:00
elif self.path[1:].startswith("read_notification"):
Render.ReadNotification(self)
elif self.path.startswith("/graph/"):
url = self.path[6:]
if "?" in url: url = url[:url.find("?")]
Render.Graph(self, url)
2024-11-19 13:47:45 +02:00
elif self.path.startswith("/pictures/"):
folder = Set.Folder()
f = open(folder+self.path, "rb")
f = f.read()
Render.headers(self, 200)
self.wfile.write(f)
elif self.path == "/css":
config = Set.Load()
filename = config.get("css", "default.css")
cssfile = open(filename, "rb")
2024-11-19 13:47:45 +02:00
cssfile = cssfile.read()
Render.headers(self, 200)
self.wfile.write(cssfile)
elif self.path.startswith("/icon/"):
folder = "icons"
icon = folder+"/"+self.path[6:]+".png"
f = open(icon, "rb")
f = f.read()
Render.headers(self, 200)
self.wfile.write(f)
2024-11-23 22:34:11 +02:00
else:
2024-11-25 23:01:02 +02:00
Render.NotFound(self)
2024-11-19 13:47:45 +02:00
config = Set.Load()
PORT = config.get("port", 8080)
serve = HTTPServer(("", PORT), handler)
try:
serve.serve_forever()
except KeyboardInterrupt:
print("\nServer Stopped!")