events are past tense

This commit is contained in:
Maximilian Hils 2017-06-15 21:12:41 +02:00
parent cd9049ee91
commit 3b355d783f
7 changed files with 51 additions and 36 deletions

View File

@ -36,14 +36,14 @@ class ConnectionEvent(Event):
self.connection = connection
class CloseConnection(ConnectionEvent):
class ConnectionClosed(ConnectionEvent):
"""
Remote has closed a connection.
"""
pass
class ReceiveData(ConnectionEvent):
class DataReceived(ConnectionEvent):
"""
Remote has sent some data.
"""
@ -53,7 +53,7 @@ class ReceiveData(ConnectionEvent):
self.data = data
class ReceiveClientData(ReceiveData):
class ClientDataReceived(DataReceived):
"""
Client has sent data.
These subclasses simplify code for simple layers with one server and one client.
@ -61,7 +61,7 @@ class ReceiveClientData(ReceiveData):
pass
class ReceiveServerData(ReceiveData):
class ServerDataReceived(DataReceived):
pass
@ -84,7 +84,16 @@ class CommandReply(Event):
class OpenConnectionReply(CommandReply):
reply: bool
command: commands.OpenConnection
reply: str
def __init__(self, command: commands.Command, ok: bool):
def __init__(self, command: commands.OpenConnection, ok: str):
super().__init__(command, ok)
class HookReply(CommandReply):
command: commands.Hook
reply: typing.Any
def __init__(self, command: commands.Hook, reply: typing.Any):
super().__init__(command, reply)

View File

@ -46,12 +46,12 @@ class ConnectionHandler:
def read(self, layer: Layer, conn: Connection, sock: socket.socket, mask: int):
data = sock.recv(4096)
if data:
self.server_event(layer, events.ReceiveData(conn, data))
self.server_event(layer, events.DataReceived(conn, data))
else:
# TODO: Needs proper teardown.
self.sel.unregister(sock)
sock.close()
self.server_event(layer, events.CloseConnection(conn))
self.server_event(layer, events.ConnectionClosed(conn))
def server_event(self, layer: Layer, event: Event):
print(">>", event)

View File

@ -26,8 +26,8 @@ class ConnectionHandler:
self.client = Client(addr)
self.context = Context(self.client)
self.layer = ReverseProxy(self.context, ("example.com", 443))
# self.layer = ReverseProxy(self.context, ("example.com", 80))
# self.layer = ReverseProxy(self.context, ("example.com", 443))
self.layer = ReverseProxy(self.context, ("example.com", 80))
self.transports: MutableMapping[Connection, StreamIO] = {
self.client: StreamIO(reader, writer)
@ -66,14 +66,14 @@ class ConnectionHandler:
data = b""
if data:
if connection == self.client:
await self.server_event(events.ReceiveClientData(connection, data))
await self.server_event(events.ClientDataReceived(connection, data))
else:
await self.server_event(events.ReceiveServerData(connection, data))
await self.server_event(events.ServerDataReceived(connection, data))
else:
connection.connected = False
if connection in self.transports:
await self.close(connection)
await self.server_event(events.CloseConnection(connection))
await self.server_event(events.ConnectionClosed(connection))
break
async def open_connection(self, command: commands.OpenConnection):
@ -89,15 +89,21 @@ class ConnectionHandler:
print("*", type(event).__name__)
async with self.lock:
print("<#", event)
layer_events = self.layer.handle_event(event)
for event in layer_events:
print("<<", event)
if isinstance(event, commands.OpenConnection):
asyncio.ensure_future(self.open_connection(event))
elif isinstance(event, commands.SendData):
self.transports[event.connection].w.write(event.data)
layer_commands = self.layer.handle_event(event)
for command in layer_commands:
print("<<", command)
if isinstance(command, commands.OpenConnection):
asyncio.ensure_future(self.open_connection(command))
elif isinstance(command, commands.SendData):
self.transports[command.connection].w.write(command.data)
elif isinstance(command, commands.Hook):
# TODO: pass to master here.
print(f"~ {command.name}: {command.data}")
asyncio.ensure_future(
self.server_event(events.HookReply(command, "hook reply"))
)
else:
raise NotImplementedError("Unexpected event: {}".format(event))
raise NotImplementedError("Unexpected event: {}".format(command))
print("#>")

View File

@ -31,14 +31,14 @@ class TCPLayer(Layer):
print(r"connection opened! \o/", ok)
self.state = self.relay_messages
@expect(events.ReceiveData, events.CloseConnection)
@expect(events.DataReceived, events.ConnectionClosed)
def relay_messages(self, event: events.Event) -> commands.TCommandGenerator:
if isinstance(event, events.ReceiveClientData):
if isinstance(event, events.ClientDataReceived):
yield commands.SendData(self.context.server, event.data)
elif isinstance(event, events.ReceiveServerData):
elif isinstance(event, events.ServerDataReceived):
yield commands.SendData(self.context.client, event.data)
elif isinstance(event, events.CloseConnection):
elif isinstance(event, events.ConnectionClosed):
warn("unimplemented: tcp.relay_message:close")
# TODO: close other connection here.

View File

@ -79,9 +79,9 @@ class TLSLayer(Layer):
else:
yield commands.SendData(conn, data)
@expect(events.CloseConnection, events.ReceiveData)
@expect(events.ConnectionClosed, events.DataReceived)
def establish_tls(self, event: events.Event) -> commands.TCommandGenerator:
if isinstance(event, events.ReceiveData):
if isinstance(event, events.DataReceived):
self.tls[event.connection].bio_write(event.data)
try:
self.tls[event.connection].do_handshake()
@ -100,15 +100,15 @@ class TLSLayer(Layer):
self.child_layer = TCPLayer(self.context)
yield from self.child_layer.handle_event(events.Start())
self.state = self.relay_messages
yield from self.state(events.ReceiveData(self.context.server, b""))
yield from self.state(events.ReceiveData(self.context.client, b""))
yield from self.state(events.DataReceived(self.context.server, b""))
yield from self.state(events.DataReceived(self.context.client, b""))
elif isinstance(event, events.CloseConnection):
elif isinstance(event, events.ConnectionClosed):
warn("unimplemented: tls.establish_tls:close")
@expect(events.CloseConnection, events.ReceiveData)
@expect(events.ConnectionClosed, events.DataReceived)
def relay_messages(self, event: events.Event) -> commands.TCommandGenerator:
if isinstance(event, events.ReceiveData):
if isinstance(event, events.DataReceived):
if event.data:
self.tls[event.connection].bio_write(event.data)
yield from self.tls_interact(event.connection)
@ -119,9 +119,9 @@ class TLSLayer(Layer):
except (SSL.WantReadError, SSL.ZeroReturnError):
return
if event.connection == self.context.client:
event_for_child = events.ReceiveClientData(self.context.client, plaintext)
event_for_child = events.ClientDataReceived(self.context.client, plaintext)
else:
event_for_child = events.ReceiveServerData(self.context.server, plaintext)
event_for_child = events.ServerDataReceived(self.context.server, plaintext)
for event_from_child in self.child_layer.handle_event(event_for_child):
if isinstance(event_from_child, commands.SendData):
@ -129,5 +129,5 @@ class TLSLayer(Layer):
yield from self.tls_interact(event_from_child.connection)
else:
yield event_from_child
elif isinstance(event, events.CloseConnection):
elif isinstance(event, events.ConnectionClosed):
warn("unimplemented: tls.relay_messages:close")

View File

@ -100,7 +100,7 @@ def exit_on_close(f):
@functools.wraps(f)
def wrapper(self, event: events.Event):
nonlocal closed
if isinstance(event, events.CloseConnection):
if isinstance(event, events.ConnectionClosed):
closed = True
if not closed:
yield from f(self, event)