From 05c7700c0f78586fc4d14cd62c85f24939a5c545 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 1 Jul 2024 19:53:05 +0400 Subject: [PATCH] initial commit --- .gitignore | 4 ++++ LICENSE | 7 +++++++ README | 4 ++++ addon.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README create mode 100644 addon.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c23fb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ + +/storage +/*.db diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c0308f --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright © 2024 Andrey DarkCat09 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..195cc8e --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +```bash +pipx install mitmproxy +mitmproxy -s addon.py +``` diff --git a/addon.py b/addon.py new file mode 100644 index 0000000..ee7a238 --- /dev/null +++ b/addon.py @@ -0,0 +1,52 @@ +import os +import time +import logging +import sqlite3 +from mitmproxy.http import HTTPFlow + + +logging.basicConfig(level=os.getenv('LOG', 'WARN')) + +storage = os.getenv('STORAGE', 'storage') +if not os.path.exists(storage): + os.mkdir(storage) + +db = sqlite3.Connection(os.getenv('SQLITE_DB_PATH', 'archive.db')) +with db as conn: + conn.executescript(''' + create table if not exists data ( + id integer primary key, + url text not null + ); + ''') + + +def response(flow: HTTPFlow) -> None: + start = time.time() + req = flow.request + url = f'{req.scheme}://{req.host}:{req.port}{req.path}' + if flow.response is None: + logging.warning('response is None: %s', url) + return + uid: int = 0 + with db as conn: + cur = conn.execute('insert into data (url) values (?) returning id', (url,)) + uid = cur.fetchone()[0] + path = os.path.join(storage, f'{uid}') + if not os.path.exists(path): + os.mkdir(path) + with open(os.path.join(path, 'headers'), 'wb') as fh: + logging.debug('%r', flow.response.headers) + fh.write(bytes(flow.response.headers)) + with open(os.path.join(path, 'body'), 'wb') as fb: + body = flow.response.raw_content + if body is None: + logging.warning('raw_content is None: %s', url) + return + fb.write(body) + logging.info('OK in %.4f sec', time.time() - start) + + +def done() -> None: + logging.info('Cleanup') + db.close()