diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index ba41bd7e..a760e496 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -1883,6 +1883,11 @@ function URI_VLESS() { proxy['ech-opts'] = echOpts; } proxy['tls-fingerprint'] = getIfPresent(params.pcs); + proxy._vcn = params.vcn + ?.split(',') + .map((name) => name.trim()) + .filter(Boolean); + proxy['name-cert-verify'] = proxy._vcn?.[0]; proxy._h2 = /(TRUE)|1/i.test(params.h2); switch (`${params.packetEncoding || ''}`.trim().toLowerCase()) { diff --git a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js index d4e70a72..729247ad 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js +++ b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js @@ -111,6 +111,14 @@ function parseTrojan(url) { proxy["tls-fingerprint"] = params.pcs; + proxy._vcn = params.vcn + ?.split(",") + .map((name) => name.trim()) + .filter(Boolean); + + proxy["name-cert-verify"] = + proxy._vcn?.[0]; + if (params.alpn) { proxy.alpn = params.alpn.split(","); diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 91bef2d6..7362788d 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -596,6 +596,13 @@ function vless(proxy) { if (proxy['tls-fingerprint']) { pcs = `&pcs=${encodeURIComponent(proxy['tls-fingerprint'])}`; } + let vcn = ''; + const certNames = Array.isArray(proxy._vcn) + ? proxy._vcn.join(',') + : proxy['name-cert-verify']; + if (Array.isArray(proxy._vcn) || certNames) { + vcn = `&vcn=${encodeURIComponent(certNames)}`; + } let ech = ''; const echConfigList = buildXrayEchConfigListFromMihomo( proxy['ech-opts'], @@ -777,7 +784,7 @@ function vless(proxy) { proxy.port }?security=${encodeURIComponent( security, - )}${vlessTransport}${packetEncoding}${alpn}${allowInsecure}${pcs}${ech}${h2}${sni}${fp}${flow}${sid}${spx}${pbk}${mode}${extra}${pqv}${encryption}#${encodeURIComponent( + )}${vlessTransport}${packetEncoding}${alpn}${allowInsecure}${pcs}${vcn}${ech}${h2}${sni}${fp}${flow}${sid}${spx}${pbk}${mode}${extra}${pqv}${encryption}#${encodeURIComponent( proxy.name, )}`; } @@ -1192,6 +1199,13 @@ export default function URI_Producer() { proxy['tls-fingerprint'], )}`; } + let trojanVcn = ''; + const trojanCertNames = Array.isArray(proxy._vcn) + ? proxy._vcn.join(',') + : proxy['name-cert-verify']; + if (Array.isArray(proxy._vcn) || trojanCertNames) { + trojanVcn = `&vcn=${encodeURIComponent(trojanCertNames)}`; + } let trojanAlpn = ''; if (proxy.alpn) { trojanAlpn = `&alpn=${encodeURIComponent( @@ -1234,7 +1248,7 @@ export default function URI_Producer() { proxy.port }?sni=${encodeURIComponent(proxy.sni || proxy.server)}${ proxy['skip-cert-verify'] ? '&allowInsecure=1' : '' - }${trojanTransport}${trojanAlpn}${trojanFp}${trojanPcs}${trojanSecurity}${trojanSid}${trojanPbk}${trojanSpx}${trojanMode}${trojanExtra}#${encodeURIComponent( + }${trojanTransport}${trojanAlpn}${trojanFp}${trojanPcs}${trojanVcn}${trojanSecurity}${trojanSid}${trojanPbk}${trojanSpx}${trojanMode}${trojanExtra}#${encodeURIComponent( proxy.name, )}`; break; diff --git a/backend/src/test/proxy-parsers/uri.spec.js b/backend/src/test/proxy-parsers/uri.spec.js index 8d8960c8..b5957341 100644 --- a/backend/src/test/proxy-parsers/uri.spec.js +++ b/backend/src/test/proxy-parsers/uri.spec.js @@ -716,6 +716,18 @@ describe('Proxy URI parser coverage', function () { }); }); + it('parses Trojan vcn values into mihomo and sidecar fields', function () { + const proxy = parseOne( + 'trojan://trojan-pass@trojan.example.com:443?vcn=first.example.com%2Csecond.example.com#Trojan%20VCN', + ); + + expect(proxy['name-cert-verify']).to.equal('first.example.com'); + expect(proxy._vcn).to.deep.equal([ + 'first.example.com', + 'second.example.com', + ]); + }); + it('parses Trojan URIs with grpc reality metadata', function () { const proxy = parseOne( 'trojan://trojan-pass@trojan-grpc.example.com?type=grpc&serviceName=grpc-service&authority=grpc.example.com&mode=multi&security=reality&pbk=pubkey==&sid=08&spx=%2Fspider&extra=%7B%22x%22%3A1%7D&udp=1&tfo=1#Trojan%20Reality', @@ -757,9 +769,7 @@ describe('Proxy URI parser coverage', function () { it('rejects colon-containing Trojan hosts that are not IPv6', function () { expect( - parseAll( - 'trojan://trojan-pass@host:123:443#Invalid%20Host', - ), + parseAll('trojan://trojan-pass@host:123:443#Invalid%20Host'), ).to.deep.equal([]); }); 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 1455629f..75686f98 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -577,6 +577,20 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses the first VLESS vcn as mihomo name-cert-verify', function () { + const proxy = parseOne( + `vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&vcn=${encodeURIComponent( + 'first.example.com, second.example.com', + )}#VLESS%20WS%20VCN`, + ); + + expect(proxy['name-cert-verify']).to.equal('first.example.com'); + expect(proxy._vcn).to.deep.equal([ + 'first.example.com', + 'second.example.com', + ]); + }); + it('parses VLESS share ECH config into mihomo ech opts', function () { const proxy = parseOne( `vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&host=cdn.example.com&path=%2Fws&ech=${encodeURIComponent( diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 20c408e2..920ca041 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -2506,6 +2506,27 @@ describe('Proxy text producers', function () { ); }); + it('produces URI VLESS links with vcn from name-cert-verify', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI WS VCN', + server: 'vless.example.com', + port: 443, + uuid: UUID, + tls: true, + 'name-cert-verify': 'edited.example.com', + _vcn: ['cert.example.com', 'backup.example.com'], + network: 'ws', + 'ws-opts': { + path: '/ws', + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless.example.com:443?security=tls&type=ws&path=%2Fws&vcn=cert.example.com%2Cbackup.example.com#URI%20WS%20VCN`, + ); + }); + it('produces URI VLESS links with ech from mihomo ech opts config', function () { const output = produceExternal('URI', { type: 'vless', @@ -5064,6 +5085,24 @@ describe('Proxy text producers', function () { ); }); + it('produces URI Trojan links with vcn from the sidecar', function () { + const output = produceExternal('URI', { + type: 'trojan', + name: 'URI Trojan VCN', + server: 'trojan.example.com', + port: 443, + password: 'secret', + tls: true, + sni: 'sni.example.com', + 'name-cert-verify': 'edited.example.com', + _vcn: ['cert.example.com', 'backup.example.com'], + }); + + expect(output).to.equal( + 'trojan://secret@trojan.example.com:443?sni=sni.example.com&vcn=cert.example.com%2Cbackup.example.com#URI%20Trojan%20VCN', + ); + }); + it('produces URI Trojan websocket links with early data metadata', function () { const output = produceExternal('URI', { type: 'trojan', diff --git a/scripts/demo.js b/scripts/demo.js index 6fba4500..c7e4f63c 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -56,6 +56,7 @@ function operator(proxies = [], targetPlatform, context) { // 31. `shadow-tls-password`/`shadow-tls-sni`/`shadow-tls-version` 这套旧字段已废弃. 请使用 `plugin: 'shadow-tls'` 和 `plugin-opts: { password, host, version }` // 32. mihomo 中 Snell shadow-tls 字段与 ss shadow-tls 字段不同, 使用的是 obfs-opts 而不是 plugin+plugin-opts, 不能与 obfs http/tls 共存. Sub-Store 内部有字段转换, 建议直接使用单行 Surge 格式 `1=snell,a.com,443,version=4,psk="1",obfs=http,obfs-host=a.com,shadow-tls-password="1",shadow-tls-sni=a.com,shadow-tls-version=3,alpn="http/1.1,h2",reuse=true` . 若想使用 JSON/JSON5/YAML 单行格式输入, 可使用 `{ "name": "1", "server": "a.com", "port": 443, "psk": "1", "version": 4, "reuse": true, "type": "snell", "obfs-opts": { "mode": "http", "host": "a.com" }, "plugin": "shadow-tls", "plugin-opts": { "host": "a.com", "password": "1", "version": 3, "alpn": [ "http/1.1", "h2" ] } }` // 33. sing-box Snell 出站默认允许 version 4/5/6, 其中 version 5 会按 sing-box 行为输出成 version 4. 开启“含不支持的协议”时保留 version 1/2/3/4/5/6. 节点上的 `_userkey` 会输出为 sing-box 的 `userkey` + // 34. VLESS/Trojan URI 的 `vcn` 对应 mihomo 的 `name-cert-verify`. Xray-core 支持用逗号分隔多个 name, mihomo 只支持一个, 因此输入时取第一个有效值, 同时用 `_vcn` 数组保留全部有效值; 输出 URI 时优先用 `_vcn` 还原为 `vcn`, 没有 `_vcn` 时才使用 `name-cert-verify`. 若想设置为其他值, 可使用脚本操作设置, 例如 `$server["name-cert-verify"] = $server._vcn?.[1] || $server._vcn?.[0]` // require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块 // 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件