Search code examples
mitmproxy

mitmproxy script seems not running?


I am trying to run a simple mitmscript script by issuing ./mitmproxy --mode transparent -s pyscript.py.The proxy works fine and there's no error info in mitmproxy console,but it seems the script didn't even run,log.txt file is empty even though proxy successfully proxied client requests:

import mitmproxy.http

class Events:
    def response(self, f: mitmproxy.http.HTTPFlow):
        try:
            with open("/home/me/mitmproxy/log.txt", "a+") as log:
                log.write("rrr")
        except:
            with open("/home/me/mitmproxy/log.txt", "a+") as log:
                log.write("sss")

    def load(self, entry: mitmproxy.addonmanager.Loader):
        with open("/home/me/mitmproxy/log.txt", "a+") as log:
            log.write("xxx")


Solution

  • You have created an add-on class, but you forgot to create a new instance of the class and register it in mitmproxy.

    To do so you have to add the following entry at the end of your script:

    addons = [
        Events()
    ]
    

    See also sample Events script for mitmproxy: https://docs.mitmproxy.org/stable/addons-events/