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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import asyncio from mitmproxy import options from mitmproxy.tools import dump
from mitmproxy import http import hashlib,pickle,os,threading,time,base64,json from bs4 import BeautifulSoup as Soup import requests from urllib.parse import urljoin
mitmstore={}
def saveFile(): with open("store.pickle", 'wb') as fw: pickle.dump(mitmstore, fw)
if os.path.exists("store.pickle"): with open("store.pickle", 'rb') as fr: mitmstore = pickle.load(fr)
def md5(url:str,data:str): return hashlib.md5(url.encode('utf-8')+data.encode('utf-8')).hexdigest()
class Addon(object):
def request(self,flow: http.HTTPFlow): request_url=flow.request.url request_headers=flow.request.headers request_body=flow.request.get_text() request_method=flow.request.method
id = md5(getUrlPath(request_url) + request_method, request_body) if id in mitmstore: print('request',id, request_url) msg=mitmstore[id] flow.response = http.Response.make(msg['response_status_code'], msg['response_content'],msg['response_headers'])
def response(self,flow: http.HTTPFlow): request_url=flow.request.url request_headers=flow.request.headers request_body=flow.request.get_text() request_method=flow.request.method
response_status_code=flow.response.status_code response_content=flow.response.content response_headers=flow.response.headers
id=md5(getUrlPath(request_url) + request_method,request_body) if not id in mitmstore: mitmstore[id]={'request_url':request_url,'request_headers':request_headers,'request_body': request_body,'request_method': request_method,'response_status_code': response_status_code,'response_content': response_content,'response_headers': response_headers}
async def start_proxy(port): print('代理启动,端口号为',port) opts = options.Options(listen_host='127.0.0.1', listen_port=port)
master = dump.DumpMaster( opts, with_termlog=False, with_dumper=False, ) master.addons.add(Addon()) await master.run() return master
|