[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:
Maximilian Hils 2017-08-16 00:12:20 +02:00
parent 1fa2e59734
commit c6262f9e9c
6 changed files with 34 additions and 35 deletions

View File

@ -40,7 +40,7 @@ class ProxyConnectionHandler(server.ConnectionHandler):
self.event_queue.put((hook.name, hook.data)) self.event_queue.put((hook.name, hook.data))
await q.get() await q.get()
if hook.blocking: if hook.blocking:
self.server_event(events.HookReply(hook, None)) self.server_event(events.HookReply(hook))
def _debug(self, *args): def _debug(self, *args):
x = log.LogEntry(" ".join(str(x) for x in args), "warn") x = log.LogEntry(" ".join(str(x) for x in args), "warn")

View File

@ -89,7 +89,6 @@ class OpenConnectionReply(CommandReply):
class HookReply(CommandReply): class HookReply(CommandReply):
command: typing.Union[commands.Hook, int] command: typing.Union[commands.Hook, int]
reply: typing.Any
def __init__(self, command: typing.Union[commands.Hook, int], reply: typing.Any): def __init__(self, command: typing.Union[commands.Hook, int]):
super().__init__(command, reply) super().__init__(command, None)

View File

@ -130,7 +130,7 @@ class SimpleConnectionHandler(ConnectionHandler):
async def handle_hook(self, hook: commands.Hook) -> None: async def handle_hook(self, hook: commands.Hook) -> None:
if hook.blocking: if hook.blocking:
self.server_event(events.HookReply(hook, None)) self.server_event(events.HookReply(hook))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -25,11 +25,11 @@ def test_open_connection_err(tctx):
assert ( assert (
tutils.playbook(tcp.TCPLayer(tctx)) tutils.playbook(tcp.TCPLayer(tctx))
<< commands.Hook("tcp_start", f) << commands.Hook("tcp_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.OpenConnection(tctx.server) << commands.OpenConnection(tctx.server)
>> events.OpenConnectionReply(-1, "Connect call failed") >> events.OpenConnectionReply(-1, "Connect call failed")
<< commands.Hook("tcp_error", f) << commands.Hook("tcp_error", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.CloseConnection(tctx.client) << commands.CloseConnection(tctx.client)
) )
@ -42,21 +42,21 @@ def test_simple(tctx):
assert ( assert (
playbook playbook
<< commands.Hook("tcp_start", f) << commands.Hook("tcp_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.OpenConnection(tctx.server) << commands.OpenConnection(tctx.server)
>> events.OpenConnectionReply(-1, None) >> events.OpenConnectionReply(-1, None)
>> events.DataReceived(tctx.client, b"hello!") >> events.DataReceived(tctx.client, b"hello!")
<< commands.Hook("tcp_message", f) << commands.Hook("tcp_message", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.SendData(tctx.server, b"hello!") << commands.SendData(tctx.server, b"hello!")
>> events.DataReceived(tctx.server, b"hi") >> events.DataReceived(tctx.server, b"hi")
<< commands.Hook("tcp_message", f) << commands.Hook("tcp_message", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.SendData(tctx.client, b"hi") << commands.SendData(tctx.client, b"hi")
>> events.ConnectionClosed(tctx.server) >> events.ConnectionClosed(tctx.server)
<< commands.CloseConnection(tctx.client) << commands.CloseConnection(tctx.client)
<< commands.Hook("tcp_end", f) << commands.Hook("tcp_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.ConnectionClosed(tctx.client) >> events.ConnectionClosed(tctx.client)
<< None << None
) )
@ -72,19 +72,19 @@ def test_simple_explicit(tctx):
tcp_start, = layer.handle_event(events.Start()) tcp_start, = layer.handle_event(events.Start())
flow = tcp_start.data flow = tcp_start.data
assert tutils._eq(tcp_start, commands.Hook("tcp_start", flow)) 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 tutils._eq(open_conn, commands.OpenConnection(tctx.server))
assert list(layer.handle_event(events.OpenConnectionReply(open_conn, None))) == [] assert list(layer.handle_event(events.OpenConnectionReply(open_conn, None))) == []
tcp_msg, = layer.handle_event(events.DataReceived(tctx.client, b"hello!")) tcp_msg, = layer.handle_event(events.DataReceived(tctx.client, b"hello!"))
assert tutils._eq(tcp_msg, commands.Hook("tcp_message", flow)) assert tutils._eq(tcp_msg, commands.Hook("tcp_message", flow))
assert flow.messages[0].content == b"hello!" 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!")) assert tutils._eq(send, commands.SendData(tctx.server, b"hello!"))
close, tcp_end = layer.handle_event(events.ConnectionClosed(tctx.server)) close, tcp_end = layer.handle_event(events.ConnectionClosed(tctx.server))
assert tutils._eq(close, commands.CloseConnection(tctx.client)) assert tutils._eq(close, commands.CloseConnection(tctx.client))
assert tutils._eq(tcp_end, commands.Hook("tcp_end", flow)) 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): def test_receive_data_before_server_connected(tctx):
@ -96,12 +96,12 @@ def test_receive_data_before_server_connected(tctx):
assert ( assert (
tutils.playbook(tcp.TCPLayer(tctx)) tutils.playbook(tcp.TCPLayer(tctx))
<< commands.Hook("tcp_start", f) << commands.Hook("tcp_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.OpenConnection(tctx.server) << commands.OpenConnection(tctx.server)
>> events.DataReceived(tctx.client, b"hello!") >> events.DataReceived(tctx.client, b"hello!")
>> events.OpenConnectionReply(-2, None) >> events.OpenConnectionReply(-2, None)
<< commands.Hook("tcp_message", f) << commands.Hook("tcp_message", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.SendData(tctx.server, b"hello!") << commands.SendData(tctx.server, b"hello!")
) )
assert f().messages assert f().messages
@ -115,13 +115,13 @@ def test_receive_data_after_server_disconnected(tctx):
assert ( assert (
tutils.playbook(tcp.TCPLayer(tctx)) tutils.playbook(tcp.TCPLayer(tctx))
<< commands.Hook("tcp_start", f) << commands.Hook("tcp_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.OpenConnection(tctx.server) << commands.OpenConnection(tctx.server)
>> events.OpenConnectionReply(-1, None) >> events.OpenConnectionReply(-1, None)
>> events.ConnectionClosed(tctx.server) >> events.ConnectionClosed(tctx.server)
<< commands.CloseConnection(tctx.client) << commands.CloseConnection(tctx.client)
<< commands.Hook("tcp_end", f) << commands.Hook("tcp_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.DataReceived(tctx.client, b"i'm late") >> events.DataReceived(tctx.client, b"i'm late")
<< None << None
) )

View File

@ -119,7 +119,7 @@ def test_client_tls(tctx: context.Context):
data = tutils.Placeholder() data = tutils.Placeholder()
assert ( assert (
playbook playbook
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.Log("PlainSendData(client, b'hello world')") << commands.Log("PlainSendData(client, b'hello world')")
<< commands.SendData(tctx.client, data) << commands.SendData(tctx.client, data)
) )

View File

@ -28,20 +28,20 @@ def test_simple(tctx, ws_playbook):
assert ( assert (
ws_playbook ws_playbook
<< commands.Hook("websocket_start", f) << 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" >> events.DataReceived(tctx.client, b"\x82\x85\x10\x11\x12\x13Xt~\x7f\x7f") # Frame with payload b"Hello"
<< commands.Hook("websocket_message", f) << 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") << commands.SendData(tctx.server, b"\x82\x85\x10\x11\x12\x13Xt~\x7f\x7f")
>> events.DataReceived(tctx.server, b'\x81\x05Hello') # Frame with payload "Hello" >> events.DataReceived(tctx.server, b'\x81\x05Hello') # Frame with payload "Hello"
<< commands.Hook("websocket_message", f) << commands.Hook("websocket_message", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.SendData(tctx.client, b'\x81\x05Hello') << commands.SendData(tctx.client, b'\x81\x05Hello')
>> events.DataReceived(tctx.client, b'\x88\x82\x10\x11\x12\x13\x13\xf9') # Closing frame >> 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.server, b'\x88\x82\x10\x11\x12\x13\x13\xf9')
<< commands.SendData(tctx.client, b'\x88\x02\x03\xe8') << commands.SendData(tctx.client, b'\x88\x02\x03\xe8')
<< commands.Hook("websocket_end", f) << commands.Hook("websocket_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.DataReceived(tctx.server, b'\x81\x05Hello') >> events.DataReceived(tctx.server, b'\x81\x05Hello')
<< None << None
) )
@ -55,12 +55,12 @@ def test_server_close(tctx, ws_playbook):
assert ( assert (
ws_playbook ws_playbook
<< commands.Hook("websocket_start", f) << commands.Hook("websocket_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.DataReceived(tctx.server, b'\x88\x02\x03\xe8') >> events.DataReceived(tctx.server, b'\x88\x02\x03\xe8')
<< commands.SendData(tctx.client, 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.SendData(tctx.server, b'\x88\x82\x10\x11\x12\x13\x13\xf9')
<< commands.Hook("websocket_end", f) << commands.Hook("websocket_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.CloseConnection(tctx.client) << commands.CloseConnection(tctx.client)
) )
@ -71,7 +71,7 @@ def test_ping_pong(tctx, ws_playbook):
assert ( assert (
ws_playbook ws_playbook
<< commands.Hook("websocket_start", f) << commands.Hook("websocket_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.DataReceived(tctx.client, b'\x89\x80\x10\x11\x12\x13') # Ping >> events.DataReceived(tctx.client, b'\x89\x80\x10\x11\x12\x13') # Ping
<< commands.Log("info", "Websocket PING received ") << commands.Log("info", "Websocket PING received ")
<< commands.SendData(tctx.server, b'\x89\x80\x10\x11\x12\x13') << commands.SendData(tctx.server, b'\x89\x80\x10\x11\x12\x13')
@ -86,14 +86,14 @@ def test_connection_failed(tctx, ws_playbook):
assert ( assert (
ws_playbook ws_playbook
<< commands.Hook("websocket_start", f) << commands.Hook("websocket_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.DataReceived(tctx.client, b"Not a valid frame") >> 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.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.SendData(tctx.client, b'\x88\x14\x03\xeaInvalid opcode 0xe')
<< commands.Hook("websocket_error", f) << commands.Hook("websocket_error", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.Hook("websocket_end", f) << commands.Hook("websocket_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
) )
@ -109,14 +109,14 @@ def test_extension(tctx):
assert ( assert (
playbook playbook
<< commands.Hook("websocket_start", f) << 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 >> events.DataReceived(tctx.client, b'\xc1\x87\x10\x11\x12\x13\xe2Y\xdf\xda\xd9\x16\x12') # Compressed Frame
<< commands.Hook("websocket_message", f) << 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') << 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 >> events.DataReceived(tctx.server, b'\xc1\x07\xf2H\xcd\xc9\xc9\x07\x00') # Compressed Frame
<< commands.Hook("websocket_message", f) << 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') << commands.SendData(tctx.client, b'\xc1\x07\xf2H\xcd\xc9\xc9\x07\x00')
) )
assert len(f().messages) == 2 assert len(f().messages) == 2
@ -129,14 +129,14 @@ def test_connection_closed(tctx, ws_playbook):
assert ( assert (
ws_playbook ws_playbook
<< commands.Hook("websocket_start", f) << commands.Hook("websocket_start", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
>> events.ConnectionClosed(tctx.server) >> events.ConnectionClosed(tctx.server)
<< commands.Log("error", "Connection closed abnormally") << commands.Log("error", "Connection closed abnormally")
<< commands.CloseConnection(tctx.client) << commands.CloseConnection(tctx.client)
<< commands.Hook("websocket_error", f) << commands.Hook("websocket_error", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
<< commands.Hook("websocket_end", f) << commands.Hook("websocket_end", f)
>> events.HookReply(-1, None) >> events.HookReply(-1)
) )
assert f().error assert f().error