def packQueue(self, pid, code, packbody): packInfo = Packager.codeFindPackage( code ) data = Packager.parsePackage( self.tname, code, packbody ) #发送回应包 if packInfo['ExistReply'] == 1: respPackage = Packager.genPackage( self.tname, 'Response', pid ) self.send( respPackage ) func = getattr(self, packInfo['Name']) func( data )
def PubKey(self, data): data = data[0] myRsa = MyRsa() myRsa.setPubKey( data ) Packager.setEncipherer( self.tname, 'rsa_public', myRsa.publicCrypt ) myAes = MyAes() myAes.generate() aes = myAes.getKey() Packager.setEncipherer( self.tname, 'aes', myAes.crypt ) key = int( raw_input( 'Please input your Secret Key: ' ) ) keyPackage = Packager.genPackage( self.tname, 'Key', 2, [key, aes]) self.send( keyPackage )
def open(self): print '='*38 while self.switch: name = raw_input( 'which terminal: ' ) for i in range( len( self.tStatus ) ): if self.tStatus[i][0] == name: if self.tStatus[i][1] == 0: wolPackager = Packager.genPackage( self.tname, 'WOL', 3, [ i ] ) self.send( wolPackager ) Logger.info( 'open %s!'%self.tStatus[i][0] ) else: Logger.info( '%s is online!'%self.tStatus[i][0] ) break;
def changeStatus( delay, destAddr ): if delay: if self.__status.has_key( destAddr ): if self.__status[ destAddr ][ 'Status' ] == False: self.activateTerminal( destAddr ) else: return; else: try: host = socket.gethostbyaddr( destAddr )[0] except socket.herror, e: if e.errno != 11004: raise Exception( 'get host name error!' ) host = destAddr mac = getMac( destAddr ) terminalID = self.setTerminal( destAddr, mac, host ) self.__status[ destAddr ] = { 'TerminalID':terminalID, 'Name':host, 'IPv4':destAddr, 'Mac':mac } queryPackInfo = Packager.nameFindPackage( 'QueryTerminals' ) OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] ) Logger.info( 'Terminal %s login!'%destAddr ) self.__status[ destAddr ][ 'Status' ] = True queryPackInfo = Packager.nameFindPackage( 'QueryStatus' ) OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
host = destAddr mac = getMac( destAddr ) terminalID = self.setTerminal( destAddr, mac, host ) self.__status[ destAddr ] = { 'TerminalID':terminalID, 'Name':host, 'IPv4':destAddr, 'Mac':mac } queryPackInfo = Packager.nameFindPackage( 'QueryTerminals' ) OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] ) Logger.info( 'Terminal %s login!'%destAddr ) self.__status[ destAddr ][ 'Status' ] = True queryPackInfo = Packager.nameFindPackage( 'QueryStatus' ) OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] ) else: if self.__status.has_key( destAddr ): if self.__status[ destAddr ][ 'Status' ] == True: Logger.info( 'Terminal %s logout!'%destAddr ) self.__status[ destAddr ][ 'Status' ] = False queryPackInfo = Packager.nameFindPackage( 'QueryStatus' ) OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] ) while self.__switch: for existsAddr in self.__status.keys(): ICMP.send_one_ping(mySocket, existsAddr, my_ID) delay = ICMP.receive_one_ping(mySocket, my_ID, 1) changeStatus( delay, existsAddr ) for i in range( 1, ipCount ): destAddr = socket.inet_ntoa( pack( '>L', netAddrBit + i ) ) if destAddr == IP or self.__status.has_key( destAddr ): continue ICMP.send_one_ping(mySocket, destAddr, my_ID) delay = ICMP.receive_one_ping(mySocket, my_ID, 1) changeStatus( delay, destAddr )