From 08831526fc79c457e4570891ea71e138db1075a4 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 2 Jun 2026 17:56:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20VMess=20cipher=20=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/core/proxy-utils/parsers/index.js | 17 ++- .../core/proxy-utils/parsers/peggy/loon.js | 14 +- .../core/proxy-utils/parsers/peggy/surge.js | 13 +- .../src/core/proxy-utils/producers/clash.js | 13 +- .../core/proxy-utils/producers/clashmeta.js | 20 +-- .../src/core/proxy-utils/producers/egern.js | 15 +-- .../src/core/proxy-utils/producers/loon.js | 4 +- backend/src/core/proxy-utils/producers/qx.js | 8 +- .../proxy-utils/producers/shadowrocket.js | 14 +- .../core/proxy-utils/producers/sing-box.js | 17 +-- .../src/core/proxy-utils/producers/stash.js | 13 +- .../src/core/proxy-utils/producers/surge.js | 5 + backend/src/core/proxy-utils/producers/uri.js | 14 +- .../src/core/proxy-utils/vmess-security.js | 86 ++++++++++++ .../src/test/proxy-parsers/pipeline.spec.js | 23 +++- .../proxy-parsers/v2ray-and-platforms.spec.js | 79 ++++++++++- .../test/proxy-producers/structured.spec.js | 126 ++++++++++++++++++ 18 files changed, 372 insertions(+), 111 deletions(-) create mode 100644 backend/src/core/proxy-utils/vmess-security.js diff --git a/backend/package.json b/backend/package.json index 570a81e8..2c370cdf 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.23.34", + "version": "2.23.35", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 4905b985..a775be13 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -30,6 +30,7 @@ import { isSupportedXrayEchConfigList, isSupportedXrayEchForceQuery, } from '../ech-utils'; +import { normalizeVmessSecurity } from '../vmess-security'; function surge_port_hopping(raw) { const [parts, port_hopping] = @@ -636,7 +637,9 @@ function URI_VMess() { type: 'vmess', server: partitions[1], port: partitions[2], - cipher: getIfNotBlank(partitions[3], 'auto'), + cipher: normalizeVmessSecurity( + getIfNotBlank(partitions[3], 'auto'), + ), uuid: partitions[4].match(/^"(.*)"$/)[1], tls: params.obfs === 'wss', udp: getIfPresent(params['udp-relay']), @@ -718,14 +721,7 @@ function URI_VMess() { port, // https://github.com/2dust/v2rayN/wiki/Description-of-VMess-share-link // https://github.com/XTLS/Xray-core/issues/91 - cipher: [ - 'auto', - 'aes-128-gcm', - 'chacha20-poly1305', - 'none', - ].includes(params.scy) - ? params.scy - : 'auto', + cipher: normalizeVmessSecurity(params.scy), uuid: params.id, alterId: parseInt( getIfPresent(params.aid ?? params.alterId, 0), @@ -2513,6 +2509,9 @@ function Clash_All() { if (proxy['benchmark-timeout']) { proxy['test-timeout'] = proxy['benchmark-timeout']; } + if (proxy.type === 'vmess') { + proxy.cipher = normalizeVmessSecurity(proxy.cipher); + } return proxy; }; diff --git a/backend/src/core/proxy-utils/parsers/peggy/loon.js b/backend/src/core/proxy-utils/parsers/peggy/loon.js index dc5c5e3a..4f3d1e63 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/loon.js +++ b/backend/src/core/proxy-utils/parsers/peggy/loon.js @@ -33,6 +33,13 @@ const grammars = String.raw` $set(proxy, "http-opts.headers.Host", transport.host); } } + + function normalizeVmessSecurity(security) { + const normalized = String(security || "").trim().toLowerCase(); + const supported = ["none", "auto", "aes-128-gcm", "chacha20-ietf-poly1305"]; + if (!supported.includes(normalized)) return "auto"; + return normalized === "chacha20-ietf-poly1305" ? "chacha20-poly1305" : normalized; + } } start = (shadowsocksr/shadowsocks/vmess/vless/trojan/https/http/socks5/hysteria2/anytls) { @@ -54,9 +61,9 @@ shadowsocks = tag equals "shadowsocks"i address method password (obfs_typev obfs $set(proxy, "plugin-opts.path", obfs.path); } } -vmess = tag equals "vmess"i address method uuid (transport/transport_host/transport_path/over_tls/tls_name/sni/tls_verification/tls_cert_sha256/tls_pubkey_sha256/tls_profile/vmess_alterId/fast_open/udp_relay/ip_mode/public_key/short_id/block_quic/others)* { +vmess = tag equals "vmess"i address vmess_method uuid (transport/transport_host/transport_path/over_tls/tls_name/sni/tls_verification/tls_cert_sha256/tls_pubkey_sha256/tls_profile/vmess_alterId/fast_open/udp_relay/ip_mode/public_key/short_id/block_quic/others)* { proxy.type = "vmess"; - proxy.cipher = proxy.cipher || "none"; + proxy.cipher = proxy.cipher || "auto"; proxy.alterId = proxy.alterId || 0; handleTransport(); } @@ -124,6 +131,9 @@ port = digits:[0-9]+ { method = comma cipher:cipher { proxy.cipher = cipher; } +vmess_method = comma cipher:$[^,]+ { + proxy.cipher = normalizeVmessSecurity(cipher); +} cipher = ("aes-128-cfb"/"aes-128-ctr"/"aes-128-gcm"/"aes-192-cfb"/"aes-192-ctr"/"aes-192-gcm"/"aes-256-cfb"/"aes-256-ctr"/"aes-256-gcm"/"auto"/"bf-cfb"/"camellia-128-cfb"/"camellia-192-cfb"/"camellia-256-cfb"/"chacha20-ietf-poly1305"/"chacha20-ietf"/"chacha20-poly1305"/"chacha20"/"none"/"rc4-md5"/"rc4"/"salsa20"/"xchacha20-ietf-poly1305"/"2022-blake3-aes-128-gcm"/"2022-blake3-aes-256-gcm"); username = & { diff --git a/backend/src/core/proxy-utils/parsers/peggy/surge.js b/backend/src/core/proxy-utils/parsers/peggy/surge.js index cefe5f59..59375821 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/surge.js +++ b/backend/src/core/proxy-utils/parsers/peggy/surge.js @@ -339,6 +339,12 @@ const grammars = String.raw` }); return result; } + function normalizeVmessSecurity(security) { + const normalized = String(security || "").trim().toLowerCase(); + const supported = ["aes-128-gcm", "chacha20-ietf-poly1305"]; + if (!supported.includes(normalized)) return "auto"; + return normalized === "chacha20-ietf-poly1305" ? "chacha20-poly1305" : normalized; + } } start = (anytls/shadowsocks/vmess/trojan/h2_connect/https/http/snell/socks5/socks5_tls/tuic/tuic_v5/wireguard/hysteria2/ssh/trust_tunnel/direct) { @@ -356,9 +362,9 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/ } handleShadowTLS(); } -vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { +vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* { proxy.type = "vmess"; - proxy.cipher = proxy.cipher || "none"; + proxy.cipher = proxy.cipher || "auto"; // Surfboard 与 Surge 默认不一致, 不管 Surfboard https://getsurfboard.com/docs/profile-format/proxy/external-proxy/vmess if (proxy.aead) { proxy.alterId = 0; @@ -523,6 +529,9 @@ vmess_aead = comma "vmess-aead" equals flag:bool { proxy.aead = flag; } method = comma "encrypt-method" equals cipher:cipher { proxy.cipher = cipher; } +vmess_method = comma "encrypt-method" equals cipher:$[^,]+ { + proxy.cipher = normalizeVmessSecurity(cipher); +} cipher = ("aes-128-cfb"/"aes-128-ctr"/"aes-128-gcm"/"aes-192-cfb"/"aes-192-ctr"/"aes-192-gcm"/"aes-256-cfb"/"aes-256-ctr"/"aes-256-gcm"/"bf-cfb"/"camellia-128-cfb"/"camellia-192-cfb"/"camellia-256-cfb"/"cast5-cfb"/"chacha20-ietf-poly1305"/"chacha20-ietf"/"chacha20-poly1305"/"chacha20"/"des-cfb"/"idea-cfb"/"none"/"rc2-cfb"/"rc4-md5"/"rc4"/"salsa20"/"seed-cfb"/"xchacha20-ietf-poly1305"/"2022-blake3-aes-128-gcm"/"2022-blake3-aes-256-gcm"); ws = comma "ws" equals flag:bool { obfs.type = "ws"; } diff --git a/backend/src/core/proxy-utils/producers/clash.js b/backend/src/core/proxy-utils/producers/clash.js index 09ad3ed7..28548c69 100644 --- a/backend/src/core/proxy-utils/producers/clash.js +++ b/backend/src/core/proxy-utils/producers/clash.js @@ -2,6 +2,7 @@ import { isPresent, produceProxyListOutput, } from '@/core/proxy-utils/producers/utils'; +import { normalizeClashVmessSecurity } from '../vmess-security'; import { deleteHttpUpgradeEarlyDataMetadata, normalizeWebSocketEarlyDataPath, @@ -81,17 +82,7 @@ export default function Clash_Producer() { delete proxy.sni; } // https://dreamacro.github.io/clash/configuration/outbound.html#vmess - if ( - isPresent(proxy, 'cipher') && - ![ - 'auto', - 'aes-128-gcm', - 'chacha20-poly1305', - 'none', - ].includes(proxy.cipher) - ) { - proxy.cipher = 'auto'; - } + proxy.cipher = normalizeClashVmessSecurity(proxy.cipher); } else if (proxy.type === 'wireguard') { proxy.keepalive = proxy.keepalive ?? proxy['persistent-keepalive']; diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index d335de9b..5c496581 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -12,6 +12,7 @@ import { } from '../transport-path'; import { ECH_DNS_FIELD } from '../ech-utils'; import $ from '@/core/app'; +import { normalizeVmessSecurity } from '../vmess-security'; const ipVersions = { dual: 'dual', @@ -161,18 +162,7 @@ export default function ClashMeta_Producer() { } // https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml#L400 // https://stash.wiki/proxy-protocols/proxy-types#vmess - if ( - isPresent(proxy, 'cipher') && - ![ - 'auto', - 'none', - 'zero', - 'aes-128-gcm', - 'chacha20-poly1305', - ].includes(proxy.cipher) - ) { - proxy.cipher = 'auto'; - } + proxy.cipher = normalizeVmessSecurity(proxy.cipher); } else if (proxy.type === 'tuic') { if (isPresent(proxy, 'alpn')) { proxy.alpn = Array.isArray(proxy.alpn) @@ -315,9 +305,9 @@ export default function ClashMeta_Producer() { proxy['h2-opts']?.headers?.host ?? proxy['h2-opts']?.headers?.Host; if ( - (isPresent(proxy, 'h2-opts.host') || - isPresent(proxy, 'h2-opts.headers.host') || - isPresent(proxy, 'h2-opts.headers.Host')) + isPresent(proxy, 'h2-opts.host') || + isPresent(proxy, 'h2-opts.headers.host') || + isPresent(proxy, 'h2-opts.headers.Host') ) { proxy['h2-opts'].host = Array.isArray(host) ? host diff --git a/backend/src/core/proxy-utils/producers/egern.js b/backend/src/core/proxy-utils/producers/egern.js index c1cf5252..adc7b583 100644 --- a/backend/src/core/proxy-utils/producers/egern.js +++ b/backend/src/core/proxy-utils/producers/egern.js @@ -4,6 +4,7 @@ import { isPresent, produceProxyListOutput, } from './utils'; +import { normalizeVmessSecurity } from '../vmess-security'; export default function Egern_Producer() { const type = 'ALL'; @@ -272,19 +273,7 @@ export default function Egern_Producer() { }; } else if (proxy.type === 'vmess') { // Egern:传输层,支持 ws/wss/http1/http2/tls,不配置则为 tcp - let security = proxy.cipher; - if ( - security && - ![ - 'auto', - 'none', - 'zero', - 'aes-128-gcm', - 'chacha20-poly1305', - ].includes(security) - ) { - security = 'auto'; - } + const security = normalizeVmessSecurity(proxy.cipher); if (proxy.network === 'ws') { proxy.transport = { [proxy.tls ? 'wss' : 'ws']: { diff --git a/backend/src/core/proxy-utils/producers/loon.js b/backend/src/core/proxy-utils/producers/loon.js index 20db49a1..0bd97c7d 100644 --- a/backend/src/core/proxy-utils/producers/loon.js +++ b/backend/src/core/proxy-utils/producers/loon.js @@ -3,6 +3,7 @@ const targetPlatform = 'Loon'; import { isPresent, Result } from './utils'; import { isIPv4, isIPv6 } from '@/utils'; import $ from '@/core/app'; +import { formatLoonVmessSecurity } from '../vmess-security'; const ipVersions = { dual: 'dual', @@ -405,10 +406,11 @@ function anytls(proxy) { function vmess(proxy) { const isReality = !!proxy['reality-opts']; + const security = formatLoonVmessSecurity(proxy.cipher); const result = new Result(proxy); result.append( - `${proxy.name}=vmess,${proxy.server},${proxy.port},${proxy.cipher},"${proxy.uuid}"`, + `${proxy.name}=vmess,${proxy.server},${proxy.port},${security},"${proxy.uuid}"`, ); if (proxy.network === 'tcp') { delete proxy.network; diff --git a/backend/src/core/proxy-utils/producers/qx.js b/backend/src/core/proxy-utils/producers/qx.js index 0b41335f..9ba35342 100644 --- a/backend/src/core/proxy-utils/producers/qx.js +++ b/backend/src/core/proxy-utils/producers/qx.js @@ -1,4 +1,5 @@ import { isPresent, isShadowsocksOverTls, Result } from './utils'; +import { formatQXVmessMethod } from '../vmess-security'; const targetPlatform = 'QX'; @@ -333,12 +334,7 @@ function vmess(proxy) { append(`vmess=${proxy.server}:${proxy.port}`); // cipher - let cipher; - if (proxy.cipher === 'auto') { - cipher = 'chacha20-ietf-poly1305'; - } else { - cipher = proxy.cipher; - } + let cipher = formatQXVmessMethod(proxy.cipher); append(`,method=${cipher}`); append(`,password=${proxy.uuid}`); diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index 9606e9f0..64450cf0 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -10,6 +10,7 @@ import { normalizeWebSocketEarlyDataPath, } from '../transport-path'; import $ from '@/core/app'; +import { normalizeVmessSecurity } from '../vmess-security'; export default function Shadowrocket_Producer() { const type = 'ALL'; @@ -63,18 +64,7 @@ export default function Shadowrocket_Producer() { } // https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml#L400 // https://stash.wiki/proxy-protocols/proxy-types#vmess - if ( - isPresent(proxy, 'cipher') && - ![ - 'auto', - 'none', - 'zero', - 'aes-128-gcm', - 'chacha20-poly1305', - ].includes(proxy.cipher) - ) { - proxy.cipher = 'auto'; - } + proxy.cipher = normalizeVmessSecurity(proxy.cipher); } else if (proxy.type === 'tuic') { if (isPresent(proxy, 'alpn')) { proxy.alpn = Array.isArray(proxy.alpn) diff --git a/backend/src/core/proxy-utils/producers/sing-box.js b/backend/src/core/proxy-utils/producers/sing-box.js index 1d79f5e2..4a54aeed 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -7,6 +7,7 @@ import { getSafeIntegerPathQueryParam, parseSafeIntegerValue, } from '../transport-path'; +import { normalizeVmessSecurity } from '../vmess-security'; const ipVersions = { ipv4: 'ipv4_only', @@ -742,7 +743,8 @@ const vmessVlessPacketEncodingParser = (proxy, parsedProxy) => { const packetEncoding = normalizeSingBoxPacketEncoding( proxy['packet-encoding'], ); - if (packetEncoding != null) parsedProxy.packet_encoding = packetEncoding; + if (packetEncoding != null) + parsedProxy.packet_encoding = packetEncoding; } else if (proxy.xudp) { parsedProxy.packet_encoding = 'xudp'; } else if (proxy['packet-addr']) { @@ -767,21 +769,10 @@ const vmessParser = (proxy = {}) => { server: proxy.server, server_port: parseInt(`${proxy.port}`, 10), uuid: proxy.uuid, - security: proxy.cipher, + security: normalizeVmessSecurity(proxy.cipher), alter_id: parseInt(`${proxy.alterId}`, 10), tls: { enabled: false, server_name: proxy.server, insecure: false }, }; - if ( - [ - 'auto', - 'none', - 'zero', - 'aes-128-gcm', - 'chacha20-poly1305', - 'aes-128-ctr', - ].indexOf(parsedProxy.security) === -1 - ) - parsedProxy.security = 'auto'; if (parsedProxy.server_port < 0 || parsedProxy.server_port > 65535) throw 'invalid port'; vmessProtocolOptionsParser(proxy, parsedProxy); diff --git a/backend/src/core/proxy-utils/producers/stash.js b/backend/src/core/proxy-utils/producers/stash.js index 0476c7f0..a99e0e84 100644 --- a/backend/src/core/proxy-utils/producers/stash.js +++ b/backend/src/core/proxy-utils/producers/stash.js @@ -3,6 +3,7 @@ import { produceProxyListOutput, supportsShadowsocksV2rayPluginMode, } from '@/core/proxy-utils/producers/utils'; +import { normalizeClashVmessSecurity } from '../vmess-security'; import { deleteHttpUpgradeEarlyDataMetadata, normalizeWebSocketEarlyDataPath, @@ -108,17 +109,7 @@ export default function Stash_Producer() { } // https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml#L400 // https://stash.wiki/proxy-protocols/proxy-types#vmess - if ( - isPresent(proxy, 'cipher') && - ![ - 'auto', - 'aes-128-gcm', - 'chacha20-poly1305', - 'none', - ].includes(proxy.cipher) - ) { - proxy.cipher = 'auto'; - } + proxy.cipher = normalizeClashVmessSecurity(proxy.cipher); } else if (proxy.type === 'tuic') { if (isPresent(proxy, 'alpn')) { proxy.alpn = Array.isArray(proxy.alpn) diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index e5ba13b2..811b0f43 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -1,6 +1,7 @@ import { Result, isPresent } from './utils'; import { isNotBlank, getIfNotBlank } from '@/utils'; import $ from '@/core/app'; +import { formatSurgeVmessEncryptMethod } from '../vmess-security'; const targetPlatform = 'Surge'; @@ -595,6 +596,10 @@ function vmess(proxy, includeUnsupportedProxy) { const result = new Result(proxy); result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`); result.appendIfPresent(`,username=${proxy.uuid}`, 'uuid'); + const encryptMethod = formatSurgeVmessEncryptMethod(proxy.cipher); + if (encryptMethod) { + result.append(`,encrypt-method=${encryptMethod}`); + } const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version']; result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version'); diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index fd75770a..729fe0c2 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -18,6 +18,7 @@ import { buildXrayEchConfigListFromMihomo, buildXrayEchFieldsFromMihomo, } from '../ech-utils'; +import { normalizeVmessSecurity } from '../vmess-security'; function toStringHeaderMap(headers, { excludeHost = false } = {}) { if (!isPlainObject(headers)) { @@ -131,19 +132,6 @@ function getTransportHost(network, transportOpts = {}) { ); } -function normalizeVmessSecurity(security) { - if ( - security && - !['aes-128-gcm', 'chacha20-poly1305', 'auto', 'none', 'zero'].includes( - security, - ) - ) { - return 'auto'; - } - - return security; -} - function mapReuseSettingsToXmux(reuseSettings) { if (!isPlainObject(reuseSettings)) { return undefined; diff --git a/backend/src/core/proxy-utils/vmess-security.js b/backend/src/core/proxy-utils/vmess-security.js new file mode 100644 index 00000000..e6605dc7 --- /dev/null +++ b/backend/src/core/proxy-utils/vmess-security.js @@ -0,0 +1,86 @@ +export const VMESS_SECURITY_AUTO = 'auto'; + +export const VMESS_SECURITY_COMMON_VALUES = [ + VMESS_SECURITY_AUTO, + 'none', + 'zero', + 'aes-128-gcm', + 'chacha20-poly1305', +]; + +export const VMESS_SECURITY_CLASH_VALUES = [ + VMESS_SECURITY_AUTO, + 'aes-128-gcm', + 'chacha20-poly1305', + 'none', +]; + +const VMESS_SECURITY_QX_METHOD_VALUES = ['none', 'chacha20-poly1305']; + +const VMESS_SECURITY_ALIASES = { + 'chacha20-ietf-poly1305': 'chacha20-poly1305', +}; + +function normalizeSecurityValue(security) { + if (security == null) return ''; + return `${security}`.trim().toLowerCase(); +} + +function canonicalizeVmessSecurity(security) { + return VMESS_SECURITY_ALIASES[security] || security; +} + +export function normalizeVmessSecurity( + security, + supportedValues = VMESS_SECURITY_COMMON_VALUES, + { acceptAliases = true, fallback = VMESS_SECURITY_AUTO } = {}, +) { + const normalized = normalizeSecurityValue(security); + if (!normalized) return fallback; + + const normalizedSupported = supportedValues.map(normalizeSecurityValue); + if (normalizedSupported.includes(normalized)) { + return canonicalizeVmessSecurity(normalized); + } + + const canonical = canonicalizeVmessSecurity(normalized); + const canonicalSupported = normalizedSupported.map( + canonicalizeVmessSecurity, + ); + if (acceptAliases && canonicalSupported.includes(canonical)) { + return canonical; + } + + return fallback; +} + +export function normalizeClashVmessSecurity(security) { + return normalizeVmessSecurity(security, VMESS_SECURITY_CLASH_VALUES); +} + +export function formatQXVmessMethod(security) { + return normalizeVmessSecurity(security, VMESS_SECURITY_QX_METHOD_VALUES, { + fallback: 'chacha20-poly1305', + }); +} + +export function formatLoonVmessSecurity(security) { + const normalized = normalizeClashVmessSecurity(security); + + return normalized === 'chacha20-poly1305' + ? 'chacha20-ietf-poly1305' + : normalized; +} + +export function formatSurgeVmessEncryptMethod(security) { + const normalized = normalizeVmessSecurity(security, [ + 'aes-128-gcm', + 'chacha20-poly1305', + ]); + + if (normalized === VMESS_SECURITY_AUTO) return undefined; + + return normalized === 'chacha20-poly1305' + ? 'chacha20-ietf-poly1305' + : normalized; +} diff --git a/backend/src/test/proxy-parsers/pipeline.spec.js b/backend/src/test/proxy-parsers/pipeline.spec.js index 4509aa0d..3cdd3f34 100644 --- a/backend/src/test/proxy-parsers/pipeline.spec.js +++ b/backend/src/test/proxy-parsers/pipeline.spec.js @@ -52,7 +52,9 @@ describe('Proxy parser pipeline coverage', function () { ], }; - const proxy = parseOne(`ssd://${Base64.encode(JSON.stringify(payload))}`); + const proxy = parseOne( + `ssd://${Base64.encode(JSON.stringify(payload))}`, + ); expectSubset(proxy, { type: 'ss', @@ -162,6 +164,25 @@ FINAL,DIRECT }); }); + it('defaults invalid VMess cipher to auto for Clash-style object inputs', function () { + const proxy = parseOne( + JSON.stringify({ + name: 'vmess-invalid-cipher', + type: 'vmess', + server: 'vmess.example.com', + port: 443, + uuid: UUID, + cipher: 'aes-128-ctr', + }), + ); + + expectSubset(proxy, { + type: 'vmess', + name: 'vmess-invalid-cipher', + cipher: 'auto', + }); + }); + it('drops invalid mihomo hop-interval values for Clash-style object inputs', function () { const invalidHopIntervals = [ { title: 'zero string', value: '0' }, diff --git a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js index 08cd61a4..4fe8a8f1 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -151,6 +151,30 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('keeps supported zero VMess security values', function () { + const share = Base64.encode( + JSON.stringify({ + ps: 'VMess Zero Security', + add: 'vmess-zero.example.com', + port: '443', + id: UUID, + aid: '0', + scy: 'zero', + }), + ); + const proxy = parseOne(`vmess://${share}`); + + expectSubset(proxy, { + type: 'vmess', + name: 'VMess Zero Security', + server: 'vmess-zero.example.com', + port: 443, + cipher: 'zero', + uuid: UUID, + alterId: 0, + }); + }); + it('parses V2rayN http transport shares as h2', function () { const share = Base64.encode( JSON.stringify({ @@ -2641,7 +2665,6 @@ describe('Platform raw-format parser coverage', function () { }, }, ]); - }); describe('Loon raw inputs', function () { @@ -2706,6 +2729,32 @@ describe('Platform raw-format parser coverage', function () { }, }, }, + { + title: 'canonicalizes Loon vmess chacha20 security', + input: `Loon VMess Chacha=vmess,loon-vmess-chacha.example.com,443,chacha20-ietf-poly1305,"${UUID}"`, + expected: { + type: 'vmess', + name: 'Loon VMess Chacha', + server: 'loon-vmess-chacha.example.com', + port: 443, + cipher: 'chacha20-poly1305', + uuid: UUID, + alterId: 0, + }, + }, + { + title: 'defaults invalid Loon vmess security to auto', + input: `Loon VMess Invalid=vmess,loon-vmess-invalid.example.com,443,unknown-cipher,"${UUID}"`, + expected: { + type: 'vmess', + name: 'Loon VMess Invalid', + server: 'loon-vmess-invalid.example.com', + port: 443, + cipher: 'auto', + uuid: UUID, + alterId: 0, + }, + }, { title: 'parses vless websocket reality lines', input: `Loon VLESS=vless,loon-vless.example.com,443,"${UUID}",transport=ws,host=cdn.example.com,path=/ws,over-tls=true,tls-name=sni.example.com,skip-cert-verify=true,flow=xtls-rprx-vision,public-key=pubkey,short-id=08`, @@ -3188,6 +3237,34 @@ describe('Platform raw-format parser coverage', function () { }, }, }, + { + title: 'canonicalizes Surge vmess chacha20 encrypt-method', + input: `Surge VMess Chacha = vmess,surge-vmess-chacha.example.com,443,username=${UUID},encrypt-method=chacha20-ietf-poly1305,vmess-aead=true`, + expected: { + type: 'vmess', + name: 'Surge VMess Chacha', + server: 'surge-vmess-chacha.example.com', + port: 443, + uuid: UUID, + cipher: 'chacha20-poly1305', + aead: true, + alterId: 0, + }, + }, + { + title: 'defaults invalid Surge vmess encrypt-method to auto', + input: `Surge VMess Invalid = vmess,surge-vmess-invalid.example.com,443,username=${UUID},encrypt-method=none,vmess-aead=true`, + expected: { + type: 'vmess', + name: 'Surge VMess Invalid', + server: 'surge-vmess-invalid.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + aead: true, + alterId: 0, + }, + }, { title: 'parses vmess websocket headers with quoted comma values and pipe separators', input: `Surge VMess WS Headers = vmess,surge-vmess.example.com,443,username=${UUID},ws=true,ws-path=/vmess,ws-headers="Host:"cdn.example.com" | X-Comma:"a,b" | User-Agent:"okhttp/3.11.0 Dalvik/2.1.0 (Linux; U; Android 11)"",skip-cert-verify=true,sni=sni.example.com,tls=true,vmess-aead=true`, diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 6672b42b..5d2dfe44 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -94,6 +94,132 @@ describe('Proxy structured producers', function () { }); }); + it('normalizes VMess security values for documented target platforms', function () { + const invalidSecurityProxy = { + type: 'vmess', + name: 'VMess Invalid Security', + server: 'vmess-invalid.example.com', + port: 443, + uuid: UUID, + cipher: 'aes-128-ctr', + alterId: 0, + }; + const chachaProxy = { + ...invalidSecurityProxy, + name: 'VMess Chacha Security', + cipher: 'chacha20-poly1305', + }; + const legacyChachaAliasProxy = { + ...invalidSecurityProxy, + name: 'VMess Chacha Alias Security', + cipher: 'chacha20-ietf-poly1305', + }; + const noneSecurityProxy = { + ...invalidSecurityProxy, + name: 'VMess None Security', + cipher: 'none', + }; + const zeroSecurityProxy = { + ...invalidSecurityProxy, + name: 'VMess Zero Security', + cipher: 'zero', + }; + const getEgernVmess = (items, name) => + items.find((item) => item.vmess?.name === name)?.vmess; + + expectSubset( + getEgernVmess( + produceInternal('Egern', invalidSecurityProxy), + 'VMess Invalid Security', + ), + { + security: 'auto', + }, + ); + expectSubset( + getEgernVmess( + loadProducedYaml('Egern', invalidSecurityProxy).proxies, + 'VMess Invalid Security', + ), + { + security: 'auto', + }, + ); + + expect(produceExternal('Loon', invalidSecurityProxy)).to.include( + ',auto,"', + ); + expect(produceExternal('Loon', chachaProxy)).to.include( + ',chacha20-ietf-poly1305,"', + ); + + expect(produceExternal('Surge', invalidSecurityProxy)).to.not.include( + 'encrypt-method=', + ); + expect(produceExternal('Surge', chachaProxy)).to.include( + ',encrypt-method=chacha20-ietf-poly1305', + ); + + expect(produceExternal('QX', invalidSecurityProxy)).to.include( + 'method=chacha20-poly1305', + ); + expect(produceExternal('QX', legacyChachaAliasProxy)).to.include( + 'method=chacha20-poly1305', + ); + expect(produceExternal('QX', noneSecurityProxy)).to.include( + 'method=none', + ); + + for (const platform of ['Clash', 'Stash']) { + expect( + loadProducedYaml(platform, invalidSecurityProxy).proxies[0] + .cipher, + ).to.equal('auto'); + expect( + loadProducedYaml(platform, legacyChachaAliasProxy).proxies[0] + .cipher, + ).to.equal('chacha20-poly1305'); + expect( + loadProducedYaml(platform, noneSecurityProxy).proxies[0].cipher, + ).to.equal('none'); + expect( + loadProducedYaml(platform, zeroSecurityProxy).proxies[0].cipher, + ).to.equal('auto'); + } + + expect( + loadProducedYaml('Mihomo', invalidSecurityProxy).proxies[0].cipher, + ).to.equal('auto'); + expect( + loadProducedYaml('Mihomo', legacyChachaAliasProxy).proxies[0] + .cipher, + ).to.equal('chacha20-poly1305'); + expect( + loadProducedYaml('Mihomo', zeroSecurityProxy).proxies[0].cipher, + ).to.equal('zero'); + expect( + loadProducedYaml('Shadowrocket', invalidSecurityProxy).proxies[0] + .cipher, + ).to.equal('auto'); + expect( + loadProducedYaml('Shadowrocket', legacyChachaAliasProxy).proxies[0] + .cipher, + ).to.equal('chacha20-poly1305'); + expect( + loadProducedYaml('Shadowrocket', zeroSecurityProxy).proxies[0] + .cipher, + ).to.equal('zero'); + + expect( + loadProducedJson('sing-box', invalidSecurityProxy).outbounds[0] + .security, + ).to.equal('auto'); + expect( + loadProducedJson('sing-box', legacyChachaAliasProxy).outbounds[0] + .security, + ).to.equal('chacha20-poly1305'); + }); + it('keeps unsupported Clash proxies when include-unsupported-proxy is enabled', function () { const internal = produceInternal( 'Clash',