-
Notifications
You must be signed in to change notification settings - Fork 47
/
mitm-rewrite.py
32 lines (25 loc) · 1023 Bytes
/
mitm-rewrite.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 logging
import re
import json
HOME_DIR = './'
DATA_DIR = HOME_DIR + 'response/'
ROUTER_FILE = HOME_DIR + 'rewrite-router.yaml'
def response(flow: http.HTTPFlow) -> None:
routers = utils.readFile(ROUTER_FILE)
url = flow.request.url
if routers is not None:
for patternURL, jsonfilename in routers.items():
if re.match(patternURL, url) is not None:
jsonfile = DATA_DIR + str(jsonfilename) + '.json'
logging.warn('>>> FOUND "' + url + '". Send response data from "' + jsonfile + '"')
data = utils.readFile(jsonfile)
if data is not None:
status = int(data['status'])
try:
content = json.dumps(data['content'])
except:
content = ''
header = data['header']
flow.response = http.Response.make(status, content, header)