From 2f640a946e17df889a58a1d3386f7ea5f10c751c Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Sat, 6 Jul 2024 00:19:27 +0800 Subject: [PATCH] fix: type --- src/services/types.d.ts | 63 ++++++++++++++++++++++------------------- src/utils/uri-parser.ts | 41 ++++++++++++++++++++------- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/services/types.d.ts b/src/services/types.d.ts index d0f9c797..e2e47c7c 100644 --- a/src/services/types.d.ts +++ b/src/services/types.d.ts @@ -247,9 +247,20 @@ interface HttpOptions { }; } +interface H2Options { + path?: string; + host?: string; +} + interface GrpcOptions { "grpc-service-name"?: string; } + +interface RealityOptions { + "public-key"?: string; + "short-id"?: string; +} + type NetworkType = "ws" | "http" | "h2" | "grpc"; // base @@ -258,22 +269,22 @@ interface IProxyBaseConfig { mptcp?: boolean; "interface-name"?: string; "routing-mark"?: number; - "ip-version"?: string; + "ip-version"?: "dual" | "ipv4" | "ipv6" | "ipv4-prefer" | "ipv6-prefer"; "dialer-proxy"?: string; } // direct interface IProxyDirectConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "direct"; } // dns interface IProxyDnsConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "dns"; } // http interface IProxyHttpConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "http"; server?: string; port?: number; @@ -287,7 +298,7 @@ interface IProxyHttpConfig extends IProxyBaseConfig { } // socks5 interface IProxySocks5Config extends IProxyBaseConfig { - name?: string; + name: string; type: "socks5"; server?: string; port?: number; @@ -300,7 +311,7 @@ interface IProxySocks5Config extends IProxyBaseConfig { } // ssh interface IProxySshConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "ssh"; server?: string; port?: number; @@ -313,7 +324,7 @@ interface IProxySshConfig extends IProxyBaseConfig { } // trojan interface IProxyTrojanConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "trojan"; server?: string; port?: number; @@ -324,7 +335,7 @@ interface IProxyTrojanConfig extends IProxyBaseConfig { fingerprint?: string; udp?: boolean; network?: NetworkType; - "reality-opts"?: {}; + "reality-opts"?: RealityOptions; "grpc-opts"?: GrpcOptions; "ws-opts"?: WsOptions; "ss-opts"?: { @@ -336,7 +347,7 @@ interface IProxyTrojanConfig extends IProxyBaseConfig { } // tuic interface IProxyTuicConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "tuic"; server?: string; port?: number; @@ -369,7 +380,7 @@ interface IProxyTuicConfig extends IProxyBaseConfig { } // vless interface IProxyVlessConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "vless"; server?: string; port?: number; @@ -382,12 +393,9 @@ interface IProxyVlessConfig extends IProxyBaseConfig { xudp?: boolean; "packet-encoding"?: string; network?: NetworkType; - "reality-opts"?: { - "public-key"?: string; - "short-id"?: string; - }; + "reality-opts"?: RealityOptions; "http-opts"?: HttpOptions; - "h2-opts"?: {}; + "h2-opts"?: H2Options; "grpc-opts"?: GrpcOptions; "ws-opts"?: WsOptions; "ws-path"?: string; @@ -399,7 +407,7 @@ interface IProxyVlessConfig extends IProxyBaseConfig { } // vmess interface IProxyVmessConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "vmess"; server?: string; port?: number; @@ -413,12 +421,9 @@ interface IProxyVmessConfig extends IProxyBaseConfig { "skip-cert-verify"?: boolean; fingerprint?: string; servername?: string; - "reality-opts"?: {}; + "reality-opts"?: RealityOptions; "http-opts"?: HttpOptions; - "h2-opts"?: { - path?: string; - host?: string; - }; + "h2-opts"?: H2Options; "grpc-opts"?: GrpcOptions; "ws-opts"?: WsOptions; "packet-addr"?: boolean; @@ -438,7 +443,7 @@ interface WireGuardPeerOptions { } // wireguard interface IProxyWireguardConfig extends IProxyBaseConfig, WireGuardPeerOptions { - name?: string; + name: string; type: "wireguard"; ip?: string; ipv6?: string; @@ -454,7 +459,7 @@ interface IProxyWireguardConfig extends IProxyBaseConfig, WireGuardPeerOptions { } // hysteria interface IProxyHysteriaConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "hysteria"; server?: string; port?: number; @@ -482,7 +487,7 @@ interface IProxyHysteriaConfig extends IProxyBaseConfig { } // hysteria2 interface IProxyHysteria2Config extends IProxyBaseConfig { - name?: string; + name: string; type: "hysteria2"; server?: string; port?: number; @@ -506,14 +511,14 @@ interface IProxyHysteria2Config extends IProxyBaseConfig { } // shadowsocks interface IProxyShadowsocksConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "ss"; server?: string; port?: number; password?: string; cipher?: string; udp?: boolean; - plugin?: string; + plugin?: "obfs" | "v2ray-plugin" | "shadow-tls" | "restls"; "plugin-opts"?: { mode?: string; host?: string; @@ -536,7 +541,7 @@ interface IProxyShadowsocksConfig extends IProxyBaseConfig { } // shadowsocksR interface IProxyshadowsocksRConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "ssr"; server?: string; port?: number; @@ -552,7 +557,7 @@ interface IProxyshadowsocksRConfig extends IProxyBaseConfig { interface IProxySmuxConfig { smux?: { enabled?: boolean; - protocol?: string; + protocol?: "smux" | "yamux" | "h2mux"; "max-connections"?: number; "min-streams"?: number; "max-streams"?: number; @@ -568,7 +573,7 @@ interface IProxySmuxConfig { } // snell interface IProxySnellConfig extends IProxyBaseConfig { - name?: string; + name: string; type: "snell"; server?: string; port?: number; diff --git a/src/utils/uri-parser.ts b/src/utils/uri-parser.ts index 6da07a60..3a7d0c7b 100644 --- a/src/utils/uri-parser.ts +++ b/src/utils/uri-parser.ts @@ -85,7 +85,7 @@ function URI_SS(line: string): IProxyShadowsocksConfig { let content = line.split("ss://")[1]; const proxy: IProxyShadowsocksConfig = { - name: trimStr(decodeURIComponent(line.split("#")[1])), + name: decodeURIComponent(line.split("#")[1]).trim(), type: "ss", server: "", port: 0, @@ -124,9 +124,7 @@ function URI_SS(line: string): IProxyShadowsocksConfig { proxy.port = parseInt( `${serverAndPort?.substring(portIdx + 1)}`.match(/\d+/)?.[0] ?? "" ); - console.log(userInfoStr); const userInfo = userInfoStr.match(/(^.*?):(.*$)/); - console.log(userInfo); proxy.cipher = userInfo?.[1]; proxy.password = userInfo?.[2]; @@ -214,8 +212,8 @@ function URI_SSR(line: string): IProxyshadowsocksRConfig { proxy = { ...proxy, name: other_params.remarks - ? trimStr(decodeBase64OrOriginal(other_params.remarks)) - : proxy.server, + ? decodeBase64OrOriginal(other_params.remarks).trim() + : proxy.server ?? "", "protocol-param": getIfNotBlank( decodeBase64OrOriginal(other_params.protoparam || "").replace(/\s/g, "") ), @@ -229,11 +227,9 @@ function URI_SSR(line: string): IProxyshadowsocksRConfig { function URI_VMESS(line: string): IProxyVmessConfig { line = line.split("vmess://")[1]; let content = decodeBase64OrOriginal(line); - console.log(content); if (/=\s*vmess/.test(content)) { // Quantumult VMess URI format const partitions = content.split(",").map((p) => p.trim()); - console.log(partitions); const params: Record = {}; for (const part of partitions) { if (part.indexOf("=") !== -1) { @@ -243,7 +239,7 @@ function URI_VMESS(line: string): IProxyVmessConfig { } const proxy: IProxyVmessConfig = { - name: trimStr(partitions[0].split("=")[0]), + name: partitions[0].split("=")[0].trim(), type: "vmess", server: partitions[1], port: parseInt(partitions[2], 10), @@ -571,7 +567,7 @@ function URI_Trojan(line: string): IProxyTrojanConfig { const proxy: IProxyTrojanConfig = parser.parse(newLine); if (isNotBlank(name)) { try { - proxy.name = trimStr(decodeURIComponent(name)); + proxy.name = decodeURIComponent(name).trim(); } catch (e) { throw Error("Can not get proxy name"); } @@ -926,7 +922,19 @@ function URI_HTTP(line: string): IProxyHttpConfig { proxy["skip-cert-verify"] = /(TRUE)|1/i.test(value); break; case "ip-version": - proxy["ip-version"] = value; + if ( + ["dual", "ipv4", "ipv6", "ipv4-prefer", "ipv6-prefer"].includes(value) + ) { + proxy["ip-version"] = value as + | "dual" + | "ipv4" + | "ipv6" + | "ipv4-prefer" + | "ipv6-prefer"; + } else { + proxy["ip-version"] = "dual"; + } + break; default: break; @@ -980,7 +988,18 @@ function URI_SOCKS(line: string): IProxySocks5Config { proxy["udp"] = /(TRUE)|1/i.test(value); break; case "ip-version": - proxy["ip-version"] = value; + if ( + ["dual", "ipv4", "ipv6", "ipv4-prefer", "ipv6-prefer"].includes(value) + ) { + proxy["ip-version"] = value as + | "dual" + | "ipv4" + | "ipv6" + | "ipv4-prefer" + | "ipv6-prefer"; + } else { + proxy["ip-version"] = "dual"; + } break; default: break;