mitmproxy/examples/change_upstream_proxy.py

30 lines
1.1 KiB
Python
Raw Normal View History

2014-11-10 14:51:26 +00:00
# This scripts demonstrates how mitmproxy can switch to a second/different upstream proxy
# in upstream proxy mode.
#
2015-05-30 00:03:28 +00:00
# Usage: mitmdump -U http://default-upstream-proxy.local:8080/ -s
# "change_upstream_proxy.py host"
from libmproxy.protocol.http import send_connect_request
2014-09-08 14:02:31 +00:00
alternative_upstream_proxy = ("localhost", 8082)
2015-05-30 00:03:28 +00:00
def should_redirect(flow):
2014-09-08 14:02:31 +00:00
return flow.request.host == "example.com"
2014-09-08 14:02:31 +00:00
def request(context, flow):
if flow.live and should_redirect(flow):
# If you want to change the target server, you should modify flow.request.host and flow.request.port
# flow.live.change_server should only be used by inline scripts to change the upstream proxy,
# unless you are sure that you know what you are doing.
2015-05-30 00:03:28 +00:00
server_changed = flow.live.change_server(
alternative_upstream_proxy,
persistent_change=True)
2014-09-08 14:02:31 +00:00
if flow.request.scheme == "https" and server_changed:
2015-05-30 00:03:28 +00:00
send_connect_request(
flow.live.c.server_conn,
flow.request.host,
flow.request.port)
2014-09-08 14:02:31 +00:00
flow.live.c.establish_ssl(server=True)