This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_common.py
200 lines (158 loc) · 5.22 KB
/
core_common.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import config
import random
import time
from lxml.builder import ElementMaker
from kbinxml import KBinXML
from utils.arc4 import EamuseARC4
from utils.lz77 import EamuseLZ77
def _add_val_as_str(elm, val):
new_val = str(val)
if elm is not None:
elm.text = new_val
else:
return new_val
def _add_bool_as_str(elm, val):
return _add_val_as_str(elm, 1 if val else 0)
def _add_list_as_str(elm, vals):
new_val = " ".join([str(val) for val in vals])
if elm is not None:
elm.text = new_val
elm.attrib["__count"] = str(len(vals))
else:
return new_val
E = ElementMaker(
typemap={
int: _add_val_as_str,
bool: _add_bool_as_str,
list: _add_list_as_str,
float: _add_val_as_str,
}
)
async def core_get_game_version_from_software_version(software_version):
_, model, dest, spec, rev, ext = software_version
ext = int(ext)
if model == "LDJ":
if ext >= 2023101800:
return 31
elif ext in range(2022101700, 2023101700):
return 30
elif ext in range(2021101300, 2022101500):
return 29
elif ext in range(2020102800, 2021100601):
return 28
elif ext in range(2019101600, 2020102201):
return 27
elif ext in range(2018110700, 2019100701):
return 26
elif ext in range(2017122100, 2018103101):
return 25
elif ext in range(2016102400, 2017082801):
return 24
elif ext in range(2015111100, 2016102001):
return 23
elif ext in range(2014091700, 2015080501):
return 22
elif ext in range(2013100200, 2014071601):
return 21
elif ext in range(2012091900, 2013090901):
return 20
elif model == "KDZ":
return 19
elif model == "JDZ":
return 18
elif model == "M32":
if ext >= 2022121400:
return 9
elif ext >= 2021042100:
return 8
elif ext >= 2019100200:
return 7
elif ext >= 2018072700:
return 6
elif ext >= 2017090600:
return 5
elif ext >= 2017011800:
return 4
elif ext in range(2015042100, 2016112302):
return 3
elif ext in range(2014021400, 2015032702):
return 2
elif ext in range(2013012400, 2014011601):
return 1
elif model == "MDX":
if ext >= 2019022600: # ???
return 19
elif model == "KFC":
if ext >= 2020090402: # ???
return 6
elif model == "REC":
return 1
else:
return 0
async def core_process_request(request):
cl = request.headers.get("Content-Length")
data = await request.body()
if not cl or not data:
return {}
if "X-Compress" in request.headers:
request.compress = request.headers.get("X-Compress")
else:
request.compress = None
if "X-Eamuse-Info" in request.headers:
xeamuseinfo = request.headers.get("X-Eamuse-Info")
key = bytes.fromhex(xeamuseinfo[2:].replace("-", ""))
xml_dec = EamuseARC4(key).decrypt(data[: int(cl)])
request.is_encrypted = True
else:
xml_dec = data[: int(cl)]
request.is_encrypted = False
if request.compress == "lz77":
xml_dec = EamuseLZ77.decode(xml_dec)
xml = KBinXML(xml_dec, convert_illegal_things=True)
root = xml.xml_doc
xml_text = xml.to_text()
request.is_binxml = KBinXML.is_binary_xml(xml_dec)
if config.verbose_log:
print()
print("\033[94mREQUEST\033[0m:")
print(xml_text)
model_parts = (root.attrib["model"], *root.attrib["model"].split(":"))
module = root[0].tag
method = root[0].attrib["method"] if "method" in root[0].attrib else None
command = root[0].attrib["command"] if "command" in root[0].attrib else None
game_version = await core_get_game_version_from_software_version(model_parts)
return {
"root": root,
"text": xml_text,
"module": module,
"method": method,
"command": command,
"model": model_parts[1],
"dest": model_parts[2],
"spec": model_parts[3],
"rev": model_parts[4],
"ext": model_parts[5],
"game_version": game_version,
}
async def core_prepare_response(request, xml):
binxml = KBinXML(xml)
if request.is_binxml:
xml_binary = binxml.to_binary()
else:
xml_binary = binxml.to_text().encode("utf-8") # TODO: Proper encoding
if config.verbose_log:
print("\033[91mRESPONSE\033[0m:")
print(binxml.to_text())
response_headers = {"User-Agent": "EAMUSE.Httpac/1.0"}
if request.is_encrypted:
xeamuseinfo = "1-%08x-%04x" % (int(time.time()), random.randint(0x0000, 0xFFFF))
response_headers["X-Eamuse-Info"] = xeamuseinfo
key = bytes.fromhex(xeamuseinfo[2:].replace("-", ""))
response = EamuseARC4(key).encrypt(xml_binary)
else:
response = bytes(xml_binary)
request.compress = None
# if request.compress == "lz77":
# response_headers["X-Compress"] = request.compress
# response = EamuseLZ77.encode(response)
return response, response_headers