def _iterate_websocket(f: websocket.WebSocketFlow) -> TEventGenerator: messages = f.messages f.messages = [] f.reply = controller.DummyReply() yield "websocket_start", f while messages: f.messages.append(messages.pop(0)) yield "websocket_message", f if f.error: yield "websocket_error", f yield "websocket_end", f
def _iterate_websocket(f: websocket.WebSocketFlow) -> TEventGenerator: messages = f.messages f.messages = [] f.reply = controller.DummyReply() yield "websocket_start", f while messages: f.messages.append(messages.pop(0)) yield "websocket_message", f if f.error: yield "websocket_error", f yield "websocket_end", f
def _iterate_websocket(f: websocket.WebSocketFlow) -> TEventGenerator: messages = f.messages f.messages = [] f.reply = controller.DummyReply() yield layers.websocket.WebsocketStartHook(f) while messages: f.messages.append(messages.pop(0)) yield layers.websocket.WebsocketMessageHook(f) if f.error: yield layers.websocket.WebsocketErrorHook(f) else: yield layers.websocket.WebsocketEndHook(f)
def __call__(self): self.flow = WebSocketFlow(self.client_conn, self.server_conn, self.handshake_flow, self) self.flow.metadata['websocket_handshake'] = self.handshake_flow.id self.handshake_flow.metadata['websocket_flow'] = self.flow.id self.channel.ask("websocket_start", self.flow) client = self.client_conn.connection server = self.server_conn.connection conns = [client, server] close_received = False try: while not self.channel.should_exit.is_set(): r = tcp.ssl_read_select(conns, 0.1) for conn in r: source_conn = self.client_conn if conn == client else self.server_conn other_conn = self.server_conn if conn == client else self.client_conn is_server = (conn == self.server_conn.connection) frame = websockets.Frame.from_file(source_conn.rfile) cont = self._handle_frame(frame, source_conn, other_conn, is_server) if not cont: if close_received: return else: close_received = True except (socket.error, exceptions.TcpException, SSL.Error) as e: s = 'server' if is_server else 'client' self.flow.error = flow.Error("WebSocket connection closed unexpectedly by {}: {}".format(s, repr(e))) self.channel.tell("websocket_error", self.flow) finally: self.channel.tell("websocket_end", self.flow)
async def inject(self, flow: WebSocketFlow): scenarios = [json.loads(open(self.scenarios_file, "r").read())][0] interval = 1 i = 0 self.logStr("Prepare for " + str(len(scenarios)) + " message/s injection on " + self.config.name) await asyncio.gather(self.loading(5, "Delaying injection for 5s")) while not flow.ended and not flow.error and i < len(scenarios): self.logStr("Injecting message on " + self.config.name + " in " + str(interval) + "s") await asyncio.gather(self.loading(interval, "Injecting message in")) self.logStr("Injecting message on " + self.config.name) flow.inject_message(flow.client_conn, str(scenarios[i])) i += 1 self.logStr("Finish Injecting " + str(len(scenarios)) + " message/s on " + self.config.name)
def __call__(self): self.flow = WebSocketFlow(self.client_conn, self.server_conn, self.handshake_flow) self.flow.metadata['websocket_handshake'] = self.handshake_flow.id self.handshake_flow.metadata['websocket_flow'] = self.flow.id self.channel.ask("websocket_start", self.flow) conns = [c.connection for c in self.connections.keys()] close_received = False try: while not self.channel.should_exit.is_set(): self._inject_messages(self.client_conn, self.flow._inject_messages_client) self._inject_messages(self.server_conn, self.flow._inject_messages_server) r = tcp.ssl_read_select(conns, 0.1) for conn in r: source_conn = self.client_conn if conn == self.client_conn.connection else self.server_conn other_conn = self.server_conn if conn == self.client_conn.connection else self.client_conn is_server = (source_conn == self.server_conn) header, frame, consumed_bytes = websocket.read_frame( source_conn.rfile) self.log( "WebSocket Frame from {}: {}, {}".format( "server" if is_server else "client", header, frame, ), "debug") data = self.connections[source_conn].receive_data( consumed_bytes) source_conn.send(data) if close_received: return for event in self.connections[source_conn].events(): if not self._handle_event(event, source_conn, other_conn, is_server): if not close_received: close_received = True except (socket.error, exceptions.TcpException, SSL.Error) as e: s = 'server' if is_server else 'client' self.flow.error = flow.Error( "WebSocket connection closed unexpectedly by {}: {}".format( s, repr(e))) self.channel.tell("websocket_error", self.flow) finally: self.flow.ended = True self.channel.tell("websocket_end", self.flow)
def websocket_message(flow: websocket.WebSocketFlow) -> None: global i i += 1 ctx.log.info(i) flow.kill()