PamGram/modules/apihelper/utility/helpers.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.5 KiB
Python
Raw Normal View History

2022-07-26 10:07:31 +00:00
import hashlib
import json
2022-07-26 10:07:31 +00:00
import random
import string
import time
import uuid
from typing import Mapping, Any, Optional
2022-07-26 10:07:31 +00:00
2022-12-10 12:37:43 +00:00
__all__ = ("get_device_id", "hex_digest", "get_ds", "get_recognize_server", "get_ua")
2022-07-26 10:07:31 +00:00
RECOGNIZE_SERVER = {
"1": "cn_gf01",
"2": "cn_gf01",
"5": "cn_qd01",
"6": "os_usa",
"7": "os_euro",
"8": "os_asia",
"9": "os_cht",
}
2022-11-14 07:35:09 +00:00
def get_device_id(name: str = ""):
return str(uuid.uuid3(uuid.NAMESPACE_URL, name))
2022-07-26 10:07:31 +00:00
2022-11-14 07:35:09 +00:00
def hex_digest(text):
_md5 = hashlib.md5() # nosec B303
_md5.update(text.encode())
return _md5.hexdigest()
def get_ds(ds_type: str = None, new_ds: bool = False, data: Any = None, params: Optional[Mapping[str, Any]] = None):
"""DS 算法
2022-11-23 01:11:29 +00:00
代码来自 https://github.com/y1ndan/genshinhelper
:param ds_type: 1:ios 2:android 4:pc web 5:mobile web
:param new_ds: 是否为DS2算法
:param data: 需要签名的Data
:param params: 需要签名的Params
:return:
"""
def new():
t = str(int(time.time()))
r = str(random.randint(100001, 200000)) # nosec
b = json.dumps(data) if data else ""
q = "&".join(f"{k}={v}" for k, v in sorted(params.items())) if params else ""
2022-11-14 07:35:09 +00:00
c = hex_digest(f"salt={salt}&t={t}&r={r}&b={b}&q={q}")
return f"{t},{r},{c}"
def old():
t = str(int(time.time()))
r = "".join(random.sample(string.ascii_lowercase + string.digits, 6))
2022-11-14 07:35:09 +00:00
c = hex_digest(f"salt={salt}&t={t}&r={r}")
return f"{t},{r},{c}"
2023-01-26 09:29:27 +00:00
app_version = "2.44.1"
client_type = "5"
2023-01-26 09:29:27 +00:00
salt = "IEIZiKYaput2OCKQprNuGsog1NZc1FkS"
ds = old()
2022-12-27 09:28:04 +00:00
if ds_type in {"android", "2"}:
client_type = "2"
2023-01-26 09:29:27 +00:00
salt = "dZAwGk4e9aC0MXXItkwnHamjA1x30IYw"
ds = old()
2022-12-27 09:28:04 +00:00
elif ds_type == "android_new":
client_type = "2"
salt = "t0qEgfub6cvueAPgR5m9aQWWVciEer7v"
ds = new()
if new_ds:
client_type = "5"
salt = "xV8v4Qu54lUKrEYFZkJhB8cuOh9Asafs"
ds = new()
return app_version, client_type, ds
2022-07-26 10:07:31 +00:00
def get_recognize_server(uid: int) -> str:
server = RECOGNIZE_SERVER.get(str(uid)[0])
if server:
return server
else:
raise TypeError(f"UID {uid} isn't associated with any recognize server")
def get_ua(device: str = "Paimon Build", version: str = "2.36.1"):
return (
f"Mozilla/5.0 (Linux; Android 12; {device}; wv) "
"AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36 "
2022-12-10 12:37:43 +00:00
f"{'miHoYoBBS/' + version if version else ''}"
)