def func(address, *args): msgID = sha1(str(random.getrandbits(255))).digest() m = Message() m.messageID = msgID m.sender.MergeFrom(self.sourceNode.getProto()) m.command = Command.Value(name.upper()) m.protoVer = PROTOCOL_VERSION for arg in args: m.arguments.append(str(arg)) m.testnet = self.multiplexer.testnet data = m.SerializeToString() d = defer.Deferred() if name != "hole_punch": seed = SEED_NODE_TESTNET if self.multiplexer.testnet else SEED_NODE hole_punch = reactor.callLater(3, self.hole_punch, seed, address[0], address[1], "True") if address in self.multiplexer: hole_punch.cancel() self._outstanding[msgID] = [d, address, hole_punch] self.log.debug("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) else: self.log.debug("sending hole punch message to %s" % args[0] + ":" + str(args[1])) self.multiplexer.send_message(data, address) return d
def receive_message(self, message, sender, connection, ban_score): if message.testnet != self.multiplexer.testnet: self.log.warning( "received message from %s with incorrect network parameters." % str(connection.dest_addr)) connection.shutdown() return False if message.protoVer < PROTOCOL_VERSION: self.log.warning( "received message from %s with incompatible protocol version." % str(connection.dest_addr)) connection.shutdown() return False self.multiplexer.vendors[sender.id] = sender msgID = message.messageID if message.command == NOT_FOUND: data = None else: data = tuple(message.arguments) if msgID in self._outstanding: self._acceptResponse(msgID, data, sender) elif message.command != NOT_FOUND: # ban_score.process_message(message) self._acceptRequest(msgID, str(Command.Name(message.command)).lower(), data, sender, connection)
def ban(self, message_type): self.log.warning("Banned %s. Reason: too many %s messages." % (self.peer_ip[0], Command.Name(message_type))) self.multiplexer.ban_ip(self.peer_ip[0]) self.multiplexer[self.peer_ip].shutdown() reactor.callLater(self.ban_time, self.multiplexer.remove_ip_ban, self.peer_ip[0])
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 func(node, *args): msgID = sha1(str(random.getrandbits(255))).digest() m = Message() m.messageID = msgID m.sender.MergeFrom(self.sourceNode.getProto()) m.command = Command.Value(name.upper()) m.protoVer = PROTOCOL_VERSION for arg in args: m.arguments.append(str(arg)) m.testnet = self.multiplexer.testnet data = m.SerializeToString() address = (node.ip, node.port) relay_addr = None if node.nat_type == SYMMETRIC or \ (node.nat_type == RESTRICTED and self.sourceNode.nat_type == SYMMETRIC): relay_addr = node.relay_node d = defer.Deferred() if m.command != HOLE_PUNCH: timeout = reactor.callLater(self._waitTimeout, self.timeout, node) self._outstanding[msgID] = [d, address, timeout] self.log.debug("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) self.multiplexer.send_message(data, address, relay_addr) if self.multiplexer[address].state != State.CONNECTED and \ node.nat_type == RESTRICTED and \ self.sourceNode.nat_type != SYMMETRIC: self.hole_punch(Node(digest("null"), node.relay_node[0], node.relay_node[1], nat_type=FULL_CONE), address[0], address[1], "True") self.log.debug("sending hole punch message to %s" % address[0] + ":" + str(address[1])) return d
def receive_message(self, message, connection, ban_score): sender = Node( message.sender.guid, message.sender.nodeAddress.ip, message.sender.nodeAddress.port, message.sender.signedPublicKey, None if not message.sender.HasField("relayAddress") else (message.sender.relayAddress.ip, message.sender.relayAddress.port), message.sender.natType, message.sender.vendor) if message.testnet != self.multiplexer.testnet: self.log.warning( "received message from %s with incorrect network parameters." % str(connection.dest_addr)) connection.shutdown() return False if message.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 = message.sender.signedPublicKey[len(message.sender. signedPublicKey) - 32:] verify_key = nacl.signing.VerifyKey(pubkey) verify_key.verify(message.sender.signedPublicKey) h = nacl.hash.sha512(message.sender.signedPublicKey) pow_hash = h[64:128] if int(pow_hash[:6], 16) >= 50 or hexlify( message.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 message.sender.vendor: self.db.VendorStore().save_vendor( message.sender.guid.encode("hex"), message.sender.SerializeToString()) msgID = message.messageID if message.command == NOT_FOUND: data = None else: data = tuple(message.arguments) if msgID in self._outstanding: self._acceptResponse(msgID, data, sender) elif message.command != NOT_FOUND: #ban_score.process_message(m) self._acceptRequest(msgID, str(Command.Name(message.command)).lower(), data, sender, connection)
def ban(self, peer, message_type): reason = Command.Name(message_type) self.log.warning("Banned %s. Reason: too many %s messages." % (peer[0], reason)) self.multiplexer.ban_ip(peer[0]) if peer in self.multiplexer: self.multiplexer[peer].shutdown() reactor.callLater(self.ban_time, self.multiplexer.remove_ip_ban, peer[0])
def _sendResponse(self, response, funcname, msgID, sender, connection): if self.noisy: self.log.debug("Sending response for msg id %s to %s" % (b64encode(msgID), sender)) m = Message() m.messageID = msgID m.sender.MergeFrom(self.proto) m.command = Command.Value(funcname.upper()) for arg in response: m.arguments.append(str(arg)) data = m.SerializeToString() connection.send_message(data)
def ban(self, peer, message_type): if message_type == 100: reason = "RECONNECTIONS" elif message_type == 110: reason = "MALFORMATTED" else: reason = Command.Name(message_type) self.log.warning("Banned %s. Reason: too many %s messages." % (peer[0], reason)) self.multiplexer.ban_ip(peer[0]) if peer in self.multiplexer: self.multiplexer[peer].shutdown() reactor.callLater(self.ban_time, self.multiplexer.remove_ip_ban, peer[0])
def _sendResponse(self, response, funcname, msgID, sender, connection): self.log.debug("sending response for msg id %s to %s" % (b64encode(msgID), sender)) m = Message() m.messageID = msgID m.sender.MergeFrom(self.sourceNode.getProto()) m.protoVer = PROTOCOL_VERSION m.testnet = self.multiplexer.testnet if response is None: m.command = NOT_FOUND else: m.command = Command.Value(funcname.upper()) for arg in response: m.arguments.append(str(arg)) data = m.SerializeToString() connection.send_message(data)
def func(address, *args): msgID = sha1(str(random.getrandbits(255))).digest() m = Message() m.messageID = msgID m.sender.MergeFrom(self.proto) m.command = Command.Value(name.upper()) for arg in args: m.arguments.append(str(arg)) data = m.SerializeToString() if self.noisy: self.log.debug("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) self.multiplexer.send_message(data, address) if name is not "hole_punch": d = defer.Deferred() timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID, address) self._outstanding[msgID] = [d, timeout] return d
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)