diff --git a/backend/package.json b/backend/package.json index feb2d5d4..32144139 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.89", + "version": "2.21.91", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 5be0f1a7..bd850e0a 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -23,7 +23,7 @@ import { findByName } from '@/utils/database'; import { produceArtifact } from '@/restful/sync'; import { getFlag, removeFlag, getISO, MMDB } from '@/utils/geo'; import Gist from '@/utils/gist'; -import { isPresent } from './producers/utils'; +import { isPresent, isShadowsocksOverTls } from './producers/utils'; import { doh } from '@/utils/dns'; import JSON5 from 'json5'; import { hex_md5 } from '@/vendor/md5'; @@ -247,6 +247,13 @@ function produce(proxies, targetPlatform, type, opts = {}) { throw new Error(`Target platform: ${targetPlatform} is not supported!`); } + const normalizedTarget = String(targetPlatform).toLowerCase(); + const supportedShadowsocksOverTlsTargets = new Set([ + 'qx', + 'quantumultx', + 'shadowrocket', + ]); + const sni_off_supported = /Surge|SurgeMac|Shadowrocket/i.test( targetPlatform, ); @@ -258,6 +265,14 @@ function produce(proxies, targetPlatform, type, opts = {}) { return false; } + if ( + isShadowsocksOverTls(proxy) && + !supportedShadowsocksOverTlsTargets.has(normalizedTarget) && + !opts['include-unsupported-proxy'] + ) { + return false; + } + // 对于 vless 和 vmess 代理,需要额外验证 UUID if (['vless', 'vmess'].includes(proxy.type)) { const isProxyUUIDValid = isValidUUID(proxy.uuid); diff --git a/backend/src/core/proxy-utils/parsers/peggy/qx.js b/backend/src/core/proxy-utils/parsers/peggy/qx.js index fb2caa72..235c9b44 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/qx.js +++ b/backend/src/core/proxy-utils/parsers/peggy/qx.js @@ -20,6 +20,15 @@ const grammars = String.raw` const obfs = {}; const $ = {}; + function setQxHttpObfs(type) { + // Preserve the original QX http-obfs token for round-trip output, + // including the upstream "vemss-http" typo that appears in QX + // examples. + proxy._qx_obfs_http = type; + obfs.type = "http"; + return type; + } + function handleObfs() { if (obfs.type === "ws" || obfs.type === "wss") { proxy.network = "ws"; @@ -73,9 +82,12 @@ shadowsocks = "shadowsocks" equals address $set(proxy, "plugin-opts.tls", true); } } else if (obfs.type === 'over-tls') { - throw new Error('ss over-tls is not supported'); + proxy.tls = true; + if (obfs.host) { + proxy.sni = obfs.host; + } } - if (obfs.type) { + if (obfs.type && obfs.type !== 'over-tls') { $set(proxy, "plugin-opts.host", obfs.host); $set(proxy, "plugin-opts.path", obfs.path); } @@ -83,7 +95,7 @@ shadowsocks = "shadowsocks" equals address } vmess = "vmess" equals address - (uuid/method/over_tls/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/tag/obfs/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/server_check_url/reality_base64_pubkey/reality_hex_shortid/others)* { + (uuid/method/over_tls/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/tag/obfs_vmess/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/server_check_url/reality_base64_pubkey/reality_hex_shortid/others)* { proxy.type = "vmess"; proxy.cipher = proxy.cipher || "none"; if (proxy.aead === false) { @@ -95,7 +107,7 @@ vmess = "vmess" equals address } vless = "vless" equals address - (uuid/method/over_tls/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/tag/obfs/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/server_check_url/reality_base64_pubkey/reality_hex_shortid/vless_flow/others)* { + (uuid/method/over_tls/tls_host/tls_pubkey_sha256/tls_alpn/tls_no_session_ticket/tls_no_session_reuse/tls_fingerprint/tls_verification/tag/obfs_vless/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/server_check_url/reality_base64_pubkey/reality_hex_shortid/vless_flow/others)* { proxy.type = "vless"; proxy.cipher = proxy.cipher || "none"; handleObfs(); @@ -176,9 +188,32 @@ tls_no_session_reuse = comma "tls-no-session-reuse" equals flag:bool { proxy["tls-no-session-reuse"] = flag; } -obfs_ss = comma "obfs" equals type:("http"/"tls"/"wss"/"ws"/"over-tls") { obfs.type = type; return type; } +obfs_ss = comma "obfs" equals ( + type:("tls"/"wss"/"ws"/"over-tls") { obfs.type = type; return type; } + / type:("http"/"vmess-http"/"vemss-http"/"shadowsocks-http") { + // QX accepts multiple http-obfs spellings for ss/vmess/vless; keep + // the original token so QX output can round-trip it unchanged. + return setQxHttpObfs(type); + } +) obfs_ssr = comma "obfs" equals type:("plain"/"http_simple"/"http_post"/"random_head"/"tls1.2_ticket_auth"/"tls1.2_ticket_fastauth") { proxy.type = "ssr"; obfs.type = type; return type; } obfs = comma "obfs" equals type:("wss"/"ws"/"over-tls"/"http") { obfs.type = type; return type; }; +obfs_vmess = comma "obfs" equals ( + type:("wss"/"ws"/"over-tls") { obfs.type = type; return type; } + / type:("http"/"vmess-http"/"vemss-http"/"shadowsocks-http") { + // QX accepts multiple http-obfs spellings for ss/vmess/vless; keep + // the original token so QX output can round-trip it unchanged. + return setQxHttpObfs(type); + } +); +obfs_vless = comma "obfs" equals ( + type:("wss"/"ws"/"over-tls") { obfs.type = type; return type; } + / type:("http"/"vmess-http"/"vemss-http"/"shadowsocks-http") { + // QX accepts multiple http-obfs spellings for ss/vmess/vless; keep + // the original token so QX output can round-trip it unchanged. + return setQxHttpObfs(type); + } +); obfs_host = comma "obfs-host" equals match:[^,]+ { obfs.host = match.join("").replace(/^"(.*)"$/, '$1'); } obfs_uri = comma "obfs-uri" equals uri:uri { obfs.path = uri; } diff --git a/backend/src/core/proxy-utils/producers/qx.js b/backend/src/core/proxy-utils/producers/qx.js index 5379ce78..a5e52503 100644 --- a/backend/src/core/proxy-utils/producers/qx.js +++ b/backend/src/core/proxy-utils/producers/qx.js @@ -1,4 +1,4 @@ -import { isPresent, Result } from './utils'; +import { isPresent, isShadowsocksOverTls, Result } from './utils'; const targetPlatform = 'QX'; @@ -54,10 +54,24 @@ export default function QX_Producer() { }; } +function getQxHttpObfs(proxy) { + // QX accepts multiple http-obfs spellings for ss/vmess/vless. Preserve + // the original token for round-trip output, including "vemss-http". + return [ + 'http', + 'vmess-http', + 'vemss-http', + 'shadowsocks-http', + ].includes(proxy._qx_obfs_http) + ? proxy._qx_obfs_http + : 'http'; +} + function shadowsocks(proxy) { const result = new Result(proxy); const append = result.append.bind(result); const appendIfPresent = result.appendIfPresent.bind(result); + const isSSOverTls = isShadowsocksOverTls(proxy); if (!proxy.cipher) { proxy.cipher = 'none'; } @@ -98,10 +112,23 @@ function shadowsocks(proxy) { if (needTls(proxy)) { proxy.tls = true; } - if (isPresent(proxy, 'plugin')) { + if (isSSOverTls) { + append(`,obfs=over-tls`); + if (isPresent(proxy, 'sni')) { + append(`,obfs-host=${proxy.sni}`); + } else { + appendIfPresent(`,obfs-host=${proxy.servername}`, 'servername'); + } + } else if (isPresent(proxy, 'plugin')) { if (proxy.plugin === 'obfs') { const opts = proxy['plugin-opts']; - append(`,obfs=${opts.mode}`); + if (opts.mode === 'http') { + // Keep the original QX http-obfs token instead of collapsing + // it back to plain "http". + append(`,obfs=${getQxHttpObfs(proxy)}`); + } else { + append(`,obfs=${opts.mode}`); + } } else if ( proxy.plugin === 'v2ray-plugin' && proxy['plugin-opts'].mode === 'websocket' @@ -147,7 +174,9 @@ function shadowsocks(proxy) { `,tls-verification=${!proxy['skip-cert-verify']}`, 'skip-cert-verify', ); - appendIfPresent(`,tls-host=${proxy.sni}`, 'sni'); + if (!isSSOverTls) { + appendIfPresent(`,tls-host=${proxy.sni}`, 'sni'); + } } // tfo @@ -325,7 +354,7 @@ function vmess(proxy) { if (proxy.tls) append(`,obfs=wss`); else append(`,obfs=ws`); } else if (proxy.network === 'http') { - append(`,obfs=http`); + append(`,obfs=${getQxHttpObfs(proxy)}`); } else if (['tcp'].includes(proxy.network)) { if (proxy.tls) append(`,obfs=over-tls`); } else if (!['tcp'].includes(proxy.network)) { @@ -431,7 +460,7 @@ function vless(proxy) { if (proxy.tls) append(`,obfs=wss`); else append(`,obfs=ws`); } else if (proxy.network === 'http') { - append(`,obfs=http`); + append(`,obfs=${getQxHttpObfs(proxy)}`); } else if (['tcp'].includes(proxy.network)) { if (proxy.tls) append(`,obfs=over-tls`); } else if (!['tcp'].includes(proxy.network)) { diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index aceda9ec..e8eaee03 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -1,5 +1,6 @@ import { isPresent, + isShadowsocksOverTls, produceProxyListOutput, } from '@/core/proxy-utils/producers/utils'; import $ from '@/core/app'; @@ -156,6 +157,13 @@ export default function Shadowrocket_Producer() { delete proxy['shadow-tls-sni']; delete proxy['shadow-tls-version']; } + if (isShadowsocksOverTls(proxy)) { + if (isPresent(proxy, 'sni')) { + proxy.servername = proxy.sni; + // 先不删 没有明确的规范 + // delete proxy.sni; + } + } } if ( diff --git a/backend/src/core/proxy-utils/producers/utils.js b/backend/src/core/proxy-utils/producers/utils.js index fee1b8f4..6abccc8f 100644 --- a/backend/src/core/proxy-utils/producers/utils.js +++ b/backend/src/core/proxy-utils/producers/utils.js @@ -30,6 +30,19 @@ export function isPresent(obj, attr) { return typeof data !== 'undefined' && data !== null; } +export function isShadowsocksOverTls(proxy) { + const normalizedNetwork = + typeof proxy?.network === 'string' + ? proxy.network.trim().toLowerCase() + : proxy?.network; + return ( + proxy?.type === 'ss' && + proxy?.tls === true && + !isPresent(proxy, 'plugin') && + (!isPresent(proxy, 'network') || normalizedNetwork === 'tcp') + ); +} + export function normalizePluginMuxValue(mux) { if (typeof mux === 'boolean') return Number(mux); if (typeof mux === 'string') { 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 7c392817..efa3abd3 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -803,6 +803,130 @@ describe('Platform raw-format parser coverage', function () { describe('Quantumult X raw inputs', function () { registerCases([ + { + title: 'parses shadowsocks over-tls lines into canonical tls fields', + input: 'shadowsocks=qx-ss-tls.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,obfs-host=a.com,tls-verification=false,udp-relay=true,fast-open=true,tag=QX SS Over TLS', + expected: { + type: 'ss', + name: 'QX SS Over TLS', + server: 'qx-ss-tls.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + 'skip-cert-verify': true, + udp: true, + tfo: true, + }, + }, + { + title: 'parses shadowsocks over-tls lines with tls-verification=true', + input: 'shadowsocks=qx-ss-tls-verified.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,obfs-host=verify.example.com,tls-verification=true,tag=QX SS Over TLS Verified', + expected: { + type: 'ss', + name: 'QX SS Over TLS Verified', + server: 'qx-ss-tls-verified.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'verify.example.com', + 'skip-cert-verify': false, + }, + }, + { + title: 'parses shadowsocks over-tls lines without obfs-host', + input: 'shadowsocks=qx-ss-tls-no-host.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,udp-relay=true,tag=QX SS Over TLS No Host', + expected: { + type: 'ss', + name: 'QX SS Over TLS No Host', + server: 'qx-ss-tls-no-host.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + udp: true, + }, + }, + { + title: 'keeps legacy shadowsocks obfs tls lines as plugin nodes', + input: 'shadowsocks=qx-ss-obfs.example.com:8388,method=aes-128-gcm,password=secret,obfs=tls,obfs-host=obfs.example.com,tag=QX SS Obfs TLS', + expected: { + type: 'ss', + name: 'QX SS Obfs TLS', + server: 'qx-ss-obfs.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + 'plugin-opts': { + mode: 'tls', + host: 'obfs.example.com', + }, + }, + }, + { + // QX accepts plain "http" as one of the shared http-obfs + // tokens for ss/vmess/vless; preserve it for round-trip output. + title: 'parses shadowsocks http lines as http obfs and keeps the QX token', + input: 'shadowsocks=qx-ss-http-plain.example.com:8388,method=aes-128-gcm,password=secret,obfs=http,obfs-host=plain.example.com,obfs-uri=/plain,tag=QX SS HTTP', + expected: { + type: 'ss', + name: 'QX SS HTTP', + server: 'qx-ss-http-plain.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + _qx_obfs_http: 'http', + 'plugin-opts': { + mode: 'http', + host: 'plain.example.com', + path: '/plain', + }, + }, + }, + { + // QX examples contain the upstream "vemss-http" typo; keep + // parsing it so the original line can round-trip unchanged. + title: 'parses shadowsocks vemss-http lines as http obfs and keeps the QX alias', + input: 'shadowsocks=qx-ss-http.example.com:8388,method=aes-128-gcm,password=secret,obfs=vemss-http,obfs-host=obfs.example.com,obfs-uri=/resource,tag=QX SS VMess HTTP', + expected: { + type: 'ss', + name: 'QX SS VMess HTTP', + server: 'qx-ss-http.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + _qx_obfs_http: 'vemss-http', + 'plugin-opts': { + mode: 'http', + host: 'obfs.example.com', + path: '/resource', + }, + }, + }, + { + title: 'parses shadowsocks shadowsocks-http lines as http obfs and keeps the QX token', + input: 'shadowsocks=qx-ss-shadowsocks-http.example.com:8388,method=aes-128-gcm,password=secret,obfs=shadowsocks-http,obfs-host=shadow.example.com,obfs-uri=/shadow,tag=QX SS Shadowsocks HTTP', + expected: { + type: 'ss', + name: 'QX SS Shadowsocks HTTP', + server: 'qx-ss-shadowsocks-http.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + _qx_obfs_http: 'shadowsocks-http', + 'plugin-opts': { + mode: 'http', + host: 'shadow.example.com', + path: '/shadow', + }, + }, + }, { title: 'parses shadowsocks v2ray-plugin wss lines', input: 'shadowsocks=qx-ss.example.com:8388,method=aes-128-gcm,password=secret,obfs=wss,obfs-host=obfs.example.com,obfs-uri=/ws,udp-relay=true,fast-open=true,tag=QX SS', @@ -865,6 +989,91 @@ describe('Platform raw-format parser coverage', function () { }, }, }, + { + title: 'parses vmess http lines as http obfs and keeps the QX token', + input: `vmess=qx-vmess-http-plain.example.com:80,method=none,password=${UUID},obfs=http,obfs-host=plain.example.com,obfs-uri=/http,tag=QX VMess HTTP`, + expected: { + type: 'vmess', + name: 'QX VMess HTTP', + server: 'qx-vmess-http-plain.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + alterId: 0, + network: 'http', + _qx_obfs_http: 'http', + 'http-opts': { + path: ['/http'], + headers: { + Host: ['plain.example.com'], + }, + }, + }, + }, + { + // QX examples contain the upstream "vemss-http" typo; keep + // parsing it so the original line can round-trip unchanged. + title: 'parses vmess vemss-http lines as http obfs and keeps the QX alias', + input: `vmess=qx-vmess-vemss-http.example.com:80,method=none,password=${UUID},obfs=vemss-http,obfs-host=vemss.example.com,obfs-uri=/vemss,tag=QX VMess VMess HTTP`, + expected: { + type: 'vmess', + name: 'QX VMess VMess HTTP', + server: 'qx-vmess-vemss-http.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + alterId: 0, + network: 'http', + _qx_obfs_http: 'vemss-http', + 'http-opts': { + path: ['/vemss'], + headers: { + Host: ['vemss.example.com'], + }, + }, + }, + }, + { + title: 'parses vmess shadowsocks-http lines as http obfs and keeps the QX alias', + input: `vmess=qx-vmess-http.example.com:80,method=none,password=${UUID},obfs=shadowsocks-http,obfs-host=cdn.example.com,obfs-uri=/resource/file,tag=QX VMess Shadowsocks HTTP`, + expected: { + type: 'vmess', + name: 'QX VMess Shadowsocks HTTP', + server: 'qx-vmess-http.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + alterId: 0, + network: 'http', + _qx_obfs_http: 'shadowsocks-http', + 'http-opts': { + path: ['/resource/file'], + headers: { + Host: ['cdn.example.com'], + }, + }, + }, + }, + { + title: 'parses vless vmess-http lines as http obfs and keeps the QX token', + input: `vless=qx-vless-http.example.com:80,method=none,password=${UUID},obfs=vmess-http,obfs-host=vless.example.com,obfs-uri=/vless-http,tag=QX VLESS HTTP`, + expected: { + type: 'vless', + name: 'QX VLESS HTTP', + server: 'qx-vless-http.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + network: 'http', + _qx_obfs_http: 'vmess-http', + 'http-opts': { + path: ['/vless-http'], + headers: { + Host: ['vless.example.com'], + }, + }, + }, + }, { title: 'parses vless reality websocket lines', input: `vless=qx-vless.example.com:443,method=none,password=${UUID},obfs=wss,obfs-host=cdn.example.com,obfs-uri=/vless,tls-verification=false,tls-host=sni.example.com,reality-base64-pubkey=pubkey,reality-hex-shortid=08,vless-flow=xtls-rprx-vision,tag=QX VLESS`, diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 4ace40c1..5948590a 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -485,6 +485,104 @@ describe('Proxy structured producers', function () { }); }); + it('maps canonical shadowsocks tls fields into Shadowrocket structured output', function () { + const proxy = { + type: 'ss', + name: 'Shadowrocket SS TLS', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + 'skip-cert-verify': true, + }; + + const internal = produceInternal('Shadowrocket', proxy)[0]; + const external = loadProducedYaml('Shadowrocket', proxy); + + expectSubset(internal, { + type: 'ss', + name: 'Shadowrocket SS TLS', + tls: true, + servername: 'a.com', + 'skip-cert-verify': true, + }); + // expect(internal).to.not.have.property('sni'); + expectSubset(external.proxies[0], { + type: 'ss', + name: 'Shadowrocket SS TLS', + tls: true, + servername: 'a.com', + 'skip-cert-verify': true, + }); + // expect(external.proxies[0]).to.not.have.property('sni'); + }); + + it('keeps canonical shadowsocks tls nodes without servername in Shadowrocket output when sni is absent', function () { + const proxy = { + type: 'ss', + name: 'Shadowrocket SS TLS No Host', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + }; + + const internal = produceInternal('Shadowrocket', proxy)[0]; + const external = loadProducedYaml('Shadowrocket', proxy); + + expectSubset(internal, { + type: 'ss', + name: 'Shadowrocket SS TLS No Host', + tls: true, + }); + expect(internal).to.not.have.property('sni'); + expect(internal).to.not.have.property('servername'); + expectSubset(external.proxies[0], { + type: 'ss', + name: 'Shadowrocket SS TLS No Host', + tls: true, + }); + expect(external.proxies[0]).to.not.have.property('sni'); + expect(external.proxies[0]).to.not.have.property('servername'); + }); + + it('maps canonical shadowsocks tcp tls fields into Shadowrocket structured output', function () { + const proxy = { + type: 'ss', + name: 'Shadowrocket SS TLS TCP', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + network: 'tcp', + }; + + const internal = produceInternal('Shadowrocket', proxy)[0]; + const external = loadProducedYaml('Shadowrocket', proxy); + + expectSubset(internal, { + type: 'ss', + name: 'Shadowrocket SS TLS TCP', + tls: true, + network: 'tcp', + servername: 'a.com', + }); + // expect(internal).to.not.have.property('sni'); + expectSubset(external.proxies[0], { + type: 'ss', + name: 'Shadowrocket SS TLS TCP', + tls: true, + network: 'tcp', + servername: 'a.com', + }); + // expect(external.proxies[0]).to.not.have.property('sni'); + }); + it('maps shadowsocks shadow-tls fields into Egern nested structures', function () { const proxy = { type: 'ss', @@ -637,6 +735,106 @@ describe('Proxy structured producers', function () { } }); + it('filters canonical shadowsocks over-tls nodes for unsupported client targets by default', function () { + const proxy = { + type: 'ss', + name: 'Unsupported SS TLS', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + }; + + for (const platform of [ + 'Clash', + 'ClashMeta', + 'Mihomo', + 'Stash', + 'Loon', + 'Surge', + 'SurgeMac', + 'Surfboard', + 'Egern', + 'sing-box', + 'URI', + 'V2Ray', + ]) { + expect(produceInternal(platform, proxy), platform).to.have.length( + 0, + ); + } + + expect(ProxyUtils.produce([proxy], 'Clash', 'external')).to.equal( + 'proxies:\n', + ); + expect(ProxyUtils.produce([proxy], 'Loon', 'external')).to.equal(''); + expect(ProxyUtils.produce([proxy], 'URI', 'external')).to.equal(''); + expect(produceInternal('QX', proxy), 'QX').to.have.length(1); + expect( + produceInternal('Shadowrocket', proxy), + 'Shadowrocket', + ).to.have.length(1); + }); + + it('keeps canonical shadowsocks over-tls nodes when include-unsupported-proxy is enabled for Clash', function () { + const proxy = { + type: 'ss', + name: 'Clash SS TLS', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + }; + + const internal = produceInternal('Clash', proxy, { + 'include-unsupported-proxy': true, + }); + + expect(internal).to.have.length(1); + expectSubset(internal[0], { + type: 'ss', + name: 'Clash SS TLS', + tls: true, + sni: 'a.com', + }); + }); + + it('still treats canonical shadowsocks tls nodes without sni as unsupported for disallowed targets', function () { + const proxy = { + type: 'ss', + name: 'Unsupported SS TLS No Host', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + }; + + expect(produceInternal('Clash', proxy)).to.have.length(0); + expect(produceExternal('Clash', proxy)).to.equal('proxies:\n'); + }); + + it('still treats canonical shadowsocks tcp tls nodes as unsupported for disallowed targets', function () { + const proxy = { + type: 'ss', + name: 'Unsupported SS TLS TCP', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + network: 'tcp', + }; + + expect(produceInternal('Clash', proxy)).to.have.length(0); + expect(ProxyUtils.produce([proxy], 'URI', 'external')).to.equal(''); + }); + it('skips invalid Egern nodes and keeps the rest of the subscription', function () { const proxies = [ { diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index cdaaa591..a3073a9b 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -6,6 +6,259 @@ import { ProxyUtils } from '@/core/proxy-utils'; import { produceExternal, UUID } from './helpers'; describe('Proxy text producers', function () { + it('produces Quantumult X shadowsocks over-tls lines from canonical tls nodes', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS Over TLS', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + 'skip-cert-verify': true, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,obfs-host=a.com,tls-verification=false,tag=QX SS Over TLS', + ); + }); + + it('produces Quantumult X shadowsocks over-tls lines without obfs-host when sni is absent', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS Over TLS No Host', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,tag=QX SS Over TLS No Host', + ); + }); + + it('produces Quantumult X shadowsocks over-tls lines for canonical tcp tls nodes', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS Over TLS TCP', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + network: 'tcp', + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,obfs-host=a.com,tag=QX SS Over TLS TCP', + ); + }); + + it('preserves Quantumult X obfs-host after Shadowrocket rewrites ss tls sni to servername', function () { + const proxy = { + type: 'ss', + name: 'QX SS Over TLS Re-emit', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + tls: true, + sni: 'a.com', + }; + + ProxyUtils.produce([proxy], 'Shadowrocket', 'internal'); + const output = ProxyUtils.produce([proxy], 'QX', 'external'); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:443,method=aes-128-gcm,password=secret,obfs=over-tls,obfs-host=a.com,tag=QX SS Over TLS Re-emit', + ); + }); + + it('keeps legacy Quantumult X shadowsocks obfs tls output unchanged', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS Obfs TLS', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'obfs', + 'plugin-opts': { + mode: 'tls', + host: 'legacy.example.com', + }, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:8388,method=aes-128-gcm,password=secret,obfs=tls,obfs-host=legacy.example.com,tag=QX SS Obfs TLS', + ); + }); + + it('preserves Quantumult X shadowsocks http token output', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS HTTP', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + _qx_obfs_http: 'http', + plugin: 'obfs', + 'plugin-opts': { + mode: 'http', + host: 'plain.example.com', + path: '/plain', + }, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:8388,method=aes-128-gcm,password=secret,obfs=http,obfs-host=plain.example.com,obfs-uri=/plain,tag=QX SS HTTP', + ); + }); + + // QX examples contain the upstream "vemss-http" typo; keep emitting it + // when the parsed source token was preserved in _qx_obfs_http. + it('preserves Quantumult X shadowsocks vemss-http alias output', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS VMess HTTP', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + _qx_obfs_http: 'vemss-http', + plugin: 'obfs', + 'plugin-opts': { + mode: 'http', + host: 'legacy.example.com', + path: '/resource', + }, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:8388,method=aes-128-gcm,password=secret,obfs=vemss-http,obfs-host=legacy.example.com,obfs-uri=/resource,tag=QX SS VMess HTTP', + ); + }); + + it('preserves Quantumult X shadowsocks shadowsocks-http token output', function () { + const output = produceExternal('QX', { + type: 'ss', + name: 'QX SS Shadowsocks HTTP', + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + _qx_obfs_http: 'shadowsocks-http', + plugin: 'obfs', + 'plugin-opts': { + mode: 'http', + host: 'shadow.example.com', + path: '/shadow', + }, + }); + + expect(output).to.equal( + 'shadowsocks=ss.example.com:8388,method=aes-128-gcm,password=secret,obfs=shadowsocks-http,obfs-host=shadow.example.com,obfs-uri=/shadow,tag=QX SS Shadowsocks HTTP', + ); + }); + + it('preserves Quantumult X vmess http token output', function () { + const output = produceExternal('QX', { + type: 'vmess', + name: 'QX VMess HTTP', + server: 'vmess.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + _qx_obfs_http: 'http', + network: 'http', + 'http-opts': { + path: '/http', + headers: { + Host: 'plain.example.com', + }, + }, + }); + + expect(output).to.equal( + `vmess=vmess.example.com:80,method=none,password=${UUID},obfs=http,obfs-uri=/http,obfs-host=plain.example.com,aead=false,tag=QX VMess HTTP`, + ); + }); + + // QX examples contain the upstream "vemss-http" typo; keep emitting it + // when the parsed source token was preserved in _qx_obfs_http. + it('preserves Quantumult X vmess vemss-http alias output', function () { + const output = produceExternal('QX', { + type: 'vmess', + name: 'QX VMess VMess HTTP', + server: 'vmess.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + _qx_obfs_http: 'vemss-http', + network: 'http', + 'http-opts': { + path: '/vemss', + headers: { + Host: 'vemss.example.com', + }, + }, + }); + + expect(output).to.equal( + `vmess=vmess.example.com:80,method=none,password=${UUID},obfs=vemss-http,obfs-uri=/vemss,obfs-host=vemss.example.com,aead=false,tag=QX VMess VMess HTTP`, + ); + }); + + it('preserves Quantumult X vmess shadowsocks-http alias output', function () { + const output = produceExternal('QX', { + type: 'vmess', + name: 'QX VMess Shadowsocks HTTP', + server: 'vmess.example.com', + port: 80, + cipher: 'none', + uuid: UUID, + _qx_obfs_http: 'shadowsocks-http', + network: 'http', + 'http-opts': { + path: '/resource/file', + headers: { + Host: 'cdn.example.com', + }, + }, + }); + + expect(output).to.equal( + `vmess=vmess.example.com:80,method=none,password=${UUID},obfs=shadowsocks-http,obfs-uri=/resource/file,obfs-host=cdn.example.com,aead=false,tag=QX VMess Shadowsocks HTTP`, + ); + }); + + it('preserves Quantumult X vless vmess-http token output', function () { + const output = produceExternal('QX', { + type: 'vless', + name: 'QX VLESS HTTP', + server: 'vless.example.com', + port: 80, + uuid: UUID, + _qx_obfs_http: 'vmess-http', + network: 'http', + 'http-opts': { + path: '/vless-http', + headers: { + Host: 'vless.example.com', + }, + }, + }); + + expect(output).to.equal( + `vless=vless.example.com:80,method=none,password=${UUID},obfs=vmess-http,obfs-uri=/vless-http,obfs-host=vless.example.com,tag=QX VLESS HTTP`, + ); + }); + it('produces Quantumult X VLESS reality websocket lines', function () { const output = produceExternal('QX', { type: 'vless', diff --git a/scripts/demo.js b/scripts/demo.js index a4e8759a..9eb6ce2e 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -37,6 +37,7 @@ function operator(proxies = [], targetPlatform, context) { // 23. `interface-name` 指定流量出站接口 只给 Surge 用的话, `interface` 也可以 // 24. Surge for macOS 可手动指定链接参数 target=SurgeMac 或在 同步配置 中指定 SurgeMac 来启用 mihomo 支援 Surge 本身不支持的协议. 设置节点字段 `_mihomoExternal` 为 `true` 可强制指定使用 mihomo External Proxy Program 输出该节点 // 25. VLESS URI extra 字段复杂且其他客户端不一定跟进, 所以输出 URI 时, 若存在原始的 `_extra` 字段, 则直接使用该字段的值输出到 URI 的 extra 部分, 而不进行解析和重组 + // 26. `_qx_obfs_http` 为 QX 的 http obfs 原始值, 例如 `http`, `vmess-http`, `vemss-http`, `shadowsocks-http`, 用于 QX 输入输出时保留原始写法. `vemss-http` 应该是 huaqian 的 typo, 没测过, 反正也支持, 报错就自己改成 `vmess-http` 吧 // require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块 // 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件