修改 request.url 而不更改 request.host

Modifying request.url without changing request.host

我正在使用带有 python 的 mitmproxy 作为 http 代理

我 运行 使用以下命令的代理:

mitmdump -s proxy.py -U http://upstreamproxy

proxy.py 如下所示:

#!/usr/bin/mitmdump
from __future__ import print_function
import pprint
import datetime
import os
import re


pp = pprint.PrettyPrinter(indent=4)

def request(context, flow):
    print("DEBUG")
    oldhost = flow.request.host
    flow.request.url = re.sub(r"www.verycd.com",r"115.182.66.26",flow.request.url)
    # flow.request.host = oldhost   #<---This will modify the url also
    print("DEBUG")

我希望将 www.verycd.com 更改为 url 中的 IP,但保持主机字段仍使用 www.verycd.com,如下所示:

GET http://115.182.66.26/ HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh,en-US;q=0.5
Host: www.verycd.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0

但 mitmproxy 总是同时更改 url 和主机 我得到以下服务器不接受

GET http://115.182.66.26/ HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh,en-US;q=0.5
Host: 115.182.66.26
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0

项目所有者已回答

https://github.com/mitmproxy/mitmproxy/issues/890