def test_transparent_tcp(tctx: Context, monkeypatch, connection_strategy): monkeypatch.setattr(platform, "original_addr", lambda sock: ("address", 22)) flow = Placeholder(TCPFlow) tctx.options.connection_strategy = connection_strategy sock = object() playbook = Playbook(modes.TransparentProxy(tctx)) ( playbook << GetSocket(tctx.client) >> reply(sock) ) if connection_strategy == "lazy": assert playbook else: assert ( playbook << OpenConnection(tctx.server) >> reply(None) >> DataReceived(tctx.server, b"hello") << NextLayerHook(Placeholder(NextLayer)) >> reply_next_layer(tcp.TCPLayer) << TcpStartHook(flow) >> reply() << TcpMessageHook(flow) >> reply() << SendData(tctx.client, b"hello") ) assert flow().messages[0].content == b"hello" assert not flow().messages[0].from_client assert tctx.server.address == ("address", 22)
def test_reverse_proxy_tcp_over_tls(tctx: Context, monkeypatch, patch, connection_strategy): """ Test client --TCP-- mitmproxy --TCP over TLS-- server reverse proxying. """ if patch: monkeypatch.setattr(tls, "ServerTLSLayer", tls.MockTLSLayer) flow = Placeholder(TCPFlow) data = Placeholder(bytes) tctx.options.mode = "reverse:https://localhost:8000" tctx.options.connection_strategy = connection_strategy playbook = Playbook(modes.ReverseProxy(tctx)) if connection_strategy == "eager": (playbook << OpenConnection(tctx.server) >> DataReceived( tctx.client, b"\x01\x02\x03") >> reply( None, to=OpenConnection(tctx.server))) else: (playbook >> DataReceived(tctx.client, b"\x01\x02\x03")) if patch: (playbook << NextLayerHook(Placeholder(NextLayer)) >> reply_next_layer( tcp.TCPLayer) << TcpStartHook(flow) >> reply()) if connection_strategy == "lazy": (playbook << OpenConnection(tctx.server) >> reply(None)) assert (playbook << TcpMessageHook(flow) >> reply() << SendData( tctx.server, data)) assert data() == b"\x01\x02\x03" else: if connection_strategy == "lazy": (playbook << NextLayerHook(Placeholder(NextLayer)) >> reply_next_layer(tcp.TCPLayer) << TcpStartHook(flow) >> reply() << OpenConnection(tctx.server) >> reply(None)) assert (playbook << TlsStartHook(Placeholder()) >> reply_tls_start() << SendData(tctx.server, data)) assert tls.parse_client_hello(data()).sni == "localhost"