4 Commits

Author SHA1 Message Date
Nova b48b682da7 html, branding change and getting ready for pre-rel 2026-03-24 22:50:26 +01:00
Nova 97896c87a7 idk anymore man 2026-03-13 22:13:59 +01:00
Nova 127612d408 smartass 2026-03-03 22:27:56 +01:00
Nova 3ff7a33695 something pog 2026-03-03 22:26:19 +01:00
5 changed files with 148 additions and 119 deletions
+23
View File
@@ -6,4 +6,27 @@ promised features missing, but I'm very much working on it live!
Every save I do increments the build number by 1, I won't publish all of them, but most of them will be published. Every save I do increments the build number by 1, I won't publish all of them, but most of them will be published.
Once a milestone is hit (e.g. a new feature fully implemented), I'll publish a release! Once a milestone is hit (e.g. a new feature fully implemented), I'll publish a release!
## Currently working features:
* New configuration is ~75% done, most features work.
* Fixed **A LOT** of unreported bugs from the old code.
* More resilliency against errors.
* Improved security.
## Project status:
Amethyst will stay in beta for a while, I want all features to work, put I will make pre-release versions that are mostly stable.
They can be found as the `amethyst-prerel-0.a.b` releases. I won't guarantee 100% stability, but waay more than just some random build.
## Install instructions:
Install Python, and change the provided config.
## Minimum requirements:
Python 3.8+
And whatever PC that happens to run that.
I recommend Python 3.10 or above though, with a PC running:
* Windows 8.1+
* macOS 10.15+
* Linux 4.19+
* FreeBSD 13.2R+
* Some other somewhat recent OS.
## Currently W.I.P. Check back later! ## Currently W.I.P. Check back later!
+26
View File
@@ -0,0 +1,26 @@
"""
This is the Amethyst API mode Python interface whatevers.
Docs will follow.
"""
# Below go imports.
import sys
import os
if not os.getcwd() in sys.path:
sys.path.append(os.getcwd())
import pywebsrv
class API:
"""
class
"""
def __init__(self):
# DO NOT USE THIS CLASS FOR PROGRAM, ONLY ON_REQUEST PLEASE!!
# Below go definitions to get things working.
self.build_response = pywebsrv.WebServer.build_binary_response
def on_request(self, req):
return self.build_response(200, "This is a test", "text/html")
+3 -3
View File
@@ -6,9 +6,9 @@
<body> <body>
<center> <center>
<h1>Hello from Amethyst!</h1> <h1>Hello from Amethyst!</h1>
<h2>This page confirms Amethyst can read files from your PC and serve them to your browser!</h2> <h2>This page confirms Amethyst can read files from your PC or server and serve them to your browser!</h2>
<p>If you see this page and you're not the server owner, tell them they misconfigured something!</p> <p>This is a test page, if you aren't the server owner, they might not have finished setting up their site, be patient. If this doesn't go away after a while, tell them they've made an oopsie</p>
<p>This server runs Amethyst Alpha Build 0039</p> <p>This server runs Amethyst Pre-Rel Build 0053</p>
</center> </center>
</body> </body>
</html> </html>
+1
View File
@@ -10,6 +10,7 @@ host localhost {
directory:/home/nova/PyWebServer/html2 directory:/home/nova/PyWebServer/html2
allowed-methods:GET,PUT allowed-methods:GET,PUT
block-ip:10.1.100.2 block-ip:10.1.100.2
apimode:0
block-ua:match("Discordbot") block-ua:match("Discordbot")
} }
+95 -116
View File
@@ -35,6 +35,8 @@ Library aswell as a standalone script:
You can easily get access to other parts of the script if you need it. You can easily get access to other parts of the script if you need it.
TODO: actually put normal comments in TODO: actually put normal comments in
TODO: INPROG: add typing to all code, new code will feature it by default.
""" """
import os import os
@@ -42,38 +44,41 @@ import mimetypes
import threading import threading
import ssl import ssl
import socket import socket
import re
# import re
import signal import signal
import sys import sys
try: try:
if not os.getcwd() in sys.path:
sys.path.append(f"{os.getcwd()}")
from certgen import AutoCertGen from certgen import AutoCertGen
except ImportError: except ImportError:
# just do nothing, it's not working anyway. # just do nothing, it's not working anyway.
# print( print(
# "WARN: You need the AutoCertGen plugin! Please install it from\n" "WARN: You need the AutoCertGen plugin! Please install it from\n"
# "https://git.novacow.ch/Nova/AutoCertGen/" "https://git.novacow.ch/Nova/AutoCertGen/"
# ) )
pass # pass
AMETHYST_BUILD_NUMBER = "0039" AMETHYST_BUILD_NUMBER = "0053"
AMETHYST_REPO = "https://git.novacow.ch/Nova/PyWebServer/" AMETHYST_REPO = "https://git.novacow.ch/Nova/PyWebServer/"
class ConfigParser: class ConfigParser:
def __init__(self, text): def __init__(self, text):
self.data = {"hosts": {}, "globals": {}} self.data: dict = {"hosts": {}, "globals": {}}
self._parse(text) self._parse(text)
def _parse(self, text): def _parse(self, text):
lines = [ lines: list = [
line.strip() line.strip()
for line in text.splitlines() for line in text.splitlines()
if line.strip() and not line.strip().startswith("#") if line.strip() and not line.strip().startswith("#")
] ]
current_block = None current_block: tuple | None = None
current_name = None current_name: str | None = None
for line in lines: for line in lines:
if line.startswith("host ") and line.endswith("{"): if line.startswith("host ") and line.endswith("{"):
@@ -93,8 +98,8 @@ class ConfigParser:
if ":" in line and current_block: if ":" in line and current_block:
key, value = line.split(":", 1) key, value = line.split(":", 1)
key = key.strip() key: str = key.strip()
value = value.strip() value: str = value.strip()
if "," in value: if "," in value:
value = [v.strip() for v in value.split(",")] value = [v.strip() for v in value.split(",")]
@@ -136,6 +141,8 @@ class FileHandler:
def read_file(self, file_path, directory=None): def read_file(self, file_path, directory=None):
if "../" in file_path or "%" in file_path: if "../" in file_path or "%" in file_path:
return 403, None return 403, None
if file_path == "api.py":
return 404, None
if directory is not None: if directory is not None:
full_path = os.path.join(directory, file_path.lstrip("/")) full_path = os.path.join(directory, file_path.lstrip("/"))
@@ -236,8 +243,6 @@ class FileHandler:
Generate some self-signed certificates using AutoCertGen Generate some self-signed certificates using AutoCertGen
TODO: doesn't work, need to fix. probably add `./` to $PATH TODO: doesn't work, need to fix. probably add `./` to $PATH
""" """
if not os.getcwd() in sys.path:
sys.path.append(f"{os.getcwd()}")
autocert = AutoCertGen() autocert = AutoCertGen()
autocert.gen_cert() autocert.gen_cert()
@@ -248,39 +253,48 @@ class RequestParser:
self.hosts = self.file_handler.read_new_config("hosts") self.hosts = self.file_handler.read_new_config("hosts")
print(f"Hosts: {self.hosts}") print(f"Hosts: {self.hosts}")
def parse_request_line(self, line): def parse_request_line(self, line, host):
"""Parses the HTTP request line.""" """Parses the HTTP request line."""
try: try:
method, path, version = line.split(" ") method, path, version = line.split(" ")
except ValueError: except ValueError:
return None, None, None return "DELETE", "/this/is/a/bogus/request", "HTTP/1.0"
if path.endswith("/"): if path.endswith("/") or ("." not in path):
path += "index.html" if not path.endswith("/"):
path += "/"
index = self.file_handler.read_new_config("index", host) or "index.html"
path += f"{index}"
return method, path, version return method, path, version
def ua_blocker(self, ua, host=None): def ua_is_allowed(self, ua, host=None):
"""Parses and matches UA to block""" """Parses and matches UA to block"""
del host
match, literal = self.file_handler.read_config("block-ua")
if ua in literal:
return False
for _ua in match:
if _ua.lower() in ua.lower():
return False
return True return True
# del host
# _list = self.file_handler.read_config("block-ua")
# if _list is None:
# return True
# match, literal = self.file_handler.parse_match_blocks(_list)
# if ua in literal:
# return False
# for _ua in match:
# if _ua.lower() in ua.lower():
# return False
# return True
def is_method_allowed(self, method): def is_method_allowed(self, method, host=None):
""" """
Checks if the HTTP method is allowed. Checks if the HTTP method is allowed.
Reads allowed methods from a configuration file. Reads allowed methods from a configuration file.
Falls back to allowing only 'GET' if the file does not exist. Falls back to allowing only 'GET' if the file does not exist.
Should (for now) only be GET as I haven't implemented the logic for PUT Should (for now) only be GET as I haven't implemented the logic for PUT
""" """
allowed_methods = ["GET"] # allowed_methods = ["GET"]
# While the logic for PUT, DELETE, etc. is not added, we shouldn't # While the logic for PUT, DELETE, etc. is not added, we shouldn't
# allow for it to attempt it. # allow for it to attempt it.
# Prepatched for new update. # Prepatched for new update.
# allowed_methods = self.file_handler.read_config("allowed-methods") allowed_methods = self.file_handler.read_new_config("allowed-methods", host)
if allowed_methods is None:
allowed_methods = ["GET"]
return method in allowed_methods return method in allowed_methods
def host_parser(self, host): def host_parser(self, host):
@@ -306,13 +320,6 @@ class RequestParser:
return True return True
#
# class ProxyServer:
# def __init__(
# self,
# ):
class WebServer: class WebServer:
def __init__( def __init__(
self, http_port=8080, https_port=8443, cert_file="cert.pem", key_file="key.pem" self, http_port=8080, https_port=8443, cert_file="cert.pem", key_file="key.pem"
@@ -349,17 +356,6 @@ class WebServer:
"This host cannot be reached without sending a `Host` header." "This host cannot be reached without sending a `Host` header."
) )
# TODO: enable experimental ipv6 support in config
# ipv6 when????/??//?????//?
# self.http_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# self.http_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# self.http_socket.bind(("0.0.0.0", self.http_port))
#
# self.https_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# self.https_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# self.https_socket.bind(("0.0.0.0", self.https_port))
self.http_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) self.http_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.http_socket.bind(("::", self.http_port)) self.http_socket.bind(("::", self.http_port))
@@ -377,18 +373,18 @@ class WebServer:
) )
self.http_404_html = ( self.http_404_html = (
"<html><head><title>HTTP 404 - PyWebServer</title></head>" "<html><head><title>HTTP 404 - Amethyst</title></head>"
f"<body><center><h1>HTTP 404 - Not Found!</h1><p>Running PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}</p>" f"<body><center><h1>HTTP 404 - Not Found!</h1><p>Running Amethyst/build-{AMETHYST_BUILD_NUMBER}</p>"
"</center></body></html>" "</center></body></html>"
) )
self.http_403_html = ( self.http_403_html = (
"<html><head><title>HTTP 403 - PyWebServer</title></head>" "<html><head><title>HTTP 403 - Amethyst</title></head>"
f"<body><center><h1>HTTP 403 - Forbidden</h1><p>Running PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}</p>" f"<body><center><h1>HTTP 403 - Forbidden</h1><p>Running Amethyst/build-{AMETHYST_BUILD_NUMBER}</p>"
"</center></body></html>" "</center></body></html>"
) )
self.http_405_html = ( self.http_405_html = (
"<html><head><title>HTTP 405 - PyWebServer</title></head>" "<html><head><title>HTTP 405 - Amethyst</title></head>"
f"<body><center><h1>HTTP 405 - Method not allowed</h1><p>Running PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}</p>" f"<body><center><h1>HTTP 405 - Method not allowed</h1><p>Running Amethyst/build-{AMETHYST_BUILD_NUMBER}</p>"
"</center></body></html>" "</center></body></html>"
) )
@@ -400,8 +396,6 @@ class WebServer:
http_thread = threading.Thread(target=self.start_http, daemon=True) http_thread = threading.Thread(target=self.start_http, daemon=True)
https_thread = threading.Thread(target=self.start_https, daemon=True) https_thread = threading.Thread(target=self.start_https, daemon=True)
# ipv6http_thread = threading.Thread(target=self.start_http_ipv6, daemon=True)
# ipv6https_thread = threading.Thread(target=self.start_https_ipv6, daemon=True)
if https is True: if https is True:
if self.skip_ssl is True: if self.skip_ssl is True:
@@ -410,15 +404,11 @@ class WebServer:
if yn.lower() == "n": if yn.lower() == "n":
exit(1) exit(1)
https_thread.start() https_thread.start()
# ipv6https_thread.start()
if http is True: if http is True:
# ipv6http_thread.start()
http_thread.start() http_thread.start()
http_thread.join() http_thread.join()
https_thread.join() https_thread.join()
# ipv6http_thread.join()
# ipv6https_thread.join()
def start_http(self): def start_http(self):
self.http_socket.listen(5) self.http_socket.listen(5)
@@ -432,32 +422,6 @@ class WebServer:
except OSError: except OSError:
break break
def start_http_ipv6(self):
self.ipv6http_socket.listen(5)
print(f"IPv6 HTTP server listening on port {self.http_port}...")
while self.running:
try:
conn, addr = self.ipv6http_socket.accept()
self.handle_connection(conn, addr)
except Exception as e:
print(f"HTTP error: {e}")
except OSError:
break
def start_https_ipv6(self):
self.ipv6https_socket.listen(5)
print(f"IPv6 HTTPS server listening on port {self.https_port}...")
while self.running:
try:
conn, addr = self.ipv6https_socket.accept()
self.handle_connection(conn, addr)
except Exception as e:
print(
f"HTTPS error: {e}"
) # be ready for ssl errors if you use a self-sign!!
except OSError:
break
def start_https(self): def start_https(self):
self.https_socket.listen(5) self.https_socket.listen(5)
print(f"HTTPS server listening on port {self.https_port}...") print(f"HTTPS server listening on port {self.https_port}...")
@@ -491,6 +455,13 @@ class WebServer:
conn.sendall(response) conn.sendall(response)
except Exception as e: except Exception as e:
print(f"Error handling connection: {e}") print(f"Error handling connection: {e}")
response = self.build_response(
500,
"Amethyst is currently unable to serve your request. Below is debug info.\r\n"
f"Error: {e}; Version: amethyst-b{AMETHYST_BUILD_NUMBER}\r\n"
"You cannot do anything at this time, the server owner has made a misconfiguration or there is a bug in the program",
)
conn.sendall(response)
finally: finally:
conn.close() conn.close()
@@ -514,7 +485,7 @@ class WebServer:
for line in data.splitlines(): for line in data.splitlines():
if "User-Agent" in line: if "User-Agent" in line:
ua = line.split(":", 1)[1].strip() ua = line.split(":", 1)[1].strip()
allowed = self.parser.ua_blocker(ua) allowed = self.parser.ua_is_allowed(ua)
if not allowed: if not allowed:
return self.build_response( return self.build_response(
403, "This UA has been blocked by the owner of this site." 403, "This UA has been blocked by the owner of this site."
@@ -523,7 +494,12 @@ class WebServer:
else: else:
return self.build_response(400, "You cannot connect without a User-Agent.") return self.build_response(400, "You cannot connect without a User-Agent.")
method, path, version = self.parser.parse_request_line(request_line) if ":" in host:
host2 = host.rsplit(":", 1)[0]
else:
host2 = host
method, path, version = self.parser.parse_request_line(request_line, host2)
if not all([method, path, version]): if not all([method, path, version]):
return self.build_response(400, "Bad Request") return self.build_response(400, "Bad Request")
@@ -533,19 +509,24 @@ class WebServer:
print("Got reload command! Reloading configuration...") print("Got reload command! Reloading configuration...")
self.file_handler = FileHandler() self.file_handler = FileHandler()
self.parser = RequestParser() self.parser = RequestParser()
return self.build_response(302, "") return self.build_response(302, "", host=host2)
if not self.parser.is_method_allowed(method): if not self.parser.is_method_allowed(method):
return self.build_response(405, self.http_405_html) return self.build_response(405, self.http_405_html)
if ":" in host:
host2 = host.rsplit(":", 1)[0]
directory = ( directory = (
self.file_handler.read_new_config("directory", host2) self.file_handler.read_new_config("directory", host2)
or self.file_handler.base_dir or self.file_handler.base_dir
) )
if self.file_handler.read_new_config("apimode", host2) is True:
if not os.path.join(os.getcwd(), directory) in sys.path:
sys.path.append(f"{os.path.join(os.getcwd(), directory)}")
import api
apiclass = api.API()
return apiclass.on_request(data)
file_content, mimetype = self.file_handler.read_file(path, directory) file_content, mimetype = self.file_handler.read_file(path, directory)
if file_content == 403: if file_content == 403:
@@ -556,12 +537,10 @@ class WebServer:
if file_content == 500: if file_content == 500:
return self.build_response( return self.build_response(
500, 500,
"PyWebServer has encountered a fatal error and cannot serve " "Amethyst has encountered a fatal error and cannot serve "
"your request. Contact the owner with this error: FATAL_FILE_RO_ACCESS", "your request. Contact the owner with this error: FATAL_FILE_RO_ACCESS",
) # When there was an issue with reading we throw this. ) # When there was an issue with reading we throw this.
# A really crude implementation of binary files. Later in 2.0 I'll actually
# make this useful.
mimetype = mimetype[0] mimetype = mimetype[0]
if mimetype is None: if mimetype is None:
# We have to assume it's binary. # We have to assume it's binary.
@@ -586,17 +565,17 @@ class WebServer:
status_message = messages.get(status_code) status_message = messages.get(status_code)
headers = ( headers = (
f"HTTP/1.1 {status_code} {status_message}\r\n" f"HTTP/1.1 {status_code} {status_message}\r\n"
f"Server: PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}\r\n" f"Server: Amethyst/amethyst-build-{AMETHYST_BUILD_NUMBER}\r\n"
f"Content-Type: {content_type}\r\n" f"Content-Type: {content_type}\r\n"
f"Content-Length: {len(binary_data)}\r\n" f"Content-Length: {len(binary_data)}\r\n"
f"Connection: close\r\n\r\n" f"Connection: close\r\n\r\n"
# Connection close is done because it is way easier to implement. # Connection close is done because it is way easier to implement.
# It's not like this program will see production use anyway. # It's not like this program will see production use anyway.
# Tbh when i'll implement HTTP2
) )
return headers.encode() + binary_data return headers.encode() + binary_data
def build_response(self, status_code, body): @staticmethod
def build_response(status_code, body, host=None):
""" """
For textfiles we'll not have to guess MIME-types, though the other function For textfiles we'll not have to guess MIME-types, though the other function
build_binary_response will be merged in here anyway. build_binary_response will be merged in here anyway.
@@ -612,7 +591,7 @@ class WebServer:
405: "Method Not Allowed", 405: "Method Not Allowed",
413: "Payload Too Large", 413: "Payload Too Large",
500: "Internal Server Error", 500: "Internal Server Error",
635: "Go Away", 621: "fuck off! :3",
} }
status_message = messages.get(status_code) status_message = messages.get(status_code)
@@ -623,7 +602,7 @@ class WebServer:
# Don't encode yet, if 302 status code we have to include location. # Don't encode yet, if 302 status code we have to include location.
headers = ( headers = (
f"HTTP/1.1 {status_code} {status_message}\r\n" f"HTTP/1.1 {status_code} {status_message}\r\n"
f"Server: PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}\r\n" f"Server: Amethyst/build-{AMETHYST_BUILD_NUMBER}\r\n"
f"Content-Length: {len(body)}\r\n" f"Content-Length: {len(body)}\r\n"
f"Connection: close\r\n\r\n" f"Connection: close\r\n\r\n"
).encode() ).encode()
@@ -632,28 +611,28 @@ class WebServer:
# 302 currently only happens when the reload is triggered. # 302 currently only happens when the reload is triggered.
# Why not 307, Moved Permanently? Because browsers will cache the # Why not 307, Moved Permanently? Because browsers will cache the
# response and not send the reload command. # response and not send the reload command.
host = self.file_handler.read_config("host")[0] # if port == 443:
port = self.file_handler.read_config( # host = f"https://{host}/"
"port-https" # else:
) or self.file_handler.read_config("port") # host = f"http://{host}/"
if port != 80 and port != 443:
if port == 8443:
host = f"https://{host}:{port}/"
else:
host = f"http://{host}:{port}/"
else:
if port == 443:
host = f"https://{host}/"
else:
host = f"http://{host}/"
headers = ( headers = (
f"HTTP/1.1 {status_code} {status_message}\r\n" f"HTTP/1.1 {status_code} {status_message}\r\n"
f"Location: {host}\r\n" f"Location: {host}\r\n"
f"Server: PyWebServer/amethyst-build-{AMETHYST_BUILD_NUMBER}\r\n" f"Server: Amethyst/build-{AMETHYST_BUILD_NUMBER}\r\n"
f"Content-Length: {len(body)}\r\n" f"Content-Length: {len(body)}\r\n"
f"Connection: close\r\n\r\n" f"Connection: close\r\n\r\n"
).encode() ).encode()
if status_code == 621:
headers = (
f"HTTP/1.1 {status_code} {status_message}\r\n"
"Server: Amethyst/build-0621\r\n"
"Content-Length: 30\r\n"
f"Connection: close\r\n\r\n"
)
body = "https://e621.net/posts/6155664"
print(f"{headers + body}")
return headers + body return headers + body
def shutdown(self, signum, frame): def shutdown(self, signum, frame):
@@ -679,7 +658,7 @@ def main():
file_handler = FileHandler() file_handler = FileHandler()
file_handler.base_dir = file_handler.read_config("directory") file_handler.base_dir = file_handler.read_config("directory")
http_port = file_handler.read_new_config("port") or 8080 http_port = file_handler.read_new_config("port") or 8080
https_port = file_handler.read_new_config("port-https") or 8443 https_port = file_handler.read_new_config("https-port") or 8443
http_enabled = bool(file_handler.read_new_config("http")) or True http_enabled = bool(file_handler.read_new_config("http")) or True
print(http_enabled) print(http_enabled)
https_enabled = bool(file_handler.read_new_config("https")) or False https_enabled = bool(file_handler.read_new_config("https")) or False