本文档使用 docker image mitmproxy/mitmproxy:9.0.1 进行演示
根据 mitmproxy 的文档,我们可以编写自定义的 Addon 进行 http 的日志采集和外发,详见 这里
docker run --rm -it \
-v /data/mitmproxy-test/:/home/mitmproxy/.mitmproxy \ # 将 mitmproxy 的默认工作路径映射到宿主机固定目录, 这样子可以确保始终使用同一个 ca 证书
-v /data/mitmproxy-test/log/:/data/log/ \ # 可选, 挂载一个目录到容器, 以便将测试的 http log 持久化保存
-v /data/mitmproxy-test/http-log-addon.py:/data/http-log-addon.py \ # 将宿主机上自定义的 py 脚本挂载到容器指定路径
-p 8181:8080 \ # 将容器内的 8080 映射到宿主机的 8181
mitmproxy/mitmproxy:9.0.1 \ # 本例中使用的镜像名称
mitmproxy \ # 运行自定义的程序, 下边都是要传给它的参数
-s /data/http-log-addon.py \ # 自定义的 py 脚本路径, 注意上边通过 -v 挂载到容器内
--mode socks5 \ # 使用 socks5 协议而非默认的 http 协议
--ssl-insecure \ # 忽略目标服务器的证书错误
--listen-host 0.0.0.0 --listen-port 8080
首先配置自己的浏览器(或者其他软件)使用 socks5 协议(以上方启动命令为准)的代理。
比如设置为: socks5://1.2.3.4:8181
然后,在浏览器中访问 http://mitm.it, 即可看到证书配置引导页面, 大致如下:
然后根据页面引导进行证书配置即可, 详细的方法还可参考 mitmproxy 的文档
此时, 我们可以使用 curl 进行测试, 命令如下:
curl -v https://www.baidu.com/test-404.html -x 'socks5://1.2.3.4:8181'
采集到的请求响应日志见这里