Exemplo n.º 1
0
 def routeRequestForNode(self, destNode, destinationPublicKey, name='new friend', avatar=''):
     
     newFriend = Node({}, {'name':'Just created for the new keys'})
     
     selectedFriend = Node(self.node.getFriend(destinationPublicKey))
     selectedFriend.set('routed_public_key', destinationPublicKey, True)
     selectedFriend.set('public_key', newFriend.get('public_key'))
     selectedFriend.set('private_key', newFriend.get('private_key'))
     selectedFriend.setModifiedToNow()
     selectedFriend.set('source_indexer_key', destNode.get('public_key'), True)
     
     destNode.add('data/friends', selectedFriend.get())
     
     sourceNodeCopy = Node(copy.deepcopy(destNode.get()))
     sourceNodeCopy.set('routed_public_key', destinationPublicKey, True)
     sourceNodeCopy.set('source_indexer_key', destNode.get('public_key'), True)
     
     
     data = b64decode(encrypt(destNode.get('private_key'), 
                              destNode.get('private_key'), 
                              json.dumps(sourceNodeCopy.get())))
     
     self.node.updateFromNode(destNode.get(), preserve_key=selectedFriend.get('public_key'))
     self.updateRelationship(destNode)
     
     packet = self._buildPacket(destNode, destNode, data, status="ROUTED_FRIEND_REQUEST")
     
     self.handlePacket(json.loads(packet))
     
     return selectedFriend.get()
Exemplo n.º 2
0
    def routeRequestThroughNode(self, destNode, destinationPublicKey, name='new friend', avatar=''):
        
        newFriend = Node({}, {'name':'Just created for the new keys'})
        
        selectedFriend = Node({}, {"name" : name, 'avatar': avatar})
                
        sourceNodeCopy = Node(copy.deepcopy(self.node.get()))
        sourceNodeCopy.add('data/friends', selectedFriend.get())
        sourceNodeCopy.set('routed_public_key', destinationPublicKey, True)

        selectedFriend.set('routed_public_key', destinationPublicKey, True)
        selectedFriend.set('public_key', newFriend.get('public_key'))
        selectedFriend.set('private_key', newFriend.get('private_key'))
        selectedFriend.setModifiedToNow()
        selectedFriend.set('source_indexer_key', destNode.get('public_key'), True)
        
        self.node.addFriend(selectedFriend.get())
        self.node.add('data/friends', selectedFriend.get())
        self.updateRelationship(destNode)
        
        sourceNodeCopy.set('public_key', newFriend.get('public_key'))
        sourceNodeCopy.set('private_key', newFriend.get('private_key'))
        
        sourceNodeCopy.set('source_indexer_key', destNode.get('public_key'), True)
        sourceNodeCopy.replaceIdentityOfFriendsWithPubKeys()
        
        data = b64decode(encrypt(destNode.get('private_key'), destNode.get('private_key'), json.dumps(sourceNodeCopy.get())))
        
        return self._doRequest(destNode, destNode, data, status="ROUTED_FRIEND_REQUEST")
Exemplo n.º 3
0
    def doRequest(self, node, indexerRequestObject, contacted, status):
        host = "%s:%s" % (node['data']['identity']['ip_address'][0]['address'],
                          node['data']['identity']['ip_address'][0]['port'])
        if host not in contacted:
            contacted.append(host)

            #### send friend request packet ####
            data = b64decode(
                encrypt(node.get('private_key'), node.get('private_key'),
                        json.dumps(indexerRequestObject.get())))
            self._doRequest(self.node.get(), node, data, status=status)
Exemplo n.º 4
0
    def syncManager(self):

        data = b64decode(
            encrypt(self.node.get('private_key'), self.node.get('private_key'),
                    json.dumps(self.node.get())))
        #simply send my entire object to manager
        responses = self._doRequest(self.node,
                                    self.node,
                                    data,
                                    status="MANAGE_SYNC")
        return responses
Exemplo n.º 5
0
 def doRequest(self, node, indexerRequestObject, contacted, status):
     host = "%s:%s" % (
         node['data']['identity']['ip_address'][0]['address'], 
         node['data']['identity']['ip_address'][0]['port']
     )
     if host not in contacted:
         contacted.append(host)
     
         #### send friend request packet ####
         data = b64decode(encrypt(node.get('private_key'), node.get('private_key'), json.dumps(indexerRequestObject.get())))
         self._doRequest(self.node.get(), node, data, status=status)
         #### end send friend request packet ####
