Is there a way to start mitmproxy v.7.0.2 programmatically in the background?
Asked Answered
S

3

7

Is there a way to start mitmproxy v.7.0.2 programmatically in the background? ProxyConfig and ProxyServer have been removed since version 7.0.0, and the code below isn't working.

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

from BigSully's gist

Sling answered 4/8, 2021 at 15:32 Comment(0)
G
4

You can put your Addon class into your_script.py and then run mitmdump -s your_script.py. mitmdump comes without the console interface and can run in the background.

We (mitmproxy devs) officially don't support manual instantiation from Python anymore because that creates a massive amount of support burden for us. If you have some Python experience you can probably find your way around.

What if my addon has additional dependencies?

Approach 1: pip install mitmproxy is still perfectly supported and gets you the same functionality as the standalone binaries. Bonus tip: You can run venv/bin/mitmproxy or venv/Scripts/mitmproxy.exe to invoke mitmproxy in your virtualenv without having your virtualenv activated.

Approach 2: You can install mitmproxy with pipx and then run pipx inject mitmproxy <your dependency name>. See https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi for details.

How can I debug mitmproxy itself?

If you are debugging from the command line (be it print statements or pdb), the easiest approach is to run mitmdump instead of mitmproxy, which provides the same functionality minus the console interface. Alternatively, you can use PyCharm's remote debug functionality, which also works while the console interface is active (https://github.com/mitmproxy/mitmproxy/blob/main/examples/contrib/remote-debug.py).

Gramercy answered 5/8, 2021 at 5:44 Comment(2)
Thanks for the comment. If your own script needs other libraries that you would normally install in a virtual environment, how is the recommended way to do this with mitmproxy now? Also, is there still some way to write the code in a GUI like pycharm and have debugger access?Moffat
I've expanded my answer, see above. :)Gramercy
K
0
    from mitmproxy.addons.proxyserver import Proxyserver
    from mitmproxy.options import Options
    from mitmproxy.tools.dump import DumpMaster
    options = Options(listen_host='127.0.0.1', listen_port=8080, http2=True)
    m = DumpMaster(options, with_termlog=True, with_dumper=False)
    m.server = Proxyserver()
    m.addons.add(
            // addons here
    )
    m.run()

Hi, I think that should do it

Kelvin answered 20/1, 2022 at 10:16 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Chavis
R
-1

This example below should work fine with mitmproxy v7

from mitmproxy.tools import main
from mitmproxy.tools.dump import DumpMaster

options = main.options.Options(listen_host='0.0.0.0', listen_port=8080)
m = DumpMaster(options=options)
# the rest is same in the previous versions
Reredos answered 14/1, 2022 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.