diff --git a/backend/package.json b/backend/package.json index 82789974..f1418eaf 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.39.6", + "version": "2.39.7", "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 f183b0d3..13225515 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -2099,6 +2099,16 @@ function URI_VLESS() { if (params.pqv) { proxy._pqv = params.pqv; } + if (params.fm) { + try { + const finalmask = JSON.parse(params.fm); + proxy._finalmask = isPlainObject(finalmask) + ? finalmask + : params.fm; + } catch (e) { + proxy._finalmask = params.fm; + } + } return proxy; }; diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 338f5473..09af81a2 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -507,22 +507,21 @@ function mergeUnsupportedXhttpExtraObject(baseObject, unsupportedObject) { return mergedExtra; } -function getExplicitExtraOverride(proxy) { - if (typeof proxy._extra === 'string') { - return proxy._extra; +function serializeUriJsonValue(value) { + if (typeof value === 'string') { + return value; } - // `_extra` only accepts JSON-like plain objects here. Broader object checks - // would accidentally stringify instances such as Date/Map/class values. - if (isPlainObject(proxy._extra)) { - return JSON.stringify(proxy._extra); + // Only serialize JSON-like plain objects, not Date/Map/class instances. + if (isPlainObject(value)) { + return JSON.stringify(value); } return undefined; } function buildVlessExtra(proxy) { - const explicitExtraOverride = getExplicitExtraOverride(proxy); + const explicitExtraOverride = serializeUriJsonValue(proxy._extra); if (explicitExtraOverride != null) { // `_extra` is an explicit user override for the final URI extra. When // present as a string or plain object, we bypass the structured xhttp @@ -641,6 +640,11 @@ function vless(proxy) { if (extraPayload) { extra = `&extra=${encodeURIComponent(extraPayload)}`; } + let fm = ''; + const finalmaskPayload = serializeUriJsonValue(proxy._finalmask); + if (finalmaskPayload) { + fm = `&fm=${encodeURIComponent(finalmaskPayload)}`; + } let mode = ''; if ( ['xhttp'].includes(proxy.network) && @@ -788,7 +792,7 @@ function vless(proxy) { proxy.port }?security=${encodeURIComponent( security, - )}${vlessTransport}${packetEncoding}${alpn}${allowInsecure}${pcs}${vcn}${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}${fm}${pqv}${encryption}#${encodeURIComponent( proxy.name, )}`; } @@ -1024,14 +1028,15 @@ export default function URI_Producer() { result = 'ssr://' + Base64.encode(result); break; case 'vmess': - if (proxy['reality-opts']) { + if (proxy['reality-opts'] || proxy._finalmask) { + // https://github.com/XTLS/Xray-core/discussions/716 excludes alterId/aid. if ( proxy.aead === false || (proxy.aead !== true && Number(proxy.alterId || 0) !== 0) ) { throw new Error( - 'VMess REALITY URI requires AEAD (alterId=0)', + 'VMess query URI format cannot represent non-AEAD authentication (alterId must be 0)', ); } result = vless({ diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index e034dc1b..90944909 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -2736,7 +2736,7 @@ describe('Proxy text producers', function () { } }); - it('rejects non-AEAD VMess REALITY URI exports without changing authentication', function () { + it('rejects non-AEAD VMess conversion to query URIs for REALITY', function () { for (const auth of [{ alterId: 64 }, { aead: false }]) { const { result, errors } = captureErrors(() => ProxyUtils.produce( @@ -2756,7 +2756,107 @@ describe('Proxy text producers', function () { ); expect(result).to.equal(''); expect(errors.join('\n')).to.include( - 'VMess REALITY URI requires AEAD', + 'VMess query URI format cannot represent non-AEAD authentication', + ); + } + }); + + it('preserves finalmask as _finalmask through VLESS and VMess AEAD URI round-trips', function () { + const fm = JSON.stringify( + { + udp: [ + { + type: 'salamander', + settings: { password: 'a&b=c#d%2F+ /中文' }, + }, + ], + }, + null, + 2, + ); + for (const type of ['vless', 'vmess']) { + for (const security of ['none', 'tls', 'reality']) { + const input = `${type}://${UUID}@[2001:db8::1]:443?security=${security}${ + security === 'reality' ? '&pbk=pubkey' : '' + }&encryption=none&fm=${encodeURIComponent( + fm, + )}#Finalmask%20%23%20test`; + const [proxy] = ProxyUtils.parse(input); + expect(proxy._finalmask).to.deep.equal(JSON.parse(fm)); + expect(proxy).not.to.have.property('finalmask'); + proxy._finalmask.udp[0].settings.password += ' edited'; + for (const platform of ['URI', 'V2Ray']) { + const output = produceExternal(platform, proxy); + const uri = + platform === 'V2Ray' ? Base64.decode(output) : output; + expect(uri).to.include( + `&fm=${encodeURIComponent( + JSON.stringify(proxy._finalmask), + )}`, + ); + const [reparsed] = ProxyUtils.parse(output); + expect(reparsed).to.include({ + type, + name: 'Finalmask # test', + server: '2001:db8::1', + }); + expect(reparsed._finalmask).to.deep.equal(proxy._finalmask); + if (type === 'vmess') { + expect(reparsed.cipher).to.equal('none'); + expect(reparsed.alterId).to.equal(0); + } + } + const mihomo = produceExternal('Mihomo', proxy); + expect(mihomo).not.to.include('_finalmask'); + expect(mihomo).not.to.include('salamander'); + } + } + }); + + it('preserves finalmask string overrides and falls back to raw text for invalid or non-object JSON', function () { + for (const type of ['vless', 'vmess']) { + for (const [fm, expected] of [ + [' { "udp": [] } ', { udp: [] }], + ...['{bad', 'null', 'false', '0', '[]', '"raw"'].map( + (value) => [value, value], + ), + ]) { + const uri = produceExternal('URI', { + type, + name: 'Finalmask String', + server: 'example.com', + port: 443, + uuid: UUID, + _finalmask: fm, + }); + expect(new URL(uri).searchParams.get('fm')).to.equal(fm); + const [proxy] = ProxyUtils.parse(uri); + expect(proxy._finalmask).to.deep.equal(expected); + } + } + }); + + it('rejects non-AEAD VMess conversion to query URIs for finalmask', function () { + for (const auth of [{ alterId: 64 }, { aead: false }]) { + const { result, errors } = captureErrors(() => + ProxyUtils.produce( + [ + { + type: 'vmess', + name: 'Legacy VMess Finalmask', + server: 'example.com', + port: 443, + uuid: UUID, + _finalmask: '{}', + ...auth, + }, + ], + 'URI', + ), + ); + expect(result).to.equal(''); + expect(errors.join('\n')).to.include( + 'VMess query URI format cannot represent non-AEAD authentication', ); } }); diff --git a/scripts/demo.js b/scripts/demo.js index adc875bb..cf3d0351 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -61,6 +61,11 @@ function operator(proxies = [], targetPlatform, context) { // 36. sing-box Hysteria 2 支持通过节点的字符串字段 `disable-chrome-parrot` 设置 `disable_chrome_parrot` // 37. Surge TrustTunnel 支持使用 Surge 格式写 `h3=true` 作为输入, 或使用节点字段 `network: 'h3'` 设置 `h3=true` // 38. QX 输入的 `tls-alpn` 会保留原始十六进制值(支持带或不带冒号), 同时解码为 `alpn` 数组; QX 输出时优先使用 `tls-alpn`, 否则将 `alpn` 编码为 `tls-alpn` + // 39. `_finalmask` 对应 Xray-core 的 `streamSettings.finalmask`. URI `fm` 参数解码后会尝试 JSON.parse, 结果为普通对象时保存对象, 解析失败或结果不是对象时保留原字符串 + // 手动设置支持普通对象或 JSON 字符串, 不要预先 encodeURIComponent. 例如: $server._finalmask = { udp: [{ type: 'salamander', settings: { password: 'example-password' } }] } + // 对象可直接修改, 例如 $server._finalmask.udp[0].settings.password = 'new-example-password'; 移除可用 delete $server._finalmask + // 输出 URI(包括 V2Ray 订阅)时, 对象会先 JSON.stringify, 字符串原样使用, 再统一 encodeURIComponent 编码为 `fm`; 其他客户端不输出此字段 + // VMess 携带此字段时使用 query 分享格式, 该格式只支持 AEAD(alterId=0), 规范: https://github.com/XTLS/Xray-core/discussions/716 // require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块 // 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件