diff --git a/backend/package.json b/backend/package.json index e0634ce9..6cc41632 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.78", + "version": "2.21.79", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/producers/sing-box.js b/backend/src/core/proxy-utils/producers/sing-box.js index 4232fde2..5977c91d 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -1,6 +1,7 @@ import ClashMeta_Producer from './clashmeta'; import $ from '@/core/app'; import { isIPv4, isIPv6, isPlainObject } from '@/utils'; +import { normalizePluginMuxValue } from './utils'; const ipVersions = { ipv4: 'ipv4_only', @@ -565,11 +566,14 @@ const ssParser = (proxy = {}) => { )}`, ); break; - case 'mux': - if (proxy['plugin-opts'].mux) - parsedProxy.multiplex = { enabled: true }; - optArr.push(`mux=${proxy['plugin-opts'].mux}`); + case 'mux': { + const mux = normalizePluginMuxValue( + proxy['plugin-opts'].mux, + ); + if (mux) parsedProxy.multiplex = { enabled: true }; + optArr.push(`mux=${mux}`); break; + } default: optArr.push(`${k}=${proxy['plugin-opts'][k]}`); } diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 71a73c27..ec821896 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -1,6 +1,7 @@ /* eslint-disable no-case-declarations */ import { Base64 } from 'js-base64'; import { isIPv6 } from '@/utils'; +import { normalizePluginMuxValue } from './utils'; function vless(proxy) { let security = 'none'; @@ -204,6 +205,7 @@ export default function URI_Producer() { ); break; case 'v2ray-plugin': + const mux = normalizePluginMuxValue(opts.mux); query += encodeURIComponent( `v2ray-plugin;obfs=${opts.mode}${ opts.host ? ';obfs-host=' + opts.host : '' @@ -216,7 +218,7 @@ export default function URI_Producer() { ? ';skip-cert-verify=' + opts['skip-cert-verify'] : '' - }${opts.mux != null ? ';mux=' + opts.mux : ''}`, + }${mux != null ? ';mux=' + mux : ''}`, ); break; case 'shadow-tls': diff --git a/backend/src/core/proxy-utils/producers/utils.js b/backend/src/core/proxy-utils/producers/utils.js index c33b76a8..fee1b8f4 100644 --- a/backend/src/core/proxy-utils/producers/utils.js +++ b/backend/src/core/proxy-utils/producers/utils.js @@ -30,6 +30,17 @@ export function isPresent(obj, attr) { return typeof data !== 'undefined' && data !== null; } +export function normalizePluginMuxValue(mux) { + if (typeof mux === 'boolean') return Number(mux); + if (typeof mux === 'string') { + const normalized = mux.trim().toLowerCase(); + if (normalized === 'true') return 1; + if (normalized === 'false') return 0; + if (/^\d+$/.test(normalized)) return parseInt(normalized, 10); + } + return mux; +} + export function produceProxyListOutput(list, type, opts = {}) { if (type === 'internal') return list; diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index a4370161..4ace40c1 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -591,7 +591,7 @@ describe('Proxy structured producers', function () { ]) { const output = produceExternal(platform, proxy); - expect(output, platform).to.match(/\n - \{/); + expect(output, platform).to.match(/\n {2}- \{/); } }); @@ -619,7 +619,7 @@ describe('Proxy structured producers', function () { prettyYaml: true, }); - expect(output, platform).to.not.match(/\n - \{/); + expect(output, platform).to.not.match(/\n {2}- \{/); expect(external.proxies, platform).to.have.length(1); if (platform === 'Egern') { @@ -724,6 +724,190 @@ describe('Proxy structured producers', function () { expect(muxOff.plugin_opts).to.include('mux=0'); }); + it('normalizes boolean v2ray-plugin mux state in sing-box plugin opts', function () { + const buildProxy = (name, mux) => ({ + type: 'ss', + name, + server: 'ss.example.com', + port: 8388, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'v2ray-plugin', + 'plugin-opts': { + mode: 'websocket', + host: 'cdn.example.com', + path: '/socket', + tls: true, + mux, + }, + }); + + const output = loadProducedJson('sing-box', [ + buildProxy('Sing-box Boolean Mux On', true), + buildProxy('Sing-box Boolean Mux Off', false), + ]); + const muxOn = output.outbounds.find( + (outbound) => outbound.tag === 'Sing-box Boolean Mux On', + ); + const muxOff = output.outbounds.find( + (outbound) => outbound.tag === 'Sing-box Boolean Mux Off', + ); + + expectSubset(muxOn, { + tag: 'Sing-box Boolean Mux On', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + multiplex: { + enabled: true, + }, + }); + expect(muxOn.plugin_opts).to.include('mux=1'); + + expectSubset(muxOff, { + tag: 'Sing-box Boolean Mux Off', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + }); + expect(muxOff).to.not.have.property('multiplex'); + expect(muxOff.plugin_opts).to.include('mux=0'); + }); + + it('round-trips Clash-style boolean v2ray-plugin mux flags into sing-box exports', function () { + const proxies = ProxyUtils.parse(`proxies: + - name: Clash Boolean Mux On + type: ss + server: ss.example.com + port: 8388 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: true + - name: Clash Boolean Mux Off + type: ss + server: ss.example.com + port: 8388 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: false +`); + + const output = loadProducedJson('sing-box', proxies); + const muxOn = output.outbounds.find( + (outbound) => outbound.tag === 'Clash Boolean Mux On', + ); + const muxOff = output.outbounds.find( + (outbound) => outbound.tag === 'Clash Boolean Mux Off', + ); + + expectSubset(muxOn, { + tag: 'Clash Boolean Mux On', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + multiplex: { + enabled: true, + }, + }); + expect(muxOn.plugin_opts).to.include('mux=1'); + + expectSubset(muxOff, { + tag: 'Clash Boolean Mux Off', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + }); + expect(muxOff).to.not.have.property('multiplex'); + expect(muxOff.plugin_opts).to.include('mux=0'); + }); + + it('round-trips Clash-style string boolean v2ray-plugin mux flags into sing-box exports', function () { + const proxies = ProxyUtils.parse(`proxies: + - name: Clash String Mux On + type: ss + server: ss.example.com + port: 8388 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: ' TRUE ' + - name: Clash String Mux Off + type: ss + server: ss.example.com + port: 8388 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: ' false ' +`); + + const output = loadProducedJson('sing-box', proxies); + const muxOn = output.outbounds.find( + (outbound) => outbound.tag === 'Clash String Mux On', + ); + const muxOff = output.outbounds.find( + (outbound) => outbound.tag === 'Clash String Mux Off', + ); + + expectSubset(muxOn, { + tag: 'Clash String Mux On', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + multiplex: { + enabled: true, + }, + }); + expect(muxOn.plugin_opts).to.include('mux=1'); + + expectSubset(muxOff, { + tag: 'Clash String Mux Off', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + }); + expect(muxOff).to.not.have.property('multiplex'); + expect(muxOff.plugin_opts).to.include('mux=0'); + }); + + it('treats string v2ray-plugin mux values from legacy JSON payloads as disabled in sing-box exports', function () { + const legacy = 'YWVzLTEyOC1nY206c2VjcmV0QHNzLmV4YW1wbGUuY29tOjgzODg='; + const plugin = + 'eyJtb2RlIjoid2Vic29ja2V0IiwiaG9zdCI6ImNkbi5leGFtcGxlLmNvbSIsInBhdGgiOiIvc29ja2V0IiwidGxzIjp0cnVlLCJtdXgiOiIwIn0='; + + const proxies = ProxyUtils.parse( + `ss://${legacy}?v2ray-plugin=${plugin}#Legacy%20JSON%20Mux%20Off`, + ); + const output = loadProducedJson('sing-box', proxies); + const outbound = output.outbounds.find( + (item) => item.tag === 'Legacy JSON Mux Off', + ); + + expectSubset(outbound, { + tag: 'Legacy JSON Mux Off', + type: 'shadowsocks', + plugin: 'v2ray-plugin', + }); + expect(outbound).to.not.have.property('multiplex'); + expect(outbound.plugin_opts).to.include('mux=0'); + }); + it('emits sing-box outbounds with reality tls and websocket transport', function () { const output = loadProducedJson('sing-box', { type: 'vless', diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index dcda9a2e..f95b865e 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -271,6 +271,150 @@ describe('Proxy text producers', function () { ); }); + it('normalizes boolean v2ray-plugin mux values to integers in URI links', function () { + const muxOnPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;tls;mux=1', + ); + const muxOffPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;mux=0', + ); + + const muxOnOutput = produceExternal('URI', { + type: 'ss', + name: 'SS Boolean Mux On', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'v2ray-plugin', + 'plugin-opts': { + mode: 'websocket', + host: 'cdn.example.com', + path: '/socket', + tls: true, + mux: true, + }, + }); + const muxOffOutput = produceExternal('URI', { + type: 'ss', + name: 'SS Boolean Mux Off', + server: 'ss.example.com', + port: 443, + cipher: 'aes-128-gcm', + password: 'secret', + plugin: 'v2ray-plugin', + 'plugin-opts': { + mode: 'websocket', + host: 'cdn.example.com', + path: '/socket', + mux: false, + }, + }); + + expect(muxOnOutput).to.equal( + `ss://${Base64.encode( + 'aes-128-gcm:secret', + )}@ss.example.com:443/?plugin=${muxOnPlugin}#SS%20Boolean%20Mux%20On`, + ); + expect(muxOffOutput).to.equal( + `ss://${Base64.encode( + 'aes-128-gcm:secret', + )}@ss.example.com:443/?plugin=${muxOffPlugin}#SS%20Boolean%20Mux%20Off`, + ); + }); + + it('round-trips Clash-style boolean v2ray-plugin mux flags into URI links', function () { + const proxies = ProxyUtils.parse(`proxies: + - name: Clash Boolean Mux On + type: ss + server: ss.example.com + port: 443 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: true + - name: Clash Boolean Mux Off + type: ss + server: ss.example.com + port: 443 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + mux: false +`); + + const output = produceExternal('URI', proxies); + const userInfo = Base64.encode('aes-128-gcm:secret'); + const muxOnPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;tls;mux=1', + ); + const muxOffPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;mux=0', + ); + + expect(output).to.equal( + [ + `ss://${userInfo}@ss.example.com:443/?plugin=${muxOnPlugin}#Clash%20Boolean%20Mux%20On`, + `ss://${userInfo}@ss.example.com:443/?plugin=${muxOffPlugin}#Clash%20Boolean%20Mux%20Off`, + ].join('\n'), + ); + }); + + it('round-trips Clash-style string boolean v2ray-plugin mux flags into URI links', function () { + const proxies = ProxyUtils.parse(`proxies: + - name: Clash String Mux On + type: ss + server: ss.example.com + port: 443 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + tls: true + mux: ' TRUE ' + - name: Clash String Mux Off + type: ss + server: ss.example.com + port: 443 + cipher: aes-128-gcm + password: secret + plugin: v2ray-plugin + plugin-opts: + mode: websocket + host: cdn.example.com + path: /socket + mux: ' false ' +`); + + const output = produceExternal('URI', proxies); + const userInfo = Base64.encode('aes-128-gcm:secret'); + const muxOnPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;tls;mux=1', + ); + const muxOffPlugin = encodeURIComponent( + 'v2ray-plugin;obfs=websocket;obfs-host=cdn.example.com;host=cdn.example.com;path=/socket;mux=0', + ); + + expect(output).to.equal( + [ + `ss://${userInfo}@ss.example.com:443/?plugin=${muxOnPlugin}#Clash%20String%20Mux%20On`, + `ss://${userInfo}@ss.example.com:443/?plugin=${muxOffPlugin}#Clash%20String%20Mux%20Off`, + ].join('\n'), + ); + }); + it('produces URI VLESS reality websocket links', function () { const output = produceExternal('URI', { type: 'vless',