mitmproxy插件和纯python启动方式。

1,517 阅读1分钟

mitmproxy官网:

python api文档

1.mitmproxy一般以插件的形式启动 mitmproxy -s 脚本.py


"""
Basic skeleton of a mitmproxy addon.

Run as follows: mitmproxy -s anatomy.py
"""
from mitmproxy import ctx


class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow):
        self.num = self.num + 1
        ctx.log.info("We've seen %d flows" % self.num)


addons = [
    Counter()
]

2.纯python启动

from mitmproxy import http, ctx
from mitmproxy import proxy, options
from mitmproxy.script import concurrent
from mitmproxy.tools.dump import DumpMaster
from mitmproxy import command
import typing
from mitmproxy import flow
from loguru import logger

class AddHeader:

    @concurrent
    def request(self, flow):
        print(flow.request.pretty_host)
        logger.info(flow.request.url)

    @concurrent
    def response(self, flow: http.HTTPFlow) -> None:
        print(flow.request.url)
        logger.info(flow.request.headers)
        page_buf = flow.response.text
# 劫持修改内容  采集数据
# flow.response.text = '注入中'+page_buf


def start():
    my_add = AddHeader()
    opts = options.Options(
        listen_host='127.0.0.1',
        listen_port=7777,  # 提供的代理
        # mode='upstream:http://127.0.0.1:8888'  # 上游代理模式
    )
    m = DumpMaster(opts)
    m.addons.add(my_add)

    try:
        m.run()
    except KeyboardInterrupt:
        m.shutdown()


if __name__ == '__main__':
    start()

3.继承到pyqt5程序中开启关闭代理,将在后边发布。