mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 23:13:43 +00:00
feat: 调整生成 mihomo 配置的逻辑, 内容在脚本操作前生成; 文件路由支持 download 参数启用下载(文件名为显示名称)
This commit is contained in:
7 files changed
+109
-39
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.28.1",
|
||||
"version": "2.28.2",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -412,28 +412,7 @@ function ScriptOperator(
|
||||
if (typeof patch !== 'object')
|
||||
throw new Error('patch is not an object');
|
||||
output.$content = ProxyUtils.yaml.safeDump(
|
||||
deepMerge(
|
||||
config ||
|
||||
(output?.$file?.sourceType === 'none'
|
||||
? {}
|
||||
: {
|
||||
proxies: await produceArtifact({
|
||||
type:
|
||||
output?.$file?.sourceType ||
|
||||
'collection',
|
||||
name: output?.$file?.sourceName,
|
||||
platform: 'mihomo',
|
||||
produceType: 'internal',
|
||||
produceOpts: {
|
||||
'delete-underscore-fields': true,
|
||||
'include-unsupported-proxy':
|
||||
output?.$file
|
||||
?.includeUnsupportedProxy,
|
||||
},
|
||||
}),
|
||||
}),
|
||||
patch,
|
||||
),
|
||||
deepMerge(config || {}, patch),
|
||||
);
|
||||
return output;
|
||||
} catch (e) {
|
||||
@@ -470,19 +449,7 @@ function ScriptOperator(
|
||||
console.log(e.message ?? e);
|
||||
}
|
||||
}
|
||||
$content = ProxyUtils.yaml.safeDump(await main(config || ($file.sourceType === 'none' ? {} : {
|
||||
proxies: await produceArtifact({
|
||||
type: $file.sourceType || 'collection',
|
||||
name: $file.sourceName,
|
||||
platform: 'mihomo',
|
||||
produceType: 'internal',
|
||||
produceOpts: {
|
||||
'delete-underscore-fields': true,
|
||||
'include-unsupported-proxy':
|
||||
$file.includeUnsupportedProxy,
|
||||
}
|
||||
}),
|
||||
})))
|
||||
$content = ProxyUtils.yaml.safeDump(await main(config || {}))
|
||||
}
|
||||
} else {
|
||||
${script}
|
||||
|
||||
@@ -69,6 +69,7 @@ async function getFile(req, res, next) {
|
||||
proxy,
|
||||
noCache,
|
||||
produceType,
|
||||
download,
|
||||
fakeFile: _fakeFile,
|
||||
} = req.query;
|
||||
let $options = {
|
||||
@@ -204,6 +205,9 @@ async function getFile(req, res, next) {
|
||||
if (produceType) {
|
||||
$.info(`指定生产类型: ${produceType}`);
|
||||
}
|
||||
if (download) {
|
||||
$.info('启用下载(文件名为显示名称)');
|
||||
}
|
||||
if (includeUnsupportedProxy) {
|
||||
$.info(`包含官方/商店版不支持的协议: ${includeUnsupportedProxy}`);
|
||||
}
|
||||
@@ -277,7 +281,7 @@ async function getFile(req, res, next) {
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
if (file.download) {
|
||||
if (file.download || download) {
|
||||
res.set(
|
||||
'Content-Disposition',
|
||||
`attachment; filename*=UTF-8''${encodeURIComponent(
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
resolveIgnoreFailedRemoteSubMode,
|
||||
shouldFallbackIgnoreFailedRemoteSub,
|
||||
} from '@/restful/ignore-failed-remote-sub';
|
||||
import { prepareMihomoProfileContent } from '@/restful/sync';
|
||||
import { normalizeClashYaml } from '@/core/proxy-utils/preprocessors';
|
||||
import { maskAgeSecretInUrl } from '@/utils/age';
|
||||
|
||||
@@ -28,7 +29,9 @@ async function previewFile(req, res) {
|
||||
try {
|
||||
const file = req.body;
|
||||
let content = '';
|
||||
if (file.type !== 'mihomoProfile') {
|
||||
if (file.type === 'mihomoProfile') {
|
||||
content = await prepareMihomoProfileContent(file);
|
||||
} else {
|
||||
if (
|
||||
file.source === 'local' &&
|
||||
!['localFirst', 'remoteFirst'].includes(file.mergeSources)
|
||||
|
||||
@@ -45,6 +45,23 @@ function formatAgeSafeUrls(errors) {
|
||||
return Object.keys(errors).map(maskAgeSecretInUrl).join(', ');
|
||||
}
|
||||
|
||||
async function prepareMihomoProfileContent(file) {
|
||||
const config = {};
|
||||
if (file?.sourceType !== 'none') {
|
||||
config.proxies = await produceArtifact({
|
||||
type: file?.sourceType || 'collection',
|
||||
name: file?.sourceName,
|
||||
platform: 'mihomo',
|
||||
produceType: 'internal',
|
||||
produceOpts: {
|
||||
'delete-underscore-fields': true,
|
||||
'include-unsupported-proxy': file?.includeUnsupportedProxy,
|
||||
},
|
||||
});
|
||||
}
|
||||
return ProxyUtils.yaml.safeDump(config);
|
||||
}
|
||||
|
||||
async function produceArtifact({
|
||||
type,
|
||||
name,
|
||||
@@ -583,7 +600,9 @@ async function produceArtifact({
|
||||
const file = sourceFile || findByName(allFiles, name);
|
||||
if (!file) throw new Error(`找不到文件 ${name}`);
|
||||
let raw = '';
|
||||
if (file.type !== 'mihomoProfile') {
|
||||
if (file.type === 'mihomoProfile') {
|
||||
raw = await prepareMihomoProfileContent(file);
|
||||
} else {
|
||||
if (
|
||||
content &&
|
||||
!['localFirst', 'remoteFirst'].includes(mergeSources)
|
||||
@@ -1220,6 +1239,7 @@ async function syncArtifact(req, res) {
|
||||
|
||||
export {
|
||||
markArtifactProducedWithoutUpload,
|
||||
prepareMihomoProfileContent,
|
||||
produceArtifact,
|
||||
produceSyncArtifactOutput,
|
||||
shouldUploadArtifact,
|
||||
|
||||
@@ -225,6 +225,20 @@ describe('file routes', function () {
|
||||
expect(fakeFileRes.sent).to.equal('from-fake-file');
|
||||
});
|
||||
|
||||
it('forces attachment download with the display name from query', async function () {
|
||||
state[FILES_KEY][0].displayName = '显示名称.yaml';
|
||||
|
||||
const res = await requestFile({ download: '1' });
|
||||
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.headers['Content-Disposition']).to.equal(
|
||||
`attachment; filename*=UTF-8''${encodeURIComponent(
|
||||
'显示名称.yaml',
|
||||
)}`,
|
||||
);
|
||||
expect(res.sent).to.equal('base');
|
||||
});
|
||||
|
||||
it('rejects fakeFile without content or url', async function () {
|
||||
state[FILES_KEY] = [];
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { FILES_KEY, SUBS_KEY } from '@/constants';
|
||||
|
||||
let $;
|
||||
let registerFileRoutes;
|
||||
let registerPreviewRoutes;
|
||||
let originalError;
|
||||
let originalInfo;
|
||||
let originalNotify;
|
||||
@@ -101,10 +102,33 @@ async function requestFile(name, query = {}) {
|
||||
return res;
|
||||
}
|
||||
|
||||
async function previewFile(file) {
|
||||
const app = createRouteApp();
|
||||
registerPreviewRoutes(app);
|
||||
const handler = app.handlers.get('POST /api/preview/file');
|
||||
const res = createResponse('/api/preview/file');
|
||||
|
||||
await handler(
|
||||
{
|
||||
body: file,
|
||||
headers: {},
|
||||
method: 'POST',
|
||||
path: '/api/preview/file',
|
||||
query: {},
|
||||
socket: {},
|
||||
url: '/api/preview/file',
|
||||
},
|
||||
res,
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
describe('mihomo profile file routes', function () {
|
||||
before(async function () {
|
||||
({ default: $ } = require('@/core/app'));
|
||||
({ default: registerFileRoutes } = require('@/restful/file'));
|
||||
({ default: registerPreviewRoutes } = require('@/restful/preview'));
|
||||
|
||||
originalRead = $.read.bind($);
|
||||
originalWrite = $.write.bind($);
|
||||
@@ -152,6 +176,44 @@ describe('mihomo profile file routes', function () {
|
||||
$.notify = () => {};
|
||||
});
|
||||
|
||||
it('builds mihomoProfile proxies before file processors run', async function () {
|
||||
state[FILES_KEY] = [
|
||||
{
|
||||
name: 'base-file',
|
||||
type: 'mihomoProfile',
|
||||
sourceType: 'subscription',
|
||||
sourceName: 'demo-sub',
|
||||
includeUnsupportedProxy: false,
|
||||
process: [],
|
||||
},
|
||||
];
|
||||
|
||||
const res = await requestFile('base-file');
|
||||
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.sent).to.include('proxies:');
|
||||
expect(res.sent).to.include('name: Supported');
|
||||
expect(res.sent).to.not.include('name: Unsupported');
|
||||
});
|
||||
|
||||
it('previews mihomoProfile proxies before file processors run', async function () {
|
||||
const res = await previewFile({
|
||||
name: 'preview-file',
|
||||
type: 'mihomoProfile',
|
||||
sourceType: 'subscription',
|
||||
sourceName: 'demo-sub',
|
||||
includeUnsupportedProxy: false,
|
||||
process: [],
|
||||
});
|
||||
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.sent.status).to.equal('success');
|
||||
expect(res.sent.data.original).to.include('proxies:');
|
||||
expect(res.sent.data.original).to.include('name: Supported');
|
||||
expect(res.sent.data.original).to.not.include('name: Unsupported');
|
||||
expect(res.sent.data.processed).to.include('name: Supported');
|
||||
});
|
||||
|
||||
it('keeps unsupported proxies for YAML mihomoProfile overrides when enabled', async function () {
|
||||
state[FILES_KEY] = [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user