Skip to content

Instantly share code, notes, and snippets.

@hackaugusto
Last active October 6, 2025 13:13
Show Gist options
  • Save hackaugusto/4c8aee22d372f1fb11731731b7f588a6 to your computer and use it in GitHub Desktop.
Save hackaugusto/4c8aee22d372f1fb11731731b7f588a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import shlex
import subprocess
import os.path
import http
import http.server
import socketserver
import urllib.parse
import ssl
import pathlib
PORT = 8001
CONFIGFILE = os.path.realpath(os.path.join(".", "config"))
KEYFILE = os.path.realpath(os.path.join(".", "snakeoil.key"))
CERTFILE = os.path.realpath(os.path.join(".", "snakeoil.pem"))
if not os.path.exists(CONFIGFILE):
with open(CONFIGFILE, "w") as h:
h.write(
"""
[ req ]
prompt = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
C = DE
ST = SnakeOil State
L = SnakeOil Locality
O = SnakeOil Org
OU = SnakeOil Unit Name
CN = SnakeOil
emailAddress = [email protected]
"""
)
if not os.path.exists(KEYFILE):
subprocess.run(
shlex.split(
f"openssl req -config {CONFIGFILE} -new -newkey rsa:4096 -nodes -keyout snakeoil.key -out snakeoil.csr"
)
)
if not os.path.exists(CERTFILE):
subprocess.run(
shlex.split(
"openssl x509 -req -sha256 -days 365 -in snakeoil.csr -signkey snakeoil.key -out snakeoil.pem"
)
)
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
cwd = pathlib.Path.cwd()
req = cwd.joinpath(self.path[1:]).resolve()
# If the request path tried to escape current directory, replace it
if not req.is_relative_to(cwd):
req = cwd
if req.is_relative_to(cwd) and req.is_file():
stat = req.stat()
self.send_response(http.HTTPStatus.OK)
self.send_header("Content-Type", "application/octet-stream")
self.send_header(
"Content-Disposition", f'attachment; filename="{req.name}"'
)
self.send_header("Content-Length", str(stat.st_size))
self.end_headers()
self.wfile.write(req.open("rb").read())
else:
string = """
<form action="/" method="post">
<input type="text" name="data" />
<input type="submit" />
</form>
<ul>
"""
for root, dirs, files in req.walk():
for file in files:
file = root.joinpath(file).relative_to(cwd)
string += f"<li><a href='{file}'>{file}</a></li>"
encoded = string.encode("UTF-8", "replace")
self.send_response(http.HTTPStatus.OK)
self.send_header("Content-Type", "text/html;charset=utf-8")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
def do_POST(self):
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
# Print the posted data to stdout, useful to pass secrets from http client
# to the server over the encrypted connection
print(urllib.parse.unquote(post_data.decode("utf8")))
# List the directory
self.do_GET()
class HTTPSServer(socketserver.TCPServer):
def get_request(self):
newsocket, fromaddr = self.socket.accept()
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(CERTFILE, KEYFILE)
connstream = context.wrap_socket(newsocket, server_side=True)
return connstream, fromaddr
with HTTPSServer(("", PORT), SimpleHTTPRequestHandler) as httpd:
print("Serving with HTTPS at port", PORT)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment