mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-30 16:38:30 +00:00
添加一些内置分流
This commit is contained in:
1 parent
23c3deb495
commit
d9a1830a30
2 files changed
+171
-106
No files matched your search
+169
-104
@@ -110,7 +110,10 @@ function service() {
|
||||
async function downloadSubscription(req, res) {
|
||||
const {name} = req.params;
|
||||
const {cache} = req.query;
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers);
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers) || "JSON";
|
||||
|
||||
$.info(`正在下载订阅:${name}`);
|
||||
|
||||
const allSubs = $.read(SUBS_KEY);
|
||||
const sub = allSubs[name];
|
||||
if (sub) {
|
||||
@@ -263,7 +266,7 @@ function service() {
|
||||
async function downloadCollection(req, res) {
|
||||
const {name} = req.params;
|
||||
const {cache} = req.query || "false";
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers);
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers) || "JSON";
|
||||
|
||||
const allCollections = $.read(COLLECTIONS_KEY);
|
||||
const allSubs = $.read(SUBS_KEY);
|
||||
@@ -424,7 +427,7 @@ function service() {
|
||||
async function downloadRule(req, res) {
|
||||
const {name} = req.params;
|
||||
const {builtin} = req.query;
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers);
|
||||
const platform = req.query.target || getPlatformFromHeaders(req.headers) || "Surge";
|
||||
|
||||
$.info(`正在下载${builtin ? "内置" : ""}分流订阅:${name}...`);
|
||||
|
||||
@@ -446,9 +449,10 @@ function service() {
|
||||
}
|
||||
}
|
||||
// remove duplicates
|
||||
rules = await RuleUtils.process(rules, [{type: "Remove Duplicate"}]);
|
||||
rules = await RuleUtils.process(rules, [{type: "Remove Duplicate Filter"}]);
|
||||
// produce output
|
||||
const output = RuleUtils.produce(rules, platform);
|
||||
$.info(`分流订阅${name}下载成功,共包含${rules.length}条分流规则。`);
|
||||
res.send(output);
|
||||
} else {
|
||||
// rule not found
|
||||
@@ -575,8 +579,7 @@ function service() {
|
||||
} else if (UA.indexOf("Decar") !== -1 || UA.indexOf("Loon") !== -1) {
|
||||
return "Loon";
|
||||
} else {
|
||||
// browser
|
||||
return "JSON";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1616,7 +1619,7 @@ var ProxyUtils = (function () {
|
||||
}
|
||||
|
||||
// rename by regex
|
||||
// keywords: [{expr: "string format regex", now: "now"}]
|
||||
// keywords: [{expr: "string format regex", now: "now"}]
|
||||
function RegexRenameOperator(regex) {
|
||||
return {
|
||||
name: "Regex Rename Operator",
|
||||
@@ -1632,7 +1635,7 @@ var ProxyUtils = (function () {
|
||||
}
|
||||
|
||||
// delete keywords operator
|
||||
// keywords: ['a', 'b', 'c']
|
||||
// keywords: ['a', 'b', 'c']
|
||||
function KeywordDeleteOperator(keywords) {
|
||||
const keywords_ = keywords.map((k) => {
|
||||
return {
|
||||
@@ -1647,7 +1650,7 @@ var ProxyUtils = (function () {
|
||||
}
|
||||
|
||||
// delete regex operator
|
||||
// regex: ['a', 'b', 'c']
|
||||
// regex: ['a', 'b', 'c']
|
||||
function RegexDeleteOperator(regex) {
|
||||
const regex_ = regex.map((r) => {
|
||||
return {
|
||||
@@ -1664,9 +1667,9 @@ var ProxyUtils = (function () {
|
||||
// use base64 encoded script to rename
|
||||
/** Example script
|
||||
function operator(proxies) {
|
||||
// do something
|
||||
return proxies;
|
||||
}
|
||||
// do something
|
||||
return proxies;
|
||||
}
|
||||
|
||||
WARNING:
|
||||
1. This function name should be `operator`!
|
||||
@@ -1781,12 +1784,13 @@ var ProxyUtils = (function () {
|
||||
}
|
||||
|
||||
// use base64 encoded script to filter proxies
|
||||
/** Script Example
|
||||
/**
|
||||
Script Example
|
||||
function func(proxies) {
|
||||
const selected = FULL(proxies.length, true);
|
||||
// do something
|
||||
return selected;
|
||||
}
|
||||
const selected = FULL(proxies.length, true);
|
||||
// do something
|
||||
return selected;
|
||||
}
|
||||
WARNING:
|
||||
1. This function name should be `func`!
|
||||
2. Always declare variables before using them!
|
||||
@@ -2016,60 +2020,6 @@ var ProxyUtils = (function () {
|
||||
return array;
|
||||
}
|
||||
|
||||
// some logical functions for proxy filters
|
||||
function AND(...args) {
|
||||
return args.reduce((a, b) => a.map((c, i) => b[i] && c));
|
||||
}
|
||||
|
||||
function OR(...args) {
|
||||
return args.reduce((a, b) => a.map((c, i) => b[i] || c));
|
||||
}
|
||||
|
||||
function NOT(array) {
|
||||
return array.map((c) => !c);
|
||||
}
|
||||
|
||||
function FULL(length, bool) {
|
||||
return [...Array(length).keys()].map(() => bool);
|
||||
}
|
||||
|
||||
function clone(object) {
|
||||
return JSON.parse(JSON.stringify(object));
|
||||
}
|
||||
|
||||
// apply filter to proxies
|
||||
function ApplyFilter(filter, proxies) {
|
||||
// select proxies
|
||||
let selected = FULL(proxies.length, true);
|
||||
try {
|
||||
selected = AND(selected, filter.func(proxies));
|
||||
} catch (err) {
|
||||
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
|
||||
}
|
||||
return proxies.filter((_, i) => selected[i]);
|
||||
}
|
||||
|
||||
// apply operator to proxies
|
||||
function ApplyOperator(operator, proxies) {
|
||||
let output = clone(proxies);
|
||||
try {
|
||||
const output_ = operator.func(output);
|
||||
if (output_) output = output_;
|
||||
} catch (err) {
|
||||
// print log and skip this operator
|
||||
console.log(`Cannot apply operator ${op.name}! Reason: ${err}`);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
function Apply(process, proxies) {
|
||||
if (process.name.indexOf("Filter") !== -1) {
|
||||
return ApplyFilter(process, proxies);
|
||||
} else if (process.name.indexOf("Operator") !== -1) {
|
||||
return ApplyOperator(process, proxies);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
"Keyword Filter": KeywordFilter,
|
||||
"Useless Filter": UselessFilter,
|
||||
@@ -2087,8 +2037,6 @@ var ProxyUtils = (function () {
|
||||
"Regex Rename Operator": RegexRenameOperator,
|
||||
"Regex Delete Operator": RegexDeleteOperator,
|
||||
"Script Operator": ScriptOperator,
|
||||
|
||||
"Apply": Apply,
|
||||
};
|
||||
})();
|
||||
const PROXY_PRODUCERS = (function () {
|
||||
@@ -2528,26 +2476,23 @@ var ProxyUtils = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
const op = PROXY_PROCESSORS[item.type];
|
||||
if (!op) {
|
||||
if (!PROXY_PROCESSORS[item.type]) {
|
||||
$.error(`Unknown operator: "${item.type}"`);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$.log(
|
||||
`Applying "${item.type}" with arguments:\n >>> ${
|
||||
JSON.stringify(item.args, null, 2) || "None"
|
||||
}`
|
||||
);
|
||||
if (item.type.indexOf('Script') !== -1) {
|
||||
proxies = PROXY_PROCESSORS.Apply(op(script), proxies);
|
||||
} else {
|
||||
proxies = PROXY_PROCESSORS.Apply(op(item.args), proxies);
|
||||
}
|
||||
} catch (err) {
|
||||
$.error(`Failed to apply "${item.type}"!\n REASON: ${err}`);
|
||||
$.log(
|
||||
`Applying "${item.type}" with arguments:\n >>> ${
|
||||
JSON.stringify(item.args, null, 2) || "None"
|
||||
}`
|
||||
);
|
||||
let processor;
|
||||
if (item.type.indexOf('Script') !== -1) {
|
||||
processor = PROXY_PROCESSORS[item.type](script);
|
||||
} else {
|
||||
processor = PROXY_PROCESSORS[item.type](item.args);
|
||||
}
|
||||
proxies = ApplyProcessor(processor, proxies);
|
||||
}
|
||||
return proxies;
|
||||
}
|
||||
@@ -2664,10 +2609,17 @@ var RuleUtils = (function () {
|
||||
const conf = YAML.eval(raw);
|
||||
const payload = conf["payload"]
|
||||
.map(
|
||||
rule => rule.replace("DST-PORT", "DEST-PORT")
|
||||
.replace("SRC-IP-CIDR", "SRC-IP")
|
||||
.replace("SRC-PORT", "IN-PORT")
|
||||
rule => {
|
||||
if (typeof rule === "string") {
|
||||
return rule.replace(/DST-PORT/i, "DEST-PORT")
|
||||
.replace(/SRC-IP-CIDR/i, "SRC-IP")
|
||||
.replace(/SRC-PORT/i, "IN-PORT")
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
)
|
||||
.filter(line => line.length > 0)
|
||||
.join("\n");
|
||||
return SurgeRuleSet().parse(payload);
|
||||
} catch (e) {
|
||||
@@ -2707,8 +2659,33 @@ var RuleUtils = (function () {
|
||||
return [SurgeRuleSet(), QX(), ClashRuleProvider()];
|
||||
})();
|
||||
const RULE_PROCESSORS = (function () {
|
||||
function RemoveDuplicate() {
|
||||
function RegexFilter({regex = [], keep = true}) {
|
||||
return {
|
||||
name: "Regex Filter",
|
||||
func: (rules) => {
|
||||
return rules.map((rule) => {
|
||||
const selected = regex.some((r) => {
|
||||
r = new RegExp(r);
|
||||
return r.test(rule);
|
||||
});
|
||||
return keep ? selected : !selected;
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function TypeFilter(types) {
|
||||
return {
|
||||
name: "Type Filter",
|
||||
func: (rules) => {
|
||||
return rules.map((rule) => types.some((t) => rule.type === t));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function RemoveDuplicateFilter() {
|
||||
return {
|
||||
name: "Remove Duplicate Filter",
|
||||
func: rules => {
|
||||
const seen = new Set();
|
||||
const result = [];
|
||||
@@ -2726,8 +2703,27 @@ var RuleUtils = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
// regex: [{expr: "string format regex", now: "now"}]
|
||||
function RegexReplaceOperator(regex) {
|
||||
return {
|
||||
name: "Regex Rename Operator",
|
||||
func: (rules) => {
|
||||
return rules.map((rule) => {
|
||||
for (const {expr, now} of regex) {
|
||||
rule.content = rule.content.replace(new RegExp(expr, "g"), now).trim();
|
||||
}
|
||||
return rule;
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
"Remove Duplicate": RemoveDuplicate
|
||||
"Regex Filter": RegexFilter,
|
||||
"Remove Duplicate Filter": RemoveDuplicateFilter,
|
||||
"Type Filter": TypeFilter,
|
||||
|
||||
"Regex Replace Operator": RegexReplaceOperator
|
||||
};
|
||||
})();
|
||||
const RULE_PRODUCERS = (function () {
|
||||
@@ -2833,17 +2829,13 @@ var RuleUtils = (function () {
|
||||
console.error(`Unknown operator: ${item.type}!`);
|
||||
continue;
|
||||
}
|
||||
const op = RULE_PROCESSORS[item.type](item.args);
|
||||
try {
|
||||
console.log(
|
||||
`Applying operator "${item.type}" with arguments: \n >>> ${
|
||||
JSON.stringify(item.args) || "None"
|
||||
}`
|
||||
);
|
||||
rules = op.func(rules);
|
||||
} catch (err) {
|
||||
console.error(`Failed to apply operator "${item.type}"!\n REASON: ${err}`);
|
||||
}
|
||||
const processor = RULE_PROCESSORS[item.type](item.args);
|
||||
console.log(
|
||||
`Applying "${item.type}" with arguments: \n >>> ${
|
||||
JSON.stringify(item.args) || "None"
|
||||
}`
|
||||
);
|
||||
rules = ApplyProcessor(processor, rules);
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
@@ -2886,11 +2878,84 @@ function getBuiltInRules() {
|
||||
"https://raw.githubusercontent.com/privacy-protection-tools/anti-AD/master/anti-ad-surge.txt",
|
||||
"https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Providers/BanAD.yaml"
|
||||
]
|
||||
},
|
||||
"Global": {
|
||||
"name": "Global",
|
||||
"description": "",
|
||||
"urls": [
|
||||
"https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Providers/ProxyGFWlist.yaml",
|
||||
"https://raw.githubusercontent.com/DivineEngine/Profiles/master/Quantumult/Filter/Global.list"
|
||||
]
|
||||
},
|
||||
"CN": {
|
||||
"name": "CN",
|
||||
"description": "",
|
||||
"urls": [
|
||||
"https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Providers/ChinaDomain.yaml",
|
||||
"https://raw.githubusercontent.com/DivineEngine/Profiles/master/Quantumult/Filter/China.list"
|
||||
]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/****************************************** Supporting Functions **********************************************************/
|
||||
function ApplyProcessor(process, objs) {
|
||||
function ApplyFilter(filter, objs) {
|
||||
// select proxies
|
||||
let selected = FULL(objs.length, true);
|
||||
try {
|
||||
selected = AND(selected, filter.func(objs));
|
||||
} catch (err) {
|
||||
// print log and skip this filter
|
||||
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
|
||||
}
|
||||
return objs.filter((_, i) => selected[i]);
|
||||
}
|
||||
|
||||
function ApplyOperator(operator, objs) {
|
||||
let output = clone(objs);
|
||||
try {
|
||||
const output_ = operator.func(output);
|
||||
if (output_) output = output_;
|
||||
} catch (err) {
|
||||
// print log and skip this operator
|
||||
console.log(`Cannot apply operator ${operator.name}! Reason: ${err}`);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
if (process.name.indexOf("Filter") !== -1) {
|
||||
return ApplyFilter(process, objs);
|
||||
} else if (process.name.indexOf("Operator") !== -1) {
|
||||
return ApplyOperator(process, objs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// some logical functions
|
||||
function AND(...args) {
|
||||
return args.reduce((a, b) => a.map((c, i) => b[i] && c));
|
||||
}
|
||||
|
||||
function OR(...args) {
|
||||
return args.reduce((a, b) => a.map((c, i) => b[i] || c));
|
||||
}
|
||||
|
||||
function NOT(array) {
|
||||
return array.map((c) => !c);
|
||||
}
|
||||
|
||||
function FULL(length, bool) {
|
||||
return [...Array(length).keys()].map(() => bool);
|
||||
}
|
||||
|
||||
function clone(object) {
|
||||
return JSON.parse(JSON.stringify(object));
|
||||
}
|
||||
|
||||
/****************************************** Own Libraries **********************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* OpenAPI
|
||||
* https://github.com/Peng-YM/QuanX/blob/master/Tools/OpenAPI/README.md
|
||||
|
||||
Vendored
+2
-2
@@ -1,2 +1,2 @@
|
||||
// UPDATED AT: 2020年11月26日 星期四 15时24分10秒 CST
|
||||
const $=API("sub-store"),Base64=new Base64Code;function service(){console.log("========== Sub-Store ===========");const e=express(),t="settings",s="subs",r="collections",n="builtin";function o(e){const t=Object.keys(e);let s="";for(let r of t)if(/USER-AGENT/i.test(r)){s=e[r];break}return-1!==s.indexOf("Quantumult%20X")?"QX":-1!==s.indexOf("Surge")?"Surge":-1!==s.indexOf("Decar")||-1!==s.indexOf("Loon")?"Loon":"JSON"}async function a(e,t=!0){const s=HTTP({headers:{"User-Agent":"Quantumult%20X"}}),r="#"+Base64.safeEncode(e),n=$.read(r),o=`#TIME-${Base64.safeEncode(e)}`,a=(new Date).getTime()-$.read(o)>864e5;if(t&&n&&!a)return $.log(`Use cached for url: ${e}`),n;let i="";try{i=(await s.get(e)).body}catch(e){throw new Error(e)}finally{$.write(i,r),$.write((new Date).getTime(),o)}if(0===i.replace(/\s/g,"").length)throw new Error("订阅内容为空!");return i}$.read(s)||$.write({},s),$.read(r)||$.write({},r),$.read(t)||$.write({},t),$.read("rules")||$.write({},"rules"),$.write({rules:getBuiltInRules()},n),e.get("/download/collection/:name",async function(e,t){const{name:n}=e.params,{cache:i}=e.query||"false",p=e.query.target||o(e.headers),l=$.read(r),c=$.read(s),u=l[n];if($.info(`正在下载组合订阅:${n}`),u){const e=u.subscriptions;let s=[];for(let t=0;t<e.length;t++){const r=c[e[t]];$.info(`正在处理子订阅:${r.name},进度--${100*((t+1)/e.length).toFixed(1)}% `);try{const e=void 0===i?"JSON"===p||"URI"===p:i,t=await a(r.url,e);let n=ProxyUtils.parse(t);n=await ProxyUtils.process(n,r.process||[]),s=s.concat(n)}catch(e){$.error(`处理组合订阅中的子订阅: ${r.name}时出现错误:${e}! 该订阅已被跳过。`)}}0===(s=await ProxyUtils.process(s,u.process||[])).length&&($.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 组合订阅:${n}中不含有效节点!`),t.status(500).json({status:"failed",message:`❌ 组合订阅${n}中不含有${p}可用的节点!`}));try{const e=ProxyUtils.produce(s,p);"JSON"===p?t.set("Content-Type","application/json").send(e):t.send(e)}catch(e){$.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 无法下载组合订阅:${n}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}}else $.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 未找到组合订阅:${n}!`),t.status(404).json({status:"failed"})}),e.get("/download/:name",async function(e,t){const{name:r}=e.params,{cache:n}=e.query,i=e.query.target||o(e.headers),p=$.read(s)[r];if(p)try{const e=void 0===n?"JSON"===i||"URI"===i:n,s=await a(p.url,e);let o=ProxyUtils.parse(s);o=await ProxyUtils.process(o,p.process||[]);const l=ProxyUtils.produce(o,i);"JSON"===i?t.set("Content-Type","application/json").send(l):t.send(l)}catch(e){$.notify("🌍 [Sub-Store] 下载订阅失败",`❌ 无法下载订阅:${r}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}else $.notify("🌍 [Sub-Store] 下载订阅失败",`❌ 未找到订阅:${r}!`),t.status(404).json({status:"failed"})}),e.route("/api/sub/:name").get(function(e,t){const{name:r}=e.params,n=$.read(s)[r];n?t.json({status:"success",data:n}):t.status(404).json({status:"failed",message:`未找到订阅:${r}!`})}).patch(function(e,t){const{name:n}=e.params;let o=e.body;const a=$.read(s);if(a[n]){const e={...a[n],...o};if($.info(`正在更新订阅: ${n}`),n!==o.name){const t=$.read(r);for(const e of Object.keys(t)){const s=t[e].subscriptions.indexOf(n);-1!==s&&(t[e].subscriptions[s]=o.name)}delete a[n],a[o.name]=e}else a[n]=e;$.write(a,s),t.json({status:"success",data:e})}else t.status(500).json({status:"failed",message:`订阅${n}不存在,无法更新!`})}).delete(function(e,t){const{name:n}=e.params;$.info(`删除订阅:${n}...`);let o=$.read(s);delete o[n],$.write(o,s);let a=$.read(r);for(const e of Object.keys(a))a[e].subscriptions=a[e].subscriptions.filter(e=>e!==n);$.write(a,r),t.json({status:"success"})}),e.route("/api/subs").get(function(e,t){const r=$.read(s);t.json({status:"success",data:r})}).post(function(e,t){const r=e.body,n=$.read(s);$.info(`正在创建订阅: ${r.name}`),n[r.name]&&t.status(500).json({status:"failed",message:`订阅${r.name}已存在!`});/^[\w-_]*$/.test(r.name)?(n[r.name]=r,$.write(n,s),t.status(201).json({status:"success",data:r})):t.status(500).json({status:"failed",message:`订阅名称 ${r.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`})}),e.route("/api/collection/:name").get(function(e,t){const{name:s}=e.params,n=$.read(r)[s];n?t.json({status:"success",data:n}):t.status(404).json({status:"failed",message:`未找到订阅集:${s}!`})}).patch(function(e,t){const{name:s}=e.params;let n=e.body;const o=$.read(r);if(o[s]){const e={...o[s],...n};$.info(`正在更新组合订阅:${s}...`),delete o[s],o[n.name||s]=e,$.write(o,r),t.json({status:"success",data:e})}else t.status(500).json({status:"failed",message:`订阅集${s}不存在,无法更新!`})}).delete(function(e,t){coLine truncated
|
||||
// UPDATED AT: 2020年11月26日 星期四 19时50分41秒 CST
|
||||
const $=API("sub-store"),Base64=new Base64Code;function service(){console.log("========== Sub-Store ===========");const e=express(),t="settings",s="subs",r="collections",n="builtin";function o(e){const t=Object.keys(e);let s="";for(let r of t)if(/USER-AGENT/i.test(r)){s=e[r];break}return-1!==s.indexOf("Quantumult%20X")?"QX":-1!==s.indexOf("Surge")?"Surge":-1!==s.indexOf("Decar")||-1!==s.indexOf("Loon")?"Loon":null}async function a(e,t=!0){const s=HTTP({headers:{"User-Agent":"Quantumult%20X"}}),r="#"+Base64.safeEncode(e),n=$.read(r),o=`#TIME-${Base64.safeEncode(e)}`,a=(new Date).getTime()-$.read(o)>864e5;if(t&&n&&!a)return $.log(`Use cached for url: ${e}`),n;let i="";try{i=(await s.get(e)).body}catch(e){throw new Error(e)}finally{$.write(i,r),$.write((new Date).getTime(),o)}if(0===i.replace(/\s/g,"").length)throw new Error("订阅内容为空!");return i}$.read(s)||$.write({},s),$.read(r)||$.write({},r),$.read(t)||$.write({},t),$.read("rules")||$.write({},"rules"),$.write({rules:getBuiltInRules()},n),e.get("/download/collection/:name",async function(e,t){const{name:n}=e.params,{cache:i}=e.query||"false",p=e.query.target||o(e.headers)||"JSON",l=$.read(r),c=$.read(s),u=l[n];if($.info(`正在下载组合订阅:${n}`),u){const e=u.subscriptions;let s=[];for(let t=0;t<e.length;t++){const r=c[e[t]];$.info(`正在处理子订阅:${r.name},进度--${100*((t+1)/e.length).toFixed(1)}% `);try{const e=void 0===i?"JSON"===p||"URI"===p:i,t=await a(r.url,e);let n=ProxyUtils.parse(t);n=await ProxyUtils.process(n,r.process||[]),s=s.concat(n)}catch(e){$.error(`处理组合订阅中的子订阅: ${r.name}时出现错误:${e}! 该订阅已被跳过。`)}}0===(s=await ProxyUtils.process(s,u.process||[])).length&&($.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 组合订阅:${n}中不含有效节点!`),t.status(500).json({status:"failed",message:`❌ 组合订阅${n}中不含有${p}可用的节点!`}));try{const e=ProxyUtils.produce(s,p);"JSON"===p?t.set("Content-Type","application/json").send(e):t.send(e)}catch(e){$.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 无法下载组合订阅:${n}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}}else $.notify("🌍 [Sub-Store] 下载组合订阅失败",`❌ 未找到组合订阅:${n}!`),t.status(404).json({status:"failed"})}),e.get("/download/:name",async function(e,t){const{name:r}=e.params,{cache:n}=e.query,i=e.query.target||o(e.headers)||"JSON";$.info(`正在下载订阅:${r}`);const p=$.read(s)[r];if(p)try{const e=void 0===n?"JSON"===i||"URI"===i:n,s=await a(p.url,e);let o=ProxyUtils.parse(s);o=await ProxyUtils.process(o,p.process||[]);const l=ProxyUtils.produce(o,i);"JSON"===i?t.set("Content-Type","application/json").send(l):t.send(l)}catch(e){$.notify("🌍 [Sub-Store] 下载订阅失败",`❌ 无法下载订阅:${r}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}else $.notify("🌍 [Sub-Store] 下载订阅失败",`❌ 未找到订阅:${r}!`),t.status(404).json({status:"failed"})}),e.route("/api/sub/:name").get(function(e,t){const{name:r}=e.params,n=$.read(s)[r];n?t.json({status:"success",data:n}):t.status(404).json({status:"failed",message:`未找到订阅:${r}!`})}).patch(function(e,t){const{name:n}=e.params;let o=e.body;const a=$.read(s);if(a[n]){const e={...a[n],...o};if($.info(`正在更新订阅: ${n}`),n!==o.name){const t=$.read(r);for(const e of Object.keys(t)){const s=t[e].subscriptions.indexOf(n);-1!==s&&(t[e].subscriptions[s]=o.name)}delete a[n],a[o.name]=e}else a[n]=e;$.write(a,s),t.json({status:"success",data:e})}else t.status(500).json({status:"failed",message:`订阅${n}不存在,无法更新!`})}).delete(function(e,t){const{name:n}=e.params;$.info(`删除订阅:${n}...`);let o=$.read(s);delete o[n],$.write(o,s);let a=$.read(r);for(const e of Object.keys(a))a[e].subscriptions=a[e].subscriptions.filter(e=>e!==n);$.write(a,r),t.json({status:"success"})}),e.route("/api/subs").get(function(e,t){const r=$.read(s);t.json({status:"success",data:r})}).post(function(e,t){const r=e.body,n=$.read(s);$.info(`正在创建订阅: ${r.name}`),n[r.name]&&t.status(500).json({status:"failed",message:`订阅${r.name}已存在!`});/^[\w-_]*$/.test(r.name)?(n[r.name]=r,$.write(n,s),t.status(201).json({status:"success",data:r})):t.status(500).json({status:"failed",message:`订阅名称 ${r.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`})}),e.route("/api/collection/:name").get(function(e,t){const{name:s}=e.params,n=$.read(r)[s];n?t.json({status:"success",data:n}):t.status(404).json({status:"failed",message:`未找到订阅集:${s}!`})}).patch(function(e,t){const{name:s}=e.params;let n=e.body;const o=$.read(r);if(o[s]){const e={...o[s],...n};$.info(`正在更新组合订阅:${s}...`),delete o[s],o[n.name||s]=e,$.write(o,r),t.json({status:"success",data:e})}else t.status(500).json({status:"failed",message:`订阅集${s}Line truncated
|
||||
Reference in new issue
Block a user