def dataReceived(self, inbound): """ As soon as any data is received, write it back. """ node = YadaServer() nodeComm = NodeCommunicator(node) mongoapi = MongoApi(nodeComm) try: jsonDict = loadInboundJson(inbound) data = getProfileFromInbound(jsonDict) decrypted = {} if data: try: decrypted.update( json.loads( decrypt(data['private_key'], data['private_key'], jsonDict['data']))) except: decrypted = jsonDict['data'] response = getattr(mongoapi, jsonDict['METHOD'])(data, decrypted) jsonResponse = json.dumps(response) self.transport.write(jsonResponse) except Exception as ex: logging.exception(ex) self.transport.write('Input not valid.') self.transport.loseConnection()
def hostedUserUpdate(self, jsonDict, request=None): idQuery = self.getManagedNode(jsonDict['public_key']) #check to see if a hosted user is trying to replicate self if idQuery: id = Node(idQuery)#p decryptedIncomingIdentity = decrypt(id.get('private_key'), id.get('private_key') ,jsonDict['data'].replace(' ', '+')) incomingId = Node(json.loads(decryptedIncomingIdentity))#user InternalId = Node(self.getManagedNode(id.get('public_key')))#data if InternalId.get(): try: InternalId.dedupIP() except: logging.warning("an error occured when running dedupIP() method") InternalId.sync(incomingId.get(), True, {}) self.updateManagedNode(InternalId) elif jsonDict.get('status', None) and jsonDict['status'] == "MANAGE_REQUEST": #replace spaces with plus symbols, this was a problem early on data = b64decode(jsonDict['data'].replace(' ', '+')) #attemped to load data into memory structure data = json.loads(data) self.addManagedNode(data) return "node is now managed by this node manager"
def dataReceived(self, inbound): """ As soon as any data is received, write it back. """ node = YadaServer() nodeComm = NodeCommunicator(node) mongoapi = MongoApi(nodeComm) try: jsonDict = loadInboundJson(inbound) data = getProfileFromInbound(jsonDict) decrypted = {} if data: try: decrypted.update(json.loads(decrypt(data["private_key"], data["private_key"], jsonDict["data"]))) except: decrypted = jsonDict["data"] response = getattr(mongoapi, jsonDict["METHOD"])(data, decrypted) jsonResponse = json.dumps(response) self.transport.write(jsonResponse) except Exception as ex: logging.exception(ex) self.transport.write("Input not valid.") self.transport.loseConnection()
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
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