代理 mitmproxy Python启动时,动态配置 block_global 参数,使用笔记(三)

代理 mitmproxy Python启动时,动态配置 block_global 参数,使用笔记(三)

为什么要加 block_global=false?,若不加,则只能本地拦截,而移动设备,或非本机请求时则无法被拦截
将报错如下:Client connection from 192.167.6.166 killed by block_global option

注意:使用 Python的非命令行启动,之前的文章已经说过了,这里就不赘述,需要的可 前往这里查看,且本片代码以此篇文章中的方式三为例来说明

方案1.使用 master.options.update()(推荐)

此种方式其实不只是配置 block_global参数,其他参数也能配置,如listen_host,listen_port等,但需要注意的是,如果非本机设备连接,则listen_host要使用本机实际 IP,而不是127.0.0.1localhost

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@ File        : test_mitmproxy_block_global.py
@ Author      : yqbao
@ Version     : V1.0.0
@ Description :
"""
from mitmproxy.options import Options
from mitmproxy.tools.dump import DumpMaster


async def config_mitmproxy(listen_host='127.0.0.1', listen_port=8888):
    """配置 mitmproxy 参数与启动"""
    options = Options()  # 为啥不在这个 Options 中配置 block_global ? 因为里面没有这个参数。
    script = Counter()  # 插件
    addons = [script]

    master = DumpMaster(options)
    # 更新配置参数
    master.options.update(listen_host=listen_host, listen_port=listen_port, block_global=False)
    master.addons.add(*addons)
    try:
        await master.run()  # 启动 mitmproxy 主循环
    except KeyboardInterrupt:
        master.shutdown()  # 当手动中断时,关闭 master


方案2.替换默认的 block 插件

此方案实际是复制默认的 block 插件,然后唯一的改动就是将 block_global 默认值 从 True 改成了 False,同样需要注意,listen_host使用本机实际 IP

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@ File        : test_mitmproxy_block_global.py
@ Author      : yqbao
@ Version     : V1.0.0
@ Description :
"""
import ipaddress
import logging

from mitmproxy import ctx,http
from mitmproxy.options import Options
from mitmproxy.proxy import mode_specs
from mitmproxy.tools.dump import DumpMaster


class Block:
    def load(self, loader):
        loader.add_option(
            "block_global",
            bool,
            False,
            """
            Block connections from public IP addresses.
            """,
        )
        loader.add_option(
            "block_private",
            bool,
            False,
            """
            Block connections from local (private) IP addresses.
            This option does not affect loopback addresses (connections from the local machine),
            which are always permitted.
            """,
        )

    def client_connected(self, client):
        parts = client.peername[0].rsplit("%", 1)
        address = ipaddress.ip_address(parts[0])
        if isinstance(address, ipaddress.IPv6Address):
            address = address.ipv4_mapped or address

        if address.is_loopback or isinstance(client.proxy_mode, mode_specs.LocalMode):
            return

        if ctx.options.block_private and address.is_private:
            logging.warning(
                f"Client connection from {client.peername[0]} killed by block_private option."
            )
            client.error = "Connection killed by block_private."

        if ctx.options.block_global and address.is_global:
            logging.warning(
                f"Client connection from {client.peername[0]} killed by block_global option."
            )
            client.error = "Connection killed by block_global."


async def config_mitmproxy(listen_host='127.0.0.1', listen_port=8888):
    """配置 mitmproxy 参数与启动"""
    options = Options(listen_host=listen_host, listen_port=listen_port)
    script = Block()  # 插件
    script1 = Counter()  # 插件
    addons = [script, script1]

    master = DumpMaster(options)
    # 需要先查找默认的 block 插件,并将其删除后,才添加修改后的 block 插件
    block_addon = master.addons.get("block")
    master.addons.remove(block_addon)

    master.addons.add(*addons)
    try:
        await master.run()  # 启动 mitmproxy 主循环
    except KeyboardInterrupt:
        master.shutdown()  # 当手动中断时,关闭 master

mitmproxy 官方文档
本文章的原文地址
GitHub主页

posted @ 2024-12-17 21:32  星尘的博客  阅读(55)  评论(0编辑  收藏  举报