Revision 591c48a6
Added by jahoti about 2 years ago
test/server.py | ||
---|---|---|
1 |
#!/usr/bin/env python3 |
|
2 |
# |
|
1 | 3 |
# Copyright (C) 2021 jahoti <jahoti@tilde.team> |
2 | 4 |
# Licensing information is collated in the `copyright` file |
3 | 5 |
|
... | ... | |
6 | 8 |
wrapping the classes in proxy_core.py |
7 | 9 |
""" |
8 | 10 |
|
9 |
from proxy_core import *
|
|
11 |
import proxy_core
|
|
10 | 12 |
from urllib.parse import parse_qs |
13 |
from misc_constants import * |
|
14 |
from world_wide_library import catalog as internet |
|
11 | 15 |
|
12 |
internet = {} # Add info here later |
|
13 |
mime_types = { |
|
14 |
"7z": "application/x-7z-compressed", "oga": "audio/ogg", |
|
15 |
"abw": "application/x-abiword", "ogv": "video/ogg", |
|
16 |
"arc": "application/x-freearc", "ogx": "application/ogg", |
|
17 |
"bin": "application/octet-stream", "opus": "audio/opus", |
|
18 |
"bz": "application/x-bzip", "otf": "font/otf", |
|
19 |
"bz2": "application/x-bzip2", "pdf": "application/pdf", |
|
20 |
"css": "text/css", "png": "image/png", |
|
21 |
"csv": "text/csv", "sh": "application/x-sh", |
|
22 |
"gif": "image/gif", "svg": "image/svg+xml", |
|
23 |
"gz": "application/gzip", "tar": "application/x-tar", |
|
24 |
"htm": "text/html", "ts": "video/mp2t", |
|
25 |
"html": "text/html", "ttf": "font/ttf", |
|
26 |
"ico": "image/vnd.microsoft.icon", "txt": "text/plain", |
|
27 |
"js": "text/javascript", "wav": "audio/wav", |
|
28 |
"jpeg": "image/jpeg", "weba": "audio/webm", |
|
29 |
"jpg": "image/jpeg", "webm": "video/webm", |
|
30 |
"json": "application/json", "woff": "font/woff", |
|
31 |
"mjs": "text/javascript", "woff2": "font/woff2", |
|
32 |
"mp3": "audio/mpeg", "xhtml": "application/xhtml+xml", |
|
33 |
"mp4": "video/mp4", "zip": "application/zip", |
|
34 |
"mpeg": "video/mpeg", |
|
35 |
"odp": "application/vnd.oasis.opendocument.presentation", |
|
36 |
"ods": "application/vnd.oasis.opendocument.spreadsheet", |
|
37 |
"odt": "application/vnd.oasis.opendocument.text", |
|
38 |
"xml": "application/xml" # text/xml if readable from casual users |
|
39 |
} |
|
40 |
|
|
41 |
class RequestHijacker(ProxyRequestHandler): |
|
42 |
certdir = global_certdir |
|
43 |
|
|
16 |
class RequestHijacker(proxy_core.ProxyRequestHandler): |
|
44 | 17 |
def handle_request(self, req_body): |
45 | 18 |
path_components = self.path.split('?', maxsplit=1) |
46 | 19 |
path = path_components[0] |
... | ... | |
103 | 76 |
|
104 | 77 |
def do_an_internet(certdir, port): |
105 | 78 |
"""Start up the proxy/server""" |
106 |
global global_certdir |
|
107 |
global_certdir = certdir |
|
108 |
|
|
109 |
httpd = ThreadingHTTPServer(('', port), RequestHijacker) |
|
79 |
proxy_core.certdir = certdir |
|
80 |
httpd = proxy_core.ThreadingHTTPServer(('', port), RequestHijacker) |
|
110 | 81 |
httpd.serve_forever() |
82 |
|
|
83 |
if __name__ == '__main__': |
|
84 |
import sys |
|
85 |
def fail(msg, error_code): |
|
86 |
print('Error:', msg) |
|
87 |
print('Usage:', sys.argv[0], '[certificates directory] (port)') |
|
88 |
sys.exit(error_code) |
|
89 |
|
|
90 |
if len(sys.argv) < 2: |
|
91 |
fail('missing required argument "certificates directory".', 1) |
|
92 |
|
|
93 |
certdir = sys.argv[1] |
|
94 |
if not proxy_core.os.path.isdir(certdir): |
|
95 |
fail('selected certificate directory does not exist.', 2) |
|
96 |
|
|
97 |
port = sys.argv[2] if len(sys.argv) > 2 else '1337' |
|
98 |
if not port.isnumeric(): |
|
99 |
fail('port must be an integer.', 3) |
|
100 |
|
|
101 |
do_an_internet(certdir, int(port)) |
Also available in: Unified diff
Make test suite mildly usable
Allow test/server.py to be run as a command and add some "webpages" for it.