async def handle_incoming(self): try: while True: try: data_raw = await self.ws.recv() logger.debug('data_raw %s' % repr(data_raw)) try: cmd = CMD.from_bytes(data_raw) except Exception as e: logger.exception('CMD raw parsing failed! %s' % repr(data_raw)) continue if cmd.token in self.__process_queues: await self.__process_queues[cmd.token].put(cmd) continue if cmd.type == CMDType.CONNECT: self.__running_tasks[cmd.token] = asyncio.create_task( self.socket_connect(cmd)) elif cmd.type == CMDType.SD: await self.send_err( cmd, 'Unexpected token for socket data!', '') except Exception as e: logger.exception('handle_incoming') return finally: pass
async def socket_connect(self, cmd): try: await self.log_start(cmd) if cmd.protocol == 'TCP': reader, writer = await asyncio.open_connection( cmd.ip, int(cmd.port)) in_q = asyncio.Queue() self.__process_queues[cmd.token] = in_q out_task = asyncio.create_task( self.__socket_data_in_handle(cmd.token, in_q, reader, writer)) await self.send_continue(cmd) while True: await asyncio.sleep(0) data = await reader.read(65536) #helps smb a lot if data == b'': await self.send_ok(cmd) return reply = WSNSocketData(cmd.token, data) await self.ws.send(reply.to_bytes()) return else: raise NotImplementedError() except Exception as e: logger.exception('socket_connect') await self.send_err(cmd, 'Socket connect failed', e) finally: out_task.cancel()
async def handle_client(self, ws, path): agentid = None try: agentid = os.urandom(16) self.clients[agentid] = ws remote_ip, remote_port = ws.remote_address logger.info('AGENT connected from %s:%d' % (remote_ip, remote_port)) while True: try: data = await ws.recv() #print('AGENT DATA RECV %s' % data) token = data[6:22] if token == b'\x00' * 16: reply = CMD.from_bytes(data) await self.signal_q_out.put( ('AGENTIN', agentid, reply)) continue await self.out_q.put((agentid, token, data)) except Exception as e: logger.exception('Error in agent handling') return except Exception as e: traceback.print_exc() finally: if agentid in self.clients: del self.clients[agentid] await self.signal_q_out.put(('AGENTOUT', agentid, None)) await ws.close()
async def __handle_in_queue(self): while True: res = await self.in_q.get() try: agentid, data = res #print('AGENT DATA SEND %s' % data) await self.clients[agentid].send(data) except Exception as e: logger.exception('failed sending agent data!')
async def __handle_in_queue(self): while True: res = await self.in_q.get() try: #print('OP DATA IN %s' % repr(res)) agentid, token, data = res tid = agentid + token if tid in self.data_lookop: try: await self.data_lookop[tid].send(data) except: # currently there is no tracking if the operator has disappeared del self.data_lookop[tid] else: print('TID NOT FOUND!') print('TOKEN: %s' % token) print('AGENTID: %s' % agentid) except Exception as e: logger.exception('OP __handle_in_queue')
async def __socket_data_in_handle(self, token, in_q, reader, writer): try: while True: cmd = await in_q.get() if cmd.type == CMDType.OK: #we are cone here! print('GOT OK! Cancelling socket!!') #await self.send_ok(cmd) return elif cmd.type == CMDType.SD: await asyncio.sleep(0) writer.write(cmd.data) await writer.drain() except Exception as e: logger.exception('__socket_data_in_handle') await self.send_err(cmd, 'socket_in failed', e) finally: writer.close() del self.__process_queues[token]