mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 23:13:43 +00:00
feat(wip): 支持 SurgeMac 下载参数透传并增强 Mihomo External 合并行为
- 下载接口新增 SurgeMac 专属参数提取方法,统一处理 `mihomoMerge / mihomoMergeName / mihomoExternal` - `downloadSubscription` 与 `downloadCollection` 都透传这些参数到生成配置逻辑,且保留现有默认行为 - SurgeMac 生产器支持通过 `opts.mihomoExternal` 控制 mihomo 输出,并在 fallback 场景下安全返回空字符串兜底 - `SurgeMac` 日志增加 `mihomoMerge / mihomoMergeName / mihomoExternal` 可观测输出,排障更清晰 - 新增下载路由测试:单订阅与组合订阅下的默认/合并/链接参数场景覆盖
This commit is contained in:
3 files changed
+271
-41
No files matched your search
@@ -11,15 +11,15 @@ const surge_Producer = Surge_Producer();
|
||||
|
||||
export default function SurgeMac_Producer() {
|
||||
const produce = (proxy, type, opts = {}) => {
|
||||
if (proxy._mihomoExternal) {
|
||||
return mihomo(proxy, type, opts);
|
||||
}
|
||||
switch (proxy.type) {
|
||||
case 'external':
|
||||
return external(proxy);
|
||||
// case 'ssr':
|
||||
// return shadowsocksr(proxy);
|
||||
default: {
|
||||
if (opts.mihomoExternal || proxy._mihomoExternal) {
|
||||
return mihomo(proxy, type, opts) || '';
|
||||
}
|
||||
try {
|
||||
return surge_Producer.produce(proxy, type, opts);
|
||||
} catch (e) {
|
||||
@@ -27,7 +27,7 @@ export default function SurgeMac_Producer() {
|
||||
opts.useMihomoExternal &&
|
||||
e instanceof SurgeUnsupportedProxyError
|
||||
) {
|
||||
const output = mihomo(proxy, type, opts);
|
||||
const output = mihomo(proxy, type, opts) || '';
|
||||
if (!output) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,22 @@ function buildEmptyNezhaPayload() {
|
||||
);
|
||||
}
|
||||
|
||||
function getMihomoExternalOptions(query) {
|
||||
const useMihomoExternal = query.target === 'SurgeMac';
|
||||
const mihomoExternal = useMihomoExternal ? query.mihomoExternal : undefined;
|
||||
const mihomoMerge = useMihomoExternal ? query.mihomoMerge : undefined;
|
||||
const mihomoMergeName = useMihomoExternal
|
||||
? query.mihomoMergeName
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
useMihomoExternal,
|
||||
mihomoExternal,
|
||||
mihomoMerge,
|
||||
mihomoMergeName,
|
||||
};
|
||||
}
|
||||
|
||||
export default function register($app) {
|
||||
$app.get('/share/col/:name/:target', async (req, res) => {
|
||||
const { target } = req.params;
|
||||
@@ -98,17 +114,8 @@ export default function register($app) {
|
||||
async function downloadSubscription(req, res) {
|
||||
let { name, nezhaIndex } = req.params;
|
||||
|
||||
const useMihomoExternal = req.query.target === 'SurgeMac';
|
||||
// 仅在 useMihomoExternal=true 时生效:让全部节点共用一个 mihomo 进程 + 多个 SOCKS5 listener
|
||||
// 等价于在脚本里对每个节点设置 _merge=true,避免每个节点 spawn 一个 mihomo 进程
|
||||
const mihomoMerge =
|
||||
useMihomoExternal &&
|
||||
['true', '1', ''].includes(String(req.query.mihomoMerge ?? ''));
|
||||
const mihomoMergeName =
|
||||
typeof req.query.mihomoMergeName === 'string' &&
|
||||
req.query.mihomoMergeName.length > 0
|
||||
? req.query.mihomoMergeName
|
||||
: undefined;
|
||||
const { useMihomoExternal, mihomoMerge, mihomoMergeName, mihomoExternal } =
|
||||
getMihomoExternalOptions(req.query);
|
||||
|
||||
const platform =
|
||||
req.query.platform ||
|
||||
@@ -119,13 +126,6 @@ async function downloadSubscription(req, res) {
|
||||
$.info(
|
||||
`正在下载订阅:${name}\n请求 User-Agent: ${reqUA}\n请求 target: ${req.query.target}\n实际输出: ${platform}`,
|
||||
);
|
||||
if (mihomoMerge) {
|
||||
$.info(
|
||||
`已启用 mihomoMerge:合并所有 Mihomo External 节点到单个进程${
|
||||
mihomoMergeName ? `(名称:${mihomoMergeName})` : ''
|
||||
}`,
|
||||
);
|
||||
}
|
||||
let {
|
||||
url,
|
||||
ua,
|
||||
@@ -203,10 +203,19 @@ async function downloadSubscription(req, res) {
|
||||
if (prettyYaml) {
|
||||
$.info(`指定输出易读 YAML: ${prettyYaml}`);
|
||||
}
|
||||
if (mihomoMerge) {
|
||||
$.info(`指定合并 Mihomo External: ${mihomoMerge}`);
|
||||
}
|
||||
if (mihomoMergeName) {
|
||||
$.info(`指定合并 Mihomo External 名称: ${mihomoMergeName}`);
|
||||
}
|
||||
|
||||
if (useMihomoExternal) {
|
||||
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
|
||||
}
|
||||
if (mihomoExternal) {
|
||||
$.info(`手动指定了 Mihomo External 链接参数: ${mihomoExternal}`);
|
||||
}
|
||||
|
||||
if (noCache) {
|
||||
$.info(`指定不使用缓存: ${noCache}`);
|
||||
@@ -244,6 +253,7 @@ async function downloadSubscription(req, res) {
|
||||
useMihomoExternal,
|
||||
merge: mihomoMerge,
|
||||
mergeName: mihomoMergeName,
|
||||
mihomoExternal,
|
||||
prettyYaml,
|
||||
},
|
||||
$options,
|
||||
@@ -437,17 +447,8 @@ async function downloadSubscription(req, res) {
|
||||
async function downloadCollection(req, res) {
|
||||
let { name, nezhaIndex } = req.params;
|
||||
|
||||
const useMihomoExternal = req.query.target === 'SurgeMac';
|
||||
// 仅在 useMihomoExternal=true 时生效:让全部节点共用一个 mihomo 进程 + 多个 SOCKS5 listener
|
||||
// 等价于在脚本里对每个节点设置 _merge=true,避免每个节点 spawn 一个 mihomo 进程
|
||||
const mihomoMerge =
|
||||
useMihomoExternal &&
|
||||
['true', '1', ''].includes(String(req.query.mihomoMerge ?? ''));
|
||||
const mihomoMergeName =
|
||||
typeof req.query.mihomoMergeName === 'string' &&
|
||||
req.query.mihomoMergeName.length > 0
|
||||
? req.query.mihomoMergeName
|
||||
: undefined;
|
||||
const { useMihomoExternal, mihomoMerge, mihomoMergeName, mihomoExternal } =
|
||||
getMihomoExternalOptions(req.query);
|
||||
|
||||
const platform =
|
||||
req.query.platform ||
|
||||
@@ -461,14 +462,6 @@ async function downloadCollection(req, res) {
|
||||
$.info(
|
||||
`正在下载组合订阅:${name}\n请求 User-Agent: ${reqUA}\n请求 target: ${req.query.target}\n实际输出: ${platform}`,
|
||||
);
|
||||
if (mihomoMerge) {
|
||||
$.info(
|
||||
`已启用 mihomoMerge:合并所有 Mihomo External 节点到单个进程${
|
||||
mihomoMergeName ? `(名称:${mihomoMergeName})` : ''
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
let {
|
||||
ignoreFailedRemoteSub,
|
||||
produceType,
|
||||
@@ -527,10 +520,19 @@ async function downloadCollection(req, res) {
|
||||
if (prettyYaml) {
|
||||
$.info(`指定输出易读 YAML: ${prettyYaml}`);
|
||||
}
|
||||
if (mihomoMerge) {
|
||||
$.info(`指定合并 Mihomo External: ${mihomoMerge}`);
|
||||
}
|
||||
if (mihomoMergeName) {
|
||||
$.info(`指定合并 Mihomo External 名称: ${mihomoMergeName}`);
|
||||
}
|
||||
|
||||
if (useMihomoExternal) {
|
||||
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
|
||||
}
|
||||
if (mihomoExternal) {
|
||||
$.info(`手动指定了 Mihomo External 链接参数: ${mihomoExternal}`);
|
||||
}
|
||||
if (noCache) {
|
||||
$.info(`指定不使用缓存: ${noCache}`);
|
||||
}
|
||||
@@ -548,6 +550,7 @@ async function downloadCollection(req, res) {
|
||||
useMihomoExternal,
|
||||
merge: mihomoMerge,
|
||||
mergeName: mihomoMergeName,
|
||||
mihomoExternal,
|
||||
prettyYaml,
|
||||
},
|
||||
$options,
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
import { expect } from 'chai';
|
||||
import { after, before, beforeEach, describe, it } from 'mocha';
|
||||
|
||||
import { COLLECTIONS_KEY, SUBS_KEY } from '@/constants';
|
||||
|
||||
const UUID = '11111111-1111-4111-8111-111111111111';
|
||||
const VLESS_WS = `vless://${UUID}@1.1.1.1:443?security=tls&type=ws&host=cdn.example.com&path=%2Fws&sni=sni.example.com#VLESS%20WS`;
|
||||
|
||||
let $;
|
||||
let registerDownloadRoutes;
|
||||
let originalError;
|
||||
let originalInfo;
|
||||
let originalNotify;
|
||||
let originalRead;
|
||||
let originalWarn;
|
||||
let originalWrite;
|
||||
let state;
|
||||
|
||||
function createRouteApp() {
|
||||
const handlers = new Map();
|
||||
|
||||
return {
|
||||
handlers,
|
||||
get(pattern, handler) {
|
||||
handlers.set(`GET ${pattern}`, handler);
|
||||
return this;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function getHandler(pattern) {
|
||||
const app = createRouteApp();
|
||||
registerDownloadRoutes(app);
|
||||
return app.handlers.get(`GET ${pattern}`);
|
||||
}
|
||||
|
||||
function createResponse(routePath) {
|
||||
return {
|
||||
headers: {},
|
||||
req: {
|
||||
route: {
|
||||
path: routePath,
|
||||
},
|
||||
},
|
||||
sent: null,
|
||||
statusCode: 200,
|
||||
removeHeader(key) {
|
||||
delete this.headers[key];
|
||||
return this;
|
||||
},
|
||||
send(payload) {
|
||||
this.sent = payload;
|
||||
return this;
|
||||
},
|
||||
set(key, value) {
|
||||
this.headers[key] = value;
|
||||
return this;
|
||||
},
|
||||
status(code) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function downloadSubscription(query) {
|
||||
const handler = getHandler('/download/:name');
|
||||
const res = createResponse('/download/:name');
|
||||
|
||||
await handler(
|
||||
{
|
||||
body: {},
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
params: { name: 'local-vless' },
|
||||
path: '/download/local-vless',
|
||||
query,
|
||||
url: '/download/local-vless',
|
||||
},
|
||||
res,
|
||||
);
|
||||
|
||||
return res.sent;
|
||||
}
|
||||
|
||||
async function downloadCollection(query) {
|
||||
const handler = getHandler('/download/collection/:name');
|
||||
const res = createResponse('/download/collection/:name');
|
||||
|
||||
await handler(
|
||||
{
|
||||
body: {},
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
params: { name: 'local-col' },
|
||||
path: '/download/collection/local-col',
|
||||
query,
|
||||
url: '/download/collection/local-col',
|
||||
},
|
||||
res,
|
||||
);
|
||||
|
||||
return res.sent;
|
||||
}
|
||||
|
||||
describe('download routes', function () {
|
||||
before(async function () {
|
||||
({ default: $ } = require('@/core/app'));
|
||||
({ default: registerDownloadRoutes } = require('@/restful/download'));
|
||||
|
||||
originalRead = $.read.bind($);
|
||||
originalWrite = $.write.bind($);
|
||||
originalInfo = $.info.bind($);
|
||||
originalWarn = $.warn.bind($);
|
||||
originalError = $.error.bind($);
|
||||
originalNotify = $.notify.bind($);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
if ($) {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
$.info = originalInfo;
|
||||
$.warn = originalWarn;
|
||||
$.error = originalError;
|
||||
$.notify = originalNotify;
|
||||
}
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
state = {
|
||||
[SUBS_KEY]: [
|
||||
{
|
||||
name: 'local-vless',
|
||||
source: 'local',
|
||||
content: VLESS_WS,
|
||||
},
|
||||
],
|
||||
[COLLECTIONS_KEY]: [
|
||||
{
|
||||
name: 'local-col',
|
||||
subscriptions: ['local-vless'],
|
||||
firstSubFlow: false,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
$.read = (key) => state[key] || [];
|
||||
$.write = (data, key) => {
|
||||
state[key] = data;
|
||||
return true;
|
||||
};
|
||||
$.info = () => {};
|
||||
$.warn = () => {};
|
||||
$.error = () => {};
|
||||
$.notify = () => {};
|
||||
});
|
||||
|
||||
it('keeps SurgeMac Mihomo external output unmerged by default', async function () {
|
||||
const output = await downloadSubscription({ target: 'SurgeMac' });
|
||||
|
||||
expect(output).to.include(
|
||||
'VLESS WS=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
expect(output).to.not.include('VLESS WS=socks5');
|
||||
expect(output).to.not.include('mihomo merged=external');
|
||||
});
|
||||
|
||||
it('enables merged Mihomo external output when the query parameter is present', async function () {
|
||||
const output = await downloadSubscription({
|
||||
target: 'SurgeMac',
|
||||
mihomoMerge: 'true',
|
||||
mihomoMergeName: 'Shared Mihomo',
|
||||
});
|
||||
|
||||
expect(output).to.include('VLESS WS=socks5,127.0.0.1,65535');
|
||||
expect(output).to.include(
|
||||
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
expect(output).to.not.include(
|
||||
'VLESS WS=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
});
|
||||
|
||||
it('treats mihomoMerge=false as enabled because query flags are presence-based', async function () {
|
||||
const output = await downloadSubscription({
|
||||
target: 'SurgeMac',
|
||||
mihomoMerge: 'false',
|
||||
mihomoMergeName: 'Shared Mihomo',
|
||||
});
|
||||
|
||||
expect(output).to.include('VLESS WS=socks5,127.0.0.1,65535');
|
||||
expect(output).to.include(
|
||||
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
expect(output).to.not.include(
|
||||
'VLESS WS=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps merged Mihomo process external when mihomoExternal is enabled', async function () {
|
||||
const output = await downloadSubscription({
|
||||
target: 'SurgeMac',
|
||||
mihomoExternal: 'true',
|
||||
mihomoMerge: 'true',
|
||||
});
|
||||
|
||||
expect(output).to.include('VLESS WS=socks5,127.0.0.1,65535');
|
||||
expect(output).to.include(
|
||||
'mihomo merged=external,exec="/usr/local/bin/mihomo",local-port=65534',
|
||||
);
|
||||
expect(output).to.not.include('mihomo merged=socks5');
|
||||
});
|
||||
|
||||
it('enables merged Mihomo external output for collection downloads', async function () {
|
||||
const output = await downloadCollection({
|
||||
target: 'SurgeMac',
|
||||
mihomoMerge: 'true',
|
||||
mihomoMergeName: 'Shared Mihomo',
|
||||
});
|
||||
|
||||
expect(output).to.include('VLESS WS=socks5,127.0.0.1,65535');
|
||||
expect(output).to.include(
|
||||
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user