mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-30 08:28:10 +00:00
feat: 文件 mihomo 配置新增操作: 从订阅添加节点
This commit is contained in:
3 files changed
+148
-1
No files matched your search
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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'),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user