Exemplo n.º 6
0
 def grantPromotion(self, destNode):
     destNodeCopyNode = Node(copy.deepcopy(destNode.get()))
     sourceNodeCopy = Node(copy.deepcopy(self.node.get()))
     
     if self.impersonate:
         dictToSend = destNode.respondWithRelationship(destNode)
     else:
         dictToSend = self.node.respondWithRelationship(destNodeCopyNode)
     dictToSend['public_key'] = self.node.get('public_key')
     dictToSend['private_key'] = self.node.get('private_key')
     data = b64decode(encrypt(destNode.get('private_key'), destNode.get('private_key'), json.dumps(dictToSend)))
     
     self._doRequest(sourceNodeCopy, destNode, data, method="PUT", status="PROMOTION_REQUEST")
Exemplo n.º 7
0
    def addManager(self, host):

        #add the new manager's host address to my ip addresses
        self.node.addIPAddress(self.node.createIPAddress(host))
        self.node.add('data/identity/ip_address',
                      self.node.createIPAddress(host))

        #save the manager friend to the node
        friendPublicKey = unicode(uuid4())
        friendPrivateKey = unicode(uuid4())
        managerFriendNode = YadaServer({}, {"name": "new manager"})
        managerFriendNode.set('public_key', friendPublicKey)
        managerFriendNode.set('private_key', friendPrivateKey)
        managerFriendNode.add('data/identity/ip_address',
                              self.node.createIPAddress(host))

        self.node.add('data/friends', managerFriendNode.get())

        #build the friend request
        meToSend = Node(copy.deepcopy(self.node.get()))
        meToSend = self.node.getClassInstanceFromNodeForNode(meToSend.get())
        meToSend = self.node.respondWithRelationship(
            copy.deepcopy(managerFriendNode))

        responses = []
        #send the friend request to the manager
        response = self._doRequest(meToSend,
                                   managerFriendNode,
                                   json.dumps(meToSend),
                                   status="FRIEND_REQUEST")
        responses.extend(response)

        #simply send my entire object to manager
        encryptedData = encrypt(managerFriendNode.get('private_key'),
                                managerFriendNode.get('private_key'),
                                json.dumps(self.node.get()))
        response = self._doRequest(self.node,
                                   managerFriendNode,
                                   b64decode(encryptedData),
                                   status="MANAGE_REQUEST")
        responses.extend(response)

        for response in responses:
            try:
                self.handlePacket(json.loads(response))
            except:
                logging.debug("failed to handle packet")
                raise
Exemplo n.º 8
0
 def updateRelationship(self, destNode, toInclude = None):
     destNodeCopyNode = Node(copy.deepcopy(destNode.get()))
     sourceNodeCopy = self.node.get()
     
     if self.impersonate:
         dictToSend = destNode.respondWithRelationship(destNode)
     else:
         dictToSend = self.node.respondWithRelationship(destNodeCopyNode)
     
     if toInclude:
         dictToSend['include_node'] = toInclude
     
     data = b64decode(encrypt(destNode.get('private_key'), destNode.get('private_key'), json.dumps(dictToSend)))
     
     responses = self._doRequest(sourceNodeCopy, destNode, data, method="GET")
     for response in responses:
         self.handlePacket(json.loads(response))
Exemplo n.º 9
0
    def grantPromotion(self, destNode):
        destNodeCopyNode = Node(copy.deepcopy(destNode.get()))
        sourceNodeCopy = Node(copy.deepcopy(self.node.get()))

        if self.impersonate:
            dictToSend = destNode.respondWithRelationship(destNode)
        else:
            dictToSend = self.node.respondWithRelationship(destNodeCopyNode)
        dictToSend['public_key'] = self.node.get('public_key')
        dictToSend['private_key'] = self.node.get('private_key')
        data = b64decode(
            encrypt(destNode.get('private_key'), destNode.get('private_key'),
                    json.dumps(dictToSend)))

        self._doRequest(sourceNodeCopy,
                        destNode,
                        data,
                        method="PUT",
                        status="PROMOTION_REQUEST")
