refactor(frontend): tighten inbound.ts cleanup wins

Checkpoint before the full any → typed pass:
- Wrap 15 case bodies in braces (no-case-declarations)
- Convert 14 let → const in genLink helpers (prefer-const)
- new Array() → [] for shadowsocks passwords (no-array-constructor)
- XrayCommonClass: HeaderEntry, FallbackEntry, JsonObject interfaces;
  fromJson/toV2Headers/toHeaders typed against them; static methods
  return JsonObject / HeaderEntry[] instead of any
- Reduce file-level eslint-disable scope from 4 rules to just
  no-explicit-any (the only one still needed)
This commit is contained in:
MHSanaei 2026-05-25 01:38:46 +02:00
parent 0217270262
commit 2a5bf835b8
No known key found for this signature in database
GPG key ID: 7E4060F2FBE5AB7A

View file

@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any, prefer-const, no-case-declarations, @typescript-eslint/no-array-constructor */ /* eslint-disable @typescript-eslint/no-explicit-any */
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { ObjectUtil, RandomUtil, Base64, NumberFormatter, SizeFormatter, Wireguard } from '@/utils'; import { ObjectUtil, RandomUtil, Base64, NumberFormatter, SizeFormatter, Wireguard } from '@/utils';
import { getRandomRealityTarget } from '@/models/reality-targets'; import { getRandomRealityTarget } from '@/models/reality-targets';
@ -137,23 +137,33 @@ Object.freeze(TCP_CONGESTION_OPTION);
Object.freeze(USERS_SECURITY); Object.freeze(USERS_SECURITY);
Object.freeze(MODE_OPTION); Object.freeze(MODE_OPTION);
export type JsonObject = Record<string, unknown>;
export interface HeaderEntry { name: string; value: string }
export interface FallbackEntry {
dest?: string | number;
name?: string;
alpn?: string;
path?: string;
xver?: number | string;
}
export class XrayCommonClass { export class XrayCommonClass {
[key: string]: any; [key: string]: any;
static toJsonArray(arr: any[]): any[] { static toJsonArray<T extends { toJson(): unknown }>(arr: T[]): unknown[] {
return arr.map((obj: any) => obj.toJson()); return arr.map((obj) => obj.toJson());
} }
static fromJson(..._args: any[]): any { static fromJson(..._args: unknown[]): XrayCommonClass | undefined {
return new XrayCommonClass(); return new XrayCommonClass();
} }
toJson(): any { toJson(): unknown {
return this; return this;
} }
static fallbackToJson(fb: any): any { static fallbackToJson(fb: FallbackEntry): JsonObject {
const out: any = { dest: fb.dest }; const out: JsonObject = { dest: fb.dest };
if (fb.name) out.name = fb.name; if (fb.name) out.name = fb.name;
if (fb.alpn) out.alpn = fb.alpn; if (fb.alpn) out.alpn = fb.alpn;
if (fb.path) out.path = fb.path; if (fb.path) out.path = fb.path;
@ -166,14 +176,15 @@ export class XrayCommonClass {
return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson()); return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
} }
static toHeaders(v2Headers: any): any[] { static toHeaders(v2Headers: unknown): HeaderEntry[] {
const newHeaders: any[] = []; const newHeaders: HeaderEntry[] = [];
if (v2Headers) { if (v2Headers && typeof v2Headers === 'object') {
Object.keys(v2Headers).forEach((key: string) => { const map = v2Headers as Record<string, string | string[]>;
const values = v2Headers[key]; Object.keys(map).forEach((key: string) => {
if (typeof (values) === 'string') { const values = map[key];
if (typeof values === 'string') {
newHeaders.push({ name: key, value: values }); newHeaders.push({ name: key, value: values });
} else { } else if (Array.isArray(values)) {
for (let i = 0; i < values.length; ++i) { for (let i = 0; i < values.length; ++i) {
newHeaders.push({ name: key, value: values[i] }); newHeaders.push({ name: key, value: values[i] });
} }
@ -183,8 +194,8 @@ export class XrayCommonClass {
return newHeaders; return newHeaders;
} }
static toV2Headers(headers: any[], arr: boolean = true): any { static toV2Headers(headers: HeaderEntry[], arr: boolean = true): Record<string, string | string[]> {
const v2Headers: any = {}; const v2Headers: Record<string, string | string[]> = {};
for (let i = 0; i < headers.length; ++i) { for (let i = 0; i < headers.length; ++i) {
const name = headers[i].name; const name = headers[i].name;
const value = headers[i].value; const value = headers[i].value;
@ -194,8 +205,9 @@ export class XrayCommonClass {
if (!(name in v2Headers)) { if (!(name in v2Headers)) {
v2Headers[name] = arr ? [value] : value; v2Headers[name] = arr ? [value] : value;
} else { } else {
if (arr) { const existing = v2Headers[name];
v2Headers[name].push(value); if (arr && Array.isArray(existing)) {
existing.push(value);
} else { } else {
v2Headers[name] = value; v2Headers[name] = value;
} }
@ -1908,7 +1920,7 @@ export class Inbound extends XrayCommonClass {
} }
isExpiry(index: number) { isExpiry(index: number) {
let exp = this.clients[index].expiryTime; const exp = this.clients[index].expiryTime;
return exp > 0 ? exp < new Date().getTime() : false; return exp > 0 ? exp < new Date().getTime() : false;
} }
@ -2031,7 +2043,7 @@ export class Inbound extends XrayCommonClass {
params.set("type", this.stream.network); params.set("type", this.stream.network);
params.set("encryption", this.settings.encryption); params.set("encryption", this.settings.encryption);
switch (type) { switch (type) {
case "tcp": case "tcp": {
const tcp = this.stream.tcp; const tcp = this.stream.tcp;
if (tcp.type === 'http') { if (tcp.type === 'http') {
const request = tcp.request; const request = tcp.request;
@ -2044,17 +2056,20 @@ export class Inbound extends XrayCommonClass {
params.set("headerType", 'http'); params.set("headerType", 'http');
} }
break; break;
case "kcp": }
case "kcp": {
const kcp = this.stream.kcp; const kcp = this.stream.kcp;
params.set("mtu", kcp.mtu); params.set("mtu", kcp.mtu);
params.set("tti", kcp.tti); params.set("tti", kcp.tti);
break; break;
case "ws": }
case "ws": {
const ws = this.stream.ws; const ws = this.stream.ws;
params.set("path", ws.path); params.set("path", ws.path);
params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host')); params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
break; break;
case "grpc": }
case "grpc": {
const grpc = this.stream.grpc; const grpc = this.stream.grpc;
params.set("serviceName", grpc.serviceName); params.set("serviceName", grpc.serviceName);
params.set("authority", grpc.authority); params.set("authority", grpc.authority);
@ -2062,11 +2077,13 @@ export class Inbound extends XrayCommonClass {
params.set("mode", "multi"); params.set("mode", "multi");
} }
break; break;
case "httpupgrade": }
case "httpupgrade": {
const httpupgrade = this.stream.httpupgrade; const httpupgrade = this.stream.httpupgrade;
params.set("path", httpupgrade.path); params.set("path", httpupgrade.path);
params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host')); params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
break; break;
}
case "xhttp": case "xhttp":
Inbound.applyXhttpExtraToParams(this.stream.xhttp, params); Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
break; break;
@ -2127,13 +2144,13 @@ export class Inbound extends XrayCommonClass {
} }
genSSLink(address: any = '', port: any = this.port, forceTls?: any, remark: any = '', clientPassword?: any, externalProxy: any = null) { genSSLink(address: any = '', port: any = this.port, forceTls?: any, remark: any = '', clientPassword?: any, externalProxy: any = null) {
let settings = this.settings; const settings = this.settings;
const type = this.stream.network; const type = this.stream.network;
const security = forceTls == 'same' ? this.stream.security : forceTls; const security = forceTls == 'same' ? this.stream.security : forceTls;
const params = new Map(); const params = new Map();
params.set("type", this.stream.network); params.set("type", this.stream.network);
switch (type) { switch (type) {
case "tcp": case "tcp": {
const tcp = this.stream.tcp; const tcp = this.stream.tcp;
if (tcp.type === 'http') { if (tcp.type === 'http') {
const request = tcp.request; const request = tcp.request;
@ -2146,17 +2163,20 @@ export class Inbound extends XrayCommonClass {
params.set("headerType", 'http'); params.set("headerType", 'http');
} }
break; break;
case "kcp": }
case "kcp": {
const kcp = this.stream.kcp; const kcp = this.stream.kcp;
params.set("mtu", kcp.mtu); params.set("mtu", kcp.mtu);
params.set("tti", kcp.tti); params.set("tti", kcp.tti);
break; break;
case "ws": }
case "ws": {
const ws = this.stream.ws; const ws = this.stream.ws;
params.set("path", ws.path); params.set("path", ws.path);
params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host')); params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
break; break;
case "grpc": }
case "grpc": {
const grpc = this.stream.grpc; const grpc = this.stream.grpc;
params.set("serviceName", grpc.serviceName); params.set("serviceName", grpc.serviceName);
params.set("authority", grpc.authority); params.set("authority", grpc.authority);
@ -2164,11 +2184,13 @@ export class Inbound extends XrayCommonClass {
params.set("mode", "multi"); params.set("mode", "multi");
} }
break; break;
case "httpupgrade": }
case "httpupgrade": {
const httpupgrade = this.stream.httpupgrade; const httpupgrade = this.stream.httpupgrade;
params.set("path", httpupgrade.path); params.set("path", httpupgrade.path);
params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host')); params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
break; break;
}
case "xhttp": case "xhttp":
Inbound.applyXhttpExtraToParams(this.stream.xhttp, params); Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
break; break;
@ -2192,11 +2214,11 @@ export class Inbound extends XrayCommonClass {
} }
let password = new Array(); const password: string[] = [];
if (this.isSS2022) password.push(settings.password); if (this.isSS2022) password.push(settings.password);
if (this.isSSMultiUser) password.push(clientPassword); if (this.isSSMultiUser) password.push(clientPassword);
let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`; const link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
const url = new URL(link); const url = new URL(link);
for (const [key, value] of params) { for (const [key, value] of params) {
url.searchParams.set(key, value) url.searchParams.set(key, value)
@ -2211,7 +2233,7 @@ export class Inbound extends XrayCommonClass {
const params = new Map(); const params = new Map();
params.set("type", this.stream.network); params.set("type", this.stream.network);
switch (type) { switch (type) {
case "tcp": case "tcp": {
const tcp = this.stream.tcp; const tcp = this.stream.tcp;
if (tcp.type === 'http') { if (tcp.type === 'http') {
const request = tcp.request; const request = tcp.request;
@ -2224,17 +2246,20 @@ export class Inbound extends XrayCommonClass {
params.set("headerType", 'http'); params.set("headerType", 'http');
} }
break; break;
case "kcp": }
case "kcp": {
const kcp = this.stream.kcp; const kcp = this.stream.kcp;
params.set("mtu", kcp.mtu); params.set("mtu", kcp.mtu);
params.set("tti", kcp.tti); params.set("tti", kcp.tti);
break; break;
case "ws": }
case "ws": {
const ws = this.stream.ws; const ws = this.stream.ws;
params.set("path", ws.path); params.set("path", ws.path);
params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host')); params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
break; break;
case "grpc": }
case "grpc": {
const grpc = this.stream.grpc; const grpc = this.stream.grpc;
params.set("serviceName", grpc.serviceName); params.set("serviceName", grpc.serviceName);
params.set("authority", grpc.authority); params.set("authority", grpc.authority);
@ -2242,11 +2267,13 @@ export class Inbound extends XrayCommonClass {
params.set("mode", "multi"); params.set("mode", "multi");
} }
break; break;
case "httpupgrade": }
case "httpupgrade": {
const httpupgrade = this.stream.httpupgrade; const httpupgrade = this.stream.httpupgrade;
params.set("path", httpupgrade.path); params.set("path", httpupgrade.path);
params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host')); params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
break; break;
}
case "xhttp": case "xhttp":
Inbound.applyXhttpExtraToParams(this.stream.xhttp, params); Inbound.applyXhttpExtraToParams(this.stream.xhttp, params);
break; break;
@ -2391,7 +2418,7 @@ export class Inbound extends XrayCommonClass {
genWireguardLinks(remark = '', remarkModel = '-ieo', hostOverride = '') { genWireguardLinks(remark = '', remarkModel = '-ieo', hostOverride = '') {
const addr = this._resolveAddr(hostOverride); const addr = this._resolveAddr(hostOverride);
const separationChar = remarkModel.charAt(0); const separationChar = remarkModel.charAt(0);
let links: any[] = []; const links: any[] = [];
this.settings.peers.forEach((_p: any, index: number) => { this.settings.peers.forEach((_p: any, index: number) => {
links.push(this.getWireguardLink(addr, this.port, remark + separationChar + (index + 1), index)); links.push(this.getWireguardLink(addr, this.port, remark + separationChar + (index + 1), index));
}); });
@ -2401,7 +2428,7 @@ export class Inbound extends XrayCommonClass {
genWireguardConfigs(remark = '', remarkModel = '-ieo', hostOverride = '') { genWireguardConfigs(remark = '', remarkModel = '-ieo', hostOverride = '') {
const addr = this._resolveAddr(hostOverride); const addr = this._resolveAddr(hostOverride);
const separationChar = remarkModel.charAt(0); const separationChar = remarkModel.charAt(0);
let links: any[] = []; const links: any[] = [];
this.settings.peers.forEach((_p: any, index: number) => { this.settings.peers.forEach((_p: any, index: number) => {
links.push(this.getWireguardTxt(addr, this.port, remark + separationChar + (index + 1), index)); links.push(this.getWireguardTxt(addr, this.port, remark + separationChar + (index + 1), index));
}); });
@ -2425,10 +2452,10 @@ export class Inbound extends XrayCommonClass {
} }
genAllLinks(remark: any = '', remarkModel: any = '-ieo', client?: any, hostOverride: any = '') { genAllLinks(remark: any = '', remarkModel: any = '-ieo', client?: any, hostOverride: any = '') {
let result: any[] = []; const result: any[] = [];
let email = client ? client.email : ''; const email = client ? client.email : '';
let addr = this._resolveAddr(hostOverride); const addr = this._resolveAddr(hostOverride);
let port = this.port; const port = this.port;
const separationChar = remarkModel.charAt(0); const separationChar = remarkModel.charAt(0);
const orderChars = remarkModel.slice(1); const orderChars = remarkModel.slice(1);
const orders: any = { const orders: any = {
@ -2437,7 +2464,7 @@ export class Inbound extends XrayCommonClass {
'o': '', 'o': '',
}; };
if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) { if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
let r = orderChars.split('').map((char: string) => orders[char]).filter((x: any) => x.length > 0).join(separationChar); const r = orderChars.split('').map((char: string) => orders[char]).filter((x: any) => x.length > 0).join(separationChar);
result.push({ result.push({
remark: r, remark: r,
link: this.genLink(addr, port, 'same', r, client) link: this.genLink(addr, port, 'same', r, client)
@ -2445,7 +2472,7 @@ export class Inbound extends XrayCommonClass {
} else { } else {
this.stream.externalProxy.forEach((ep: any) => { this.stream.externalProxy.forEach((ep: any) => {
orders['o'] = ep.remark; orders['o'] = ep.remark;
let r = orderChars.split('').map((char: string) => orders[char]).filter((x: any) => x.length > 0).join(separationChar); const r = orderChars.split('').map((char: string) => orders[char]).filter((x: any) => x.length > 0).join(separationChar);
result.push({ result.push({
remark: r, remark: r,
link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client, ep) link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client, ep)
@ -2456,9 +2483,9 @@ export class Inbound extends XrayCommonClass {
} }
genInboundLinks(remark = '', remarkModel = '-ieo', hostOverride = '') { genInboundLinks(remark = '', remarkModel = '-ieo', hostOverride = '') {
let addr = this._resolveAddr(hostOverride); const addr = this._resolveAddr(hostOverride);
if (this.clients) { if (this.clients) {
let links: any[] = []; const links: any[] = [];
this.clients.forEach((client: any) => { this.clients.forEach((client: any) => {
this.genAllLinks(remark, remarkModel, client, hostOverride).forEach((l: any) => { this.genAllLinks(remark, remarkModel, client, hostOverride).forEach((l: any) => {
links.push(l.link); links.push(l.link);
@ -2846,7 +2873,7 @@ Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
} }
toJson() { toJson() {
return XrayCommonClass.fallbackToJson(this); return XrayCommonClass.fallbackToJson(this as unknown as FallbackEntry);
} }
static fromJson(json: any = []) { static fromJson(json: any = []) {
@ -2926,7 +2953,7 @@ Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
} }
toJson() { toJson() {
return XrayCommonClass.fallbackToJson(this); return XrayCommonClass.fallbackToJson(this as unknown as FallbackEntry);
} }
static fromJson(json: any = []) { static fromJson(json: any = []) {