async def handle_in_q(self): try: data = b'' while True: while True: msg_data = self.get_one_message(data) if msg_data is None: break await self.in_queue.put((msg_data, None)) data = data[len(msg_data):] temp, err = await self.proxy_in_queue.get() #print(temp) if err is not None: raise err if temp == b'' or temp is None: logger.debug('Server finished!') return data += temp continue except asyncio.CancelledError: return except Exception as e: logger.exception('handle_in_q') await self.in_queue.put((None, e)) finally: self.proxy_task.cancel()
async def handle_out_q(self): try: while True: data = await self.out_queue.get() if data is None: logger.debug('Client finished!') return self.writer.write(data) await self.writer.drain() except asyncio.CancelledError: return except: logger.exception('handle_out_q') finally: self.writer.close() self.handle_in_task.cancel()
def connect(self): if self._con is not None: logger.debug('Already connected!') return try: #setting up authentication self.login_credential = self.auth_handler.select() #setting up connection self.target = self.proxy_handler.select() self._tree = self.target.tree except Exception as e: logger.exception( 'Failed getting authentication or target to work.') return False if self.login_credential.is_anonymous() == True: logger.debug( 'Getting server info via Anonymous BIND on server %s' % self.target.get_host()) self._srv = Server(self.target.get_host(), use_ssl=self.target.is_ssl(), get_info=ALL) self._con = Connection(self._srv, receive_timeout=self.target.timeout, auto_bind=True) else: self._srv = Server(self.target.get_host(), use_ssl=self.target.is_ssl(), get_info=ALL) self._con = Connection( self._srv, user=self.login_credential.get_msuser(), password=self.login_credential.password, authentication=self.login_credential.get_authmethod(), receive_timeout=self.target.timeout, auto_bind=True) logger.debug('Performing BIND to server %s' % self.target.get_host()) if not self._con.bind(): if 'description' in self._con.result: raise Exception('Failed to bind to server! Reason: %s' % self._con.result['description']) raise Exception('Failed to bind to server! Reason: %s' % self._con.result) if not self._tree: logger.debug('Search tree base not defined, selecting root tree') info = self.get_server_info() if 'defaultNamingContext' not in info.other: #really rare cases, the DC doesnt reply to DSA requests!!! #in this case you will need to manually instruct the connection object on which tree it should perform the searches on raise Exception( 'Could not get the defaultNamingContext! You will need to specify the "tree" parameter manually!' ) self._tree = info.other['defaultNamingContext'][0] logger.debug('Selected tree: %s' % self._tree) logger.debug('Connected to server!') return True