Refactor xCloud & xHome interceptors

This commit is contained in:
redphx 2024-10-14 20:08:47 +07:00
parent 82ee00b4ae
commit e5bd7e64a7
2 changed files with 42 additions and 42 deletions

View File

@ -13,9 +13,25 @@ import { BypassServerIps } from "@/enums/bypass-servers";
import { PrefKey } from "@/enums/pref-keys";
import { getPref, StreamResolution, StreamTouchController } from "./settings-storages/global-settings-storage";
export
class XcloudInterceptor {
static async #handleLogin(request: RequestInfo | URL, init?: RequestInit) {
export class XcloudInterceptor {
private static readonly SERVER_EMOJIS = {
AustraliaEast: '🇦🇺',
AustraliaSouthEast: '🇦🇺',
BrazilSouth: '🇧🇷',
EastUS: '🇺🇸',
EastUS2: '🇺🇸',
JapanEast: '🇯🇵',
KoreaCentral: '🇰🇷',
MexicoCentral: '🇲🇽',
NorthCentralUs: '🇺🇸',
SouthCentralUS: '🇺🇸',
UKSouth: '🇬🇧',
WestEurope: '🇪🇺',
WestUS: '🇺🇸',
WestUS2: '🇺🇸',
};
private static async handleLogin(request: RequestInfo | URL, init?: RequestInit) {
const bypassServer = getPref(PrefKey.SERVER_BYPASS_RESTRICTION);
if (bypassServer !== 'off') {
const ip = BypassServerIps[bypassServer as keyof typeof BypassServerIps];
@ -35,24 +51,8 @@ class XcloudInterceptor {
RemotePlayManager.getInstance().xcloudToken = obj.gsToken;
// Get server list
const serverEmojis = {
AustraliaEast: '🇦🇺',
AustraliaSouthEast: '🇦🇺',
BrazilSouth: '🇧🇷',
EastUS: '🇺🇸',
EastUS2: '🇺🇸',
JapanEast: '🇯🇵',
KoreaCentral: '🇰🇷',
MexicoCentral: '🇲🇽',
NorthCentralUs: '🇺🇸',
SouthCentralUS: '🇺🇸',
UKSouth: '🇬🇧',
WestEurope: '🇪🇺',
WestUS: '🇺🇸',
WestUS2: '🇺🇸',
};
const serverRegex = /\/\/(\w+)\./;
const serverEmojis = XcloudInterceptor.SERVER_EMOJIS;
for (let region of obj.offeringSettings.regions) {
const regionName = region.name as keyof typeof serverEmojis;
@ -91,7 +91,7 @@ class XcloudInterceptor {
return response;
}
static async #handlePlay(request: RequestInfo | URL, init?: RequestInit) {
private static async handlePlay(request: RequestInfo | URL, init?: RequestInit) {
const PREF_STREAM_TARGET_RESOLUTION = getPref(PrefKey.STREAM_TARGET_RESOLUTION);
const PREF_STREAM_PREFERRED_LOCALE = getPref(PrefKey.STREAM_PREFERRED_LOCALE);
@ -129,7 +129,7 @@ class XcloudInterceptor {
return NATIVE_FETCH(newRequest);
}
static async #handleWaitTime(request: RequestInfo | URL, init?: RequestInit) {
private static async handleWaitTime(request: RequestInfo | URL, init?: RequestInit) {
const response = await NATIVE_FETCH(request, init);
if (getPref(PrefKey.UI_LOADING_SCREEN_WAIT_TIME)) {
@ -143,7 +143,7 @@ class XcloudInterceptor {
return response;
}
static async #handleConfiguration(request: RequestInfo | URL, init?: RequestInit) {
private static async handleConfiguration(request: RequestInfo | URL, init?: RequestInit) {
if ((request as Request).method !== 'GET') {
return NATIVE_FETCH(request, init);
}
@ -213,13 +213,13 @@ class XcloudInterceptor {
// Server list
if (url.endsWith('/v2/login/user')) {
return XcloudInterceptor.#handleLogin(request, init);
return XcloudInterceptor.handleLogin(request, init);
} else if (url.endsWith('/sessions/cloud/play')) { // Get session
return XcloudInterceptor.#handlePlay(request, init);
return XcloudInterceptor.handlePlay(request, init);
} else if (url.includes('xboxlive.com') && url.includes('/waittime/')) {
return XcloudInterceptor.#handleWaitTime(request, init);
return XcloudInterceptor.handleWaitTime(request, init);
} else if (url.endsWith('/configuration')) {
return XcloudInterceptor.#handleConfiguration(request, init);
return XcloudInterceptor.handleConfiguration(request, init);
} else if (url && url.endsWith('/ice') && url.includes('/sessions/') && (request as Request).method === 'GET') {
return patchIceCandidates(request as Request);
}

View File

@ -10,7 +10,7 @@ import type { RemotePlayConsoleAddresses } from "@/types/network";
import { RemotePlayManager } from "@/modules/remote-play-manager";
export class XhomeInterceptor {
static #consoleAddrs: RemotePlayConsoleAddresses = {};
private static consoleAddrs: RemotePlayConsoleAddresses = {};
private static readonly BASE_DEVICE_INFO = {
appInfo: {
@ -52,7 +52,7 @@ export class XhomeInterceptor {
},
};
static async #handleLogin(request: Request) {
private static async handleLogin(request: Request) {
try {
const clone = (request as Request).clone();
@ -74,7 +74,7 @@ export class XhomeInterceptor {
return NATIVE_FETCH(request);
}
static async #handleConfiguration(request: Request | URL) {
private static async handleConfiguration(request: Request | URL) {
const response = await NATIVE_FETCH(request);
const obj = await response.clone().json()
@ -90,15 +90,15 @@ export class XhomeInterceptor {
const serverDetails = obj.serverDetails;
if (serverDetails.ipAddress) {
XhomeInterceptor.#consoleAddrs[serverDetails.ipAddress] = processPorts(serverDetails.port);
XhomeInterceptor.consoleAddrs[serverDetails.ipAddress] = processPorts(serverDetails.port);
}
if (serverDetails.ipV4Address) {
XhomeInterceptor.#consoleAddrs[serverDetails.ipV4Address] = processPorts(serverDetails.ipV4Port);
XhomeInterceptor.consoleAddrs[serverDetails.ipV4Address] = processPorts(serverDetails.ipV4Port);
}
if (serverDetails.ipV6Address) {
XhomeInterceptor.#consoleAddrs[serverDetails.ipV6Address] = processPorts(serverDetails.ipV6Port);
XhomeInterceptor.consoleAddrs[serverDetails.ipV6Address] = processPorts(serverDetails.ipV6Port);
}
response.json = () => Promise.resolve(obj);
@ -107,7 +107,7 @@ export class XhomeInterceptor {
return response;
}
static async #handleInputConfigs(request: Request | URL, opts: {[index: string]: any}) {
private static async handleInputConfigs(request: Request | URL, opts: {[index: string]: any}) {
const response = await NATIVE_FETCH(request);
if (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) !== StreamTouchController.ALL) {
@ -144,7 +144,7 @@ export class XhomeInterceptor {
return response;
}
static async #handleTitles(request: Request) {
private static async handleTitles(request: Request) {
const clone = request.clone();
const headers: {[index: string]: any} = {};
@ -163,7 +163,7 @@ export class XhomeInterceptor {
return NATIVE_FETCH(request);
}
static async #handlePlay(request: RequestInfo | URL) {
private static async handlePlay(request: RequestInfo | URL) {
const clone = (request as Request).clone();
const body = await clone.json();
@ -216,17 +216,17 @@ export class XhomeInterceptor {
// Get console IP
if (url.includes('/configuration')) {
return XhomeInterceptor.#handleConfiguration(request);
return XhomeInterceptor.handleConfiguration(request);
} else if (url.endsWith('/sessions/home/play')) {
return XhomeInterceptor.#handlePlay(request);
return XhomeInterceptor.handlePlay(request);
} else if (url.includes('inputconfigs')) {
return XhomeInterceptor.#handleInputConfigs(request, opts);
return XhomeInterceptor.handleInputConfigs(request, opts);
} else if (url.includes('/login/user')) {
return XhomeInterceptor.#handleLogin(request);
return XhomeInterceptor.handleLogin(request);
} else if (url.endsWith('/titles')) {
return XhomeInterceptor.#handleTitles(request);
return XhomeInterceptor.handleTitles(request);
} else if (url && url.endsWith('/ice') && url.includes('/sessions/') && (request as Request).method === 'GET') {
return patchIceCandidates(request, XhomeInterceptor.#consoleAddrs);
return patchIceCandidates(request, XhomeInterceptor.consoleAddrs);
}
return await NATIVE_FETCH(request);