mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-10-22 17:44:36 -05:00
Update config URL and use config data
This commit is contained in:
@@ -1,26 +1,16 @@
|
||||
import createDebug from 'debug';
|
||||
import * as persist from 'node-persist';
|
||||
import { Response } from 'node-fetch';
|
||||
import { FlapgApiResponse, FResult } from '../../api/f.js';
|
||||
import { NintendoAccountSessionTokenJwtPayload, NintendoAccountToken, NintendoAccountUser } from '../../api/na.js';
|
||||
import { NintendoAccountSessionTokenJwtPayload } from '../../api/na.js';
|
||||
import { Jwt } from '../../util/jwt.js';
|
||||
import { AccountLogin, CoralErrorResponse } from '../../api/coral-types.js';
|
||||
import CoralApi, { ZNCA_CLIENT_ID } from '../../api/coral.js';
|
||||
import { CoralErrorResponse } from '../../api/coral-types.js';
|
||||
import CoralApi, { CoralAuthData, ZNCA_CLIENT_ID } from '../../api/coral.js';
|
||||
import ZncProxyApi from '../../api/znc-proxy.js';
|
||||
import { checkUseLimit, SHOULD_LIMIT_USE } from './util.js';
|
||||
|
||||
const debug = createDebug('nxapi:auth:nso');
|
||||
|
||||
export interface SavedToken {
|
||||
uuid: string;
|
||||
timestamp: string;
|
||||
nintendoAccountToken: NintendoAccountToken;
|
||||
user: NintendoAccountUser;
|
||||
f: FResult;
|
||||
flapg?: FlapgApiResponse['result'];
|
||||
nsoAccount: AccountLogin;
|
||||
credential: AccountLogin['webApiServerCredential'];
|
||||
const debug = createDebug('nxapi:auth:coral');
|
||||
|
||||
export interface SavedToken extends CoralAuthData {
|
||||
expires_at: number;
|
||||
proxy_url?: string;
|
||||
}
|
||||
@@ -92,7 +82,7 @@ export async function getToken(
|
||||
|
||||
const nso = proxy_url ?
|
||||
new ZncProxyApi(proxy_url, token) :
|
||||
new CoralApi(existingToken.credential.accessToken);
|
||||
CoralApi.createWithSavedToken(existingToken);
|
||||
|
||||
nso.onTokenExpired = createTokenExpiredHandler(storage, token, nso, existingToken);
|
||||
|
||||
@@ -114,7 +104,7 @@ async function renewToken(
|
||||
const data = await nso.renewToken(token, previousToken.user);
|
||||
|
||||
const existingToken: SavedToken = {
|
||||
user: previousToken.user,
|
||||
...previousToken,
|
||||
...data,
|
||||
expires_at: Date.now() + (data.credential.expiresIn * 1000),
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import createDebug from 'debug';
|
||||
import * as persist from 'node-persist';
|
||||
import { ZNMA_CLIENT_ID } from '../../api/moon.js';
|
||||
import { NintendoAccountSessionTokenJwtPayload, NintendoAccountToken, NintendoAccountUser } from '../../api/na.js';
|
||||
import { MoonAuthData, ZNMA_CLIENT_ID } from '../../api/moon.js';
|
||||
import { NintendoAccountSessionTokenJwtPayload } from '../../api/na.js';
|
||||
import { Jwt } from '../../util/jwt.js';
|
||||
import MoonApi from '../../api/moon.js';
|
||||
import { checkUseLimit, LIMIT_REQUESTS, SHOULD_LIMIT_USE } from './util.js';
|
||||
@@ -11,10 +11,7 @@ const debug = createDebug('nxapi:auth:moon');
|
||||
// Higher rate limit for parental controls, as the token expires sooner
|
||||
const LIMIT_PERIOD = 15 * 60 * 1000; // 15 minutes
|
||||
|
||||
export interface SavedMoonToken {
|
||||
nintendoAccountToken: NintendoAccountToken;
|
||||
user: NintendoAccountUser;
|
||||
|
||||
export interface SavedMoonToken extends MoonAuthData {
|
||||
expires_at: number;
|
||||
}
|
||||
|
||||
@@ -66,7 +63,7 @@ export async function getPctlToken(storage: persist.LocalStorage, token: string,
|
||||
await storage.setItem('NintendoAccountToken-pctl.' + existingToken.user.id, token);
|
||||
|
||||
return {
|
||||
moon: new MoonApi(existingToken.nintendoAccountToken.access_token!, existingToken.user.id),
|
||||
moon: MoonApi.createWithSavedToken(existingToken),
|
||||
data: existingToken,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ export const GITHUB_MIRROR_URL = 'https://github.com/samuelthomas2774/nxapi';
|
||||
export const ISSUES_URL = 'https://github.com/samuelthomas2774/nxapi/issues';
|
||||
export const ZNCA_API_USE_URL = 'https://gitlab.fancy.org.uk/samuel/nxapi#splatnet2statink-and-flapg';
|
||||
export const USER_AGENT_INFO_URL = 'https://gitlab.fancy.org.uk/samuel/nxapi#user-agent-strings';
|
||||
export const CONFIG_URL = 'https://fancy.org.uk/api/nxapi/config';
|
||||
|
||||
@@ -7,11 +7,11 @@ import { ErrorResponse } from '../api/util.js';
|
||||
import { timeoutSignal } from '../util/misc.js';
|
||||
import { getUserAgent } from '../util/useragent.js';
|
||||
import { paths } from '../util/storage.js';
|
||||
import { dev, dir, git, version } from '../util/product.js';
|
||||
import { dev, dir, embedded_default_remote_config, git, version } from '../util/product.js';
|
||||
import { CONFIG_URL } from './constants.js';
|
||||
|
||||
const debug = createDebug('nxapi:remote-config');
|
||||
|
||||
const CONFIG_URL = 'https://nxapi.ta.fancy.org.uk/data/config.json';
|
||||
/** Maximum time in seconds to consider cached data fresh */
|
||||
const MAX_FRESH = 24 * 60 * 60; // 1 day in seconds
|
||||
/** Maximum time in seconds to allow using cached data after it's considered stale */
|
||||
@@ -19,7 +19,8 @@ const MAX_STALE = 24 * 60 * 60; // 1 day in seconds
|
||||
|
||||
const default_config: NxapiRemoteConfig = {
|
||||
require_version: [version],
|
||||
...JSON.parse(await fs.readFile(path.join(dir, 'resources', 'common', 'remote-config.json'), 'utf-8')),
|
||||
...(embedded_default_remote_config ??
|
||||
JSON.parse(await fs.readFile(path.join(dir, 'resources', 'common', 'remote-config.json'), 'utf-8'))),
|
||||
};
|
||||
|
||||
async function loadRemoteConfig() {
|
||||
@@ -46,6 +47,8 @@ async function loadRemoteConfig() {
|
||||
} catch (err) {}
|
||||
|
||||
try {
|
||||
debug('Getting remote config from %s, must revalidate: %s', url, must_revalidate);
|
||||
|
||||
const config = await getRemoteConfig(url, undefined, data ? {
|
||||
previous: data.data,
|
||||
updated_at: new Date(data.updated_at),
|
||||
@@ -95,8 +98,6 @@ async function getRemoteConfig(url: string, useragent?: string, cache?: {
|
||||
updated_at: Date;
|
||||
etag: string | null;
|
||||
}) {
|
||||
debug('Getting remote config from %s', url);
|
||||
|
||||
const [signal, cancel] = timeoutSignal();
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
@@ -159,7 +160,7 @@ export enum RemoteConfigMode {
|
||||
}
|
||||
|
||||
export const mode =
|
||||
process.env.NXAPI_ENABLE_REMOTE_CONFIG !== '1' ? RemoteConfigMode.DISABLE :
|
||||
process.env.NXAPI_ENABLE_REMOTE_CONFIG === '0' ? RemoteConfigMode.DISABLE :
|
||||
process.env.NXAPI_REMOTE_CONFIG_FALLBACK === '1' ? RemoteConfigMode.OPPORTUNISTIC :
|
||||
RemoteConfigMode.REQUIRE;
|
||||
|
||||
@@ -176,6 +177,8 @@ if (cache && !config.require_version.includes(version)) {
|
||||
|
||||
export default config;
|
||||
|
||||
debug('using config', RemoteConfigMode[mode], config);
|
||||
|
||||
export interface RemoteConfigCacheData {
|
||||
created_at: number;
|
||||
updated_at: number;
|
||||
@@ -200,6 +203,7 @@ export interface NxapiRemoteConfig {
|
||||
// If null the API should not be used
|
||||
coral: CoralRemoteConfig | null;
|
||||
coral_auth: {
|
||||
default: DefaultZncaApiProvider;
|
||||
splatnet2statink: {} | null;
|
||||
flapg: {} | null;
|
||||
imink: {} | null;
|
||||
@@ -207,6 +211,11 @@ export interface NxapiRemoteConfig {
|
||||
moon: MoonRemoteConfig | null;
|
||||
}
|
||||
|
||||
export type DefaultZncaApiProvider =
|
||||
'flapg' |
|
||||
'imink' |
|
||||
['nxapi', string];
|
||||
|
||||
export interface CoralRemoteConfig {
|
||||
znca_version: string; // '2.1.1'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user