1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-10 06:16:46 +00:00

Moved some things around (aka "cleanup").

This commit is contained in:
Markus Birth 2018-02-05 16:52:06 +01:00
parent 648e89f9be
commit feb2f72fe1
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 36 additions and 25 deletions

View File

@ -27,7 +27,7 @@ class FotaCheck(
dumpmgr.DumpMgrMixin dumpmgr.DumpMgrMixin
): ):
"""Main API handler class.""" """Main API handler class."""
VDKEY = b"eJwdjwEOwDAIAr8kKFr//7HhmqXp8AIIDrYAgg8byiUXrwRJRXja+d6iNxu0AhUooDCN9rd6rDLxmGIakUVWo3IGCTRWqCAt6X4jGEIUAxgN0eYWnp+LkpHQAg/PsO90ELsy0Npm/n2HbtPndFgGEV31R9OmT4O4nrddjc3Qt6nWscx7e+WRHq5UnOudtjw5skuV09pFhvmqnOEIs4ljPeel1wfLYUF4\n"
CKTP = default_enum("CKTP", ["AUTO", "MANUAL"]) CKTP = default_enum("CKTP", ["AUTO", "MANUAL"])
MODE = default_enum("MODE", {"OTA": 2, "FULL": 4}) MODE = default_enum("MODE", {"OTA": 2, "FULL": 4})
RTD = default_enum("RTD", ["UNROOTED", "ROOTED"]) RTD = default_enum("RTD", ["UNROOTED", "ROOTED"])

View File

@ -8,6 +8,9 @@
import errno import errno
import glob import glob
import os import os
import random
import time
from math import floor
from . import ansi from . import ansi
@ -18,9 +21,16 @@ class DumpMgrMixin:
"""Populate dump file name.""" """Populate dump file name."""
self.last_dump_filename = None self.last_dump_filename = None
@staticmethod
def get_timestamp_random():
"""Generate timestamp + random part to avoid collisions."""
millis = floor(time.time() * 1000)
tail = "{:06d}".format(random.randint(0, 999999))
return "{}_{}".format(str(millis), tail)
def write_dump(self, data): def write_dump(self, data):
"""Write dump to file.""" """Write dump to file."""
outfile = os.path.normpath("logs/{}.xml".format(self.get_salt())) outfile = os.path.normpath("logs/{}.xml".format(self.get_timestamp_random()))
if not os.path.exists(os.path.dirname(outfile)): if not os.path.exists(os.path.dirname(outfile)):
try: try:
os.makedirs(os.path.dirname(outfile)) os.makedirs(os.path.dirname(outfile))

View File

@ -38,17 +38,15 @@ from defusedxml import ElementTree
} }
''' '''
VDKEY_B64Z = b"eJwdjwEOwDAIAr8kKFr//7HhmqXp8AIIDrYAgg8byiUXrwRJRXja+d6iNxu0AhUooDCN9rd6rDLxmGIakUVWo3IGCTRWqCAt6X4jGEIUAxgN0eYWnp+LkpHQAg/PsO90ELsy0Npm/n2HbtPndFgGEV31R9OmT4O4nrddjc3Qt6nWscx7e+WRHq5UnOudtjw5skuV09pFhvmqnOEIs4ljPeel1wfLYUF4\n"
class TclRequestMixin: def get_salt():
"""A mixin component for TCL's download request API."""
@staticmethod
def get_salt():
"""Generate cryptographic salt.""" """Generate cryptographic salt."""
millis = floor(time.time() * 1000) millis = floor(time.time() * 1000)
tail = "{:06d}".format(random.randint(0, 999999)) tail = "{:06d}".format(random.randint(0, 999999))
return "{}{}".format(str(millis), tail) return "{}{}".format(str(millis), tail)
def get_vk2(self, params_dict, cltp): def get_vk2(params_dict, cltp):
"""Generate salted hash of API parameters.""" """Generate salted hash of API parameters."""
params_dict["cltp"] = cltp params_dict["cltp"] = cltp
query = "" query = ""
@ -56,26 +54,29 @@ class TclRequestMixin:
if query: if query:
query += "&" query += "&"
query += key + "=" + str(val) query += key + "=" + str(val)
vdk = zlib.decompress(binascii.a2b_base64(self.VDKEY)) vdk = zlib.decompress(binascii.a2b_base64(VDKEY_B64Z))
query += vdk.decode("utf-8") query += vdk.decode("utf-8")
engine = hashlib.sha1() engine = hashlib.sha1()
engine.update(bytes(query, "utf-8")) engine.update(bytes(query, "utf-8"))
hexhash = engine.hexdigest() hexhash = engine.hexdigest()
return hexhash return hexhash
class TclRequestMixin:
"""A mixin component for TCL's download request API."""
def do_request(self, curef, fvver, tvver, fw_id): def do_request(self, curef, fvver, tvver, fw_id):
"""Perform download request with given parameters.""" """Perform download request with given parameters."""
url = "https://" + self.g2master + "/download_request.php" url = "https://" + self.g2master + "/download_request.php"
params = OrderedDict() params = OrderedDict()
params["id"] = self.serid params["id"] = self.serid
params["salt"] = self.get_salt() params["salt"] = get_salt()
params["curef"] = curef params["curef"] = curef
params["fv"] = fvver params["fv"] = fvver
params["tv"] = tvver params["tv"] = tvver
params["type"] = self.ftype params["type"] = self.ftype
params["fw_id"] = fw_id params["fw_id"] = fw_id
params["mode"] = self.mode.value params["mode"] = self.mode.value
params["vk"] = self.get_vk2(params, self.cltp.value) params["vk"] = get_vk2(params, self.cltp.value)
params["cltp"] = self.cltp.value params["cltp"] = self.cltp.value
params["cktp"] = self.cktp.value params["cktp"] = self.cktp.value
params["rtd"] = self.rtd.value params["rtd"] = self.rtd.value