initial commit

This commit is contained in:
DarkCat09 2024-07-01 19:53:05 +04:00
commit 05c7700c0f
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
4 changed files with 67 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
__pycache__/
/storage
/*.db

7
LICENSE Normal file
View file

@ -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.

4
README Normal file
View file

@ -0,0 +1,4 @@
```bash
pipx install mitmproxy
mitmproxy -s addon.py
```

52
addon.py Normal file
View file

@ -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()