async def recv(self, count, forceRecv=0): try: if self.is_proxy is False: # the TCP call already buffers one messages per get data, err = await asyncio.wait_for( self.connection.in_queue.get(), timeout=self.target.timeout) return data, err err = None while True: if len(self.buffer) >= 24: response_header = MSRPCRespHeader(self.buffer) if len(self.buffer) >= response_header['frag_len']: msg_data = self.buffer[:response_header['frag_len']] self.buffer = self.buffer[response_header['frag_len']:] return msg_data, err data, err = await asyncio.wait_for( self.connection.in_queue.get(), timeout=10) if err is not None: return None, err self.buffer += data except Exception as e: print(e) return None, e
async def handle_incoming(self): """ Reads data bytes from the socket and dispatches it to the incoming queue """ try: while not self.disconnected.is_set( ) or not self.shutdown_evt.is_set(): try: msg_data = b'' data = await self.reader.readexactly(24) msg_data += data response_header = MSRPCRespHeader(msg_data) data = await self.reader.readexactly( response_header['frag_len'] - 24) msg_data += data await self.in_queue.put((msg_data, None)) except Exception as e: await self.in_queue.put((None, e)) return except asyncio.CancelledError: #the SMB connection is terminating return except Exception as e: logger.exception('[DCERPCTCPConnection] handle_incoming %s' % str(e)) await self.in_queue.put((None, e)) await self.disconnect()
async def __handle_incoming(self): try: data = b'' while True: x, err = await self.connection.in_queue.get() if err is not None: raise err data += x if len(data) >= 24: #MSRPCRespHeader._SIZE response_header = MSRPCRespHeader(data) while len(data) < response_header['frag_len']: x, err = await self.connection.in_queue.get() if err is not None: raise err data += x response_data = data[:response_header['frag_len']] data = data[response_header['frag_len']:] await self.msg_in_queue.put(response_data) except asyncio.CancelledError: self.exception_evt.set() return except Exception as e: logger.exception('__handle_incoming') self.exception_evt.set() return