diff --git a/backend/package.json b/backend/package.json index 7f5593cf..acaaeb64 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/src/core/proxy-utils/processors/index.js b/backend/src/core/proxy-utils/processors/index.js index 09427194..97c66262 100644 --- a/backend/src/core/proxy-utils/processors/index.js +++ b/backend/src/core/proxy-utils/processors/index.js @@ -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} diff --git a/backend/src/restful/file.js b/backend/src/restful/file.js index d3042484..9291e512 100644 --- a/backend/src/restful/file.js +++ b/backend/src/restful/file.js @@ -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( diff --git a/backend/src/restful/preview.js b/backend/src/restful/preview.js index 999f5712..82875529 100644 --- a/backend/src/restful/preview.js +++ b/backend/src/restful/preview.js @@ -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) diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index 9c3436a9..74ddb748 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -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, diff --git a/backend/src/test/restful/file.spec.js b/backend/src/test/restful/file.spec.js index 24e9d58b..ee70e74b 100644 --- a/backend/src/test/restful/file.spec.js +++ b/backend/src/test/restful/file.spec.js @@ -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] = []; diff --git a/backend/src/test/restful/mihomo-profile-file.spec.js b/backend/src/test/restful/mihomo-profile-file.spec.js index 0068066f..53240ab1 100644 --- a/backend/src/test/restful/mihomo-profile-file.spec.js +++ b/backend/src/test/restful/mihomo-profile-file.spec.js @@ -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] = [ {