-
Notifications
You must be signed in to change notification settings - Fork 74
/
ds.py
60 lines (51 loc) · 1.85 KB
/
ds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Dynamic secret generation."""
import hashlib
import json
import random
import string
import time
import typing
from genshin import constants, types
__all__ = ["generate_cn_dynamic_secret", "generate_dynamic_secret", "get_ds_headers"]
def generate_dynamic_secret(salt: str = constants.DS_SALT[types.Region.OVERSEAS]) -> str:
"""Create a new overseas dynamic secret."""
t = int(time.time())
r = "".join(random.choices(string.ascii_letters, k=6))
h = hashlib.md5(f"salt={salt}&t={t}&r={r}".encode()).hexdigest()
return f"{t},{r},{h}"
def generate_cn_dynamic_secret(
body: typing.Any = None,
query: typing.Optional[typing.Mapping[str, typing.Any]] = None,
*,
salt: str = constants.DS_SALT[types.Region.CHINESE],
) -> str:
"""Create a new chinese dynamic secret."""
t = int(time.time())
r = random.randint(100001, 200000)
b = json.dumps(body) if body else ""
q = "&".join(f"{k}={v}" for k, v in sorted(query.items())) if query else ""
h = hashlib.md5(f"salt={salt}&t={t}&r={r}&b={b}&q={q}".encode()).hexdigest()
return f"{t},{r},{h}"
def get_ds_headers(
region: types.Region,
data: typing.Any = None,
params: typing.Optional[typing.Mapping[str, typing.Any]] = None,
lang: typing.Optional[str] = None,
) -> typing.Dict[str, typing.Any]:
"""Get ds http headers."""
if region == types.Region.OVERSEAS:
ds_headers = {
"x-rpc-app_version": "1.5.0",
"x-rpc-client_type": "5",
"x-rpc-language": lang,
"ds": generate_dynamic_secret(),
}
elif region == types.Region.CHINESE:
ds_headers = {
"x-rpc-app_version": "2.11.1",
"x-rpc-client_type": "5",
"ds": generate_cn_dynamic_secret(data, params),
}
else:
raise TypeError(f"{region!r} is not a valid region.")
return ds_headers