aboutsummaryrefslogtreecommitdiff
path: root/test/world_wide_library.py
blob: 4865b0a10ba437994567ed3b8a18179104769f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# SPDX-License-Identifier: AGPL-3.0-or-later

"""
Our helpful little stand-in for the Internet
"""

# This file is part of Haketilo.
#
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use of this code
# in a proprietary program, I am not going to enforce this in court.

from hashlib import sha256
from pathlib import Path
from shutil import rmtree
from threading import Lock

from .misc_constants import here

served_scripts = {}
served_scripts_lock = Lock()

def start_serving_script(script_text):
    """
    Register given script so that it is served at
    https://serve.scrip.ts/?sha256=<script's_sha256_sum>

    Returns the URL at which script will be served.

    This function lacks thread safety. Might moght consider fixing this if it
    turns
    """
    sha256sum = sha256(script_text.encode()).digest().hex()
    served_scripts_lock.acquire()
    served_scripts[sha256sum] = script_text
    served_scripts_lock.release()

    return f'https://serve.scrip.ts/?sha256={sha256sum}'

def serve_script(command, get_params, post_params):
    """
    info() callback to pass to request-handling code in server.py. Facilitates
    serving scripts that have been registered with start_serving_script().
    """
    served_scripts_lock.acquire()
    try:
        script = served_scripts.get(get_params['sha256'][0])
    finally:
        served_scripts_lock.release()
    if script is None:
        return 404, {}, b''

    return 200, {'Content-Type': 'application/javascript'}, script

def dump_scripts(directory='./injected_scripts'):
    """
    Write all scripts that have been registered with start_serving_script()
    under the provided directory. If the directory already exists, it is wiped
    beforehand. If it doesn't exist, it is created.
    """
    directory = Path(directory)
    rmtree(directory, ignore_errors=True)
    directory.mkdir(parents=True)

    served_scripts_lock.acquire()
    for sha256, script in served_scripts.items():
        with open(directory / sha256, 'wt') as file:
            file.write(script)
    served_scripts_lock.release()

catalog = {
    'http://gotmyowndoma.in':
    (302, {'location': 'http://gotmyowndoma.in/index.html'}, None),
    'http://gotmyowndoma.in/':
    (302, {'location': 'http://gotmyowndoma.in/index.html'}, None),
    'http://gotmyowndoma.in/index.html':
    (200, {}, here / 'data' / 'pages' / 'gotmyowndomain.html'),

    'https://gotmyowndoma.in':
    (302, {'location': 'https://gotmyowndoma.in/index.html'}, None),
    'https://gotmyowndoma.in/':
    (302, {'location': 'https://gotmyowndoma.in/index.html'}, None),
    'https://gotmyowndoma.in/index.html':
    (200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html'),

    'https://serve.scrip.ts/': serve_script,

    'https://site.with.scripts.block.ed':
    (302, {'location': 'https://site.with.scripts.block.ed/index.html'}, None),
    'https://site.with.scripts.block.ed/':
    (302, {'location': 'https://site.with.scripts.block.ed/index.html'}, None),
    'https://site.with.scripts.block.ed/index.html':
    (200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html'),

    'https://site.with.scripts.allow.ed':
    (302, {'location': 'https://site.with.scripts.allow.ed/index.html'}, None),
    'https://site.with.scripts.allow.ed/':
    (302, {'location': 'https://site.with.scripts.allow.ed/index.html'}, None),
    'https://site.with.scripts.allow.ed/index.html':
    (200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html'),

    'https://site.with.paylo.ad':
    (302, {'location': 'https://site.with.paylo.ad/index.html'}, None),
    'https://site.with.paylo.ad/':
    (302, {'location': 'https://site.with.paylo.ad/index.html'}, None),
    'https://site.with.paylo.ad/index.html':
    (200, {}, here / 'data' / 'pages' / 'gotmyowndomain_https.html')
}