Project

General

Profile

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

haketilo / test / haketilo_test / server.py @ fd9f2fc4

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 of 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
import traceback
35

    
36
from .proxy_core import ProxyRequestHandler, ThreadingHTTPServer
37
from .misc_constants import *
38
from .world_wide_library import catalog as internet
39

    
40
class RequestHijacker(ProxyRequestHandler):
41
    def handle_request(self, req_body):
42
        path_components = self.path.split('?', maxsplit=1)
43
        path = path_components[0]
44
        try:
45
            # Response format: (status_code, headers (dict. of strings),
46
            #       body as bytes or filename containing body as string)
47
            if path in internet:
48
                info = internet[path]
49
                if type(info) is tuple:
50
                    status_code, headers, body_file = info
51
                    resp_body = b''
52
                    if body_file is not None:
53
                        if 'Content-Type' not in headers:
54
                            ext = body_file.suffix[1:]
55
                            if ext and ext in mime_types:
56
                                headers['Content-Type'] = mime_types[ext]
57

    
58
                        with open(body_file, mode='rb') as f:
59
                            resp_body = f.read()
60
                else:
61
                    # A function to evaluate to get the response
62
                    get_params, post_params = {}, {}
63
                    if len(path_components) == 2:
64
                        get_params = parse_qs(path_components[1])
65

    
66
                    # Parse POST parameters; currently only supports
67
                    # application/x-www-form-urlencoded
68
                    if req_body:
69
                        post_params = parse_qs(req_body.encode())
70

    
71
                    status_code, headers, resp_body = info(self.command, get_params, post_params)
72
                    if type(resp_body) == str:
73
                        resp_body = resp_body.encode()
74

    
75
                if type(status_code) != int or status_code <= 0:
76
                    raise Exception('Invalid status code %r' % status_code)
77

    
78
                for header, header_value in headers.items():
79
                    if type(header) != str:
80
                        raise Exception('Invalid header key %r' % header)
81

    
82
                    elif type(header_value) != str:
83
                        raise Exception('Invalid header value %r' % header_value)
84
            else:
85
                status_code, headers = 404, {'Content-Type': 'text/plain'}
86
                resp_body = b'Handler for this URL not found.'
87

    
88
        except Exception:
89
            status_code = 500
90
            headers     = {'Content-Type': 'text/plain'}
91
            resp_body   = b'Internal Error:\n' + traceback.format_exc().encode()
92

    
93
        headers['Content-Length'] = str(len(resp_body))
94
        self.send_response(status_code)
95
        for header, header_value in headers.items():
96
            self.send_header(header, header_value)
97

    
98
        self.end_headers()
99
        if resp_body:
100
            self.wfile.write(resp_body)
101

    
102
def do_an_internet(certdir=Path.cwd() / 'certs',
103
                   port=default_proxy_port):
104
    """Start up the proxy/server"""
105
    class RequestHijackerWithCertdir(RequestHijacker):
106
        def __init__(self, *args, **kwargs):
107
            super().__init__(*args, certdir=certdir, **kwargs)
108

    
109
    httpd = ThreadingHTTPServer(('', port), RequestHijackerWithCertdir)
110
    Thread(target=httpd.serve_forever).start()
111

    
112
    return httpd
(9-9/11)