From dd5c2920f22806902f47a59441e080dacfd7590f Mon Sep 17 00:00:00 2001 From: swee Date: Tue, 14 Jan 2025 19:14:42 -0800 Subject: [PATCH] Update server.py --- server.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 8c7472a..9bc4788 100644 --- a/server.py +++ b/server.py @@ -1,9 +1,13 @@ #!/usr/bin/python3 -import asyncio, traceback, socket, ssl +import asyncio, traceback, socket, ssl, zipfile +from OpenSSL import crypto from urllib.parse import urlparse, quote from flask import Flask, request, redirect, send_file, Response from hypercorn.config import Config from hypercorn.asyncio import serve +from pathlib import Path +homefolder = str(Path.home()) +import uuid app = Flask(__name__) @app.route("/") def root(): @@ -14,6 +18,31 @@ def external(): @app.route("/cross-server.png") def crosserver(): return send_file("cross-server.png") +@app.route("/gencert.zip"): + random_name = str(uuid.uuid4()) + k = crypto.PKey() + k.generate_key(crypto.TYPE_RSA, 1024) + cert = crypto.X509() + cert.get_subject().C = "Earth" + cert.get_subject().ST = "Earth" + cert.get_subject().L = "Earth" + cert.get_subject().O = "Gem2Browser " + random_name + cert.get_subject().OU = "Gem2Browser " + random_name + cert.get_subject().CN = "g2b.swee.codes" + cert.set_serial_number(1000) + cert.gmtime_adj_notBefore(0) + cert.gmtime_adj_notAfter(10*365*24*60*60) + cert.set_issuer(cert.get_subject()) + cert.set_pubkey(k) + cert.sign(k, 'sha1') + #open(homefolder + "/" + random_name + "-privkey.pem", "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) + #open(homefolder + "/" + random_name + "-cert.pem", "wb").write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) + zip_buffer = io.BytesIO() + with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: + for file_name, data in [('cert.pem', io.BytesIO(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))), + ('privkey.pem', io.BytesIO(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)))]: + zip_file.writestr(file_name, data.getvalue()) + return zip_buffer.getvalue() @app.route("/style.css") def style(): return send_file("style.css")