BDServer/modules/Run.py

262 lines
7.5 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
2024-12-04 14:25:58 +02:00
from modules import Email
2024-11-19 13:47:45 +02:00
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"]
try:
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+" : ******* ] "
cookie = self.cookie
toprint = consoleForm(cookie)+" "+b+"[ "+str(datetime.datetime.now())+" ] "+self.path+" "+q
except:
cookie = None
toprint = consoleForm(cookie)+" "+b+"[ "+str(datetime.datetime.now())+" ] "+self.requestline
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-28 05:30:15 +02:00
def do_POST(self):
self.path = self.path.replace("/..", "/")
self.path = self.path.replace("%27", "'")
self.cookie = self.headers.get("Cookie")
self.newview = False
2024-11-30 22:03:24 +02:00
if self.cookie: self.cookie = self.cookie[-200:]
2024-11-28 05:30:15 +02:00
# Failing early to make sure that nobody will
# try attacking this part of the server.
2024-12-03 00:30:02 +02:00
commands = ["do_edit",
"do_login",
"do_register",
"change_password"]
2024-11-28 05:30:15 +02:00
found = False
for i in commands:
if i in self.path:
found = True
break
if not found:
Render.AccessDenied(self)
return
# Not allowing more than 1 MB for the sake of
# network. A full book of Sheiny The Hacker
# which was posted on blenderdumbass.org is
# only about 160 KB. 1MB should be plenty.
length = int(self.headers["Content-Length"])
if length < 1000000:
try:
text = self.rfile.read(length).decode("utf-8")
except:
Render.AccessDenied(self)
return
else:
Render.AccessDenied(self)
return
#parsed_url = urllib.parse.urlparse(text)
self.parsed = urllib.parse.parse_qs(text)
if self.path[1:].startswith("do_edit"):
Render.Publish(self)
elif self.path[1:].startswith("do_login"):
Render.Login(self)
2024-12-03 00:30:02 +02:00
elif self.path[1:].startswith("change_password"):
Render.UpdatePassword(self)
elif self.path[1:].startswith("do_register"):
Render.Register(self)
2024-11-28 05:30:15 +02:00
else:
Render.NotFound(self)
2024-11-19 13:47:45 +02:00
def do_GET(self):
self.path = self.path.replace("/..", "/")
self.path = self.path.replace("%27", "'")
self.path = self.path.replace("%22", '"')
2024-11-19 13:47:45 +02:00
parsed_url = urllib.parse.urlparse(self.path)
self.parsed = urllib.parse.parse_qs(parsed_url.query)
2024-11-28 05:30:15 +02:00
self.cookie = self.headers.get("Cookie")
2024-11-30 22:03:24 +02:00
if self.cookie: self.cookie = self.cookie[-200:]
# ignore = ["/rss"]
# if not self.cookie and not self.path.startswith(tuple(ignore)):
# Render.Redirect(self, self.path)
# return
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("?")]
2024-11-27 21:57:44 +02:00
try:
Render.ArticlePage(self, url)
except:
Render.NotFound(self)
elif self.path[1:].startswith("account"):
url = self.path[9:]
if "?" in url: url = url[:url.find("?")]
2024-11-27 21:57:44 +02:00
try:
Render.AccountPage(self, url)
except:
Render.NotFound(self)
2024-11-23 22:34:11 +02:00
elif self.path[1:].startswith("login"):
Render.LoginPage(self)
2024-11-28 05:30:15 +02:00
elif self.path[1:].startswith("editor"):
Render.EditorPage(self)
2024-12-03 00:30:02 +02:00
elif self.path[1:].startswith("email_verify"):
Email.VerifyPage(self)
2024-11-24 23:28:19 +02:00
elif self.path[1:].startswith("register"):
Render.RegisterPage(self)
2024-11-28 05:30:15 +02:00
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-28 05:30:15 +02:00
elif self.path[1:].startswith("grant_publication_rights"):
Render.UpdatePublicationRights(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-12-04 14:25:58 +02:00
elif self.path[1:].startswith("sign_petition"):
Email.SignPetition(self)
2024-11-24 23:28:19 +02:00
2024-11-25 23:01:02 +02:00
elif self.path[1:].startswith("read_notification"):
Render.ReadNotification(self)
2024-11-29 16:20:48 +02:00
elif self.path[1:].startswith("search"):
Render.Search(self)
2024-11-25 23:01:02 +02:00
elif self.path.startswith("/graph/"):
url = self.path[6:]
if "?" in url: url = url[:url.find("?")]
Render.Graph(self, url)
2024-11-27 17:02:54 +02:00
2024-11-29 16:20:48 +02:00
elif self.path.startswith("/rss"):
2024-11-27 17:02:54 +02:00
Render.RSS(self)
2024-11-19 13:47:45 +02:00
elif self.path.startswith("/pictures/"):
2024-11-27 21:57:44 +02:00
try:
folder = Set.Folder()
f = open(folder+self.path, "rb")
f = f.read()
Render.headers(self, 200)
self.wfile.write(f)
except:
Render.NotFound(self)
2024-11-19 13:47:45 +02:00
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!")