Last active
March 30, 2024 21:33
-
-
Save BigSully/3da478792ee331cb2e5ece748393f8c4 to your computer and use it in GitHub Desktop.
start mitmproxy in background programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mitmproxy.options import Options | |
from mitmproxy.proxy.config import ProxyConfig | |
from mitmproxy.proxy.server import ProxyServer | |
from mitmproxy.tools.dump import DumpMaster | |
import threading | |
import asyncio | |
import time | |
class Addon(object): | |
def __init__(self): | |
self.num = 1 | |
def request(self, flow): | |
flow.request.headers["count"] = str(self.num) | |
def response(self, flow): | |
self.num = self.num + 1 | |
flow.response.headers["count"] = str(self.num) | |
print(self.num) | |
# see source mitmproxy/master.py for details | |
def loop_in_thread(loop, m): | |
asyncio.set_event_loop(loop) # This is the key. | |
m.run_loop(loop.run_forever) | |
if __name__ == "__main__": | |
options = Options(listen_host='0.0.0.0', listen_port=8080, http2=True) | |
m = DumpMaster(options, with_termlog=False, with_dumper=False) | |
config = ProxyConfig(options) | |
m.server = ProxyServer(config) | |
m.addons.add(Addon()) | |
# run mitmproxy in backgroud, especially integrated with other server | |
loop = asyncio.get_event_loop() | |
t = threading.Thread( target=loop_in_thread, args=(loop,m) ) | |
t.start() | |
# Other servers, such as a web server, might be started then. | |
time.sleep(20) | |
print('going to shutdown mitmproxy') | |
m.shutdown() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mitmproxy works with asyncio now... Here's a working solution