from hashlib import md5, sha1, sha384 import math, random, time,requests def MD5(s): m = md5() m.update(str.encode(s, encoding="utf-8")) return m.hexdigest() def SHA1(s): sha = sha1() sha.update(str.encode(s, encoding="utf-8")) return sha.hexdigest() def SHA384(s): sha = sha384() sha.update(str.encode(s, encoding="utf-8")) return sha.hexdigest() def I(nonce="03423d9c06bd4bbb45b5fb9059a9ed4f"): return MD5(nonce) def j(timeStamp): return SHA1(timeStamp) def P(nonce, client_id): return MD5(nonce + client_id) def S(time): s = "EOi^0N5sWWHhkrF2A0gekY9U20BgnAcr" res = SHA1(s + str(time)) return res[len(res)-16:len(res) - 1] def o(e, f): b = e t = f n = "" r1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" while b: o = b % 36 l = b / 36 n = r1[o] + n b = round(math.floor(l)) return ("0000000000000000" + n)[-t:] def get_trace_id(e): return o(e, 9) + o(random.randint(0, 78364164095), 7) def get_uuid(t): e = int(time.time() * 1000) # e = 1700013194061 template = "xxxxxxxxxxxxxyxxxxyxxxxxxxxxxxxx" if t else "xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxx" uuid = "" i = -1 uuids = [] while i < len(template) - 1: i += 1 if template[i] == "-": uuids.append(uuid) uuid = "" continue elif template[i] == "4": uuid += template[i] continue n = (e + 16 * random.random()) % 16 e = e // 16 char = format(int(n) if template[i] == 'x' else 3 & int(n) | 8, "x") uuid += char uuids.append(uuid) return "-".join(uuids) def get_header_nonce(): return get_uuid(True) def server(key): url = f"http://127.0.0.1:3000/hn/get_signature?nonce={key}" res = requests.get(url) return res.text def build_headers(): times = int(time.time() * 1000) nonce = get_header_nonce() sid = f"S_{get_trace_id(times - 300)}" traceid = get_trace_id(times) client_id = get_uuid(False) s = S(times) M = f'{I(nonce)}!{j(str(times))}!{P(nonce, client_id)}!{server(s)}' h = { "x-b3-traceid": traceid, "x-client-id": client_id, "x-client-nonce": nonce, "x-client-sign": SHA384(M), "x-client-time": str(times), "x-client-sid": sid } # print(h) return h if __name__ == '__main__': build_headers()