Exemplo n.º 10
0
    def routeRequestThroughNode(self,
                                destNode,
                                destinationPublicKey,
                                name='new friend',
                                avatar=''):

        newFriend = Node({}, {'name': 'Just created for the new keys'})

        selectedFriend = Node({}, {"name": name, 'avatar': avatar})

        sourceNodeCopy = Node(copy.deepcopy(self.node.get()))
        sourceNodeCopy.add('data/friends', selectedFriend.get())
        sourceNodeCopy.set('routed_public_key', destinationPublicKey, True)

        selectedFriend.set('routed_public_key', destinationPublicKey, True)
        selectedFriend.set('public_key', newFriend.get('public_key'))
        selectedFriend.set('private_key', newFriend.get('private_key'))
        selectedFriend.setModifiedToNow()
        selectedFriend.set('source_indexer_key', destNode.get('public_key'),
                           True)

        self.node.addFriend(selectedFriend.get())
        self.node.add('data/friends', selectedFriend.get())
        self.updateRelationship(destNode)

        sourceNodeCopy.set('public_key', newFriend.get('public_key'))
        sourceNodeCopy.set('private_key', newFriend.get('private_key'))

        sourceNodeCopy.set('source_indexer_key', destNode.get('public_key'),
                           True)
        sourceNodeCopy.replaceIdentityOfFriendsWithPubKeys()

        data = b64decode(
            encrypt(destNode.get('private_key'), destNode.get('private_key'),
                    json.dumps(sourceNodeCopy.get())))

        return self._doRequest(destNode,
                               destNode,
                               data,
                               status="ROUTED_FRIEND_REQUEST")
Exemplo n.º 11
0
    def routeRequestForNode(self,
                            destNode,
                            destinationPublicKey,
                            name='new friend',
                            avatar=''):

        newFriend = Node({}, {'name': 'Just created for the new keys'})

        selectedFriend = Node(self.node.getFriend(destinationPublicKey))
        selectedFriend.set('routed_public_key', destinationPublicKey, True)
        selectedFriend.set('public_key', newFriend.get('public_key'))
        selectedFriend.set('private_key', newFriend.get('private_key'))
        selectedFriend.setModifiedToNow()
        selectedFriend.set('source_indexer_key', destNode.get('public_key'),
                           True)

        destNode.add('data/friends', selectedFriend.get())

        sourceNodeCopy = Node(copy.deepcopy(destNode.get()))
        sourceNodeCopy.set('routed_public_key', destinationPublicKey, True)
        sourceNodeCopy.set('source_indexer_key', destNode.get('public_key'),
                           True)

        data = b64decode(
            encrypt(destNode.get('private_key'), destNode.get('private_key'),
                    json.dumps(sourceNodeCopy.get())))

        self.node.updateFromNode(destNode.get(),
                                 preserve_key=selectedFriend.get('public_key'))
        self.updateRelationship(destNode)

        packet = self._buildPacket(destNode,
                                   destNode,
                                   data,
                                   status="ROUTED_FRIEND_REQUEST")

        self.handlePacket(json.loads(packet))

        return selectedFriend.get()
Exemplo n.º 12
0
    def updateRelationship(self, destNode, toInclude=None):
        destNodeCopyNode = Node(copy.deepcopy(destNode.get()))
        sourceNodeCopy = self.node.get()

        if self.impersonate:
            dictToSend = destNode.respondWithRelationship(destNode)
        else:
            dictToSend = self.node.respondWithRelationship(destNodeCopyNode)

        if toInclude:
            dictToSend['include_node'] = toInclude

        data = b64decode(
            encrypt(destNode.get('private_key'), destNode.get('private_key'),
                    json.dumps(dictToSend)))

        responses = self._doRequest(sourceNodeCopy,
                                    destNode,
                                    data,
                                    method="GET")
        for response in responses:
            self.handlePacket(json.loads(response))
