Created
December 3, 2019 12:52
-
-
Save hackaugusto/4c8aee22d372f1fb11731731b7f588a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import shlex | |
import subprocess | |
import os.path | |
import http | |
import http.server | |
import socketserver | |
import urllib.parse | |
import ssl | |
PORT = 8001 | |
KEYFILE = os.path.realpath(os.path.join(".", "snakeoil.key")) | |
CERTFILE = os.path.realpath(os.path.join(".", "snakeoil.pem")) | |
if not os.path.exists(KEYFILE): | |
subprocess.run( | |
shlex.split( | |
"openssl req -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" | |
) | |
) | |
PAGE_UNENCODED = """\ | |
<!DOCTYPE html> | |
<html> | |
<body> | |
<form action="/" method="post"> | |
<input type="text" name="data" /> | |
<input type="submit" /> | |
</form> | |
</body> | |
</html> | |
""" | |
PAGE = PAGE_UNENCODED.encode("UTF-8", "replace") | |
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(http.HTTPStatus.OK) | |
self.send_header("Content-Type", "text/html;charset=utf-8") | |
self.send_header("Content-Length", str(len(PAGE))) | |
self.end_headers() | |
self.wfile.write(PAGE) | |
def do_POST(self): | |
content_length = int(self.headers["Content-Length"]) | |
post_data = self.rfile.read(content_length) | |
print(urllib.parse.unquote(post_data.decode("utf8"))) | |
self.do_GET() | |
class HTTPSServer(socketserver.TCPServer): | |
def get_request(self): | |
newsocket, fromaddr = self.socket.accept() | |
connstream = ssl.wrap_socket( | |
newsocket, server_side=True, certfile=CERTFILE, keyfile=KEYFILE, | |
) | |
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