def receive_message(self, datagram): if len(datagram) < 166: self.log.warning("received datagram too small from %s, ignoring" % self.addr) return False m = Message() try: m.ParseFromString(datagram) self.node = Node(m.sender.guid, m.sender.nodeAddress.ip, m.sender.nodeAddress.port, m.sender.publicKey, None if not m.sender.HasField("relayAddress") else (m.sender.relayAddress.ip, m.sender.relayAddress.port), m.sender.natType, m.sender.vendor) pubkey = m.sender.publicKey verify_key = nacl.signing.VerifyKey(pubkey) signature = m.signature m.ClearField("signature") verify_key.verify(m.SerializeToString(), signature) h = nacl.hash.sha512(m.sender.publicKey) pow_hash = h[40:] if int(pow_hash[:6], 16) >= 50 or m.sender.guid.encode("hex") != h[:40]: raise Exception('Invalid GUID') except Exception: # If message isn't formatted property then ignore self.log.warning("received an invalid message from %s, ignoring" % self.addr) return False for processor in self.processors: if m.command in processor or m.command == NOT_FOUND: processor.receive_message(m, self.node, self.connection, self.ban_score)
def receive_message(self, datagram): if len(datagram) < 166: self.log.warning( "received datagram too small from %s, ignoring" % self.addr) return False try: m = Message() m.ParseFromString(datagram) self.node = Node( m.sender.guid, m.sender.nodeAddress.ip, m.sender.nodeAddress.port, m.sender.publicKey, None if not m.sender.HasField("relayAddress") else (m.sender.relayAddress.ip, m.sender.relayAddress.port), m.sender.natType, m.sender.vendor) self.remote_node_version = m.protoVer if self.time_last_message == 0: h = nacl.hash.sha512(m.sender.publicKey) pow_hash = h[40:] if int(pow_hash[:6], 16) >= 50 or m.sender.guid.encode("hex") != h[:40]: raise Exception('Invalid GUID') for processor in self.processors: if m.command in processor or m.command == NOT_FOUND: processor.receive_message(m, self.node, self.connection, self.ban_score) if m.command != PING: self.time_last_message = time.time() except Exception: # If message isn't formatted property then ignore self.log.warning( "received an invalid message from %s, ignoring" % self.addr) return False
def receive_message(self, datagram, connection): m = Message() try: m.ParseFromString(datagram) sender = node.Node(m.sender.guid, m.sender.ip, m.sender.port, m.sender.signedPublicKey, m.sender.vendor) except Exception: # If message isn't formatted property then ignore self.log.warning("Received unknown message from %s, ignoring" % str(connection.dest_addr)) return False # Check that the GUID is valid. If not, ignore if self.router.isNewNode(sender): try: pubkey = m.sender.signedPublicKey[len(m.sender.signedPublicKey) - 32:] verify_key = nacl.signing.VerifyKey(pubkey) verify_key.verify(m.sender.signedPublicKey) h = nacl.hash.sha512(m.sender.signedPublicKey) pow_hash = h[64:128] if int(pow_hash[:6], 16) >= 50 or hexlify(m.sender.guid) != h[:40]: raise Exception('Invalid GUID') except Exception: self.log.warning("Received message from sender with invalid GUID, ignoring") return False if m.sender.vendor: VendorStore().save_vendor(m.sender.guid, m.sender.ip, m.sender.port, m.sender.signedPublicKey) msgID = m.messageID data = tuple(m.arguments) if msgID in self._outstanding: self._acceptResponse(msgID, data, sender) else: self._acceptRequest(msgID, str(Command.Name(m.command)).lower(), data, sender, connection)
def receive_message(self, datagram): if len(datagram) < 166: self.log.warning( "received datagram too small from %s, ignoring" % str(self.connection.dest_addr)) return False m = Message() try: m.ParseFromString(datagram) for processor in self.processors: if m.command in processor: processor.receive_message(datagram, self.connection) except Exception: # If message isn't formatted property then ignore self.log.warning("Received unknown message from %s, ignoring" % str(self.connection.dest_addr)) return False
def receive_message(self, datagram): if len(datagram) < 166: self.log.warning( "received datagram too small from %s, ignoring" % self.addr) return False m = Message() try: m.ParseFromString(datagram) self.node = Node(m.sender.guid, m.sender.ip, m.sender.port, m.sender.signedPublicKey, m.sender.vendor) for processor in self.processors: if m.command in processor or m.command == NOT_FOUND: processor.receive_message(datagram, self.connection) except Exception: # If message isn't formatted property then ignore self.log.warning("received unknown message from %s, ignoring" % self.addr) return False
def receive_message(self, datagram, connection, ban_score): m = Message() try: m.ParseFromString(datagram) sender = node.Node(m.sender.guid, m.sender.ip, m.sender.port, m.sender.signedPublicKey, m.sender.vendor) except Exception: # If message isn't formatted property then ignore self.log.warning("received unknown message from %s, ignoring" % str(connection.dest_addr)) return False if m.testnet != self.multiplexer.testnet: self.log.warning( "received message from %s with incorrect network parameters." % str(connection.dest_addr)) connection.shutdown() return False if m.protoVer < PROTOCOL_VERSION: self.log.warning( "received message from %s with incompatible protocol version." % str(connection.dest_addr)) connection.shutdown() return False # Check that the GUID is valid. If not, ignore if self.router.isNewNode(sender): try: pubkey = m.sender.signedPublicKey[len(m.sender.signedPublicKey ) - 32:] verify_key = nacl.signing.VerifyKey(pubkey) verify_key.verify(m.sender.signedPublicKey) h = nacl.hash.sha512(m.sender.signedPublicKey) pow_hash = h[64:128] if int(pow_hash[:6], 16) >= 50 or hexlify( m.sender.guid) != h[:40]: raise Exception('Invalid GUID') except Exception: self.log.warning( "received message from sender with invalid GUID, ignoring") connection.shutdown() return False if m.sender.vendor: self.db.VendorStore().save_vendor(m.sender.guid.encode("hex"), m.sender.ip, m.sender.port, m.sender.signedPublicKey) msgID = m.messageID if m.command == NOT_FOUND: data = None else: data = tuple(m.arguments) if msgID in self._outstanding: self._acceptResponse(msgID, data, sender) elif m.command != NOT_FOUND: ban_score.process_message(m) self._acceptRequest(msgID, str(Command.Name(m.command)).lower(), data, sender, connection)