diff --git a/backend/package.json b/backend/package.json index cbd20d25..ccea39f2 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.29.0", + "version": "2.30.0", "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 97c66262..cbc117cd 100644 --- a/backend/src/core/proxy-utils/processors/index.js +++ b/backend/src/core/proxy-utils/processors/index.js @@ -475,6 +475,66 @@ function ScriptOperator( }; } +const ADD_PROXIES_FROM_SUBSCRIPTION_OPERATOR = + 'Add Proxies From Subscription Operator'; + +function normalizeMihomoConfig(content) { + if (!content) return {}; + + const config = YAML.safeLoad(content); + return isPlainObject(config) ? config : {}; +} + +function getMihomoProfileProxies(config) { + return Array.isArray(config?.proxies) ? config.proxies : []; +} + +function AddProxiesFromSubscriptionOperator({ + sourceType = 'subscription', + sourceName, + includeUnsupportedProxy, + position = 'replace', +} = {}) { + const apply = async (input) => { + if (input?.$file?.type !== 'mihomoProfile') return input; + + const config = normalizeMihomoConfig(input.$content); + const currentProxies = getMihomoProfileProxies(config); + const proxies = await produceArtifact({ + type: sourceType, + name: sourceName, + platform: 'mihomo', + produceType: 'internal', + produceOpts: { + 'delete-underscore-fields': true, + 'include-unsupported-proxy': includeUnsupportedProxy, + }, + }); + + switch (position) { + case 'front': + config.proxies = [...proxies, ...currentProxies]; + break; + case 'back': + config.proxies = [...currentProxies, ...proxies]; + break; + case 'replace': + default: + config.proxies = proxies; + break; + } + + input.$content = ProxyUtils.yaml.safeDump(config); + return input; + }; + + return { + name: ADD_PROXIES_FROM_SUBSCRIPTION_OPERATOR, + func: apply, + nodeFunc: apply, + }; +} + function parseIP4P(IP4P) { let server; let port; @@ -1136,6 +1196,8 @@ export default { 'Regex Rename Operator': RegexRenameOperator, 'Regex Delete Operator': RegexDeleteOperator, 'Script Operator': ScriptOperator, + [ADD_PROXIES_FROM_SUBSCRIPTION_OPERATOR]: + AddProxiesFromSubscriptionOperator, [RESPONSE_TRANSFORMER]: ResponseTransformer, 'Handle Duplicate Operator': HandleDuplicateOperator, 'Resolve Domain Operator': ResolveDomainOperator, diff --git a/backend/src/test/restful/mihomo-profile-file.spec.js b/backend/src/test/restful/mihomo-profile-file.spec.js index 53240ab1..a8297347 100644 --- a/backend/src/test/restful/mihomo-profile-file.spec.js +++ b/backend/src/test/restful/mihomo-profile-file.spec.js @@ -299,4 +299,89 @@ describe('mihomo profile file routes', function () { expect(res.sent).to.include('mixed-port: 7892'); expect(res.sent).to.include('name: Unsupported'); }); + + it('adds subscription proxies to mihomoProfile files without moving an existing proxies key', async function () { + state[FILES_KEY] = [ + { + name: 'add-proxies-file', + type: 'mihomoProfile', + sourceType: 'none', + process: [ + { + type: 'Script Operator', + args: { + mode: 'script', + content: + 'mixed-port: 7890\n' + + 'proxies:\n' + + ' note: not-array\n' + + 'proxy-groups:\n' + + ' - name: Auto\n' + + ' type: select\n' + + ' proxies: [Supported]', + }, + }, + { + type: 'Add Proxies From Subscription Operator', + args: { + sourceType: 'subscription', + sourceName: 'demo-sub', + includeUnsupportedProxy: false, + position: 'back', + }, + }, + ], + }, + ]; + + const res = await requestFile('add-proxies-file'); + + expect(res.statusCode).to.equal(200); + expect(res.sent.indexOf('mixed-port:')).to.be.lessThan( + res.sent.indexOf('proxies:'), + ); + expect(res.sent.indexOf('proxies:')).to.be.lessThan( + res.sent.indexOf('proxy-groups:'), + ); + expect(res.sent).to.include('name: Supported'); + expect(res.sent).to.not.include('name: Unsupported'); + }); + + it('can insert subscription proxies before existing mihomoProfile proxies', async function () { + state[FILES_KEY] = [ + { + name: 'insert-proxies-file', + type: 'mihomoProfile', + sourceType: 'none', + process: [ + { + type: 'Script Operator', + args: { + mode: 'script', + content: + 'proxies:\n' + + ' - name: Existing\n' + + ' type: direct', + }, + }, + { + type: 'Add Proxies From Subscription Operator', + args: { + sourceType: 'subscription', + sourceName: 'demo-sub', + includeUnsupportedProxy: false, + position: 'front', + }, + }, + ], + }, + ]; + + const res = await requestFile('insert-proxies-file'); + + expect(res.statusCode).to.equal(200); + expect(res.sent.indexOf('name: Supported')).to.be.lessThan( + res.sent.indexOf('name: Existing'), + ); + }); });