commit bf8958d006694d5890f42114362a167cc32fb353 Author: norohind <60548839+norohind@users.noreply.github.com> Date: Wed Oct 26 13:14:22 2022 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..923fe09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +intercepts +venv \ No newline at end of file diff --git a/deduplication.py b/deduplication.py new file mode 100644 index 0000000..724c647 --- /dev/null +++ b/deduplication.py @@ -0,0 +1,39 @@ +from pathlib import Path +from collections import defaultdict + +BASE_PATH = 'intercepts' + + +# As cron record: +# zstd --rm -z intercepts/*/*.json 2>&1 | grep -v "No such file or directory -- ignored" && python3 deduplication.py + +def take_hash_from_name(name: str) -> str: + return name.split('.')[2] + + +def remove_unique(files: dict[str, list[Path]]): + for file_hash in tuple(files.keys()): + files_list = files[file_hash] + if len(files_list) == 1: + del files[file_hash] + + +def main(): + for host_name in Path(BASE_PATH).iterdir(): + if host_name.is_dir(): + files: dict[str, list[Path]] = defaultdict(list) + for intercept_file in host_name.iterdir(): + if intercept_file.is_file(): + file_hash = take_hash_from_name(intercept_file.name) + files[file_hash].append(intercept_file) + + remove_unique(files) + + for duplicated_files in files.values(): + for file in duplicated_files[1:]: + print(f'Removing {file.name}') + file.unlink() + + +if __name__ == '__main__': + main() diff --git a/tm_intercept_script.js b/tm_intercept_script.js new file mode 100644 index 0000000..cb2c5f2 --- /dev/null +++ b/tm_intercept_script.js @@ -0,0 +1,51 @@ +// ==UserScript== +// @name XHR Requests logger +// @namespace http://tampermonkey.net/ +// @version 0.2 +// @description None +// @author None +// @match https://myshows.me/* +// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== +// @grant none +// ==/UserScript== + +(function() { + 'use strict'; + +(function(open) { + XMLHttpRequest.prototype.open = function() { + this.addEventListener("load", function() { + if (!this.responseURL.includes('/rpc/')) { + return; + } + + // Request body + // Response body + // Status code + // Endpoint + // Method + const request = { + "host": document.location.host, + "status_code": this.status, + "endpoint": this.responseURL, + "method": this.__sentry_xhr__.method, + "request_body": this.__sentry_xhr__.body, + "response_body": this.response + }; + // console.log(this.responseURL, this.response, this.status); + // console.log(this) + // console.log(JSON.stringify(request)) + let xhr = new XMLHttpRequest(); + xhr.open("POST", "https://specify.domain/upload"); + + xhr.setRequestHeader("Accept", "application/json"); + xhr.setRequestHeader("Content-Type", "application/json"); + + xhr.send(JSON.stringify(request)); + + }, false); + open.apply(this, arguments); + }; +})(XMLHttpRequest.prototype.open); + +})(); \ No newline at end of file diff --git a/web_log_server.py b/web_log_server.py new file mode 100644 index 0000000..42de589 --- /dev/null +++ b/web_log_server.py @@ -0,0 +1,31 @@ +import falcon +from pathlib import Path +from datetime import datetime +import hashlib +import json + + +class Upload: + def on_post(self, req: falcon.request.Request, resp: falcon.response.Response): + resp.status = falcon.HTTP_OK + + content = req.bounded_stream.read() + json_content = json.loads(content) + hostname = json_content['host'] + + content_hash = hashlib.md5(content).hexdigest() + + file_path = Path('intercepts').joinpath(hostname) + file_path.mkdir(exist_ok=True, parents=True) + filename = file_path.joinpath(f'{datetime.now().isoformat().replace(":", "-")}.{content_hash}.json') + + with open(filename, mode='bw') as file: + file.write(content) + + +application = falcon.App(cors_enable=True) +application.add_route('/upload', Upload()) + +if __name__ == '__main__': + import waitress + waitress.serve(application, host='127.0.0.1', port=3241)