Exemplo n.º 13
0
 def addManager(self, host):
     
     #add the new manager's host address to my ip addresses
     self.node.addIPAddress(self.node.createIPAddress(host))
     self.node.add('data/identity/ip_address', self.node.createIPAddress(host))
     
     #save the manager friend to the node
     friendPublicKey = unicode(uuid4())
     friendPrivateKey = unicode(uuid4())
     managerFriendNode = YadaServer({}, {"name" : "new manager" })
     managerFriendNode.set('public_key', friendPublicKey)
     managerFriendNode.set('private_key', friendPrivateKey)
     managerFriendNode.add('data/identity/ip_address', self.node.createIPAddress(host))
     
     self.node.add('data/friends', managerFriendNode.get())
             
     #build the friend request
     meToSend = Node(copy.deepcopy(self.node.get()))
     meToSend = self.node.getClassInstanceFromNodeForNode(meToSend.get())
     meToSend = self.node.respondWithRelationship(copy.deepcopy(managerFriendNode))
     
     responses = []
     #send the friend request to the manager
     response = self._doRequest(meToSend, managerFriendNode, json.dumps(meToSend), status="FRIEND_REQUEST")
     responses.extend(response)
     
     #simply send my entire object to manager
     encryptedData = encrypt(managerFriendNode.get('private_key'), managerFriendNode.get('private_key'), json.dumps(self.node.get()))
     response = self._doRequest(self.node, managerFriendNode, b64decode(encryptedData), status="MANAGE_REQUEST")
     responses.extend(response)
     
     for response in responses:
         try:
             self.handlePacket(json.loads(response))
         except:
             logging.debug("failed to handle packet")
             raise
