forked from carpetsage/egg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
400 lines (380 loc) · 10.3 KB
/
index.ts
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
import { sha256 } from "js-sha256";
import { ei } from "../proto";
import { decodeMessage } from "./decode";
import { encodeMessage } from "./encode";
import {
APP_BUILD,
APP_VERSION,
CLIENT_VERSION,
PLATFORM,
PLATFORM_STRING,
} from "./version";
export * from "./decode";
export * from "./encode";
export * from "./utils";
export * from "./version";
const API_ROOT = import.meta.env.DEV && import.meta.env.VITE_APP_MOCK
? "/api"
: "https://egg-forwarder.carpet.workers.dev/?url=https://ctx-dot-auxbrainhome.appspot.com";
const CONFIG_GIST_URL =
"https://gist.githubusercontent.com/carpetsage/373992bc6c5e00f8abd39dfb752845c0/raw/config.json";
const TIMEOUT = 8000;
// A valid userId donated by a volunteer.
const defaultUserId = atob("RUk2MjkxOTQwOTY4MjM1MDA4");
/**
* Makes an API request.
* @param endpoint - Path of API endpoint, e.g. /ei/coop_status.
* @param encodedPayload - base64-encoded request payload.
* @returns base64-encoded response payload.
* @throws Throws an error on network failure (including timeout) or non-2XX response.
*/
export async function request(
endpoint: string,
encodedPayload: string,
): Promise<string> {
const controller = new AbortController();
setTimeout(() => controller.abort(), TIMEOUT);
const url = API_ROOT + endpoint;
try {
const resp = await fetch(url, {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `data=${encodedPayload}`,
signal: controller.signal,
});
const text = await resp.text();
if (resp.status < 200 || resp.status >= 300) {
throw new Error(`HTTP ${resp.status}: ${text}`);
}
return text;
} catch (e) {
if (e instanceof Error && e.name === "AbortError") {
throw new Error(
`POST ${url} data=${encodedPayload}: timeout after ${TIMEOUT}ms.`,
);
} else if (e instanceof TypeError) {
throw new TypeError(
`POST ${url} data=${encodedPayload}: ${e} ` +
`(please check any ad/content blocking solution you might be using, e.g. uBlock, Brave, Pi-hole, NextDNS, etc.)`,
);
} else {
throw new Error(`POST ${url} data=${encodedPayload}: ${e}`);
}
}
}
/**
* @param userId
* @returns
* @throws
*/
export async function requestContractsArchive(
userId: string,
): Promise<ei.IContractsArchive> {
userId = processUserId(userId);
const requestPayload = basicRequestInfo(userId);
const encodedRequestPayload = encodeMessage(
ei.BasicRequestInfo,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei_ctx/get_contracts_archive",
encodedRequestPayload,
);
return decodeMessage(
ei.ContractsArchive,
encodedResponsePayload,
true,
) as ei.IContractsArchive;
}
export async function requestShellShowcase(
userId?: string,
): Promise<ei.IShellShowcaseListingSet> {
userId = userId ?? defaultUserId;
const requestPayload = basicRequestInfo(userId);
const encodedRequestPayload = encodeMessage(
ei.BasicRequestInfo,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/get_shell_showcase",
encodedRequestPayload,
);
return decodeMessage(
ei.ShellShowcaseListingSet,
encodedResponsePayload,
false,
) as ei.IShellShowcaseListingSet;
}
export async function joinCoopRequest(
contractId: string,
coopCode: string,
userId?: string,
): Promise<ei.IJoinCoopResponse> {
userId = userId ?? defaultUserId;
const requestPayload: ei.IJoinCoopRequest = {
rinfo: basicRequestInfo(userId),
contractIdentifier: contractId,
coopIdentifier: coopCode,
userId,
clientVersion: CLIENT_VERSION,
};
const encodedRequestPayload = encodeMessage(
ei.ContractCoopStatusRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/coop_status_basic",
encodedRequestPayload,
);
return decodeMessage(
ei.JoinCoopResponse,
encodedResponsePayload,
true,
) as ei.IJoinCoopResponse;
}
export async function requestCoopStatusBasic(
contractId: string,
coopCode: string,
userId?: string,
): Promise<ei.IJoinCoopResponse> {
userId = userId ?? defaultUserId;
const requestPayload: ei.IContractCoopStatusRequest = {
rinfo: basicRequestInfo(userId),
contractIdentifier: contractId,
coopIdentifier: coopCode,
userId,
clientVersion: CLIENT_VERSION,
};
const encodedRequestPayload = encodeMessage(
ei.ContractCoopStatusRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/coop_status_basic",
encodedRequestPayload,
);
return decodeMessage(
ei.JoinCoopResponse,
encodedResponsePayload,
true,
) as ei.IJoinCoopResponse;
}
/**
* Pulls ConfigResponse json from gist - proto for ConfigResponse is broken in some
* way I don't understand and have spent too long trying to fix
* @param [userId]
* @returns
* @throws
*/
export async function requestConfig(
_userid?: string,
): Promise<ei.IConfigResponse> {
try {
const resp = await fetch(CONFIG_GIST_URL);
const text = await resp.text();
const config = JSON.parse(text);
return ei.ConfigResponse.fromObject(config) as ei.ConfigResponse;
} catch (e) {
throw new Error(`Error fetching config from: ${CONFIG_GIST_URL} : ${e}`);
}
}
/**
* @param [userId]
* @returns
* @throws
*/
export async function requestPeriodicals(
userId?: string,
): Promise<ei.IPeriodicalsResponse> {
// A valid userId is required for a complete response.
userId = userId ?? defaultUserId;
const requestPayload: ei.IGetPeriodicalsRequest = {
rinfo: basicRequestInfo(userId),
};
const encodedRequestPayload = encodeMessage(
ei.GetPeriodicalsRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/get_periodicals",
encodedRequestPayload,
);
return decodeMessage(
ei.PeriodicalsResponse,
encodedResponsePayload,
true,
) as ei.IPeriodicalsResponse;
}
/**
* @param userId
* @returns
* @throws
*/
export async function requestFirstContact(
userId: string,
): Promise<ei.IEggIncFirstContactResponse> {
userId = processUserId(userId);
const requestPayload: ei.IEggIncFirstContactRequest = {
rinfo: basicRequestInfo(""),
eiUserId: userId,
deviceId: "wasmegg", // This is actually bot_name for /ei/bot_first_contact, operating on an honor system.
clientVersion: CLIENT_VERSION,
platform: PLATFORM,
};
const encodedRequestPayload = encodeMessage(
ei.EggIncFirstContactRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/bot_first_contact",
encodedRequestPayload,
);
return decodeMessage(
ei.EggIncFirstContactResponse,
encodedResponsePayload,
false,
) as ei.IEggIncFirstContactResponse;
}
/**
* @param contractId
* @param coopCode
* @param [userId]
* @returns
* @throws
*/
export async function requestCoopStatus(
contractId: string,
coopCode: string,
userId?: string,
): Promise<ei.IContractCoopStatusResponse> {
// A valid userId is now required.
userId = userId ?? defaultUserId;
const requestPayload: ei.IContractCoopStatusRequest = {
rinfo: basicRequestInfo(userId),
contractIdentifier: contractId,
coopIdentifier: coopCode,
userId,
clientVersion: CLIENT_VERSION,
};
const encodedRequestPayload = encodeMessage(
ei.ContractCoopStatusRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/coop_status",
encodedRequestPayload,
);
const status = decodeMessage(
ei.ContractCoopStatusResponse,
encodedResponsePayload,
true,
) as ei.IContractCoopStatusResponse;
if (!status.localTimestamp) {
status.localTimestamp = Date.now() / 1000;
}
return status;
}
/**
* @param contractId
* @param coopCode
* @param league - 0 for elite, 1 for standard.
* @param grade - 0 for undefined, 1-5 for c,b,a,aa,aaa
* @returns
* @throws
*/
export async function requestQueryCoop(
contractId: string,
coopCode: string,
league: number | undefined,
grade: number | undefined,
userId?: string,
): Promise<ei.IQueryCoopResponse> {
userId = userId ?? defaultUserId;
const requestPayload: ei.IQueryCoopRequest = {
rinfo: basicRequestInfo(userId),
contractIdentifier: contractId,
coopIdentifier: coopCode,
grade: ei.Contract.PlayerGrade.GRADE_A,
};
const encodedRequestPayload = encodeMessage(
ei.QueryCoopRequest,
requestPayload,
);
const encodedResponsePayload = await request(
"/ei/query_coop",
encodedRequestPayload,
);
return decodeMessage(
ei.QueryCoopResponse,
encodedResponsePayload,
false,
) as ei.IQueryCoopResponse;
}
/**
* @param userId
* @param missionId
* @returns
* @throws
*/
export async function requestAfxCompleteMission(
userId: string,
missionId: string,
): Promise<ei.ICompleteMissionResponse> {
userId = processUserId(userId);
return decodeCompleteMissionResponse(
await requestAfxCompleteMissionRaw(userId, missionId),
);
}
export async function requestAfxCompleteMissionRaw(
userId: string,
missionId: string,
): Promise<string> {
userId = processUserId(userId);
const requestPayload: ei.IMissionRequest = {
eiUserId: userId,
info: {
identifier: missionId,
},
rinfo: basicRequestInfo(userId),
};
const encodedRequestPayload = encodeMessage(
ei.MissionRequest,
requestPayload,
);
return await request("/ei_afx/complete_mission", encodedRequestPayload);
}
export function decodeCompleteMissionResponse(
payload: string,
): ei.ICompleteMissionResponse {
return decodeMessage(
ei.CompleteMissionResponse,
payload,
true,
) as ei.ICompleteMissionResponse;
}
export function basicRequestInfo(userId: string): ei.IBasicRequestInfo {
return {
eiUserId: userId,
clientVersion: CLIENT_VERSION,
version: APP_VERSION,
build: APP_BUILD,
platform: PLATFORM_STRING,
};
}
const userIdSha256Blacklist = [
"bba75a6d240f86d6a43d76e8e231d7b5f9a83c3078b2c7998290aad1660a50f9",
"773b99e5d2076c6597655cca7b37124061822eff9b5b4b0f53f985eaa8476f5b",
];
// Enforces a blacklist, but allow 'mk2!EI...' as the super user bypass.
function processUserId(userId: string): string {
if (userId.startsWith("mk2!")) {
return userId.slice(4);
}
if (userIdSha256Blacklist.includes(sha256(userId))) {
throw new Error(`${userId}: this ID has been blacklisted`);
}
return userId;
}