feat: clash 系/sing-box mux 逻辑处理; dev 保持两种

This commit is contained in:
xream committed 2026-04-03 17:05:05 +08:00
1 parent 0883ec9a30
commit 690b2c1e04
10 files changed
+199 -145

No files matched your search

+7 -1
View File
@@ -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
+20 -141
View File
@@ -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');
});
+148
View File
@@ -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;
});
+4 -2
View File
@@ -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"
},
@@ -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': {
@@ -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'] =
@@ -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'] =
@@ -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'] =
@@ -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]}`);
@@ -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'] =