# 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 # Copyright (C) 2021 Wojtek Kosior # # 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 . # # # 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= 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://gotmyowndoma.in/scripts_to_block_1.html': (200, {}, here / 'data' / 'pages' / 'scripts_to_block_1.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') }