如何使用mitmproxy将HTTPS流量重定向到本地HTTP服务器?

时间:2019-08-23 13:16:06

标签: networking mitmproxy

我正在尝试设置mitmproxy,以便可以从浏览器向https://{my-domain}发出请求,并使其返回运行在http://localhost:3000的本地服务器的响应,但是我无法获得https请求访问我的本地服务器。我看到了mitmproxy的调试语句。另外,我可以使它适用于http流量,但不适用于https。

我阅读了mitmproxy addon docsapi docs 我已经安装了证书,并且可以通过代理监视https。

我正在使用Mitmproxy:4.0.4和Python:3.7.4

这是我的插件( local-redirect.py ),以及我如何运行mitmproxy:

from mitmproxy import ctx
import mitmproxy.http

class LocalRedirect:

  def __init__(self):
    print('Loaded redirect addon')

  def request(self, flow: mitmproxy.http.HTTPFlow):
    if 'my-actual-domain-here' in flow.request.pretty_host:
      ctx.log.info("pretty host is: %s" % flow.request.pretty_host)
      flow.request.host = "localhost"
      flow.request.port = 3000
      flow.request.scheme = 'http'

addons = [
  LocalRedirect()
]
$ mitmdump -s local-redirect.py | grep pretty

当我从服务器访问url时,看到日志记录语句,但是浏览器挂起了该请求,并且没有对本地服务器进行任何请求。

1 个答案:

答案 0 :(得分:0)

上面的插件很好,但是我的本地服务器不支持HTTP2。

使用isatty选项是一种快速解决方案:

--no-http2

mitmproxy -s local-redirect.py --no-http2 --view-filter localhost