Exemplo n.º 14
0
 def handlePacket(self, packet, callbackCls = None):
     
     packetData = b64decode(packet['data'])
     friend = self.node.getFriend(packet['public_key'])
     if not friend and packet.get('status', None) not in ['FRIEND_REQUEST', "REGISTER_REQUEST"]:
         node = self.node.getManagedNode(packet['public_key'])
         if not node:
             friend = self.node.getFriend(packet['public_key'])
             raise BaseException("No identity found for packet.")
     
     if packet.get('status', None) in ['FRIEND_REQUEST', "REGISTER_REQUEST"]:
         response = self.node.handleFriendRequest(json.loads(packetData))
         
         #Response will be None if the node does not automatically approve friend requests
         if response:
             responseData = Node(response)
             responsePacket = \
                 {
                     "method" : "PUT",
                     "public_key" : response['public_key'],
                     "data" : encrypt(response['private_key'], response['private_key'], json.dumps(responseData.get()))
                 }     
             return responsePacket
         else:
             return None
     
     elif packet.get('status', None) in ['PROMOTION_REQUEST']:
         
         data = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         decrypted = json.loads(data)
         response = self.node.handlePromotionRequest(decrypted, friend['public_key'])
         
         #Response will be None if the node does not automatically approve friend requests
         if response:
             responseData = Node(response)
             responsePacket = \
                 {
                     "method" : "PUT",
                     "public_key" : response['public_key'],
                     "data" : encrypt(response['private_key'], response['private_key'], json.dumps(responseData.get()))
                 }     
             return responsePacket
         else:
             return None
         
     elif packet.get('status', None) == 'ROUTED_FRIEND_REQUEST':
         
         data = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         decrypted = json.loads(data)
         
         #updating at the median node
         self.node.handleRoutedFriendRequest(decrypted)
         
         #updating the destination
         requestedFriend = self.node.getFriend(decrypted['routed_public_key'])
         if not requestedFriend:
             requestedFriend = self.node.getFriendBySourceIndexerKey(decrypted['routed_public_key'])
             if not requestedFriend:
                 return \
                     {
                         "status" : "404"
                     }  
             
         requestedFriendNode = self.node.getClassInstanceFromNodeForNode(requestedFriend)
         self.updateRelationship(requestedFriendNode)
         
         #updating the originator
         requestingFriendNode = self.node.getClassInstanceFromNodeForNode(friend)
         self.updateRelationship(requestingFriendNode)
     
     elif packet.get('status', None) == 'ROUTED_MESSAGE':
         
         data = decrypt(friend['private_key'], friend['private_key'], packetData)
         self.node.handleRoutedMessage(data['data'])
         
     elif packet.get('status', None) == 'MANAGE_REQUEST':
         
         try:
             data = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         except:
             data = packetData
         responseData = self.node.handleManageRequest(json.loads(data))
         responseData = Node(responseData)
         return \
             {
                 "method" : "PUT",
                 "public_key" : responseData.get('public_key'),
                 "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
             }  
         
     elif packet.get('status', None) == 'MANAGE_SYNC':
         
         data = decrypt(node['private_key'], node['private_key'], b64encode(packetData))
         responseData = self.node.syncManagedNode(Node(json.loads(data)))
         responseData = Node(responseData)
         return \
             {
                 "method" : "SYNC",
                 "public_key" : responseData.get('public_key'),
                 "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
             }  
     
     elif packet.get('status', None) in ['INDEXER_REQUEST_ACCEPT', 'INDEXER_REQUEST_UPDATE']:
         packetData = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         loadedData = json.loads(packetData)
         if len(loadedData['data']['friends']) == 2:#is the indexer friends with these two?
             for remoteFriend in loadedData['data']['friends'][0]['data']['friends']:
                 friend1 = self.node.getFriend(remoteFriend['public_key'])
                 if friend1:
                     break
             for remoteFriend in loadedData['data']['friends'][1]['data']['friends']:
                 friend2 = self.node.getFriend(remoteFriend['public_key'])
                 if friend2:
                     break
             if friend1 and friend2:#is are these two friends with the sending indexer
                 confirmed1 = False
                 for friend in friend1['data']['friends']:
                     if friend['public_key'] == loadedData['data']['friends'][0]['routed_public_key'] or \
                         friend['public_key'] == loadedData['data']['friends'][0]['source_indexer_key'] or \
                         friend['public_key'] == loadedData['data']['friends'][0]['public_key']:
                         confirmed1 = friend
                         break
                 confirmed2 = False
                 for friend in friend2['data']['friends']:
                     if friend['public_key'] == loadedData['data']['friends'][1]['routed_public_key'] or \
                         friend['public_key'] == loadedData['data']['friends'][1]['source_indexer_key'] or \
                         friend['public_key'] == loadedData['data']['friends'][1]['public_key']:
                         confirmed2 = friend
                         break
                 if confirmed1 and confirmed2:
                     callbackCls = callbackCls()
                     if packet.get('status', None) == 'INDEXER_REQUEST_UPDATE':
                         callbackCls.friendRequestSuccess(friend1, friend2)
                     elif packet.get('status', None) == 'INDEXER_REQUEST_ACCEPT':
                         callbackCls.friendAcceptSuccess(friend1, friend2)
                     return {}
                 else:
                     return {}
         #show some kind of notification
         
     elif packet.get('method', None) == 'PUT':
         data = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         self.node.updateFromNode(json.loads(data))
     
     elif packet.get('method', None) == 'IMPERSONATE':
         
         data = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         self.node.updateFromNode(json.loads(data), impersonate = True)
         responseData = self.node.respondWithRelationship(Node(json.loads(data)), impersonate = True)
         responseData = Node(responseData)
         return \
             {
                 "method" : "PUT",
                 "public_key" : responseData.get('public_key'),
                 "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
             }
         
     elif packet.get('method', None) == 'GET':
         
         packetData = decrypt(friend['private_key'], friend['private_key'], b64encode(packetData))
         start = time.time()
         friend = json.loads(packetData)
         if 'messages' in friend['data']:
             callbackCls = callbackCls()
             callbackCls.handleMessages(friend['data']['messages'])
         self.node.updateFromNode(friend)
         end = time.time()
         print "updateFromNode: %s" % (end - start)
         start = time.time()
         responseData = self.node.respondWithRelationship(Node(json.loads(packetData)))
         end = time.time()
         print "updateFromNode: %s" % (end - start)
         start = time.time()
         responseData = Node(responseData)
         encryptedResponse =  {
                 "method" : "PUT",
                 "public_key" : responseData.get('public_key'),
                 "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
             }
         end = time.time()
         print "updateFromNode: %s" % (end - start)
         return encryptedResponse
             
     elif packet.get('method', None) == 'SYNC':
         node = self.node.get()
         data = decrypt(node['private_key'], node['private_key'], b64encode(packetData))
         responseData = self.node.sync(json.loads(data))
         responseData = Node(responseData)
         return responseData
Exemplo n.º 15
0
 def syncManager(self):
     
     data = b64decode(encrypt(self.node.get('private_key'), self.node.get('private_key'), json.dumps(self.node.get())))
     #simply send my entire object to manager
     responses = self._doRequest(self.node, self.node, data, status="MANAGE_SYNC")
     return responses
