2018-02-03 20:40:17 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-03 00:36:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-03 20:40:17 +00:00
|
|
|
# pylint: disable=C0111,C0326,C0103
|
|
|
|
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Tools to interface with TCL's download request API."""
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
import binascii
|
|
|
|
import hashlib
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
import zlib
|
|
|
|
from collections import OrderedDict
|
2018-02-03 20:24:36 +00:00
|
|
|
from math import floor
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
from defusedxml import ElementTree
|
|
|
|
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
'''
|
|
|
|
private HashMap<String, String> buildDownloadUrisParams(UpdatePackageInfo updatePackageInfo) {
|
|
|
|
FotaLog.m28v(TAG, "doAfterCheck");
|
|
|
|
String salt = FotaUtil.salt();
|
|
|
|
HashMap linkedHashMap = new LinkedHashMap();
|
|
|
|
linkedHashMap.put("id", this.internalBuilder.getParam("id"));
|
|
|
|
linkedHashMap.put("salt", salt);
|
|
|
|
linkedHashMap.put("curef", updatePackageInfo.mCuref);
|
|
|
|
linkedHashMap.put("fv", updatePackageInfo.mFv);
|
|
|
|
linkedHashMap.put("tv", updatePackageInfo.mTv);
|
|
|
|
linkedHashMap.put("type", "Firmware");
|
|
|
|
linkedHashMap.put("fw_id", updatePackageInfo.mFirmwareId);
|
|
|
|
linkedHashMap.put("mode", "2");
|
|
|
|
linkedHashMap.put("vk", generateVk2((LinkedHashMap) linkedHashMap.clone()));
|
|
|
|
linkedHashMap.put("cltp", "10");
|
|
|
|
linkedHashMap.put("cktp", this.internalBuilder.getParam("cktp"));
|
|
|
|
linkedHashMap.put("rtd", this.internalBuilder.getParam("rtd"));
|
|
|
|
linkedHashMap.put("chnl", this.internalBuilder.getParam("chnl"));
|
|
|
|
return linkedHashMap;
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
2018-02-05 15:52:06 +00:00
|
|
|
VDKEY_B64Z = b"eJwdjwEOwDAIAr8kKFr//7HhmqXp8AIIDrYAgg8byiUXrwRJRXja+d6iNxu0AhUooDCN9rd6rDLxmGIakUVWo3IGCTRWqCAt6X4jGEIUAxgN0eYWnp+LkpHQAg/PsO90ELsy0Npm/n2HbtPndFgGEV31R9OmT4O4nrddjc3Qt6nWscx7e+WRHq5UnOudtjw5skuV09pFhvmqnOEIs4ljPeel1wfLYUF4\n"
|
|
|
|
|
2018-02-06 18:27:36 +00:00
|
|
|
|
2018-02-05 15:52:06 +00:00
|
|
|
def get_salt():
|
|
|
|
"""Generate cryptographic salt."""
|
|
|
|
millis = floor(time.time() * 1000)
|
|
|
|
tail = "{:06d}".format(random.randint(0, 999999))
|
|
|
|
return "{}{}".format(str(millis), tail)
|
|
|
|
|
2018-02-06 18:27:36 +00:00
|
|
|
|
2018-02-05 15:52:06 +00:00
|
|
|
def get_vk2(params_dict, cltp):
|
|
|
|
"""Generate salted hash of API parameters."""
|
|
|
|
params_dict["cltp"] = cltp
|
|
|
|
query = ""
|
|
|
|
for key, val in params_dict.items():
|
|
|
|
if query:
|
|
|
|
query += "&"
|
|
|
|
query += key + "=" + str(val)
|
|
|
|
vdk = zlib.decompress(binascii.a2b_base64(VDKEY_B64Z))
|
|
|
|
query += vdk.decode("utf-8")
|
|
|
|
engine = hashlib.sha1()
|
|
|
|
engine.update(bytes(query, "utf-8"))
|
|
|
|
hexhash = engine.hexdigest()
|
|
|
|
return hexhash
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-06 18:27:36 +00:00
|
|
|
|
2018-02-03 12:46:17 +00:00
|
|
|
class TclRequestMixin:
|
2018-02-03 21:25:26 +00:00
|
|
|
"""A mixin component for TCL's download request API."""
|
2018-02-03 00:36:08 +00:00
|
|
|
|
2018-02-06 18:27:36 +00:00
|
|
|
def prep_request(self, curef, fvver, tvver, fw_id):
|
|
|
|
"""Prepare URL and device parameters for download request."""
|
2018-02-03 00:36:08 +00:00
|
|
|
url = "https://" + self.g2master + "/download_request.php"
|
|
|
|
params = OrderedDict()
|
2018-02-03 20:24:36 +00:00
|
|
|
params["id"] = self.serid
|
2018-02-05 15:52:06 +00:00
|
|
|
params["salt"] = get_salt()
|
2018-02-03 00:36:08 +00:00
|
|
|
params["curef"] = curef
|
2018-02-03 20:24:36 +00:00
|
|
|
params["fv"] = fvver
|
|
|
|
params["tv"] = tvver
|
|
|
|
params["type"] = self.ftype
|
2018-02-03 00:36:08 +00:00
|
|
|
params["fw_id"] = fw_id
|
2018-02-03 20:24:36 +00:00
|
|
|
params["mode"] = self.mode.value
|
2018-02-05 15:52:06 +00:00
|
|
|
params["vk"] = get_vk2(params, self.cltp.value)
|
2018-02-03 20:24:36 +00:00
|
|
|
params["cltp"] = self.cltp.value
|
|
|
|
params["cktp"] = self.cktp.value
|
|
|
|
params["rtd"] = self.rtd.value
|
2018-02-03 00:36:08 +00:00
|
|
|
if self.mode == self.MODE.FULL:
|
2018-02-03 20:24:36 +00:00
|
|
|
params["foot"] = 1
|
|
|
|
params["chnl"] = self.chnl.value
|
2018-02-06 18:27:36 +00:00
|
|
|
return url, params
|
2018-02-03 00:36:08 +00:00
|
|
|
|
2018-02-06 18:27:36 +00:00
|
|
|
def do_request(self, curef, fvver, tvver, fw_id):
|
|
|
|
"""Perform download request with given parameters."""
|
|
|
|
url, params = self.prep_request(curef, fvver, tvver, fw_id)
|
2018-02-03 20:24:36 +00:00
|
|
|
# print(repr(dict(params)))
|
2018-02-03 00:36:08 +00:00
|
|
|
req = self.sess.post(url, data=params)
|
|
|
|
if req.status_code == 200:
|
2018-02-03 20:24:36 +00:00
|
|
|
req.encoding = "utf-8" # Force encoding as server doesn't give one
|
2018-02-03 00:36:08 +00:00
|
|
|
self.write_dump(req.text)
|
|
|
|
return req.text
|
|
|
|
else:
|
|
|
|
print("REQUEST: " + repr(req))
|
|
|
|
print(repr(req.headers))
|
|
|
|
print(repr(req.text))
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_request(xmlstr):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Parse output of ``do_request``."""
|
2018-02-03 00:36:08 +00:00
|
|
|
root = ElementTree.fromstring(xmlstr)
|
|
|
|
file = root.find("FILE_LIST").find("FILE")
|
2018-02-03 20:24:36 +00:00
|
|
|
fileid = file.find("FILE_ID").text
|
2018-02-03 00:36:08 +00:00
|
|
|
fileurl = file.find("DOWNLOAD_URL").text
|
|
|
|
s3_fileurl_node = file.find("S3_DOWNLOAD_URL")
|
|
|
|
s3_fileurl = ""
|
|
|
|
if s3_fileurl_node:
|
|
|
|
s3_fileurl = s3_fileurl_node.text
|
|
|
|
slave_list = root.find("SLAVE_LIST").findall("SLAVE")
|
|
|
|
enc_list = root.find("SLAVE_LIST").findall("ENCRYPT_SLAVE")
|
|
|
|
s3_slave_list = root.find("SLAVE_LIST").findall("S3_SLAVE")
|
|
|
|
slaves = [s.text for s in slave_list]
|
|
|
|
encslaves = [s.text for s in enc_list]
|
|
|
|
s3_slaves = [s.text for s in s3_slave_list]
|
|
|
|
return fileid, fileurl, slaves, encslaves, s3_fileurl, s3_slaves
|