diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-02-09 16:39:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-09 16:39:20 +0100 |
commit | 1084588103e11ad2a7555df1070f96fb5dc77a45 (patch) | |
tree | 71894e4e710fb5bd4c445c353452ba255345694f | |
parent | 7a205e80aa94600291f87c96e51f13abe9fb4703 (diff) | |
parent | 9b97b6389127e6166fa0f1ad08951ccdb4a36290 (diff) | |
download | mitmproxy-1084588103e11ad2a7555df1070f96fb5dc77a45.tar.gz mitmproxy-1084588103e11ad2a7555df1070f96fb5dc77a45.tar.bz2 mitmproxy-1084588103e11ad2a7555df1070f96fb5dc77a45.zip |
Merge pull request #1998 from mhils/fix-change-upstream
Fix change_upstream_proxy_server
-rw-r--r-- | mitmproxy/proxy/protocol/http.py | 12 | ||||
-rw-r--r-- | test/mitmproxy/test_server.py | 23 |
2 files changed, 33 insertions, 2 deletions
diff --git a/mitmproxy/proxy/protocol/http.py b/mitmproxy/proxy/protocol/http.py index 59de070f..b70afe33 100644 --- a/mitmproxy/proxy/protocol/http.py +++ b/mitmproxy/proxy/protocol/http.py @@ -88,6 +88,10 @@ class UpstreamConnectLayer(base.Layer): layer() def _send_connect_request(self): + self.log("Sending CONNECT request", "debug", [ + "Proxy Server: {}".format(self.ctx.server_conn.address), + "Connect to: {}:{}".format(self.connect_request.host, self.connect_request.port) + ]) self.send_request(self.connect_request) resp = self.read_response(self.connect_request) if resp.status_code != 200: @@ -101,6 +105,7 @@ class UpstreamConnectLayer(base.Layer): pass # swallow the message def change_upstream_proxy_server(self, address): + self.log("Changing upstream proxy to {} (CONNECTed)".format(repr(address)), "debug") if address != self.server_conn.via.address: self.ctx.set_server(address) @@ -432,10 +437,13 @@ class HttpLayer(base.Layer): except (exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): self.log("Failed to send error response to client: {}".format(message), "debug") - def change_upstream_proxy_server(self, address) -> None: + def change_upstream_proxy_server(self, address): # Make set_upstream_proxy_server always available, # even if there's no UpstreamConnectLayer - if address != self.server_conn.address: + if hasattr(self.ctx, "change_upstream_proxy_server"): + self.ctx.change_upstream_proxy_server(address) + elif address != self.server_conn.address: + self.log("Changing upstream proxy to {} (not CONNECTed)".format(repr(address)), "debug") self.set_server(address) def establish_server_connection(self, host: str, port: int, scheme: str): diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 6e7ca275..af121534 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -995,6 +995,29 @@ class TestUpstreamProxySSL( assert not self.chain[1].tmaster.state.flows[0].server_conn.via assert self.chain[1].tmaster.state.flow_count() == 1 + def test_change_upstream_proxy_connect(self): + # skip chain[0]. + self.proxy.tmaster.addons.add( + UpstreamProxyChanger( + ("127.0.0.1", self.chain[1].port) + ) + ) + p = self.pathoc() + with p.connect(): + req = p.request("get:'/p/418'") + + assert req.status_code == 418 + assert self.chain[0].tmaster.state.flow_count() == 0 + assert self.chain[1].tmaster.state.flow_count() == 1 + + +class UpstreamProxyChanger: + def __init__(self, addr): + self.address = addr + + def request(self, f): + f.live.change_upstream_proxy_server(self.address) + class RequestKiller: def __init__(self, exclude): |