def run(self):
        while True:
            if len(self.queue) > 0 and self.send_enabled:
                for item in self.queue:
                    try:
                        res = self.http_client.post(item.url, item.body)
                        if res.status_code == 401:
                            item.retry = False
                        elif res.status_code != 200:
                            item.retry = True
                        self.queue.remove(item)

                        Logger.debug("Event successfully sent; {}".format(item.body))
                    except Exception as e:
                        Logger.error("Failed to send event; {}".format(e))
                        if item.retry:
                            if len(self.coefficients) == self.attempt + 1:
                                self.attempt = 0

                            back_off = self.coefficients[self.attempt] * self.options.interval
                            Logger.debug("Automatic back-off of {}".format(back_off))
                            self.send_enabled = False
                            time.sleep(back_off)
                            self.send_enabled = True
                time.sleep(self.interval/1000)
Exemplo n.º 2
0
 def encrypt(cls, text, cipher_key):
     try:
         key = cipher_key[:cls.KEY_SIZE]
         iv = Random.new().read(AES.block_size)
         cipher = AES.new(key.encode("utf8"), AES.MODE_CBC, iv)
         raw = str(cls._pad(text))
         return hexlify(iv + cipher.encrypt(raw.encode("utf8")))
     except Exception as e:
         Logger.error("Could not encrypt text {}; {}".format(text, e))
         return None
    def stop_event_persist(self):
        if self.send_enabled:
            Logger.debug("Attempting to stop automatic event persistence")
            try:
                self.flush()
                if self.thread:
                    self.thread.stop()
            except ValueError as e:
                Logger.error("Could not stop event scheduler; {}".format(e))

            Logger.debug("Stopped event persistence")
Exemplo n.º 4
0
 def decrypt(cls, encrypted, cipher_key):
     try:
         key = cipher_key[:cls.KEY_SIZE]
         content = unhexlify(encrypted)
         iv = content[:cls.BLOCK_SIZE]
         cipher_text = content[cls.BLOCK_SIZE:]
         aes = AES.new(key.encode("utf8"), AES.MODE_CBC, iv)
         rv = aes.decrypt(cipher_text).decode("utf-8").strip()
         secret = json.loads(rv)
         return ClientToken(secret.get("cid"), secret.get("vid"),
                            secret.get("fp"))
     except Exception as e:
         Logger.error("Could not decrypt str {}; {}".format(encrypted, e))
         return None