-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathf.ts
372 lines (307 loc) · 10.8 KB
/
f.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
import process from 'node:process';
import { randomUUID } from 'node:crypto';
import { fetch, Headers } from 'undici';
import { defineResponse, ErrorResponse } from './util.js';
import createDebug from '../util/debug.js';
import { timeoutSignal } from '../util/misc.js';
import { getUserAgent } from '../util/useragent.js';
const debugFlapg = createDebug('nxapi:api:flapg');
const debugImink = createDebug('nxapi:api:imink');
const debugZncaApi = createDebug('nxapi:api:znca-api');
export abstract class ZncaApi {
constructor(
public useragent?: string
) {}
abstract genf(
token: string, hash_method: HashMethod,
user?: {na_id: string; coral_user_id?: string;},
): Promise<FResult>;
}
export enum HashMethod {
CORAL = 1,
WEB_SERVICE = 2,
}
//
// flapg
//
export async function flapg(
hash_method: HashMethod, token: string,
timestamp?: string | number, request_id?: string,
useragent?: string
) {
const { default: { coral_auth: { flapg: config } } } = await import('../common/remote-config.js');
if (!config) throw new Error('Remote configuration prevents flapg API use');
debugFlapg('Getting f parameter', {
hash_method, token, timestamp, request_id,
});
const req: FlapgApiRequest = {
hash_method: '' + hash_method as `${HashMethod}`,
token,
timestamp: typeof timestamp === 'number' ? '' + timestamp : undefined,
request_id,
};
const [signal, cancel] = timeoutSignal();
const response = await fetch('https://flapg.com/ika/api/login-main', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': getUserAgent(useragent),
},
body: JSON.stringify(req),
signal,
}).finally(cancel);
if (response.status !== 200) {
throw await ErrorResponse.fromResponse(response, '[flapg] Non-200 status code');
}
const data = await response.json() as FlapgApiResponse;
debugFlapg('Got f parameter', data);
return defineResponse(data, response);
}
/** @deprecated */
export enum FlapgIid {
/** Nintendo Switch Online app token */
NSO = 'nso',
/** Web service token */
APP = 'app',
}
export interface FlapgApiRequest {
hash_method: '1' | '2';
token: string;
timestamp?: string;
request_id?: string;
}
export type FlapgApiResponse = IminkFResponse;
export type FlapgApiError = IminkFError;
export class ZncaApiFlapg extends ZncaApi {
async genf(token: string, hash_method: HashMethod) {
const request_id = randomUUID();
const result = await flapg(hash_method, token, undefined, request_id, this.useragent);
return {
provider: 'flapg' as const,
hash_method, token, request_id,
timestamp: result.timestamp,
f: result.f,
result,
};
}
}
//
// imink
//
export async function iminkf(
hash_method: HashMethod, token: string,
timestamp?: number, request_id?: string,
user?: {na_id: string; coral_user_id?: string;},
useragent?: string,
) {
const { default: { coral_auth: { imink: config } } } = await import('../common/remote-config.js');
if (!config) throw new Error('Remote configuration prevents imink API use');
debugImink('Getting f parameter', {
hash_method, token, timestamp, request_id,
});
const req: IminkFRequest = {
hash_method,
token,
timestamp: typeof timestamp === 'number' ? '' + timestamp : undefined,
request_id,
...user,
};
const [signal, cancel] = timeoutSignal();
const response = await fetch('https://api.imink.app/f', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': getUserAgent(useragent),
},
body: JSON.stringify(req),
signal,
}).finally(cancel);
if (response.status !== 200) {
throw await ErrorResponse.fromResponse(response, '[imink] Non-200 status code');
}
const data = await response.json() as IminkFResponse | IminkFError;
if ('error' in data) {
throw new ErrorResponse('[imink] ' + data.reason, response, data);
}
debugImink('Got f parameter "%s"', data.f);
return defineResponse(data, response);
}
export interface IminkFRequest {
hash_method: 1 | 2 | '1' | '2';
token: string;
timestamp?: string | number;
request_id?: string;
}
export interface IminkFResponse {
f: string;
timestamp: number;
request_id: string;
}
export interface IminkFError {
reason: string;
error: true;
}
export class ZncaApiImink extends ZncaApi {
async genf(token: string, hash_method: HashMethod, user?: {na_id: string; coral_user_id?: string;}) {
const request_id = randomUUID();
const result = await iminkf(hash_method, token, undefined, request_id, user, this.useragent);
return {
provider: 'imink' as const,
hash_method, token, request_id,
timestamp: result.timestamp,
f: result.f,
user,
result,
};
}
}
//
// nxapi znca API server
//
export async function genf(
url: string, hash_method: HashMethod,
token: string, timestamp?: number, request_id?: string,
user?: {na_id: string; coral_user_id?: string;},
app?: {platform?: string; version?: string;},
useragent?: string,
) {
debugZncaApi('Getting f parameter', {
url, hash_method, token, timestamp, request_id, user,
znca_platform: app?.platform, znca_version: app?.version,
});
const req: AndroidZncaFRequest = {
hash_method: '' + hash_method as `${HashMethod}`,
token,
timestamp,
request_id,
...user,
};
const headers = new Headers({
'Content-Type': 'application/json',
'User-Agent': getUserAgent(useragent),
});
if (app?.platform) headers.append('X-znca-Platform', app.platform);
if (app?.version) headers.append('X-znca-Version', app.version);
const [signal, cancel] = timeoutSignal();
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(req),
signal,
}).finally(cancel);
if (response.status !== 200) {
throw await ErrorResponse.fromResponse(response, '[znca-api] Non-200 status code');
}
const data = await response.json() as AndroidZncaFResponse | AndroidZncaFError;
if ('error' in data) {
debugZncaApi('Error getting f parameter "%s"', data.error);
throw new ErrorResponse<AndroidZncaFError>('[znca-api] ' + (data.error_message ?? data.error), response, data);
}
debugZncaApi('Got f parameter', data, response.headers);
return defineResponse(data, response);
}
export interface AndroidZncaFRequest {
hash_method: '1' | '2';
token: string;
timestamp?: string | number;
request_id?: string;
}
export interface AndroidZncaFResponse {
f: string;
timestamp?: number;
request_id?: string;
warnings?: {error: string; error_message: string}[];
}
export interface AndroidZncaFError {
error: string;
error_message?: string;
errors?: {error: string; error_message: string}[];
warnings?: {error: string; error_message: string}[];
}
export class ZncaApiNxapi extends ZncaApi {
constructor(readonly url: string, readonly app?: {platform?: string; version?: string;}, useragent?: string) {
super(useragent);
}
async genf(token: string, hash_method: HashMethod, user?: {na_id: string; coral_user_id?: string}) {
const request_id = randomUUID();
const result = await genf(this.url + '/f', hash_method, token, undefined, request_id,
user, this.app, this.useragent);
return {
provider: 'nxapi' as const,
url: this.url + '/f',
hash_method, token, request_id,
timestamp: result.timestamp!, // will be included as not sent in request
f: result.f,
user,
result,
};
}
}
export async function f(token: string, hash_method: HashMethod | `${HashMethod}`, options?: ZncaApiOptions): Promise<FResult>;
export async function f(token: string, hash_method: HashMethod | `${HashMethod}`, useragent?: string): Promise<FResult>;
export async function f(token: string, hash_method: HashMethod | `${HashMethod}`, options?: ZncaApiOptions | string): Promise<FResult> {
if (typeof options === 'string') options = {useragent: options};
if (typeof hash_method === 'string') hash_method = parseInt(hash_method);
const provider = getPreferredZncaApiFromEnvironment(options) ?? await getDefaultZncaApi(options);
return provider.genf(token, hash_method, options?.user);
}
export type FResult = {
provider: string;
hash_method: HashMethod;
token: string;
timestamp: number;
request_id: string;
f: string;
user?: {na_id: string; coral_user_id?: string;};
result: unknown;
} & ({
provider: 'flapg';
result: FlapgApiResponse;
} | {
provider: 'imink';
result: IminkFResponse;
} | {
provider: 'nxapi';
url: string;
result: AndroidZncaFResponse;
});
interface ZncaApiOptions {
useragent?: string;
platform?: string;
version?: string;
user?: {na_id: string; coral_user_id?: string;};
}
export function getPreferredZncaApiFromEnvironment(options?: ZncaApiOptions): ZncaApi | null;
export function getPreferredZncaApiFromEnvironment(useragent?: string): ZncaApi | null;
export function getPreferredZncaApiFromEnvironment(options?: ZncaApiOptions | string): ZncaApi | null {
if (typeof options === 'string') options = {useragent: options};
if (process.env.NXAPI_ZNCA_API) {
if (process.env.NXAPI_ZNCA_API === 'flapg') {
return new ZncaApiFlapg(options?.useragent);
}
if (process.env.NXAPI_ZNCA_API === 'imink') {
return new ZncaApiImink(options?.useragent);
}
throw new Error('Unknown znca API provider');
}
if (process.env.ZNCA_API_URL) {
return new ZncaApiNxapi(process.env.ZNCA_API_URL, options, options?.useragent);
}
return null;
}
export async function getDefaultZncaApi(options?: ZncaApiOptions): Promise<ZncaApi>;
export async function getDefaultZncaApi(useragent?: string): Promise<ZncaApi>;
export async function getDefaultZncaApi(options?: ZncaApiOptions | string) {
if (typeof options === 'string') options = {useragent: options};
const { default: { coral_auth: { default: provider } } } = await import('../common/remote-config.js');
if (provider === 'flapg') {
return new ZncaApiFlapg(options?.useragent);
}
if (provider === 'imink') {
return new ZncaApiImink(options?.useragent);
}
if (provider[0] === 'nxapi') {
return new ZncaApiNxapi(provider[1], options, options?.useragent);
}
throw new Error('Invalid znca API provider');
}