示例#1
0
    def on_message(self, device: Device, message: bytes) -> dict:
        if validate_token(message):
            self.logger.encrypted_message("Received bytes: {}".format(message))
            message = decrypt(message, device.key)
            self.logger.message("Received decrypted: {}".format(message))

            try:
                message = json.loads(message, cls=MessageDecoder)
            except json.JSONDecodeError as e:
                self.logger.error("Failed to decode message {}: {}".format(
                    message, e))
                raise Exception("Failed to decode message")

            if device.is_verified():
                return message
            else:
                if message.get("event_type") == "assign":
                    if str(message.get(
                            "encryption_key")).encode() == device.custom_key:
                        device.key = device.custom_key
                        device.set_verified(True)
                        self.connected.emit(device, device.ip(), device.port())
                        self.logger.info("Device {} validated".format(
                            device.id()))
                    else:
                        self.logger.warning(
                            "Assigned keys do not match! Bot {} will be kicked!"
                            .format(device.id()))
                        device.kick()
        else:
            self.decryption_error.emit(device, message)
            raise Exception("Message is not valid")
        return {}
示例#2
0
 def on_connected(self, device: Device, ip, port):
     device.key = self.key
     device.custom_key = generate_key()
     device.write({
         "event_type": "assign",
         "encryption_key": device.custom_key.decode()
     })