mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
[sans-io] remove hook reply
This is not desired as hooks modify the passed object itself instead of returning a custom reply.
This commit is contained in:
parent
1fa2e59734
commit
c6262f9e9c
@ -40,7 +40,7 @@ class ProxyConnectionHandler(server.ConnectionHandler):
|
||||
self.event_queue.put((hook.name, hook.data))
|
||||
await q.get()
|
||||
if hook.blocking:
|
||||
self.server_event(events.HookReply(hook, None))
|
||||
self.server_event(events.HookReply(hook))
|
||||
|
||||
def _debug(self, *args):
|
||||
x = log.LogEntry(" ".join(str(x) for x in args), "warn")
|
||||
|
@ -89,7 +89,6 @@ class OpenConnectionReply(CommandReply):
|
||||
|
||||
class HookReply(CommandReply):
|
||||
command: typing.Union[commands.Hook, int]
|
||||
reply: typing.Any
|
||||
|
||||
def __init__(self, command: typing.Union[commands.Hook, int], reply: typing.Any):
|
||||
super().__init__(command, reply)
|
||||
def __init__(self, command: typing.Union[commands.Hook, int]):
|
||||
super().__init__(command, None)
|
||||
|
@ -130,7 +130,7 @@ class SimpleConnectionHandler(ConnectionHandler):
|
||||
|
||||
async def handle_hook(self, hook: commands.Hook) -> None:
|
||||
if hook.blocking:
|
||||
self.server_event(events.HookReply(hook, None))
|
||||
self.server_event(events.HookReply(hook))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -25,11 +25,11 @@ def test_open_connection_err(tctx):
|
||||
assert (
|
||||
tutils.playbook(tcp.TCPLayer(tctx))
|
||||
<< commands.Hook("tcp_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.OpenConnection(tctx.server)
|
||||
>> events.OpenConnectionReply(-1, "Connect call failed")
|
||||
<< commands.Hook("tcp_error", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.CloseConnection(tctx.client)
|
||||
)
|
||||
|
||||
@ -42,21 +42,21 @@ def test_simple(tctx):
|
||||
assert (
|
||||
playbook
|
||||
<< commands.Hook("tcp_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.OpenConnection(tctx.server)
|
||||
>> events.OpenConnectionReply(-1, None)
|
||||
>> events.DataReceived(tctx.client, b"hello!")
|
||||
<< commands.Hook("tcp_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.server, b"hello!")
|
||||
>> events.DataReceived(tctx.server, b"hi")
|
||||
<< commands.Hook("tcp_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.client, b"hi")
|
||||
>> events.ConnectionClosed(tctx.server)
|
||||
<< commands.CloseConnection(tctx.client)
|
||||
<< commands.Hook("tcp_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.ConnectionClosed(tctx.client)
|
||||
<< None
|
||||
)
|
||||
@ -72,19 +72,19 @@ def test_simple_explicit(tctx):
|
||||
tcp_start, = layer.handle_event(events.Start())
|
||||
flow = tcp_start.data
|
||||
assert tutils._eq(tcp_start, commands.Hook("tcp_start", flow))
|
||||
open_conn, = layer.handle_event(events.HookReply(tcp_start, None))
|
||||
open_conn, = layer.handle_event(events.HookReply(tcp_start))
|
||||
assert tutils._eq(open_conn, commands.OpenConnection(tctx.server))
|
||||
assert list(layer.handle_event(events.OpenConnectionReply(open_conn, None))) == []
|
||||
tcp_msg, = layer.handle_event(events.DataReceived(tctx.client, b"hello!"))
|
||||
assert tutils._eq(tcp_msg, commands.Hook("tcp_message", flow))
|
||||
assert flow.messages[0].content == b"hello!"
|
||||
|
||||
send, = layer.handle_event(events.HookReply(tcp_msg, None))
|
||||
send, = layer.handle_event(events.HookReply(tcp_msg))
|
||||
assert tutils._eq(send, commands.SendData(tctx.server, b"hello!"))
|
||||
close, tcp_end = layer.handle_event(events.ConnectionClosed(tctx.server))
|
||||
assert tutils._eq(close, commands.CloseConnection(tctx.client))
|
||||
assert tutils._eq(tcp_end, commands.Hook("tcp_end", flow))
|
||||
assert list(layer.handle_event(events.HookReply(tcp_end, None))) == []
|
||||
assert list(layer.handle_event(events.HookReply(tcp_end))) == []
|
||||
|
||||
|
||||
def test_receive_data_before_server_connected(tctx):
|
||||
@ -96,12 +96,12 @@ def test_receive_data_before_server_connected(tctx):
|
||||
assert (
|
||||
tutils.playbook(tcp.TCPLayer(tctx))
|
||||
<< commands.Hook("tcp_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.OpenConnection(tctx.server)
|
||||
>> events.DataReceived(tctx.client, b"hello!")
|
||||
>> events.OpenConnectionReply(-2, None)
|
||||
<< commands.Hook("tcp_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.server, b"hello!")
|
||||
)
|
||||
assert f().messages
|
||||
@ -115,13 +115,13 @@ def test_receive_data_after_server_disconnected(tctx):
|
||||
assert (
|
||||
tutils.playbook(tcp.TCPLayer(tctx))
|
||||
<< commands.Hook("tcp_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.OpenConnection(tctx.server)
|
||||
>> events.OpenConnectionReply(-1, None)
|
||||
>> events.ConnectionClosed(tctx.server)
|
||||
<< commands.CloseConnection(tctx.client)
|
||||
<< commands.Hook("tcp_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.client, b"i'm late")
|
||||
<< None
|
||||
)
|
||||
|
@ -119,7 +119,7 @@ def test_client_tls(tctx: context.Context):
|
||||
data = tutils.Placeholder()
|
||||
assert (
|
||||
playbook
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.Log("PlainSendData(client, b'hello world')")
|
||||
<< commands.SendData(tctx.client, data)
|
||||
)
|
||||
|
@ -28,20 +28,20 @@ def test_simple(tctx, ws_playbook):
|
||||
assert (
|
||||
ws_playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.client, b"\x82\x85\x10\x11\x12\x13Xt~\x7f\x7f") # Frame with payload b"Hello"
|
||||
<< commands.Hook("websocket_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.server, b"\x82\x85\x10\x11\x12\x13Xt~\x7f\x7f")
|
||||
>> events.DataReceived(tctx.server, b'\x81\x05Hello') # Frame with payload "Hello"
|
||||
<< commands.Hook("websocket_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.client, b'\x81\x05Hello')
|
||||
>> events.DataReceived(tctx.client, b'\x88\x82\x10\x11\x12\x13\x13\xf9') # Closing frame
|
||||
<< commands.SendData(tctx.server, b'\x88\x82\x10\x11\x12\x13\x13\xf9')
|
||||
<< commands.SendData(tctx.client, b'\x88\x02\x03\xe8')
|
||||
<< commands.Hook("websocket_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.server, b'\x81\x05Hello')
|
||||
<< None
|
||||
)
|
||||
@ -55,12 +55,12 @@ def test_server_close(tctx, ws_playbook):
|
||||
assert (
|
||||
ws_playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.server, b'\x88\x02\x03\xe8')
|
||||
<< commands.SendData(tctx.client, b'\x88\x02\x03\xe8')
|
||||
<< commands.SendData(tctx.server, b'\x88\x82\x10\x11\x12\x13\x13\xf9')
|
||||
<< commands.Hook("websocket_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.CloseConnection(tctx.client)
|
||||
)
|
||||
|
||||
@ -71,7 +71,7 @@ def test_ping_pong(tctx, ws_playbook):
|
||||
assert (
|
||||
ws_playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.client, b'\x89\x80\x10\x11\x12\x13') # Ping
|
||||
<< commands.Log("info", "Websocket PING received ")
|
||||
<< commands.SendData(tctx.server, b'\x89\x80\x10\x11\x12\x13')
|
||||
@ -86,14 +86,14 @@ def test_connection_failed(tctx, ws_playbook):
|
||||
assert (
|
||||
ws_playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.client, b"Not a valid frame")
|
||||
<< commands.SendData(tctx.server, b'\x88\x94\x10\x11\x12\x13\x13\xfb[}fp~zt1}cs~vv0!jv')
|
||||
<< commands.SendData(tctx.client, b'\x88\x14\x03\xeaInvalid opcode 0xe')
|
||||
<< commands.Hook("websocket_error", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.Hook("websocket_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
)
|
||||
|
||||
|
||||
@ -109,14 +109,14 @@ def test_extension(tctx):
|
||||
assert (
|
||||
playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.DataReceived(tctx.client, b'\xc1\x87\x10\x11\x12\x13\xe2Y\xdf\xda\xd9\x16\x12') # Compressed Frame
|
||||
<< commands.Hook("websocket_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.server, b'\xc1\x87\x10\x11\x12\x13\xe2Y\xdf\xda\xd9\x16\x12')
|
||||
>> events.DataReceived(tctx.server, b'\xc1\x07\xf2H\xcd\xc9\xc9\x07\x00') # Compressed Frame
|
||||
<< commands.Hook("websocket_message", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.SendData(tctx.client, b'\xc1\x07\xf2H\xcd\xc9\xc9\x07\x00')
|
||||
)
|
||||
assert len(f().messages) == 2
|
||||
@ -129,14 +129,14 @@ def test_connection_closed(tctx, ws_playbook):
|
||||
assert (
|
||||
ws_playbook
|
||||
<< commands.Hook("websocket_start", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
>> events.ConnectionClosed(tctx.server)
|
||||
<< commands.Log("error", "Connection closed abnormally")
|
||||
<< commands.CloseConnection(tctx.client)
|
||||
<< commands.Hook("websocket_error", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
<< commands.Hook("websocket_end", f)
|
||||
>> events.HookReply(-1, None)
|
||||
>> events.HookReply(-1)
|
||||
)
|
||||
|
||||
assert f().error
|
||||
|
Loading…
Reference in New Issue
Block a user