算法暴露接口(xhs、dy、ks、wx、hnw)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.4 KiB

7 months ago
  1. from hashlib import md5, sha1, sha384
  2. import math, random, time,requests
  3. def MD5(s):
  4. m = md5()
  5. m.update(str.encode(s, encoding="utf-8"))
  6. return m.hexdigest()
  7. def SHA1(s):
  8. sha = sha1()
  9. sha.update(str.encode(s, encoding="utf-8"))
  10. return sha.hexdigest()
  11. def SHA384(s):
  12. sha = sha384()
  13. sha.update(str.encode(s, encoding="utf-8"))
  14. return sha.hexdigest()
  15. def I(nonce="03423d9c06bd4bbb45b5fb9059a9ed4f"):
  16. return MD5(nonce)
  17. def j(timeStamp):
  18. return SHA1(timeStamp)
  19. def P(nonce, client_id):
  20. return MD5(nonce + client_id)
  21. def S(time):
  22. s = "EOi^0N5sWWHhkrF2A0gekY9U20BgnAcr"
  23. res = SHA1(s + str(time))
  24. return res[len(res)-16:len(res) - 1]
  25. def o(e, f):
  26. b = e
  27. t = f
  28. n = ""
  29. r1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  30. while b:
  31. o = b % 36
  32. l = b / 36
  33. n = r1[o] + n
  34. b = round(math.floor(l))
  35. return ("0000000000000000" + n)[-t:]
  36. def get_trace_id(e):
  37. return o(e, 9) + o(random.randint(0, 78364164095), 7)
  38. def get_uuid(t):
  39. e = int(time.time() * 1000)
  40. # e = 1700013194061
  41. template = "xxxxxxxxxxxxxyxxxxyxxxxxxxxxxxxx" if t else "xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxx"
  42. uuid = ""
  43. i = -1
  44. uuids = []
  45. while i < len(template) - 1:
  46. i += 1
  47. if template[i] == "-":
  48. uuids.append(uuid)
  49. uuid = ""
  50. continue
  51. elif template[i] == "4":
  52. uuid += template[i]
  53. continue
  54. n = (e + 16 * random.random()) % 16
  55. e = e // 16
  56. char = format(int(n) if template[i] == 'x' else 3 & int(n) | 8, "x")
  57. uuid += char
  58. uuids.append(uuid)
  59. return "-".join(uuids)
  60. def get_header_nonce():
  61. return get_uuid(True)
  62. def server(key):
  63. url = f"http://127.0.0.1:3000/hn/get_signature?nonce={key}"
  64. res = requests.get(url)
  65. return res.text
  66. def build_headers():
  67. times = int(time.time() * 1000)
  68. nonce = get_header_nonce()
  69. sid = f"S_{get_trace_id(times - 300)}"
  70. traceid = get_trace_id(times)
  71. client_id = get_uuid(False)
  72. s = S(times)
  73. M = f'{I(nonce)}!{j(str(times))}!{P(nonce, client_id)}!{server(s)}'
  74. h = {
  75. "x-b3-traceid": traceid,
  76. "x-client-id": client_id,
  77. "x-client-nonce": nonce,
  78. "x-client-sign": SHA384(M),
  79. "x-client-time": str(times),
  80. "x-client-sid": sid
  81. }
  82. # print(h)
  83. return h
  84. if __name__ == '__main__':
  85. build_headers()