From aa815a52469ce8e09f404c6f5e66af0f47c78829 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 29 Mar 2021 09:09:39 +0200 Subject: [PATCH] make connection_strategy=eager the default --- mitmproxy/addons/proxyserver.py | 7 +++++-- mitmproxy/proxy/layers/modes.py | 2 +- test/mitmproxy/proxy/layers/http/test_http.py | 2 ++ test/mitmproxy/proxy/layers/test_modes.py | 2 ++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mitmproxy/addons/proxyserver.py b/mitmproxy/addons/proxyserver.py index 549d05739..5fb2f8e15 100644 --- a/mitmproxy/addons/proxyserver.py +++ b/mitmproxy/addons/proxyserver.py @@ -83,8 +83,11 @@ class Proxyserver: def load(self, loader): loader.add_option( - "connection_strategy", str, "lazy", - "Determine when server connections should be established.", + "connection_strategy", str, "eager", + "Determine when server connections should be established. When set to lazy, mitmproxy " + "tries to defer establishing an upstream connection as long as possible. This makes it possible to " + "use server replay while being offline. When set to eager, mitmproxy can detect protocols with " + "server-side greetings, as well as accurately mirror TLS ALPN negotiation.", choices=("eager", "lazy") ) loader.add_option( diff --git a/mitmproxy/proxy/layers/modes.py b/mitmproxy/proxy/layers/modes.py index 501b14697..e099da27e 100644 --- a/mitmproxy/proxy/layers/modes.py +++ b/mitmproxy/proxy/layers/modes.py @@ -23,7 +23,7 @@ class DestinationKnown(layer.Layer, metaclass=ABCMeta): child_layer: layer.Layer def finish_start(self) -> layer.CommandGenerator[Optional[str]]: - if self.context.options.connection_strategy == "eager": + if self.context.options.connection_strategy == "eager" and self.context.server.address: err = yield commands.OpenConnection(self.context.server) if err: self._handle_event = self.done # type: ignore diff --git a/test/mitmproxy/proxy/layers/http/test_http.py b/test/mitmproxy/proxy/layers/http/test_http.py index 7a25ea079..ced153632 100644 --- a/test/mitmproxy/proxy/layers/http/test_http.py +++ b/test/mitmproxy/proxy/layers/http/test_http.py @@ -546,6 +546,7 @@ def test_http_proxy_tcp(tctx, mode, close_first): """Test TCP over HTTP CONNECT.""" server = Placeholder(Server) f = Placeholder(TCPFlow) + tctx.options.connection_strategy = "lazy" if mode == "upstream": tctx.options.mode = "upstream:http://proxy:8080" @@ -813,6 +814,7 @@ def test_http_server_aborts(tctx, stream): "response", "error"]) def test_kill_flow(tctx, when): """Test that we properly kill flows if instructed to do so""" + tctx.options.connection_strategy = "lazy" server = Placeholder(Server) connect_flow = Placeholder(HTTPFlow) flow = Placeholder(HTTPFlow) diff --git a/test/mitmproxy/proxy/layers/test_modes.py b/test/mitmproxy/proxy/layers/test_modes.py index 346590878..12b8aec29 100644 --- a/test/mitmproxy/proxy/layers/test_modes.py +++ b/test/mitmproxy/proxy/layers/test_modes.py @@ -119,6 +119,7 @@ def test_reverse_proxy(tctx, keep_host_header): """ server = Placeholder(Server) tctx.options.mode = "reverse:http://localhost:8000" + tctx.options.connection_strategy = "lazy" tctx.options.keep_host_header = keep_host_header assert ( Playbook(modes.ReverseProxy(tctx), hooks=False) @@ -321,6 +322,7 @@ def test_socks5_success(address: str, packed: bytes, tctx: Context): def test_socks5_trickle(tctx: Context): + tctx.options.connection_strategy = "lazy" playbook = Playbook(modes.Socks5Proxy(tctx)) for x in CLIENT_HELLO: playbook >> DataReceived(tctx.client, bytes([x]))