Project

General

Profile

Download (3.74 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / server.py @ e9b7f4d7

1
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
2
# Licensing information is collated in the `copyright` file
3

    
4
"""
5
A modular "virtual network" proxy,
6
wrapping the classes in proxy_core.py
7
"""
8

    
9
from proxy_core import *
10
from urllib.parse import parse_qs
11

    
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
	def handle_request(self, req_body):
43
		path_components = self.path.split('?', maxsplit=1)
44
		path = path_components[0]
45
		try:
46
			# Response format: (status_code, headers (dict. of strings),
47
			#       body as bytes or filename containing body as string)
48
			if path in internet:
49
				info = internet[path]
50
				if type(info) == tuple:
51
					status_code, headers, body_file = info
52
					if type(body_file) == str:
53
						if 'Content-Type' not in headers and '.' in body_file:
54
							ext = body_file.rsplit('.', maxsplit=1)[-1]
55
							if ext in mime_types:
56
								headers['Content-Type'] = mime_types[ext]
57
							
58
						with open(body_file, mode='rb') as f:
59
							body_file = f.read()
60
						
61
				else:
62
					# A function to evaluate to get the response
63
					get_params, post_params = {}, {}
64
					if len(path_components) == 2:
65
						get_params = parse_qs(path_components[1])
66
					
67
					# Parse POST parameters; currently only supports
68
					# application/x-www-form-urlencoded
69
					if req_body:
70
						post_params = parse_qs(req_body.encode())
71

    
72
					status_code, headers, body_file = info(self.command, get_params, post_params)
73
					if type(body_file) == str:
74
						body_file = body_file.encode()
75
			
76
				if type(status_code) != int or status_code <= 0:
77
					raise Exception('Invalid status code %r' % status_code)
78
				
79
				for header, header_value in headers.items():
80
					if type(header) != str:
81
						raise Exception('Invalid header key %r' % header)
82
					
83
					elif type(header_value) != str:
84
						raise Exception('Invalid header value %r' % header_value)
85
			else:
86
				status_code, headers = 404, {'Content-Type': 'text/plain'}
87
				body_file = b'Handler for this URL not found.'
88
		
89
		except Exception as e:
90
			status_code, headers, body_file = 500, {'Content-Type': 'text/plain'}, b'Internal Error:\n' + repr(e).encode()
91
		
92
		headers['Content-Length'] = str(len(body_file))
93
		self.send_response(status_code)
94
		for header, header_value in headers.items():
95
			self.send_header(header, header_value)
96
		
97
		self.end_headers()
98
		self.wfile.write(body_file)
99
		
100

    
101

    
102
def do_an_internet(port=1337):
103
	"""Start up the proxy/server"""
104
	
105
	httpd = ThreadingHTTPServer(('', port), RequestHijacker)
106
	httpd.serve_forever()
(3-3/3)