diff --git a/server.py b/server.py index 7f230ce..97fcded 100644 --- a/server.py +++ b/server.py @@ -20,9 +20,34 @@ def external(): @app.route("/cross-server.png") def crosserver(): return send_file("cross-server.png") +@app.route("/loadcert") +def loadcert(): + return send_file("loadcert.html") +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() == "pem" +@app.route("/certload", methods=['GET', 'POST']) +def loadcert(): + if request.method == 'POST': + if 'cert' not in request.files: + return "Invalid request. (cert is missing!)" + if 'privkey' not in request.files: + return "Invalid request. (privkey is missing!)" + cert = request.files['cert'] + privkey = request.files['privkey'] + if cert.filename == '' or privkey.filename == '': + return "Please upload a certificate and private key." + if allowed_file(cert.filename) and allowed_file(privkey.filename): + random_name = ''.join(random.choice(string.ascii_lowercase+string.digits+string.ascii_uppercase) for i in range(8)) + cert.save(homefolder + "/certs/" + randomname + "-chain.pem") + privkey.save(homefolder + "/certs/" + randomname + "-privkey.pem") + return redirect("/") + else: + return "Both files must be a .pem file, you might want to generate a certificate via the home page." + else: + return "Cannot go to /certload with GET, perhaps you're looking for /loadcert" @app.route("/gencert.zip") def gencert(): - random_name = ''.join(random.choice(string.ascii_lowercase) for i in range(8)) + random_name = ''.join(random.choice(string.ascii_lowercase+string.digits+string.ascii_uppercase) for i in range(8)) k = crypto.PKey() k.generate_key(crypto.TYPE_RSA, 1024) cert = crypto.X509() @@ -45,6 +70,8 @@ def gencert(): 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()) + open(homefolder + "/certs/" + random_name + "-chain.pem", "wb").write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) + open(homefolder + "/certs/" + random_name + "-privkey.pem", "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) resp = make_response(zip_buffer.getvalue()) resp.set_cookie('certname',random_name) resp.mimetype = "application/zip"