Skip to content

Commit

Permalink
feat: 文件支持 Mihomo 配置, 支持使用覆写; target 名称适配大小写和别名
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jan 3, 2025
1 parent 95b7557 commit 85a3e2e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.16.2",
"version": "2.16.3",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
92 changes: 89 additions & 3 deletions backend/src/core/proxy-utils/processors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { hex_md5 } from '@/vendor/md5';
import { ProxyUtils } from '@/core/proxy-utils';
import { produceArtifact } from '@/restful/sync';
import { SETTINGS_KEY } from '@/constants';
import YAML from '@/utils/yaml';

import env from '@/utils/env';
import {
Expand All @@ -21,6 +22,46 @@ import {
getRmainingDays,
} from '@/utils/flow';

function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
function trimWrap(str) {
if (str.startsWith('<') && str.endsWith('>')) {
return str.slice(1, -1);
}
return str;
}
function deepMerge(target, _other) {
const other = typeof _other === 'string' ? JSON.parse(_other) : _other;
for (const key in other) {
if (isObject(other[key])) {
if (key.endsWith('!')) {
const k = trimWrap(key.slice(0, -1));
target[k] = other[key];
} else {
const k = trimWrap(key);
if (!target[k]) Object.assign(target, { [k]: {} });
deepMerge(target[k], other[k]);
}
} else if (Array.isArray(other[key])) {
if (key.startsWith('+')) {
const k = trimWrap(key.slice(1));
if (!target[k]) Object.assign(target, { [k]: [] });
target[k] = [...other[key], ...target[k]];
} else if (key.endsWith('+')) {
const k = trimWrap(key.slice(0, -1));
if (!target[k]) Object.assign(target, { [k]: [] });
target[k] = [...target[k], ...other[key]];
} else {
const k = trimWrap(key);
Object.assign(target, { [k]: other[key] });
}
} else {
Object.assign(target, { [key]: other[key] });
}
}
return target;
}
/**
The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows:
{
Expand Down Expand Up @@ -321,6 +362,33 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
name: 'Script Operator',
func: async (proxies) => {
let output = proxies;
if (output?.$file?.type === 'mihomoProfile') {
try {
let patch = YAML.safeLoad(script);
if (typeof patch !== 'object') patch = {};
output.$content = ProxyUtils.yaml.safeDump(
deepMerge(
{
proxies: await produceArtifact({
type:
output?.$file?.sourceType ||
'collection',
name: output?.$file?.sourceName,
platform: 'mihomo',
produceType: 'internal',
produceOpts: {
'delete-underscore-fields': true,
},
}),
},
patch,
),
);
return output;
} catch (e) {
// console.log(e);
}
}
await (async function () {
const operator = createDynamicFunction(
'operator',
Expand All @@ -339,9 +407,27 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
'operator',
`async function operator(input = []) {
if (input && (input.$files || input.$content)) {
let { $content, $files, $options } = input
${script}
return { $content, $files, $options }
let { $content, $files, $options, $file } = input
if($file.type === 'mihomoProfile') {
${script}
if(typeof main === 'function') {
const config = {
proxies: await produceArtifact({
type: $file.sourceType || 'collection',
name: $file.sourceName,
platform: 'mihomo',
produceType: 'internal',
produceOpts: {
'delete-underscore-fields': true
}
}),
}
$content = ProxyUtils.yaml.safeDump(await main(config))
}
} else {
${script}
}
return { $content, $files, $options, $file }
} else {
let proxies = input
let list = []
Expand Down
2 changes: 1 addition & 1 deletion backend/src/core/proxy-utils/producers/clashmeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default function ClashMeta_Producer() {
delete proxy.id;
delete proxy.resolved;
delete proxy['no-resolve'];
if (type !== 'internal') {
if (type !== 'internal' || opts['delete-underscore-fields']) {
for (const key in proxy) {
if (proxy[key] == null || /^_/i.test(key)) {
delete proxy[key];
Expand Down
17 changes: 17 additions & 0 deletions backend/src/core/proxy-utils/producers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,37 @@ function JSON_Producer() {
}

export default {
qx: QX_Producer(),
QX: QX_Producer(),
QuantumultX: QX_Producer(),
surge: Surge_Producer(),
Surge: Surge_Producer(),
SurgeMac: SurgeMac_Producer(),
Loon: Loon_Producer(),
Clash: Clash_Producer(),
meta: ClashMeta_Producer(),
clashmeta: ClashMeta_Producer(),
'clash.meta': ClashMeta_Producer(),
'Clash.Meta': ClashMeta_Producer(),
ClashMeta: ClashMeta_Producer(),
mihomo: ClashMeta_Producer(),
Mihomo: ClashMeta_Producer(),
uri: URI_Producer(),
URI: URI_Producer(),
v2: V2Ray_Producer(),
v2ray: V2Ray_Producer(),
V2Ray: V2Ray_Producer(),
json: JSON_Producer(),
JSON: JSON_Producer(),
stash: Stash_Producer(),
Stash: Stash_Producer(),
shadowrocket: Shadowrocket_Producer(),
Shadowrocket: Shadowrocket_Producer(),
ShadowRocket: Shadowrocket_Producer(),
surfboard: Surfboard_Producer(),
Surfboard: Surfboard_Producer(),
singbox: singbox_Producer(),
'sing-box': singbox_Producer(),
egern: Egern_Producer(),
Egern: Egern_Producer(),
};
2 changes: 1 addition & 1 deletion backend/src/restful/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function previewFile(req, res) {
const processed =
Array.isArray(file.process) && file.process.length > 0
? await ProxyUtils.process(
{ $files: files, $content: filesContent },
{ $files: files, $content: filesContent, $file: file },
file.process,
)
: { $content: filesContent, $files: files };
Expand Down
7 changes: 6 additions & 1 deletion backend/src/restful/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@ async function produceArtifact({
const processed =
Array.isArray(file.process) && file.process.length > 0
? await ProxyUtils.process(
{ $files: files, $content: filesContent, $options },
{
$files: files,
$content: filesContent,
$options,
$file: file,
},
file.process,
)
: { $content: filesContent, $files: files, $options };
Expand Down

0 comments on commit 85a3e2e

Please sign in to comment.