-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmitm-record.py
32 lines (26 loc) · 1.22 KB
/
mitm-record.py
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
from mitmproxy import http
from mitmutils import utils
import os
import re
import time
import logging
CONFIG_FILE = './record-request.yaml'
def response(flow: http.HTTPFlow) -> None:
matches = utils.readFile(CONFIG_FILE)
url = flow.request.url
if matches is not None:
for patternURL, dumpFolder in matches.items():
if not os.path.exists(dumpFolder):
os.makedirs(dumpFolder)
if re.match(patternURL, url) is not None:
dumpFile = dumpFolder + '/' + str(int(round(time.time() * 1000)))
logging.info('>>> Save ' + url + ' request details to ' + dumpFile)
with open(dumpFile, 'a') as f:
f.write(str(flow.request.method) + ' ' + str(flow.request.url) + '\n')
for k, v in flow.request.headers.items():
f.write(str(k) + ': ' + str(v) + '\n')
f.write('\n' + str(flow.request.content.decode('utf-8')) + '\n')
f.write('---\n')
for k, v in flow.response.headers.items():
f.write(str(k) + ': ' + str(v) + '\n')
f.write('\n' + str(flow.response.content.decode('utf-8')) + '\n')