-
Notifications
You must be signed in to change notification settings - Fork 7
/
majsoul.js
436 lines (427 loc) · 13.4 KB
/
majsoul.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
const protobuf = require("protobufjs");
const assert = require("assert");
const WebSocket = require("ws");
const rp = require("request-promise");
const uuidv4 = require("uuid/v4");
const fs = require("fs");
const p = require("path");
const crypto = require("crypto");
const { URL_BASE, ACCESS_TOKEN, PREFERRED_SERVER, OAUTH_TYPE } = require("./env");
const USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36";
const HEADERS = {
"User-Agent": USER_AGENT,
"If-Modified-Since": "0",
Referer: URL_BASE,
"sec-ch-ua": '"Chromium";v="100", "Google Chrome";v="100"',
"sec-ch-ua-platform": "Windows",
};
/**
* * Shuffles array in place.
* * @param {Array} a items An array containing the items.
* */
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
class MajsoulProtoCodec {
constructor(pbDef, version) {
this._pb = protobuf.Root.fromJSON(pbDef);
this._index = 1;
this._wrapper = this._pb.nested.lq.Wrapper;
this._inflightRequests = {};
this.version = version;
this.rawDefinition = pbDef;
}
lookupMethod(path) {
if (typeof path === "string") {
path = path.split(".");
}
if (0 === path.length) {
return null;
}
const service = this._pb.lookupService(path.slice(0, -1));
if (!service) {
return null;
}
const name = path[path.length - 1];
return service.methods[name];
}
/**
* @param {Buffer} buf
*/
decodeMessage(buf) {
const { REQUEST, RESPONSE } = MajsoulProtoCodec;
const type = buf[0];
assert([REQUEST, RESPONSE].includes(type));
const reqIndex = buf[1] | (buf[2] << 8);
const msg = this._wrapper.decode(buf.slice(3));
let typeObj, methodName;
if (type === REQUEST) {
methodName = msg.name;
const methodObj = this.lookupMethod(msg.name);
const typeName = methodObj.requestType;
typeObj = methodObj.parent.parent.lookupType(typeName);
} else {
({ typeObj, methodName } = this._inflightRequests[reqIndex] || {});
if (!typeObj) {
throw new Error(`Unknown request ${reqIndex}`);
}
delete this._inflightRequests[reqIndex];
}
return {
type,
reqIndex,
methodName,
payload: typeObj.decode(msg.data),
};
}
decodeDataMessage(buf, typeName) {
const msg = this._wrapper.decode(buf);
const typeObj = this._pb.lookupType(typeName || msg.name);
return {
dataType: msg.name,
payload: typeObj.decode(msg.data),
};
}
encodeRequest({ methodName, payload }) {
const currentIndex = this._index++;
const methodObj = this.lookupMethod(methodName);
const requestType = methodObj.parent.parent.lookupType(methodObj.requestType);
const responseType = methodObj.parent.parent.lookupType(methodObj.responseType);
const msg = this._wrapper
.encode({
name: methodName,
data: requestType.encode(payload).finish(),
})
.finish();
this._inflightRequests[currentIndex] = {
methodName,
typeObj: responseType,
};
return Buffer.concat([Buffer.from([MajsoulProtoCodec.REQUEST, currentIndex & 0xff, currentIndex >> 8]), msg]);
}
}
Object.assign(MajsoulProtoCodec, {
REQUEST: 2,
RESPONSE: 3,
});
class MajsoulConnection {
constructor(server, codec, onConnect, timeout = 15000) {
if (!Array.isArray(server)) {
server = [server];
}
this._servers = server;
this._timeout = timeout;
this._pendingMessages = [];
this._codec = codec;
this._onConnect = onConnect;
this.reconnect();
}
reconnect() {
this._ready = false;
if (this._socket) {
this._socket.terminate();
}
this._createWaiter();
shuffle(this._servers);
const server = this._servers[0];
console.log("Connecting to " + server);
let agent = undefined;
if (process.env.http_proxy) {
console.log(`Using proxy ${process.env.http_proxy}`);
const url = require("url");
const HttpsProxyAgent = require("https-proxy-agent");
agent = new HttpsProxyAgent(url.parse(process.env.http_proxy));
}
this._socket = new WebSocket(server, {
agent,
headers: HEADERS,
insecureHTTPParser: true,
});
this._socket.on("message", (data) => {
this._pendingMessages.push(data);
this._waiterResolve();
});
this._socket.on("unexpected-response", (_, res) => {
console.log("MajsoulConnection: Unexpected response:", res.statusCode);
try {
this._socket.terminate();
} catch (e) {}
this._waiterResolve();
if (res.statusCode >= 500) {
this.reconnect();
}
});
this._socket.on("open", () => {
this._waiterResolve();
this._pendingMessages = [];
this._onConnect(this)
.then(() => {
this._ready = true;
this._waiterResolve();
})
.catch((e) => {
console.error("Error in onOpen:", e);
this._socket.terminate();
this._socket = null;
this._waiterResolve();
setTimeout(() => this._waiterResolve(), 100);
});
});
}
async waitForReady() {
while (!this._ready) {
if (
!this._socket ||
this._socket.readyState === WebSocket.CLOSED ||
this._socket.readyState === WebSocket.CLOSING
) {
console.log("WebSocket closed before successful connection");
throw new Error("WebSocket closed before successful connection");
}
this._createWaiter();
await this._wait();
}
}
_createWaiter() {
if (this._waiter && !this._waiter._resolved) {
return;
}
this._waiter = new Promise((resolve) => {
var self = this;
this._waiterResolve = function () {
resolve();
self._waiter._resolved = true;
};
});
}
async _wait() {
try {
await Promise.race([
this._waiter,
new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), this._timeout)),
]);
} catch (e) {
if (e.message !== "timeout") {
throw e;
}
throw new Error("timeout");
}
}
close() {
this._socket.terminate();
this._pendingMessages.push(undefined);
this._waiterResolve();
}
async readMessage() {
while (!this._pendingMessages.length) {
if (!this._socket || this._socket.readyState === WebSocket.CLOSED) {
return undefined;
}
await this._wait();
this._createWaiter();
}
return this._pendingMessages.shift();
}
async rpcCall(methodName, payload) {
if (!this._socket) {
throw new Error("Connection is broken");
}
if (this._socket.readyState === WebSocket.CONNECTING) {
await this._wait();
}
if (this._socket.readyState !== WebSocket.OPEN) {
this._pendingMessages = [];
throw new Error("Connection is not opened");
}
const req = this._codec.encodeRequest({ methodName, payload });
this._socket.send(req);
const resp = await this.readMessage();
return this._codec.decodeMessage(resp).payload;
}
}
const cookiejar = rp.jar();
async function getRes(path, bustCache) {
let url = `${URL_BASE}${path}`;
const cacheHash = crypto.createHash("sha256").update(url).digest("hex");
if (bustCache) {
url += "?randv=" + Math.random().toString().slice(2);
}
const cacheDir = p.join(__dirname, ".cache");
const cacheFile = p.join(cacheDir, cacheHash);
const result = await rp({
uri: url,
json: true,
timeout: 2500,
jar: cookiejar,
headers: HEADERS,
}).catch((e) => {
try {
console.log(`Using cache for ${path} (${cacheHash})`);
return JSON.parse(fs.readFileSync(cacheFile, { encoding: "utf8" }));
} catch (e) {}
return Promise.reject(e);
});
if (result === undefined) {
throw new Error("Failed to get resource " + path);
}
fs.mkdirSync(cacheDir, { recursive: true });
fs.writeFileSync(p.join(cacheDir, cacheHash), JSON.stringify(result));
return result;
}
async function fetchLatestDataDefinition() {
const versionInfo = await getRes("version.json", true);
const resInfo = await getRes(`resversion${versionInfo.version}.json`);
const pbVersion = resInfo.res["res/proto/liqi.json"].prefix;
const pbDef = await getRes(`${pbVersion}/res/proto/liqi.json`);
return {
version: pbVersion,
dataDefinition: pbDef,
};
}
async function createMajsoulConnection(accessToken = ACCESS_TOKEN, preferredServer = PREFERRED_SERVER) {
let serverListUrl = process.env.SERVER_LIST_URL;
const wsScheme = process.env.WS_SCHEME || "wss";
const versionInfo = await getRes("version.json", true);
const resInfo = await getRes(`resversion${versionInfo.version}.json`);
const pbVersion = resInfo.res["res/proto/liqi.json"].prefix;
const pbDef = await getRes(`${pbVersion}/res/proto/liqi.json`);
const config = await getRes(`${resInfo.res["config.json"].prefix}/config.json`);
const ipDef = config.ip.filter((x) => x.name === "player")[0];
const triedListUrl = [];
let serverList = null;
let numTries = 0;
let lastError = null;
while (true) {
numTries++;
if (numTries > 10) {
throw lastError;
}
try {
if (!serverListUrl) {
preferredServer = shuffle((preferredServer || "").split(","))[0];
serverListUrl = ipDef.region_urls[preferredServer] || ipDef.region_urls.mainland;
if (!serverListUrl) {
const allServerListUrls = shuffle(
ipDef.region_urls.length ? ipDef.region_urls : Object.values(ipDef.region_urls)
);
if (
allServerListUrls.length > 1 &&
(allServerListUrls[0].url || allServerListUrls[0]) == triedListUrl[triedListUrl.length - 1]
) {
allServerListUrls.shift();
}
serverListUrl = allServerListUrls[0];
if (serverListUrl.url) {
serverListUrl = serverListUrl.url;
}
assert(typeof serverListUrl === "string");
triedListUrl.push(serverListUrl);
}
serverListUrl += "?service=ws-gateway&protocol=ws&ssl=true&rv=" + Math.random().toString().slice(2);
console.log(serverListUrl);
}
serverList = await rp({
uri: serverListUrl,
json: true,
timeout: 2500,
jar: cookiejar,
headers: HEADERS,
});
// console.log(serverList);
if (serverList.maintenance) {
console.log("Maintenance in progress");
await new Promise((resolve) => setTimeout(resolve, 30000));
return;
}
break;
} catch (e) {
if (process.env.SERVER_LIST_URL) {
throw e;
}
// console.log(e);
lastError = e;
serverListUrl = null;
preferredServer = "";
await new Promise((resolve) => setTimeout(resolve, 1000 + numTries * 1000));
}
}
const proto = new MajsoulProtoCodec(pbDef, pbVersion);
const serverIndex = Math.floor(Math.random() * serverList.servers.length);
const type = parseInt(OAUTH_TYPE) || 0;
let server = serverList.servers[serverIndex];
if (server.indexOf("maj-soul") > -1) {
server += "/gateway";
}
let shouldRetry = false;
try {
const conn = new MajsoulConnection(`${wsScheme}://${server}`, proto, async (conn) => {
console.log("Connection established, sending heartbeat");
await conn.rpcCall(".lq.Lobby.heatbeat", { no_operation_counter: 0 });
await new Promise((resolve) => setTimeout(resolve, 100));
shouldRetry = false;
console.log(`Authenticating (${versionInfo.version})`);
conn.clientVersionString = "web-" + versionInfo.version.replace(/\.[a-z]+$/i, "");
if (type === 7) {
const [code, uid] = accessToken.split("-");
const resp = await conn.rpcCall(".lq.Lobby.oauth2Auth", {
type,
code,
uid,
client_version_string: conn.clientVersionString,
});
accessToken = resp.access_token;
}
// console.log(accessToken);
let resp = await conn.rpcCall(".lq.Lobby.oauth2Check", { type, access_token: accessToken });
// console.log(resp);
if (!resp.has_account) {
await new Promise((res) => setTimeout(res, 2000));
resp = await conn.rpcCall(".lq.Lobby.oauth2Check", { type, access_token: accessToken });
}
assert(resp.has_account);
resp = await conn.rpcCall(".lq.Lobby.oauth2Login", {
type,
access_token: accessToken,
reconnect: false,
device: {
platform: "pc",
hardware: "pc",
os: "windows",
os_version: "win10",
is_browser: true,
software: "Chrome",
sale_platform: "web",
},
random_key: uuidv4(),
client_version: { resource: versionInfo.version },
currency_platforms: [],
client_version_string: conn.clientVersionString,
});
// console.log(resp);
assert(resp.account_id);
console.log("Connection ready");
});
await conn.waitForReady();
return conn;
} catch (e) {
console.error(e);
if (!shouldRetry) {
console.error("Not retrying");
return Promise.reject(e);
}
return createMajsoulConnection(accessToken);
}
}
exports.MajsoulProtoCodec = MajsoulProtoCodec;
exports.MajsoulConnection = MajsoulConnection;
exports.createMajsoulConnection = createMajsoulConnection;
exports.fetchLatestDataDefinition = fetchLatestDataDefinition;
exports.getRes = getRes;