def rc4_params(self, method, url, params: dict):
     nonce = miutils.gen_nonce()
     signed_nonce = self.signed_nonce(nonce)
     params['rc4_hash__'] = MiotCloud.sha1_sign(method, url, params,
                                                signed_nonce)
     for k, v in params.items():
         params[k] = MiotCloud.encrypt_data(signed_nonce, v)
     params.update({
         'signature':
         MiotCloud.sha1_sign(method, url, params, signed_nonce),
         'ssecurity':
         self.ssecurity,
         '_nonce':
         nonce,
     })
     return params
 def request(self, url, params, **kwargs):
     self.session = self.api_session()
     timeout = kwargs.get('timeout', self.http_timeout)
     try:
         nonce = miutils.gen_nonce()
         signed_nonce = miutils.signed_nonce(self.ssecurity, nonce)
         signature = miutils.gen_signature(url.replace('/app/', '/'),
                                           signed_nonce, nonce, params)
         post_data = {
             'signature': signature,
             '_nonce': nonce,
             'data': params['data'],
         }
         response = self.session.post(url, data=post_data, timeout=timeout)
         return response.text
     except requests.exceptions.HTTPError as exc:
         _LOGGER.error('Error while executing request to %s: %s', url, exc)
     except MiCloudException as exc:
         _LOGGER.error(
             'Error while decrypting response of request to %s: %s', url,
             exc)
Example #3
0
    def request(self, url, params):
        if not self.service_token or not self.user_id:
            raise MiCloudException(
                "Cannot execute request. service token or userId missing. Make sure to login."
            )

        self.session = requests.Session()
        self.session.headers.update({'User-Agent': self.useragent})

        logging.debug("Send request: %s to %s", params['data'], url)
        self.session.headers.update({
            'x-xiaomi-protocal-flag-cli':
            'PROTOCAL-HTTP2',
            'content-type':
            'application/x-www-form-urlencoded'
        })
        self.session.cookies.update({
            'userId':
            str(self.user_id),
            'yetAnotherServiceToken':
            self.service_token,
            'serviceToken':
            self.service_token,
            'locale':
            str(self.locale),
            'timezone':
            str(self.timezone),
            'is_daylight':
            str(time.daylight),
            'dst_offset':
            str(time.localtime().tm_isdst * 60 * 60 * 1000),
            'channel':
            'MI_APP_STORE'
        })
        for c in self.session.cookies:
            logging.debug('Cookie: %s', c)

        try:
            nonce = miutils.gen_nonce()
            signed_nonce = miutils.signed_nonce(self.ssecurity, nonce)
            signature = miutils.gen_signature(url.replace("/app", ""),
                                              signed_nonce, nonce, params)

            post_data = {
                'signature': signature,
                '_nonce': nonce,
                'data': params['data']
            }

            response = self.session.post(url, data=post_data)
            if response.status_code == 403:
                self.service_token = None

            return response.text
        except requests.exceptions.HTTPError as e:
            self.service_token = None
            logging.exception("Error while executing request to %s :%s", url,
                              str(e))
        except MiCloudException as e:
            logging.exception(
                "Error while decrypting response of request to %s :%s", url,
                str(e))
        except Exception as e:
            logging.exception("Error while executing request to %s :%s", url,
                              str(e))
Example #4
0
    def request(self, url, params):
        if not self.service_token or not self.user_id:
            raise MiCloudException(
                "Cannot execute request. service token or userId missing. Make sure to login."
            )

        logging.debug("Send request: %s to %s", params['data'], url)

        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': self.useragent,
            'Accept-Encoding': 'identity',
            'x-xiaomi-protocal-flag-cli': 'PROTOCAL-HTTP2',
            'content-type': 'application/x-www-form-urlencoded',
            'MIOT-ENCRYPT-ALGORITHM': 'ENCRYPT-RC4'
        })
        self.session.cookies.update({
            'userId':
            str(self.user_id),
            'yetAnotherServiceToken':
            self.service_token,
            'serviceToken':
            self.service_token,
            'locale':
            str(self.locale),
            'timezone':
            str(self.timezone),
            'is_daylight':
            str(time.daylight),
            'dst_offset':
            str(time.localtime().tm_isdst * 60 * 60 * 1000),
            'channel':
            'MI_APP_STORE'
        })
        for c in self.session.cookies:
            logging.debug('Cookie: %s', c)

        try:
            nonce = miutils.gen_nonce()
            signed_nonce = miutils.signed_nonce(self.ssecurity, nonce)
            post_data = miutils.generate_enc_params(url, "POST", signed_nonce,
                                                    nonce, params,
                                                    self.ssecurity)

            response = self.session.post(url, data=post_data)
            if response.status_code == 403:
                self.service_token = None

            return miutils.decrypt_rc4(
                miutils.signed_nonce(self.ssecurity, post_data["_nonce"]),
                response.text)
        except requests.exceptions.HTTPError as e:
            self.service_token = None
            logging.exception("Error while executing request to %s :%s", url,
                              str(e))
        except MiCloudException as e:
            logging.exception(
                "Error while decrypting response of request to %s :%s", url,
                str(e))
        except Exception as e:
            logging.exception("Error while executing request to %s :%s", url,
                              str(e))