mitmproxy代码方式忽略特定主机报错问题求助
问题描述
用mitmproxy创建代理拦截请求,代理本身可正常运行,但配置ignore_hosts忽略特定主机时,持续抛出re.error: nothing to repeat at position 0错误。
现有代码
import asyncio from mitmproxy import options from mitmproxy.tools import dump class RequestLogger: def request(self, flow): print(flow.request.host) ig = ['localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12' , '*.localdomain.com' ] async def start_proxy(host,port): opts = options.Options() opts.update(listen_host=host, listen_port=port, ignore_hosts=ig) master = dump.DumpMaster(opts,with_termlog=True,with_dumper=True) master.addons.add(RequestLogger()) await master.run() return master if __name__ == '__main__': asyncio.run(start_proxy("0.0.0.0",8080))
报错信息
[21:12:58.880] Addon error: nothing to repeat at position 0 Traceback (most recent call last): File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 111, in next_layer nextlayer.layer = self._next_layer( ^^^^^^^^^^^^^^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 133, in _next_layer if self._ignore_connection(context, data_client, data_server): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 244, in _ignore_connection return any( ^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 245, in <genexpr> re.search(rex, host, re.IGNORECASE) File "C:\Python\Lib\re\__init__.py", line 176, in search return _compile(pattern, flags).search(string) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\__init__.py", line 294, in _compile p = _compiler.compile(pattern, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_compiler.py", line 745, in compile p = _parser.parse(p, flags) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 989, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 464, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 691, in _parse raise source.error("nothing to repeat", re.error: nothing to repeat at position 0 [21:12:59.006] Addon error: nothing to repeat at position 0 Traceback (most recent call last): File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 111, in next_layer nextlayer.layer = self._next_layer( ^^^^^^^^^^^^^^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 133, in _next_layer if self._ignore_connection(context, data_client, data_server): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 244, in _ignore_connection return any( ^^^^ File "c:\Users\PhenomAmd\AppData\Local\pypoetry\Cache\virtualenvs\proxylsf-Ra9sO3Vd-py3.11\Lib\site-packages\mitmproxy\addons\next_layer.py", line 245, in <genexpr> re.search(rex, host, re.IGNORECASE) File "C:\Python\Lib\re\__init__.py", line 176, in search return _compile(pattern, flags).search(string) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\__init__.py", line 294, in _compile p = _compiler.compile(pattern, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_compiler.py", line 745, in compile p = _parser.parse(p, flags) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 989, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 464, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python\Lib\re\_parser.py", line 691, in _parse raise source.error("nothing to repeat", re.error: nothing to repeat at position 0
也曾尝试以下写法,但问题未解决:
ignore=[] hosts = ['host1','host2','host3'] for host in hosts: ignore.append(host) opts.update(listen_host=host, listen_port=port, ignore_hosts=ignore)
解决方案
错误根源是ignore_hosts中的*.localdomain.com被mitmproxy当作正则表达式解析,而*在正则中是特殊字符(表示重复前一个元素0或多次),但它前面没有可重复的内容,导致正则编译失败。
有两种修正方法:
- 直接改用合法正则:将
*.localdomain.com替换为.*\.localdomain\.com,其中.*匹配任意前缀,\.转义.避免其被当作正则通配符。 - 用
fnmatch.translate()转换shell通配符:该方法可将*.xxx.com这类shell风格通配符自动转换成合法正则表达式,无需手动处理特殊字符。
修正后的完整代码示例:
import asyncio import fnmatch from mitmproxy import options from mitmproxy.tools import dump class RequestLogger: def request(self, flow): print(flow.request.host) ig = [ 'localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', fnmatch.translate('*.localdomain.com') ] async def start_proxy(host,port): opts = options.Options() opts.update(listen_host=host, listen_port=port, ignore_hosts=ig) master = dump.DumpMaster(opts,with_termlog=True,with_dumper=True) master.addons.add(RequestLogger()) await master.run() return master if __name__ == '__main__': asyncio.run(start_proxy("0.0.0.0",8080))
注:mitmproxy的ignore_hosts支持CIDR格式(如你使用的IP段)、纯字符串匹配(如host1),这些格式无需额外处理。
内容的提问来源于stack exchange,提问作者Minioua
相关产品推荐
相关产品推荐

