diff --git a/backend/package.json b/backend/package.json index cbb0c89d..7e836c42 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.83", + "version": "2.21.84", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 7456dbb3..8357bd99 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -693,6 +693,115 @@ function URI_VLESS() { return /^vless:\/\//.test(line); }; const parse = (line) => { + const mapXmuxToReuseSettings = (xmux) => { + if (!isPlainObject(xmux)) { + return undefined; + } + + const reuseSettings = {}; + const xmuxFieldMap = { + maxConnections: 'max-connections', + maxConcurrency: 'max-concurrency', + cMaxReuseTimes: 'c-max-reuse-times', + hMaxRequestTimes: 'h-max-request-times', + hMaxReusableSecs: 'h-max-reusable-secs', + }; + + for (const [sourceKey, targetKey] of Object.entries( + xmuxFieldMap, + )) { + const value = xmux[sourceKey]; + if (typeof value === 'string' && value !== '') { + reuseSettings[targetKey] = value; + } else if ( + typeof value === 'number' && + Number.isFinite(value) + ) { + reuseSettings[targetKey] = `${value}`; + } + } + + return Object.keys(reuseSettings).length > 0 + ? reuseSettings + : undefined; + }; + + const parseDownloadSettings = (downloadSettings) => { + if (!isPlainObject(downloadSettings)) { + return undefined; + } + + const parsedDownloadSettings = {}; + if (isNotBlank(downloadSettings.address)) { + parsedDownloadSettings.server = downloadSettings.address; + } + + if ( + typeof downloadSettings.port === 'number' && + Number.isFinite(downloadSettings.port) + ) { + parsedDownloadSettings.port = parseInt( + `${downloadSettings.port}`, + 10, + ); + } + + if ( + typeof downloadSettings.security === 'string' && + downloadSettings.security.toLowerCase() === 'tls' + ) { + parsedDownloadSettings.tls = true; + } + + if (isPlainObject(downloadSettings.tlsSettings)) { + if (isNotBlank(downloadSettings.tlsSettings.serverName)) { + parsedDownloadSettings.servername = + downloadSettings.tlsSettings.serverName; + } + if (isNotBlank(downloadSettings.tlsSettings.fingerprint)) { + parsedDownloadSettings['client-fingerprint'] = + downloadSettings.tlsSettings.fingerprint; + } + if (Array.isArray(downloadSettings.tlsSettings.alpn)) { + const alpn = downloadSettings.tlsSettings.alpn.filter( + (item) => typeof item === 'string' && item !== '', + ); + if (alpn.length > 0) { + parsedDownloadSettings.alpn = alpn; + } + } + } + + if (isPlainObject(downloadSettings.xhttpSettings)) { + if (isNotBlank(downloadSettings.xhttpSettings.path)) { + parsedDownloadSettings.path = + downloadSettings.xhttpSettings.path; + } + if (isNotBlank(downloadSettings.xhttpSettings.host)) { + parsedDownloadSettings.host = + downloadSettings.xhttpSettings.host; + } + if (downloadSettings.xhttpSettings.noGRPCHeader === true) { + parsedDownloadSettings['no-grpc-header'] = true; + } + if (isNotBlank(downloadSettings.xhttpSettings.xPaddingBytes)) { + parsedDownloadSettings['x-padding-bytes'] = + downloadSettings.xhttpSettings.xPaddingBytes; + } + + const reuseSettings = mapXmuxToReuseSettings( + downloadSettings.xhttpSettings.extra?.xmux, + ); + if (reuseSettings) { + parsedDownloadSettings['reuse-settings'] = reuseSettings; + } + } + + return Object.keys(parsedDownloadSettings).length > 0 + ? parsedDownloadSettings + : undefined; + }; + line = line.split('vless://')[1]; let isShadowrocket; let parsed = /^(.*?)@(.*?):(\d+)\/?(\?(.*?))?(?:#(.*?))?$/.exec(line); @@ -721,6 +830,7 @@ function URI_VLESS() { server, port, uuid, + udp: true, }; const params = {}; for (const addon of addons.split('&')) { @@ -761,6 +871,17 @@ function URI_VLESS() { proxy._pcs = getIfPresent(params.pcs); proxy._h2 = /(TRUE)|1/i.test(params.h2); + switch (`${params.packetEncoding || ''}`.toLowerCase()) { + case 'none': + break; + case 'packet': + proxy['packet-addr'] = true; + break; + default: + proxy.xudp = true; + break; + } + if (['reality'].includes(params.security)) { const opts = {}; if (params.pbk) { @@ -778,14 +899,16 @@ function URI_VLESS() { } } let httpupgrade = false; - proxy.network = params.type; + proxy.network = params.type || 'tcp'; if (proxy.network === 'tcp' && params.headerType === 'http') { proxy.network = 'http'; + } else if (proxy.network === 'http') { + proxy.network = 'h2'; } else if (proxy.network === 'httpupgrade') { proxy.network = 'ws'; httpupgrade = true; } - if (!proxy.network && isShadowrocket && params.obfs) { + if (!params.type && isShadowrocket && params.obfs) { proxy.network = params.obfs; if (['none'].includes(proxy.network)) { proxy.network = 'tcp'; @@ -824,6 +947,9 @@ function URI_VLESS() { if (params.path) { opts.path = params.path; } + if (proxy.network === 'http' && params.method) { + opts.method = params.method; + } // https://github.com/XTLS/Xray-core/issues/91 if (['grpc'].includes(proxy.network)) { opts['_grpc-type'] = params.mode || 'gun'; @@ -832,6 +958,30 @@ function URI_VLESS() { opts['v2ray-http-upgrade'] = true; opts['v2ray-http-upgrade-fast-open'] = true; } + if (params.ed) { + const maxEarlyDataRaw = `${params.ed}`; + if (!/^\d+$/.test(maxEarlyDataRaw)) { + throw new Error( + `bad WebSocket max early data size: ${params.ed}`, + ); + } + const maxEarlyData = parseInt(maxEarlyDataRaw, 10); + if (!Number.isSafeInteger(maxEarlyData)) { + throw new Error( + `bad WebSocket max early data size: ${params.ed}`, + ); + } + if (httpupgrade) { + opts['max-early-data'] = maxEarlyData; + } else if (proxy.network === 'ws') { + opts['max-early-data'] = maxEarlyData; + opts['early-data-header-name'] = + params.eh || 'Sec-WebSocket-Protocol'; + } + } + if (params.eh && (proxy.network === 'ws' || httpupgrade)) { + opts['early-data-header-name'] = params.eh; + } if (Object.keys(opts).length > 0) { proxy[`${proxy.network}-opts`] = opts; } @@ -846,90 +996,70 @@ function URI_VLESS() { if (params.extra) { proxy._extra = params.extra; } - // 太麻烦了 暂时 extra 原封不动 - // 单独解析一下 - if (params.mode) { - if (['xhttp'].includes(proxy.network)) { - let extra = {}; - try { - extra = proxy._extra ? JSON.parse(proxy._extra) : {}; - } catch (e) { - $.error( - `Failed to parse extra field as JSON: ${proxy._extra}`, - ); - } - if (extra?.downloadSettings) { - $.error( - 'It is too complex to convert the downloadSettings in extra into the Mihomo format, so it is not supported.', - ); - } - const xhttpOpts = { - ...proxy[`${proxy.network}-opts`], - mode: params.mode, - }; - if (extra?.['noGRPCHeader'] === true) { - xhttpOpts['no-grpc-header'] = true; - } - if (isNotBlank(extra?.['xPaddingBytes'])) { - xhttpOpts['x-padding-bytes'] = extra['xPaddingBytes']; - } - const scMaxEachPostBytes = - extra?.['scMaxEachPostBytes']; - if ( - typeof scMaxEachPostBytes === 'string' || - typeof scMaxEachPostBytes === 'number' - ) { - const normalizedValue = `${scMaxEachPostBytes}`; - if (/^\s*[1-9]\d*\s*$/.test(normalizedValue)) { - xhttpOpts['sc-max-each-post-bytes'] = parseInt( - normalizedValue, - 10, - ); - } else { - const rangeMatch = normalizedValue.match( - /^\s*([1-9]\d*)\s*-\s*([1-9]\d*)\s*$/, - ); - if (rangeMatch) { - const lowerBound = parseInt(rangeMatch[1], 10); - const upperBound = parseInt(rangeMatch[2], 10); - - if (upperBound >= lowerBound) { - xhttpOpts['sc-max-each-post-bytes'] = - upperBound; - } - } - } - } - if (isPlainObject(extra?.xmux)) { - const reuseSettings = {}; - const xmuxFieldMap = { - maxConnections: 'max-connections', - maxConcurrency: 'max-concurrency', - cMaxReuseTimes: 'c-max-reuse-times', - hMaxRequestTimes: 'h-max-request-times', - hMaxReusableSecs: 'h-max-reusable-secs', - }; - for (const [sourceKey, targetKey] of Object.entries( - xmuxFieldMap, - )) { - const value = extra.xmux[sourceKey]; - if (typeof value === 'string' && value !== '') { - reuseSettings[targetKey] = value; - } else if ( - typeof value === 'number' && - Number.isFinite(value) - ) { - reuseSettings[targetKey] = `${value}`; - } - } - if (Object.keys(reuseSettings).length > 0) { - xhttpOpts['reuse-settings'] = reuseSettings; - } - } - proxy[`${proxy.network}-opts`] = xhttpOpts; - } else { - proxy._mode = params.mode; + if (['xhttp'].includes(proxy.network)) { + let extra = {}; + try { + extra = proxy._extra ? JSON.parse(proxy._extra) : {}; + } catch (e) { + $.error( + `Failed to parse extra field as JSON: ${proxy._extra}`, + ); } + const xhttpOpts = { + ...(proxy[`${proxy.network}-opts`] || {}), + }; + if (params.mode) { + xhttpOpts.mode = params.mode; + } + if (extra?.['noGRPCHeader'] === true) { + xhttpOpts['no-grpc-header'] = true; + } + if (isNotBlank(extra?.['xPaddingBytes'])) { + xhttpOpts['x-padding-bytes'] = extra['xPaddingBytes']; + } + const scMaxEachPostBytes = extra?.['scMaxEachPostBytes']; + if ( + typeof scMaxEachPostBytes === 'string' || + typeof scMaxEachPostBytes === 'number' + ) { + const normalizedValue = `${scMaxEachPostBytes}`; + if (/^\s*[1-9]\d*\s*$/.test(normalizedValue)) { + xhttpOpts['sc-max-each-post-bytes'] = parseInt( + normalizedValue, + 10, + ); + } else { + const rangeMatch = normalizedValue.match( + /^\s*([1-9]\d*)\s*-\s*([1-9]\d*)\s*$/, + ); + if (rangeMatch) { + const lowerBound = parseInt(rangeMatch[1], 10); + const upperBound = parseInt(rangeMatch[2], 10); + + if (upperBound >= lowerBound) { + xhttpOpts['sc-max-each-post-bytes'] = + upperBound; + } + } + } + } + if (isPlainObject(extra?.xmux)) { + const reuseSettings = mapXmuxToReuseSettings(extra.xmux); + if (reuseSettings) { + xhttpOpts['reuse-settings'] = reuseSettings; + } + } + const downloadSettings = parseDownloadSettings( + extra?.downloadSettings, + ); + if (downloadSettings) { + xhttpOpts['download-settings'] = downloadSettings; + } + if (Object.keys(xhttpOpts).length > 0) { + proxy[`${proxy.network}-opts`] = xhttpOpts; + } + } else if (params.mode) { + proxy._mode = params.mode; } } if (params.encryption) { diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index ec821896..57fe54bb 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -3,6 +3,159 @@ import { Base64 } from 'js-base64'; import { isIPv6 } from '@/utils'; import { normalizePluginMuxValue } from './utils'; +function isObject(value) { + return value != null && typeof value === 'object' && !Array.isArray(value); +} + +function getTransportHost(network, transportOpts = {}) { + if (network === 'h2') { + return ( + transportOpts.host ?? + transportOpts.headers?.host ?? + transportOpts.headers?.Host + ); + } + if (network === 'xhttp') { + return ( + transportOpts.host ?? + transportOpts.headers?.Host ?? + transportOpts.headers?.host + ); + } + return ( + transportOpts.headers?.Host ?? + transportOpts.headers?.host ?? + transportOpts.host + ); +} + +function mapReuseSettingsToXmux(reuseSettings) { + if (!isObject(reuseSettings)) { + return undefined; + } + + const xmux = {}; + const reuseFieldMap = { + 'max-connections': 'maxConnections', + 'max-concurrency': 'maxConcurrency', + 'c-max-reuse-times': 'cMaxReuseTimes', + 'h-max-request-times': 'hMaxRequestTimes', + 'h-max-reusable-secs': 'hMaxReusableSecs', + }; + + for (const [sourceKey, targetKey] of Object.entries(reuseFieldMap)) { + const value = reuseSettings[sourceKey]; + if (typeof value === 'string' && value !== '') { + xmux[targetKey] = value; + } else if (typeof value === 'number' && Number.isFinite(value)) { + xmux[targetKey] = `${value}`; + } + } + + return Object.keys(xmux).length > 0 ? xmux : undefined; +} + +function buildXhttpDownloadSettings(downloadSettings) { + if (!isObject(downloadSettings)) { + return undefined; + } + + const result = {}; + if (downloadSettings.server) { + result.address = downloadSettings.server; + } + if ( + downloadSettings.port != null && + !Number.isNaN(parseInt(`${downloadSettings.port}`, 10)) + ) { + result.port = parseInt(`${downloadSettings.port}`, 10); + } + if (downloadSettings.tls) { + result.security = 'tls'; + } + + const tlsSettings = {}; + if (downloadSettings.servername) { + tlsSettings.serverName = downloadSettings.servername; + } + if (downloadSettings['client-fingerprint']) { + tlsSettings.fingerprint = downloadSettings['client-fingerprint']; + } + if (downloadSettings.alpn) { + tlsSettings.alpn = Array.isArray(downloadSettings.alpn) + ? downloadSettings.alpn + : [downloadSettings.alpn]; + } + if (Object.keys(tlsSettings).length > 0) { + result.tlsSettings = tlsSettings; + } + + const xhttpSettings = {}; + if (downloadSettings.path) { + xhttpSettings.path = downloadSettings.path; + } + if (downloadSettings.host) { + xhttpSettings.host = downloadSettings.host; + } + if (downloadSettings['no-grpc-header']) { + xhttpSettings.noGRPCHeader = true; + } + if (downloadSettings['x-padding-bytes']) { + xhttpSettings.xPaddingBytes = downloadSettings['x-padding-bytes']; + } + + const xmux = mapReuseSettingsToXmux(downloadSettings['reuse-settings']); + if (xmux) { + xhttpSettings.extra = { xmux }; + } + if (Object.keys(xhttpSettings).length > 0) { + result.xhttpSettings = xhttpSettings; + } + + return Object.keys(result).length > 0 ? result : undefined; +} + +function buildStructuredVlessExtra(proxy) { + const xhttpOpts = proxy['xhttp-opts'] || {}; + const extra = {}; + + if (xhttpOpts['no-grpc-header'] === true) { + extra.noGRPCHeader = true; + } + if (xhttpOpts['x-padding-bytes']) { + extra.xPaddingBytes = xhttpOpts['x-padding-bytes']; + } + if (xhttpOpts['sc-max-each-post-bytes'] != null) { + extra.scMaxEachPostBytes = xhttpOpts['sc-max-each-post-bytes']; + } + + const xmux = mapReuseSettingsToXmux(xhttpOpts['reuse-settings']); + if (xmux) { + extra.xmux = xmux; + } + + const downloadSettings = buildXhttpDownloadSettings( + xhttpOpts['download-settings'], + ); + if (downloadSettings) { + extra.downloadSettings = downloadSettings; + } + + return Object.keys(extra).length > 0 ? JSON.stringify(extra) : ''; +} + +function buildVlessExtra(proxy) { + if (proxy.network !== 'xhttp') { + return proxy._extra || ''; + } + + if (proxy._extra) { + return proxy._extra; + } + + return buildStructuredVlessExtra(proxy); +} + function vless(proxy) { let security = 'none'; const isReality = proxy['reality-opts']; @@ -61,8 +214,9 @@ function vless(proxy) { flow = `&flow=${encodeURIComponent(proxy.flow)}`; } let extra = ''; - if (proxy._extra) { - extra = `&extra=${encodeURIComponent(proxy._extra)}`; + const extraPayload = buildVlessExtra(proxy); + if (extraPayload) { + extra = `&extra=${encodeURIComponent(extraPayload)}`; } let mode = ''; if ( @@ -86,9 +240,16 @@ function vless(proxy) { let vlessType = proxy.network; if (proxy.network === 'ws' && proxy['ws-opts']?.['v2ray-http-upgrade']) { vlessType = 'httpupgrade'; + } else if (proxy.network === 'http') { + vlessType = 'tcp'; + } else if (proxy.network === 'h2') { + vlessType = 'http'; } let vlessTransport = `&type=${encodeURIComponent(vlessType)}`; + if (proxy.network === 'http') { + vlessTransport += '&headerType=http'; + } if (['grpc'].includes(proxy.network)) { // https://github.com/XTLS/Xray-core/issues/91 vlessTransport += `&mode=${encodeURIComponent( @@ -100,10 +261,11 @@ function vless(proxy) { } } + const transportOpts = proxy[`${proxy.network}-opts`] || {}; let vlessTransportServiceName = - proxy[`${proxy.network}-opts`]?.[`${proxy.network}-service-name`]; - let vlessTransportPath = proxy[`${proxy.network}-opts`]?.path; - let vlessTransportHost = proxy[`${proxy.network}-opts`]?.headers?.Host; + transportOpts?.[`${proxy.network}-service-name`]; + let vlessTransportPath = transportOpts?.path; + let vlessTransportHost = getTransportHost(proxy.network, transportOpts); if (vlessTransportPath) { vlessTransport += `&path=${encodeURIComponent( Array.isArray(vlessTransportPath) @@ -123,6 +285,11 @@ function vless(proxy) { vlessTransportServiceName, )}`; } + if (proxy.network === 'http' && proxy['http-opts']?.method) { + vlessTransport += `&method=${encodeURIComponent( + proxy['http-opts'].method, + )}`; + } if (proxy.network === 'kcp') { if (proxy.seed) { vlessTransport += `&seed=${encodeURIComponent(proxy.seed)}`; @@ -133,12 +300,46 @@ function vless(proxy) { )}`; } } + if ( + proxy.network === 'ws' && + !proxy['ws-opts']?.['v2ray-http-upgrade'] && + proxy['ws-opts']?.['max-early-data'] != null + ) { + vlessTransport += `&ed=${encodeURIComponent( + proxy['ws-opts']['max-early-data'], + )}`; + } + if ( + proxy.network === 'ws' && + proxy['ws-opts']?.['v2ray-http-upgrade'] && + proxy['ws-opts']?.['max-early-data'] != null + ) { + vlessTransport += `&ed=${encodeURIComponent( + proxy['ws-opts']['max-early-data'], + )}`; + } + const earlyDataHeaderName = proxy['ws-opts']?.['early-data-header-name']; + if ( + earlyDataHeaderName && + (proxy['ws-opts']?.['v2ray-http-upgrade'] || + proxy['ws-opts']?.['max-early-data'] == null || + earlyDataHeaderName !== 'Sec-WebSocket-Protocol') + ) { + vlessTransport += `&eh=${encodeURIComponent(earlyDataHeaderName)}`; + } + + let packetEncoding = ''; + if (proxy['packet-addr']) { + packetEncoding = '&packetEncoding=packet'; + } else if (proxy.udp === true && !proxy.xudp) { + packetEncoding = '&packetEncoding=none'; + } return `vless://${proxy.uuid}@${proxy.server}:${ proxy.port }?security=${encodeURIComponent( security, - )}${vlessTransport}${alpn}${allowInsecure}${pcs}${ech}${h2}${sni}${fp}${flow}${sid}${spx}${pbk}${mode}${extra}${pqv}${encryption}#${encodeURIComponent( + )}${vlessTransport}${packetEncoding}${alpn}${allowInsecure}${pcs}${ech}${h2}${sni}${fp}${flow}${sid}${spx}${pbk}${mode}${extra}${pqv}${encryption}#${encodeURIComponent( proxy.name, )}`; } 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 a790fc4b..42c4f39b 100644 --- a/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js +++ b/backend/src/test/proxy-parsers/v2ray-and-platforms.spec.js @@ -2,7 +2,7 @@ import { Base64 } from 'js-base64'; import { expect } from 'chai'; import { describe, it } from 'mocha'; -import { UUID, expectSubset, parseOne } from './helpers'; +import { UUID, expectSubset, parseAll, parseOne } from './helpers'; describe('VMess and VLESS parser coverage', function () { describe('VMess URIs', function () { @@ -245,6 +245,8 @@ describe('VMess and VLESS parser coverage', function () { 'skip-cert-verify': true, 'client-fingerprint': 'chrome', alpn: ['h2'], + udp: true, + xudp: true, network: 'ws', 'ws-opts': { path: '/ws', @@ -255,6 +257,49 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses websocket VLESS shares with packet encoding and early data', function () { + const proxy = parseOne( + `vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&host=cdn.example.com&path=%2Fws&packetEncoding=packet&ed=2048&eh=X-Data#VLESS%20WS%20Early`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS WS Early', + server: 'vless-ws.example.com', + port: 443, + uuid: UUID, + tls: true, + udp: true, + 'packet-addr': true, + network: 'ws', + 'ws-opts': { + path: '/ws', + headers: { + Host: 'cdn.example.com', + }, + 'max-early-data': 2048, + 'early-data-header-name': 'X-Data', + }, + }); + expect(proxy).to.not.have.property('xudp'); + }); + + it('rejects websocket VLESS shares with malformed early data size', function () { + expect( + parseAll( + `vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&host=cdn.example.com&path=%2Fws&ed=2048foo#VLESS%20WS%20Broken`, + ), + ).to.have.length(0); + }); + + it('rejects websocket VLESS shares with oversized early data size', function () { + expect( + parseAll( + `vless://${UUID}@vless-ws.example.com:443?type=ws&security=tls&host=cdn.example.com&path=%2Fws&ed=999999999999999999999#VLESS%20WS%20Too%20Large`, + ), + ).to.have.length(0); + }); + it('parses grpc reality VLESS shares', function () { const proxy = parseOne( `vless://${UUID}@vless-grpc.example.com:443?type=grpc&security=reality&serviceName=grpc-service&authority=grpc.example.com&mode=multi&pbk=pubkey&sid=08&spx=%2Fspider&flow=xtls-rprx-vision&encryption=none&pqv=1&alpn=h2#VLESS%20Reality`, @@ -288,7 +333,7 @@ describe('VMess and VLESS parser coverage', function () { it('parses tcp http-header VLESS shares', function () { const proxy = parseOne( - `vless://${UUID}@vless-http.example.com:80?type=tcp&headerType=http&host=http.example.com&path=%2Fedge#VLESS%20HTTP`, + `vless://${UUID}@vless-http.example.com:80?type=tcp&headerType=http&host=http.example.com&path=%2Fedge&method=GET#VLESS%20HTTP`, ); expectSubset(proxy, { @@ -301,6 +346,7 @@ describe('VMess and VLESS parser coverage', function () { headers: { Host: ['http.example.com'], }, + method: 'GET', path: ['/edge'], }, }); @@ -328,6 +374,30 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses httpupgrade VLESS shares with early data metadata', function () { + const proxy = parseOne( + `vless://${UUID}@vless-upgrade.example.com:443?type=httpupgrade&host=upgrade.example.com&path=%2Fupgrade&ed=1024&eh=X-Upgrade#VLESS%20Upgrade%20Early`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS Upgrade Early', + server: 'vless-upgrade.example.com', + port: 443, + network: 'ws', + 'ws-opts': { + path: '/upgrade', + headers: { + Host: 'upgrade.example.com', + }, + 'v2ray-http-upgrade': true, + 'v2ray-http-upgrade-fast-open': true, + 'max-early-data': 1024, + 'early-data-header-name': 'X-Upgrade', + }, + }); + }); + it('parses kcp VLESS shares', function () { const proxy = parseOne( `vless://${UUID}@vless-kcp.example.com:443?type=kcp&headerType=srtp&host=kcp.example.com&path=%2Fkcp&seed=seed-value&mode=packet&extra=extra-value#VLESS%20KCP`, @@ -352,9 +422,9 @@ describe('VMess and VLESS parser coverage', function () { }); }); - it('parses h2 VLESS shares', function () { + it('parses h2 VLESS shares from share-link http transport', function () { const proxy = parseOne( - `vless://${UUID}@vless-h2.example.com:443?type=h2&host=h2.example.com&path=%2Fh2&h2=1#VLESS%20H2`, + `vless://${UUID}@vless-h2.example.com:443?type=http&host=h2.example.com&path=%2Fh2&h2=1&packetEncoding=none#VLESS%20H2`, ); expectSubset(proxy, { @@ -362,6 +432,7 @@ describe('VMess and VLESS parser coverage', function () { name: 'VLESS H2', server: 'vless-h2.example.com', port: 443, + udp: true, network: 'h2', _h2: true, 'h2-opts': { @@ -371,6 +442,7 @@ describe('VMess and VLESS parser coverage', function () { path: '/h2', }, }); + expect(proxy).to.not.have.property('xudp'); }); it('parses xhttp VLESS shares with mihomo transport extras', function () { @@ -419,6 +491,110 @@ describe('VMess and VLESS parser coverage', function () { }); }); + it('parses xhttp VLESS shares with downloadSettings extra', function () { + const extra = JSON.stringify({ + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + tlsSettings: { + serverName: 'download-sni.example.com', + fingerprint: 'chrome', + alpn: ['h2', 'http/1.1'], + }, + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + noGRPCHeader: true, + xPaddingBytes: '32-64', + extra: { + xmux: { + maxConnections: '8', + hMaxReusableSecs: '900', + }, + }, + }, + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&mode=stream-up&extra=${encodeURIComponent(extra)}#VLESS%20XHTTP%20Download`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Download', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + mode: 'stream-up', + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + 'client-fingerprint': 'chrome', + alpn: ['h2', 'http/1.1'], + path: '/download', + host: 'download-host.example.com', + 'no-grpc-header': true, + 'x-padding-bytes': '32-64', + 'reuse-settings': { + 'max-connections': '8', + 'h-max-reusable-secs': '900', + }, + }, + }, + }); + }); + + it('parses xhttp VLESS shares with downloadSettings extra without mode', function () { + const extra = JSON.stringify({ + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + tlsSettings: { + serverName: 'download-sni.example.com', + }, + }, + }); + const proxy = parseOne( + `vless://${UUID}@vless-xhttp.example.com:443?type=xhttp&security=tls&host=cdn.example.com&path=%2Fxhttp&extra=${encodeURIComponent(extra)}#VLESS%20XHTTP%20Download%20No%20Mode`, + ); + + expectSubset(proxy, { + type: 'vless', + name: 'VLESS XHTTP Download No Mode', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + path: '/xhttp', + headers: { + Host: 'cdn.example.com', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + }, + }, + }); + expect(proxy['xhttp-opts']).to.not.have.property('mode'); + }); + it('parses xhttp VLESS shares with range-form scMaxEachPostBytes', function () { const extra = JSON.stringify({ noGRPCHeader: true, diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index f95b865e..c1cb2a67 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -444,7 +444,107 @@ describe('Proxy text producers', function () { }); expect(output).to.equal( - `vless://${UUID}@vless.example.com:443?security=reality&type=ws&path=%2Fws&host=cdn.example.com&alpn=h2&sni=sni.example.com&fp=chrome&flow=xtls-rprx-vision&sid=08&spx=%2Fspider&pbk=pubkey#URI%20Reality`, + `vless://${UUID}@vless.example.com:443?security=reality&type=ws&path=%2Fws&host=cdn.example.com&ed=2048&alpn=h2&sni=sni.example.com&fp=chrome&flow=xtls-rprx-vision&sid=08&spx=%2Fspider&pbk=pubkey#URI%20Reality`, + ); + }); + + it('produces URI VLESS websocket links with packet encoding and custom early data headers', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI WS Early', + server: 'vless.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + udp: true, + 'packet-addr': true, + network: 'ws', + 'ws-opts': { + path: '/ws', + headers: { + Host: 'cdn.example.com', + }, + 'max-early-data': 2048, + 'early-data-header-name': 'X-Data', + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless.example.com:443?security=tls&type=ws&path=%2Fws&host=cdn.example.com&ed=2048&eh=X-Data&packetEncoding=packet&sni=sni.example.com#URI%20WS%20Early`, + ); + }); + + it('produces URI VLESS fake-http links with method and headerType', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI HTTP', + server: 'vless-http.example.com', + port: 80, + uuid: UUID, + network: 'http', + udp: true, + 'http-opts': { + path: ['/edge'], + method: 'GET', + headers: { + Host: ['http.example.com'], + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-http.example.com:80?security=none&type=tcp&headerType=http&path=%2Fedge&host=http.example.com&method=GET&packetEncoding=none#URI%20HTTP`, + ); + }); + + it('produces URI VLESS httpupgrade links with early data metadata', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI Upgrade', + server: 'vless-upgrade.example.com', + port: 443, + uuid: UUID, + tls: true, + network: 'ws', + 'ws-opts': { + path: '/upgrade', + headers: { + Host: 'upgrade.example.com', + }, + 'v2ray-http-upgrade': true, + 'v2ray-http-upgrade-fast-open': true, + 'max-early-data': 1024, + 'early-data-header-name': 'X-Upgrade', + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-upgrade.example.com:443?security=tls&type=httpupgrade&path=%2Fupgrade&host=upgrade.example.com&ed=1024&eh=X-Upgrade#URI%20Upgrade`, + ); + }); + + it('produces URI VLESS h2 links using share-link http transport type', function () { + const output = produceExternal('URI', { + type: 'vless', + name: 'URI H2', + server: 'vless-h2.example.com', + port: 443, + uuid: UUID, + tls: true, + udp: true, + network: 'h2', + _h2: true, + 'h2-opts': { + path: '/h2', + headers: { + host: ['h2.example.com'], + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-h2.example.com:443?security=tls&type=http&path=%2Fh2&host=h2.example.com&packetEncoding=none&h2=1#URI%20H2`, ); }); @@ -477,6 +577,116 @@ describe('Proxy text producers', function () { ); }); + it('prefers raw _extra over structured xhttp fields when producing URI VLESS links', function () { + const extra = JSON.stringify({ + customField: 'keep-me', + downloadSettings: { + address: 'old.example.com', + port: 443, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Raw Extra', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + _extra: extra, + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent(extra)}#URI%20XHTTP%20Raw%20Extra`, + ); + }); + + it('produces URI VLESS xhttp links from structured download settings', function () { + const extra = JSON.stringify({ + noGRPCHeader: true, + xPaddingBytes: '64-128', + xmux: { + maxConnections: '8', + }, + downloadSettings: { + address: 'download.example.com', + port: 8443, + security: 'tls', + tlsSettings: { + serverName: 'download-sni.example.com', + fingerprint: 'chrome', + alpn: ['h2', 'http/1.1'], + }, + xhttpSettings: { + path: '/download', + host: 'download-host.example.com', + noGRPCHeader: true, + xPaddingBytes: '32-64', + extra: { + xmux: { + maxConcurrency: '16-32', + }, + }, + }, + }, + }); + const output = produceExternal('URI', { + type: 'vless', + name: 'URI XHTTP Download', + server: 'vless-xhttp.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'sni.example.com', + network: 'xhttp', + 'xhttp-opts': { + path: '/xhttp', + mode: 'stream-up', + headers: { + Host: 'cdn.example.com', + }, + 'no-grpc-header': true, + 'x-padding-bytes': '64-128', + 'reuse-settings': { + 'max-connections': '8', + }, + 'download-settings': { + server: 'download.example.com', + port: 8443, + tls: true, + servername: 'download-sni.example.com', + 'client-fingerprint': 'chrome', + alpn: ['h2', 'http/1.1'], + path: '/download', + host: 'download-host.example.com', + 'no-grpc-header': true, + 'x-padding-bytes': '32-64', + 'reuse-settings': { + 'max-concurrency': '16-32', + }, + }, + }, + }); + + expect(output).to.equal( + `vless://${UUID}@vless-xhttp.example.com:443?security=tls&type=xhttp&path=%2Fxhttp&host=cdn.example.com&sni=sni.example.com&mode=stream-up&extra=${encodeURIComponent(extra)}#URI%20XHTTP%20Download`, + ); + }); + it('produces V2Ray exports as base64 encoded URI lists', function () { const output = produceExternal('V2Ray', { type: 'vless', diff --git a/scripts/demo.js b/scripts/demo.js index fbbe996b..a4e8759a 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -36,6 +36,7 @@ function operator(proxies = [], targetPlatform, context) { // 22. `sing-box` 支持使用完整的 `_curve_preferences` 结构设置 `tls` 的 `curve_preferences` // 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 部分, 而不进行解析和重组 // require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块 // 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件