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 checksum API."""
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
import json
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
from defusedxml import ElementTree
|
2018-02-06 00:03:25 +00:00
|
|
|
|
2018-02-05 15:38:15 +00:00
|
|
|
from . import credentials
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-06 00:03:25 +00:00
|
|
|
|
2018-02-03 12:46:17 +00:00
|
|
|
class TclChecksumMixin:
|
2018-02-03 21:25:26 +00:00
|
|
|
"""A mixin component for TCL's checksum API."""
|
2018-02-03 00:36:08 +00:00
|
|
|
def do_checksum(self, encslave, address, uri):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Perform checksum request with given parameters."""
|
2018-02-03 00:36:08 +00:00
|
|
|
url = "http://" + encslave + "/checksum.php"
|
2018-02-05 15:38:15 +00:00
|
|
|
params = credentials.get_creds2()
|
2018-02-03 00:36:08 +00:00
|
|
|
|
|
|
|
payload = {address: uri}
|
|
|
|
payload_json = json.dumps(payload)
|
2018-02-03 20:24:36 +00:00
|
|
|
params[b"address"] = bytes(payload_json, "utf-8")
|
2018-02-03 00:36:08 +00:00
|
|
|
|
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)
|
|
|
|
# <ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER> means ERROR (invalid ADDRESS!)
|
|
|
|
if "<ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER>" in req.text:
|
|
|
|
print("INVALID URI: {}".format(uri))
|
|
|
|
raise SystemExit
|
|
|
|
return req.text
|
|
|
|
else:
|
|
|
|
print("CHECKSUM: " + repr(req))
|
|
|
|
print(repr(req.headers))
|
|
|
|
print(repr(req.text))
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_checksum(xmlstr):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Parse output of ``do_checksum``."""
|
2018-02-03 00:36:08 +00:00
|
|
|
root = ElementTree.fromstring(xmlstr)
|
|
|
|
file = root.find("FILE_CHECKSUM_LIST").find("FILE")
|
|
|
|
file_addr = file.find("ADDRESS").text
|
|
|
|
sha1_enc_footer = file.find("ENCRYPT_FOOTER").text
|
|
|
|
sha1_footer = file.find("FOOTER").text
|
|
|
|
sha1_body = file.find("BODY").text
|
|
|
|
return file_addr, sha1_body, sha1_enc_footer, sha1_footer
|