From 690b2c1e04429b451e20ac52da1371a509f3d144 Mon Sep 17 00:00:00 2001 From: xream Date: Fri, 3 Apr 2026 17:02:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20clash=20=E7=B3=BB/sing-box=20mux=20?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86;=20dev=20=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E4=B8=A4=E7=A7=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +- backend/dev-esbuild.js | 161 +++--------------- backend/esbuild-dev.js | 148 ++++++++++++++++ backend/package.json | 6 +- backend/src/core/proxy-utils/parsers/index.js | 4 +- .../src/core/proxy-utils/producers/clash.js | 4 + .../core/proxy-utils/producers/clashmeta.js | 4 + .../proxy-utils/producers/shadowrocket.js | 4 + .../core/proxy-utils/producers/sing-box.js | 1 + .../src/core/proxy-utils/producers/stash.js | 4 + 10 files changed, 199 insertions(+), 145 deletions(-) create mode 100644 backend/esbuild-dev.js diff --git a/README.md b/README.md index 20acc7c0..76b8fcd3 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,13 @@ pnpm i ``` ``` -SUB_STORE_BACKEND_API_PORT=3000 pnpm dev +SUB_STORE_BACKEND_API_PORT=3000 pnpm esbuild:dev +``` + +or this one if you're using `Termux` + +``` +SUB_STORE_BACKEND_API_PORT=3000 pnpm run --parallel "/^dev:.*/" ``` ### Build diff --git a/backend/dev-esbuild.js b/backend/dev-esbuild.js index 4bb60596..63b4c2ee 100644 --- a/backend/dev-esbuild.js +++ b/backend/dev-esbuild.js @@ -1,148 +1,27 @@ #!/usr/bin/env node -const path = require('path'); -const { spawn } = require('child_process'); -const { context } = require('esbuild'); - -const outfile = path.join(__dirname, 'sub-store.min.js'); - -let serverProcess = null; -let buildContext = null; -let shuttingDown = false; -let restartQueue = Promise.resolve(); - -function log(message) { - console.log(`[dev:esbuild] ${message}`); -} - -function startServer() { - const child = spawn(process.execPath, [outfile], { - cwd: __dirname, - stdio: 'inherit', - }); - - serverProcess = child; - log(`server started (pid ${child.pid})`); - - child.on('exit', (code, signal) => { - if (serverProcess === child) { - serverProcess = null; - } - - if (!shuttingDown && signal !== 'SIGTERM') { - log( - `server exited${code === null ? '' : ` with code ${code}`}${ - signal ? ` (${signal})` : '' - }`, - ); - } - }); -} - -async function stopServer() { - const child = serverProcess; - if (!child) { - return; - } - - serverProcess = null; - - if (child.exitCode !== null || child.signalCode !== null) { - return; - } - - await new Promise((resolve) => { - const timeout = setTimeout(() => { - child.kill('SIGKILL'); - }, 3000); - - child.once('exit', () => { - clearTimeout(timeout); - resolve(); - }); - - child.kill('SIGTERM'); - }); -} - -async function restartServer() { - if (serverProcess) { - log('restarting server'); - await stopServer(); - } - - if (!shuttingDown) { - startServer(); - } -} - -async function shutdown(signal) { - if (shuttingDown) { - return; - } - - shuttingDown = true; - log(`received ${signal}, shutting down`); - - if (buildContext) { - await buildContext.dispose(); - } - - await restartQueue.catch((error) => { - console.error(error); - }); - await stopServer(); -} +const { build } = require('esbuild'); !(async () => { - buildContext = await context({ - entryPoints: ['src/main.js'], - bundle: true, - minify: false, - sourcemap: false, - platform: 'node', - format: 'cjs', - outfile, - logOverride: { - 'direct-eval': 'silent', - }, - plugins: [ - { - name: 'restart-server-on-build', - setup(build) { - build.onStart(() => { - log('building'); - }); + const artifacts = [{ src: 'src/main.js', dest: 'sub-store.min.js' }]; - build.onEnd((result) => { - if (result.errors.length > 0) { - log(`build failed with ${result.errors.length} error(s)`); - return; - } - - log('build succeeded'); - restartQueue = restartQueue - .catch((error) => { - console.error(error); - }) - .then(() => restartServer()); - return restartQueue; - }); - }, + for await (const artifact of artifacts) { + await build({ + entryPoints: [artifact.src], + bundle: true, + minify: false, + sourcemap: false, + platform: 'node', + format: 'cjs', + outfile: artifact.dest, + logOverride: { + 'direct-eval': 'silent', }, - ], - }); - - process.on('SIGINT', () => { - shutdown('SIGINT').finally(() => process.exit(0)); - }); - process.on('SIGTERM', () => { - shutdown('SIGTERM').finally(() => process.exit(0)); - }); - - await buildContext.watch(); - log('watching for changes'); + }); + } })() - .catch((error) => { - console.error(error); - process.exitCode = 1; + .catch((e) => { + console.log(e); + }) + .finally(() => { + console.log('done'); }); diff --git a/backend/esbuild-dev.js b/backend/esbuild-dev.js new file mode 100644 index 00000000..4bb60596 --- /dev/null +++ b/backend/esbuild-dev.js @@ -0,0 +1,148 @@ +#!/usr/bin/env node +const path = require('path'); +const { spawn } = require('child_process'); +const { context } = require('esbuild'); + +const outfile = path.join(__dirname, 'sub-store.min.js'); + +let serverProcess = null; +let buildContext = null; +let shuttingDown = false; +let restartQueue = Promise.resolve(); + +function log(message) { + console.log(`[dev:esbuild] ${message}`); +} + +function startServer() { + const child = spawn(process.execPath, [outfile], { + cwd: __dirname, + stdio: 'inherit', + }); + + serverProcess = child; + log(`server started (pid ${child.pid})`); + + child.on('exit', (code, signal) => { + if (serverProcess === child) { + serverProcess = null; + } + + if (!shuttingDown && signal !== 'SIGTERM') { + log( + `server exited${code === null ? '' : ` with code ${code}`}${ + signal ? ` (${signal})` : '' + }`, + ); + } + }); +} + +async function stopServer() { + const child = serverProcess; + if (!child) { + return; + } + + serverProcess = null; + + if (child.exitCode !== null || child.signalCode !== null) { + return; + } + + await new Promise((resolve) => { + const timeout = setTimeout(() => { + child.kill('SIGKILL'); + }, 3000); + + child.once('exit', () => { + clearTimeout(timeout); + resolve(); + }); + + child.kill('SIGTERM'); + }); +} + +async function restartServer() { + if (serverProcess) { + log('restarting server'); + await stopServer(); + } + + if (!shuttingDown) { + startServer(); + } +} + +async function shutdown(signal) { + if (shuttingDown) { + return; + } + + shuttingDown = true; + log(`received ${signal}, shutting down`); + + if (buildContext) { + await buildContext.dispose(); + } + + await restartQueue.catch((error) => { + console.error(error); + }); + await stopServer(); +} + +!(async () => { + buildContext = await context({ + entryPoints: ['src/main.js'], + bundle: true, + minify: false, + sourcemap: false, + platform: 'node', + format: 'cjs', + outfile, + logOverride: { + 'direct-eval': 'silent', + }, + plugins: [ + { + name: 'restart-server-on-build', + setup(build) { + build.onStart(() => { + log('building'); + }); + + build.onEnd((result) => { + if (result.errors.length > 0) { + log(`build failed with ${result.errors.length} error(s)`); + return; + } + + log('build succeeded'); + restartQueue = restartQueue + .catch((error) => { + console.error(error); + }) + .then(() => restartServer()); + return restartQueue; + }); + }, + }, + ], + }); + + process.on('SIGINT', () => { + shutdown('SIGINT').finally(() => process.exit(0)); + }); + process.on('SIGTERM', () => { + shutdown('SIGTERM').finally(() => process.exit(0)); + }); + + await buildContext.watch(); + log('watching for changes'); +})() + .catch((error) => { + console.error(error); + process.exitCode = 1; + }); diff --git a/backend/package.json b/backend/package.json index f58f7d50..3ff297ba 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.21.73", + "version": "2.21.74", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { @@ -8,7 +8,9 @@ "test": "mocha src/test/**/*.spec.js --require @babel/register --recursive", "serve": "node sub-store.min.js", "start": "nodemon -w src -w package.json --exec babel-node src/main.js", - "dev": "node dev-esbuild.js", + "dev:esbuild": "nodemon -w src -w package.json dev-esbuild.js", + "dev:run": "nodemon -w sub-store.min.js sub-store.min.js", + "esbuild:dev": "node esbuild-dev.js", "bundle:esbuild": "node bundle-esbuild.js", "changelog": "conventional-changelog -p cli -i CHANGELOG.md -s" }, diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 9294580c..fa5a4f6f 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -319,7 +319,9 @@ function URI_SS() { 'skip-cert-verify': ['1', 'true', 1, true].includes( params['skip-cert-verify'], ), - mux: params.mux, + mux: /^\d+$/.test(params.mux) + ? parseInt(params.mux, 10) + : undefined, }; break; case 'shadow-tls': { diff --git a/backend/src/core/proxy-utils/producers/clash.js b/backend/src/core/proxy-utils/producers/clash.js index ebd1d9ca..fda2dee2 100644 --- a/backend/src/core/proxy-utils/producers/clash.js +++ b/backend/src/core/proxy-utils/producers/clash.js @@ -160,6 +160,10 @@ export default function Clash_Producer() { proxy[`${proxy.network}-opts`].path = '/'; } } + let mux = proxy['plugin-opts']?.mux; + if (mux != null && typeof mux !== 'boolean') { + proxy['plugin-opts'].mux = mux > 0 ? true : false; + } if (proxy['plugin-opts']?.tls) { if (isPresent(proxy, 'skip-cert-verify')) { proxy['plugin-opts']['skip-cert-verify'] = diff --git a/backend/src/core/proxy-utils/producers/clashmeta.js b/backend/src/core/proxy-utils/producers/clashmeta.js index 196cb4a5..75e847ea 100644 --- a/backend/src/core/proxy-utils/producers/clashmeta.js +++ b/backend/src/core/proxy-utils/producers/clashmeta.js @@ -239,6 +239,10 @@ export default function ClashMeta_Producer() { } } + let mux = proxy['plugin-opts']?.mux; + if (mux != null && typeof mux !== 'boolean') { + proxy['plugin-opts'].mux = mux > 0 ? true : false; + } if (proxy['plugin-opts']?.tls) { if (isPresent(proxy, 'skip-cert-verify')) { proxy['plugin-opts']['skip-cert-verify'] = diff --git a/backend/src/core/proxy-utils/producers/shadowrocket.js b/backend/src/core/proxy-utils/producers/shadowrocket.js index 3422e3d8..0d9b44f7 100644 --- a/backend/src/core/proxy-utils/producers/shadowrocket.js +++ b/backend/src/core/proxy-utils/producers/shadowrocket.js @@ -214,6 +214,10 @@ export default function Shadowrocket_Producer() { proxy[`${proxy.network}-opts`].path = '/'; } } + let mux = proxy['plugin-opts']?.mux; + if (mux != null && typeof mux !== 'boolean') { + proxy['plugin-opts'].mux = mux > 0 ? true : false; + } if (proxy['plugin-opts']?.tls) { if (isPresent(proxy, 'skip-cert-verify')) { proxy['plugin-opts']['skip-cert-verify'] = diff --git a/backend/src/core/proxy-utils/producers/sing-box.js b/backend/src/core/proxy-utils/producers/sing-box.js index 0587ef70..30be1d01 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -540,6 +540,7 @@ const ssParser = (proxy = {}) => { case 'mux': if (proxy['plugin-opts'].mux) parsedProxy.multiplex = { enabled: true }; + optArr.push(`mux=${proxy['plugin-opts'].mux}`); break; default: optArr.push(`${k}=${proxy['plugin-opts'][k]}`); diff --git a/backend/src/core/proxy-utils/producers/stash.js b/backend/src/core/proxy-utils/producers/stash.js index 126a736d..9207b4b3 100644 --- a/backend/src/core/proxy-utils/producers/stash.js +++ b/backend/src/core/proxy-utils/producers/stash.js @@ -281,6 +281,10 @@ export default function Stash_Producer() { proxy[`${proxy.network}-opts`].path = '/'; } } + let mux = proxy['plugin-opts']?.mux; + if (mux != null && typeof mux !== 'boolean') { + proxy['plugin-opts'].mux = mux > 0 ? true : false; + } if (proxy['plugin-opts']?.tls) { if (isPresent(proxy, 'skip-cert-verify')) { proxy['plugin-opts']['skip-cert-verify'] =