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_transparent_eager_connect_failure(tctx: Context, monkeypatch): """Test that we recover from a transparent mode resolve error.""" tctx.options.connection_strategy = "eager" monkeypatch.setattr(platform, "original_addr", lambda sock: ("address", 22)) assert (Playbook(modes.TransparentProxy(tctx), logs=True) << GetSocket( tctx.client) >> reply(object()) << OpenConnection(tctx.server) >> reply("something something") << CloseConnection( tctx.client) >> ConnectionClosed(tctx.client))
def test_transparent_failure(tctx: Context, monkeypatch): """Test that we recover from a transparent mode resolve error.""" def raise_err(sock): raise RuntimeError("platform-specific error") monkeypatch.setattr(platform, "original_addr", raise_err) assert (Playbook(modes.TransparentProxy(tctx), logs=True) << GetSocket( tctx.client ) >> reply(object()) << Log( "Transparent mode failure: RuntimeError('platform-specific error')", "info"))