Exemplo n.º 16
0
 def getFriendRequestsFromIndexer(self, node, indexer):
     selectedFriendIndexer = Node(node.matchFriend(indexer))
     dataToSend = '{"method" : "GET", "public_key" : "%s", "data" : "%s"}' %(selectedFriendIndexer.get('public_key'), encrypt(selectedFriendIndexer.get('private_key'), selectedFriendIndexer.get('private_key'), json.dumps({'query':'friend_requests', 'data':{'friends':[{'public_key':x['public_key']} for x in node.get('data/friends')]}})))
     return self.queryIndexer(dataToSend, selectedFriendIndexer, node) 
Exemplo n.º 17
0
    def handlePacket(self, packet, callbackCls=None):

        packetData = b64decode(packet['data'])
        friend = self.node.getFriend(packet['public_key'])
        if not friend and packet.get(
                'status', None) not in ['FRIEND_REQUEST', "REGISTER_REQUEST"]:
            node = self.node.getManagedNode(packet['public_key'])
            if not node:
                friend = self.node.getFriend(packet['public_key'])
                raise BaseException("No identity found for packet.")

        if packet.get('status',
                      None) in ['FRIEND_REQUEST', "REGISTER_REQUEST"]:
            response = self.node.handleFriendRequest(json.loads(packetData))

            #Response will be None if the node does not automatically approve friend requests
            if response:
                responseData = Node(response)
                responsePacket = \
                    {
                        "method" : "PUT",
                        "public_key" : response['public_key'],
                        "data" : encrypt(response['private_key'], response['private_key'], json.dumps(responseData.get()))
                    }
                return responsePacket
            else:
                return None

        elif packet.get('status', None) in ['PROMOTION_REQUEST']:

            data = decrypt(friend['private_key'], friend['private_key'],
                           b64encode(packetData))
            decrypted = json.loads(data)
            response = self.node.handlePromotionRequest(
                decrypted, friend['public_key'])

            #Response will be None if the node does not automatically approve friend requests
            if response:
                responseData = Node(response)
                responsePacket = \
                    {
                        "method" : "PUT",
                        "public_key" : response['public_key'],
                        "data" : encrypt(response['private_key'], response['private_key'], json.dumps(responseData.get()))
                    }
                return responsePacket
            else:
                return None

        elif packet.get('status', None) == 'ROUTED_FRIEND_REQUEST':

            data = decrypt(friend['private_key'], friend['private_key'],
                           b64encode(packetData))
            decrypted = json.loads(data)

            #updating at the median node
            self.node.handleRoutedFriendRequest(decrypted)

            #updating the destination
            requestedFriend = self.node.getFriend(
                decrypted['routed_public_key'])
            if not requestedFriend:
                requestedFriend = self.node.getFriendBySourceIndexerKey(
                    decrypted['routed_public_key'])
                if not requestedFriend:
                    return \
                        {
                            "status" : "404"
                        }

            requestedFriendNode = self.node.getClassInstanceFromNodeForNode(
                requestedFriend)
            self.updateRelationship(requestedFriendNode)

            #updating the originator
            requestingFriendNode = self.node.getClassInstanceFromNodeForNode(
                friend)
            self.updateRelationship(requestingFriendNode)

        elif packet.get('status', None) == 'ROUTED_MESSAGE':

            data = decrypt(friend['private_key'], friend['private_key'],
                           packetData)
            self.node.handleRoutedMessage(data['data'])

        elif packet.get('status', None) == 'MANAGE_REQUEST':

            try:
                data = decrypt(friend['private_key'], friend['private_key'],
                               b64encode(packetData))
            except:
                data = packetData
            responseData = self.node.handleManageRequest(json.loads(data))
            responseData = Node(responseData)
            return \
                {
                    "method" : "PUT",
                    "public_key" : responseData.get('public_key'),
                    "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
                }

        elif packet.get('status', None) == 'MANAGE_SYNC':

            data = decrypt(node['private_key'], node['private_key'],
                           b64encode(packetData))
            responseData = self.node.syncManagedNode(Node(json.loads(data)))
            responseData = Node(responseData)
            return \
                {
                    "method" : "SYNC",
                    "public_key" : responseData.get('public_key'),
                    "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
                }

        elif packet.get('status', None) in [
                'INDEXER_REQUEST_ACCEPT', 'INDEXER_REQUEST_UPDATE'
        ]:
            packetData = decrypt(friend['private_key'], friend['private_key'],
                                 b64encode(packetData))
            loadedData = json.loads(packetData)
            if len(loadedData['data']
                   ['friends']) == 2:  #is the indexer friends with these two?
                for remoteFriend in loadedData['data']['friends'][0]['data'][
                        'friends']:
                    friend1 = self.node.getFriend(remoteFriend['public_key'])
                    if friend1:
                        break
                for remoteFriend in loadedData['data']['friends'][1]['data'][
                        'friends']:
                    friend2 = self.node.getFriend(remoteFriend['public_key'])
                    if friend2:
                        break
                if friend1 and friend2:  #is are these two friends with the sending indexer
                    confirmed1 = False
                    for friend in friend1['data']['friends']:
                        if friend['public_key'] == loadedData['data']['friends'][0]['routed_public_key'] or \
                            friend['public_key'] == loadedData['data']['friends'][0]['source_indexer_key'] or \
                            friend['public_key'] == loadedData['data']['friends'][0]['public_key']:
                            confirmed1 = friend
                            break
                    confirmed2 = False
                    for friend in friend2['data']['friends']:
                        if friend['public_key'] == loadedData['data']['friends'][1]['routed_public_key'] or \
                            friend['public_key'] == loadedData['data']['friends'][1]['source_indexer_key'] or \
                            friend['public_key'] == loadedData['data']['friends'][1]['public_key']:
                            confirmed2 = friend
                            break
                    if confirmed1 and confirmed2:
                        callbackCls = callbackCls()
                        if packet.get('status',
                                      None) == 'INDEXER_REQUEST_UPDATE':
                            callbackCls.friendRequestSuccess(friend1, friend2)
                        elif packet.get('status',
                                        None) == 'INDEXER_REQUEST_ACCEPT':
                            callbackCls.friendAcceptSuccess(friend1, friend2)
                        return {}
                    else:
                        return {}
            #show some kind of notification

        elif packet.get('method', None) == 'PUT':
            data = decrypt(friend['private_key'], friend['private_key'],
                           b64encode(packetData))
            self.node.updateFromNode(json.loads(data))

        elif packet.get('method', None) == 'IMPERSONATE':

            data = decrypt(friend['private_key'], friend['private_key'],
                           b64encode(packetData))
            self.node.updateFromNode(json.loads(data), impersonate=True)
            responseData = self.node.respondWithRelationship(Node(
                json.loads(data)),
                                                             impersonate=True)
            responseData = Node(responseData)
            return \
                {
                    "method" : "PUT",
                    "public_key" : responseData.get('public_key'),
                    "data" : encrypt(responseData.get('private_key'), responseData.get('private_key'), json.dumps(responseData.get()))
                }

        elif packet.get('method', None) == 'GET':

            packetData = decrypt(friend['private_key'], friend['private_key'],
                                 b64encode(packetData))
            start = time.time()
            friend = json.loads(packetData)
            if 'messages' in friend['data']:
                callbackCls = callbackCls()
                callbackCls.handleMessages(friend['data']['messages'])
            self.node.updateFromNode(friend)
            end = time.time()
            print "updateFromNode: %s" % (end - start)
            start = time.time()
            responseData = self.node.respondWithRelationship(
                Node(json.loads(packetData)))
            end = time.time()
            print "updateFromNode: %s" % (end - start)
            start = time.time()
            responseData = Node(responseData)
            encryptedResponse = {
                "method":
                "PUT",
                "public_key":
                responseData.get('public_key'),
                "data":
                encrypt(responseData.get('private_key'),
                        responseData.get('private_key'),
                        json.dumps(responseData.get()))
            }
            end = time.time()
            print "updateFromNode: %s" % (end - start)
            return encryptedResponse

        elif packet.get('method', None) == 'SYNC':
            node = self.node.get()
            data = decrypt(node['private_key'], node['private_key'],
                           b64encode(packetData))
            responseData = self.node.sync(json.loads(data))
            responseData = Node(responseData)
            return responseData