1
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2
|
|
3
|
"""
|
4
|
A modular "virtual network" proxy,
|
5
|
wrapping the classes in proxy_core.py
|
6
|
"""
|
7
|
|
8
|
# This file is part of Haketilo.
|
9
|
#
|
10
|
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
|
11
|
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
|
12
|
#
|
13
|
# This program is free software: you can redistribute it and/or modify
|
14
|
# it under the terms of the GNU Affero General Public License as
|
15
|
# published by the Free Software Foundation, either version 3 of the
|
16
|
# License, or (at your option) any later version.
|
17
|
#
|
18
|
# This program is distributed in the hope that it will be useful,
|
19
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
# GNU Affero General Public License for more details.
|
22
|
#
|
23
|
# You should have received a copy of the GNU Affero General Public License
|
24
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
25
|
#
|
26
|
#
|
27
|
# I, Wojtek Kosior, thereby promise not to sue for violation of this
|
28
|
# file's license. Although I request that you do not make use this code
|
29
|
# in a proprietary program, I am not going to enforce this in court.
|
30
|
|
31
|
from pathlib import Path
|
32
|
from urllib.parse import parse_qs
|
33
|
from threading import Thread
|
34
|
|
35
|
from .proxy_core import ProxyRequestHandler, ThreadingHTTPServer
|
36
|
from .config import *
|
37
|
from .world_wide_library import catalog as internet
|
38
|
|
39
|
class RequestHijacker(ProxyRequestHandler):
|
40
|
def handle_request(self, req_body):
|
41
|
path_components = self.path.split('?', maxsplit=1)
|
42
|
path = path_components[0]
|
43
|
try:
|
44
|
# Response format: (status_code, headers (dict. of strings),
|
45
|
# body as bytes or filename containing body as string)
|
46
|
if path in internet:
|
47
|
info = internet[path]
|
48
|
if type(info) is tuple:
|
49
|
status_code, headers, body_file = info
|
50
|
resp_body = b''
|
51
|
if body_file is not None:
|
52
|
if 'Content-Type' not in headers:
|
53
|
ext = body_file.suffix[1:]
|
54
|
if ext and ext in mime_types:
|
55
|
headers['Content-Type'] = mime_types[ext]
|
56
|
|
57
|
with open(body_file, mode='rb') as f:
|
58
|
resp_body = f.read()
|
59
|
else:
|
60
|
# A function to evaluate to get the response
|
61
|
get_params, post_params = {}, {}
|
62
|
if len(path_components) == 2:
|
63
|
get_params = parse_qs(path_components[1])
|
64
|
|
65
|
# Parse POST parameters; currently only supports
|
66
|
# application/x-www-form-urlencoded
|
67
|
if req_body:
|
68
|
post_params = parse_qs(req_body.encode())
|
69
|
|
70
|
status_code, headers, resp_body = info(self.command, get_params, post_params)
|
71
|
if type(resp_body) == str:
|
72
|
resp_body = resp_body.encode()
|
73
|
|
74
|
if type(status_code) != int or status_code <= 0:
|
75
|
raise Exception('Invalid status code %r' % status_code)
|
76
|
|
77
|
for header, header_value in headers.items():
|
78
|
if type(header) != str:
|
79
|
raise Exception('Invalid header key %r' % header)
|
80
|
|
81
|
elif type(header_value) != str:
|
82
|
raise Exception('Invalid header value %r' % header_value)
|
83
|
else:
|
84
|
status_code, headers = 404, {'Content-Type': 'text/plain'}
|
85
|
resp_body = b'Handler for this URL not found.'
|
86
|
|
87
|
except Exception as e:
|
88
|
status_code, headers, resp_body = 500, {'Content-Type': 'text/plain'}, b'Internal Error:\n' + repr(e).encode()
|
89
|
|
90
|
headers['Content-Length'] = str(len(resp_body))
|
91
|
self.send_response(status_code)
|
92
|
for header, header_value in headers.items():
|
93
|
self.send_header(header, header_value)
|
94
|
|
95
|
self.end_headers()
|
96
|
if resp_body:
|
97
|
self.wfile.write(resp_body)
|
98
|
|
99
|
def do_an_internet(certdir=default_cert_dir, port=default_proxy_port):
|
100
|
"""Start up the proxy/server"""
|
101
|
class RequestHijackerWithCertdir(RequestHijacker):
|
102
|
def __init__(self, *args, **kwargs):
|
103
|
super().__init__(*args, certdir=certdir, **kwargs)
|
104
|
|
105
|
httpd = ThreadingHTTPServer(('', port), RequestHijackerWithCertdir)
|
106
|
Thread(target=httpd.serve_forever).start()
|
107
|
|
108
|
return httpd
|