diff --git a/hyp/__init__.py b/hyp/__init__.py new file mode 100644 index 0000000..649ec85 --- /dev/null +++ b/hyp/__init__.py @@ -0,0 +1,5 @@ +from .hyp import UploadHandler, check_cert, serve + +if __name__ == '__main__': + from .hyp import main + main() \ No newline at end of file diff --git a/hyp.py b/hyp/hyp.py similarity index 83% rename from hyp.py rename to hyp/hyp.py index 466fb1a..bcc0b81 100644 --- a/hyp.py +++ b/hyp/hyp.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 import argparse +import os import sys import ssl import cgi -import os import os.path as path import http.server as http - upload_page = '''
''' + class UploadHandler(http.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) @@ -48,6 +48,10 @@ class UploadHandler(http.BaseHTTPRequestHandler): def check_cert(dir): + """ + check_dir(path) -> bool + Test whether dir constain a valid key/cert pair + """ try: for i in 'cert', 'key': open(path.join(dir, 'https-%s.pem' % i)).close() @@ -60,7 +64,12 @@ def check_cert(dir): return True -def main(address, port, tls_dir, upload): +def serve(address, port, tls_dir, upload): + """ + Starts serving on http[s]://address:port + tls_dir (filepath): directory which contains the key/cert pair + upload (bool): turn on/off the upload only server mode + """ use_tls = check_cert(tls_dir) port = 80 if port == 443 and not use_tls else port bind = (address, port) @@ -71,10 +80,15 @@ def main(address, port, tls_dir, upload): else: server = http.HTTPServer(bind, http.SimpleHTTPRequestHandler) if use_tls: + try: + protocol = ssl.PROTOCOL_TLSv1_2 + except AttributeError: + print('hyp needs TLSv1.2. You must have openssl >= 1.0.1') + sys.exit(1) tls_socket = ssl.wrap_socket(server.socket, server_side=True, certfile=path.join(tls_dir, 'https-cert.pem'), keyfile=path.join(tls_dir,'https-key.pem'), - ssl_version=ssl.PROTOCOL_TLSv1_2) + ssl_version=protocol) server.socket = tls_socket except Exception as e: print('Error %d: %s' % e.args) @@ -89,7 +103,7 @@ def main(address, port, tls_dir, upload): server.socket.close() -if __name__ == '__main__': +def main(): parser = argparse.ArgumentParser(description='Hyperminimal https server') parser.add_argument('address', nargs='?', type=str, default='0.0.0.0', @@ -102,8 +116,8 @@ if __name__ == '__main__': parser.add_argument('-t', '--tls', type=str, dest='tls_dir', metavar='DIR', default='/usr/local/etc/openssl', - help='cert/key couple directory. Must be PEM ' + help='cert/key pair directory. Must be PEM ' 'formatted and named https-key.pem, https-cert.pem ' '(default: /usr/local/etc/openssl)') args = parser.parse_args() - main(**vars(args)) + serve(**vars(args)) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1152a6a --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup + +setup(name='hyp', + version='1.0.0', + description='Hyperminimal https server', + url='http://github.com/rnhmjoj/hyp', + author='Michele Guerini Rocco', + license='MIT-GPL', + packages=['hyp'], + entry_points={ + 'console_scripts': ['hyp = hyp.hyp:main'] + }, + ) \ No newline at end of file