Project

General

Profile

« Previous | Next » 

Revision 591c48a6

Added by jahoti about 2 years ago

Make test suite mildly usable

Allow test/server.py to be run as a command and add some "webpages" for it.

View differences:

copyright
89 89
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
90 90
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 91

  
92
Files: test/gorilla.py
92
Files: test/gorilla.py test/misc_constants.py test/world_wide_library.py test/data/pages/*
93 93
Copyright: 2021 jahoti <jahoti@tilde.team>
94 94
   2021 jahoti <jahoti@tilde.team>
95 95
License: AGPL-3+ or Alicense-1.0
test/data/pages/gotmyowndomain.html
1
<html>
2
	<head>
3
		<meta name=charset value="latin1">
4
		<title>
5
			Schrodinger's Document
6
		</title>
7
	</head>
8
	<body>
9
		A nice, simple page for testing.
10
		<script>
11
			document.write('<p><b>Or so you thought...</b></p>');
12
		</script>
13
	</body>
14
</html>
test/data/pages/gotmyowndomain_https.html
1
<html>
2
	<head>
3
		<meta name=charset value="latin1">
4
		<title>
5
			Schrodinger's Document
6
		</title>
7
	</head>
8
	<body>
9
		A nice, simple page for testing (using HTTPS).
10
		<script>
11
			document.write('<p><b>Or so you thought...</b></p>');
12
		</script>
13
	</body>
14
</html>
test/init.sh
7 7
# Initialize the root certificate for the tests proxy server
8 8
# Make sure this is run in the directory where they will be put!
9 9

  
10

  
11
if [ -n "$1" ]; then
12
    cd "$1"
13
fi
10 14
openssl genrsa -out ca.key 2048
11 15
openssl genrsa -out cert.key 2048
12 16
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/CN=Hachette Test"
test/misc_constants.py
1
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
2
# Licensing information is collated in the `copyright` file
3

  
4
"""
5
Miscellaneous data that were found useful
6
"""
7

  
8
mime_types = {
9
	"7z": "application/x-7z-compressed",	"oga": "audio/ogg",
10
	"abw": "application/x-abiword",		"ogv": "video/ogg",
11
	"arc": "application/x-freearc",		"ogx": "application/ogg",
12
	"bin": "application/octet-stream",	"opus": "audio/opus",
13
	"bz": "application/x-bzip",		"otf": "font/otf",
14
	"bz2": "application/x-bzip2",		"pdf": "application/pdf",
15
	"css": "text/css",			"png": "image/png",
16
	"csv": "text/csv",			"sh": "application/x-sh",
17
	"gif": "image/gif",			"svg": "image/svg+xml",
18
	"gz": "application/gzip",		"tar": "application/x-tar",
19
	"htm": "text/html",			"ts": "video/mp2t",
20
	"html": "text/html",			"ttf": "font/ttf",
21
	"ico": "image/vnd.microsoft.icon",	"txt": "text/plain",
22
	"js": "text/javascript",		"wav": "audio/wav",
23
	"jpeg": "image/jpeg",			"weba": "audio/webm",
24
	"jpg": "image/jpeg",			"webm": "video/webm",
25
	"json": "application/json",		"woff": "font/woff",	
26
	"mjs": "text/javascript",		"woff2": "font/woff2",
27
	"mp3": "audio/mpeg",			"xhtml": "application/xhtml+xml",
28
	"mp4": "video/mp4",			"zip": "application/zip",
29
	"mpeg": "video/mpeg",
30
	"odp": "application/vnd.oasis.opendocument.presentation",
31
	"ods": "application/vnd.oasis.opendocument.spreadsheet",
32
	"odt": "application/vnd.oasis.opendocument.text",
33
	"xml": "application/xml" # text/xml if readable from casual users
34
}
test/proxy_core.py
5 5
"""
6 6
The core for a "virtual network" proxy
7 7

  
8
Be sure to run this inside your intended certificates directory.
8
Be sure to set certdir to your intended certificates directory before running.
9 9
"""
10 10

  
11 11
import os, socket, ssl, subprocess, sys, threading, time
......
20 20

  
21 21
class ProxyRequestHandler(BaseHTTPRequestHandler):
22 22
	"""Handles a network request made to the proxy"""
23
	certdir = ''
24
	
25 23
	def log_error(self, format, *args):
26 24
		# suppress "Request timed out: timeout('timed out',)"
27 25
		if isinstance(args[0], socket.timeout):
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))
test/world_wide_library.py
1
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
2
# Licensing information is collated in the `copyright` file
3

  
4
"""
5
Our helpful little stand-in for the Internet
6
"""
7

  
8
catalog = {
9
	'http://gotmyowndoma.in': (302, {'location': 'http://gotmyowndoma.in/index.html'}, b''),
10
	'http://gotmyowndoma.in/': (302, {'location': 'http://gotmyowndoma.in/index.html'}, b''),
11
	'http://gotmyowndoma.in/index.html': (200, {}, 'data/pages/gotmyowndomain.html'),
12
	'https://gotmyowndoma.in': (302, {'location': 'https://gotmyowndoma.in/index.html'}, b''),
13
	'https://gotmyowndoma.in/': (302, {'location': 'https://gotmyowndoma.in/index.html'}, b''),
14
	'https://gotmyowndoma.in/index.html': (200, {}, 'data/pages/gotmyowndomain_https.html')
15
}

Also available in: Unified diff