mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-30 08:28:10 +00:00
提供更方便的脚本操作接口
This commit is contained in:
1 parent
a0d8e79d3c
commit
910f2c1b67
2 files changed
+38
-75
No files matched your search
+37
-74
@@ -202,11 +202,6 @@ async function parseSub(sub, platform) {
|
||||
$.info(`Parsers initialized.`);
|
||||
let proxies = $parser.parse(raw);
|
||||
|
||||
// filters
|
||||
const $filter = ProxyFilter();
|
||||
// operators
|
||||
const $operator = ProxyOperator();
|
||||
|
||||
for (const item of sub.process || []) {
|
||||
let script;
|
||||
// process script
|
||||
@@ -234,13 +229,14 @@ async function parseSub(sub, platform) {
|
||||
JSON.stringify(item.args) || "None"
|
||||
}`
|
||||
);
|
||||
if (item.type.indexOf("Script") !== -1) {
|
||||
$filter.setFilter(filter(script));
|
||||
} else {
|
||||
$filter.setFilter(filter(item.args));
|
||||
}
|
||||
|
||||
try {
|
||||
proxies = $filter.process(proxies);
|
||||
if (item.type.indexOf("Script") !== -1) {
|
||||
proxies = processFilter(filter(script), proxies);
|
||||
} else {
|
||||
proxies = processFilter(filter(item.args), proxies);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
$.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`);
|
||||
}
|
||||
@@ -253,13 +249,12 @@ async function parseSub(sub, platform) {
|
||||
JSON.stringify(item.args) || "None"
|
||||
}`
|
||||
);
|
||||
if (item.type.indexOf("Script") !== -1) {
|
||||
$operator.setOperator(operator(script));
|
||||
} else {
|
||||
$operator.setOperator(operator(item.args));
|
||||
}
|
||||
try {
|
||||
proxies = $operator.process(proxies);
|
||||
if (item.type.indexOf("Script") !== -1) {
|
||||
proxies = processOperator(operator(script), proxies);
|
||||
} else {
|
||||
proxies = processOperator(operator(item.args), proxies);
|
||||
}
|
||||
} catch (err) {
|
||||
`Failed to apply operator "${item.type}"!\n REASON: ${err}`;
|
||||
}
|
||||
@@ -729,53 +724,28 @@ function ProxyParser(targetPlatform) {
|
||||
};
|
||||
}
|
||||
|
||||
function ProxyFilter() {
|
||||
let filter;
|
||||
|
||||
function setFilter(arg) {
|
||||
filter = arg;
|
||||
}
|
||||
|
||||
function processFilter(filter, proxies) {
|
||||
// select proxies
|
||||
function process(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]);
|
||||
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 {
|
||||
process,
|
||||
setFilter,
|
||||
};
|
||||
return proxies.filter((_, i) => selected[i]);
|
||||
}
|
||||
|
||||
function ProxyOperator() {
|
||||
let operator;
|
||||
|
||||
function setOperator(arg) {
|
||||
operator = arg;
|
||||
function processOperator(operator, proxies) {
|
||||
let output = objClone(proxies);
|
||||
try {
|
||||
const output_ = operator.func(output);
|
||||
if (output_) output = output_;
|
||||
} catch (err) {
|
||||
// print log and skip this operator
|
||||
console.log(`ERROR: cannot apply operator ${op.name}! Reason: ${err}`);
|
||||
}
|
||||
|
||||
// run all operators
|
||||
function process(proxies) {
|
||||
let output = objClone(proxies);
|
||||
try {
|
||||
const output_ = operator.func(output);
|
||||
if (output_) output = output_;
|
||||
} catch (err) {
|
||||
// print log and skip this operator
|
||||
console.log(`ERROR: cannot apply operator ${op.name}! Reason: ${err}`);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
return { setOperator, process };
|
||||
return output;
|
||||
}
|
||||
|
||||
/**************************** URI Format ***************************************/
|
||||
@@ -1799,7 +1769,6 @@ function URI_Producer() {
|
||||
);
|
||||
break;
|
||||
default:
|
||||
console.log(`FUCK`);
|
||||
throw new Error(`Unsupported plugin option: ${proxy.plugin}`);
|
||||
}
|
||||
}
|
||||
@@ -2018,13 +1987,16 @@ function ScriptOperator(script) {
|
||||
(function () {
|
||||
// interface to get internal operators
|
||||
const $get = (name, args) => {
|
||||
const operator = AVAILABLE_OPERATORS[name];
|
||||
if (operator) {
|
||||
return operator(args).func;
|
||||
} else {
|
||||
throw new Error(`No operator named ${name} is found!`);
|
||||
}
|
||||
const item = AVAILABLE_OPERATORS[name] || AVAILABLE_FILTERS[name];
|
||||
return item(args);
|
||||
};
|
||||
const $process = (item, proxies) => {
|
||||
if (item.name.indexOf("Filter") !== -1) {
|
||||
return processOperator(item, proxies);
|
||||
} else if (item.name.indexOf("Operator") !== -1) {
|
||||
return processFilter(item, proxies);
|
||||
}
|
||||
}
|
||||
eval(script);
|
||||
output = operator(proxies);
|
||||
})();
|
||||
@@ -2132,15 +2104,6 @@ function ScriptFilter(script) {
|
||||
func: (proxies) => {
|
||||
let output = FULL(proxies.length, true);
|
||||
!(function () {
|
||||
// interface to get internal filters
|
||||
const $get = (name, args) => {
|
||||
const filter = AVAILABLE_FILTERS[name];
|
||||
if (filter) {
|
||||
return filter(args).func;
|
||||
} else {
|
||||
throw new Error(`No filter named ${name} is found!`);
|
||||
}
|
||||
};
|
||||
eval(script);
|
||||
output = filter(proxies);
|
||||
})();
|
||||
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
const $=API("sub-store"),$app=express();$.http=HTTP({headers:{"User-Agent":"Quantumult%20X"}});const SUBS_KEY="subs",COLLECTIONS_KEY="collections",AVAILABLE_FILTERS={"Keyword Filter":KeywordFilter,"Useless Filter":UselessFilter,"Region Filter":RegionFilter,"Regex Filter":RegexFilter,"Type Filter":TypeFilter,"Script Filter":ScriptFilter},AVAILABLE_OPERATORS={"Set Property Operator":SetPropertyOperator,"Flag Operator":FlagOperator,"Sort Operator":SortOperator,"Keyword Sort Operator":KeywordSortOperator,"Keyword Rename Operator":KeywordRenameOperator,"Keyword Delete Operator":KeywordDeleteOperator,"Regex Rename Operator":RegexRenameOperator,"Regex Delete Operator":RegexDeleteOperator,"Script Operator":ScriptOperator};async function IP_API(e,t){const s=decodeURIComponent(e.params.server),r=await $.http.get(`http://ip-api.com/json/${s}?lang=zh-CN`).then(e=>JSON.parse(e.body));t.json(r)}async function downloadResource(e){let t=await $.http.get(e).then(e=>e.body).catch(t=>{res.status(500).json({status:"failed",message:`Cannot refresh remote resource: ${e}\n Reason: ${t}`})});const s=t.indexOf("proxies:");if(-1!==s){const e=t.lastIndexOf("}")+1;t=t.substring(s,e)}return t}async function refreshResource(e,t){const s=new Base64Code,{url:r}=e.body,n=await downloadResource(r);$.write(n,`#${s.safeEncode(r)}`),t.json({status:"success"})}async function downloadSub(e,t){const{name:s}=e.params,r=e.query.target||getPlatformFromHeaders(e.headers),n=$.read(SUBS_KEY);if(n[s]){const e=n[s];try{const s=await parseSub(e,r);t.send(s)}catch(e){t.status(500).json({status:"failed",message:e})}}else t.status(404).json({status:"failed",message:`订阅${s}不存在!`})}async function parseSub(e,t){let s;const r=(new Base64Code).safeEncode(e.url);if("Raw"===t||"URI"===t){const t=$.read(`#${r}`);t?(await $.wait(10),s=t):(s=await downloadResource(e.url),$.write(s,`#${r}`))}else s=await downloadResource(e.url),$.write(s,`#${e.url}`);$.info("======================================================================="),$.info(`Processing subscription: ${e.name}, target platform ==> ${t}.`),$.info("Initializing parsers...");const n=ProxyParser(t);n.addParsers([Clash_All,URI_SS,URI_SSR,URI_VMess,URI_Trojan,QX_SS,QX_SSR,QX_VMess,QX_Trojan,QX_Http,Loon_SS,Loon_SSR,Loon_VMess,Loon_Trojan,Loon_Http,Surge_SS,Surge_VMess,Surge_Trojan,Surge_Http]),$.info("Parsers initialized.");let o=n.parse(s);const a=ProxyFilter(),i=ProxyOperator();for(const t of e.process||[]){let e;if(-1!==t.type.indexOf("Script")){const{mode:s,content:r}=t.args;e="link"===s?await $.http.get(r).then(e=>e.body).catch(e=>{throw new Error(`Error when downloading remote script: ${t.args.content}.\n Reason: ${e}`)}):r}if(-1!==t.type.indexOf("Filter")){const s=AVAILABLE_FILTERS[t.type];if(s){$.info(`Applying filter "${t.type}" with arguments:\n >>> ${JSON.stringify(t.args)||"None"}`),-1!==t.type.indexOf("Script")?a.setFilter(s(e)):a.setFilter(s(t.args));try{o=a.process(o)}catch(e){$.error(`Failed to apply filter "${t.type}"!\n REASON: ${e}`)}}}else if(-1!==t.type.indexOf("Operator")){const s=AVAILABLE_OPERATORS[t.type];if(s){$.info(`Applying operator "${t.type}" with arguments: \n >>> ${JSON.stringify(t.args)||"None"}`),-1!==t.type.indexOf("Script")?i.setOperator(s(e)):i.setOperator(s(t.args));try{o=i.process(o)}catch(e){t.type}}}}return n.addProducers([QX_Producer,Loon_Producer,Surge_Producer,Raw_Producer,URI_Producer]),n.produce(o)}function getFlowHeaders(e,t){const s=Object.keys(e).filter(e=>/SUBSCRIPTION-USERINFO/i.test(e))[0];if(s){const t=e[s],r=Number(t.match(/upload=(\d+)/)[1]),n=Number(t.match(/download=(\d+)/)[1]),o=Number(t.match(/total=(\d+)/)[1]),a=(t.match(/expire=(\d+)/)||[])[1];return{key:"Subscription-Userinfo",value:`${r?"upload="+r+";":""}${n?"download="+n+";":""}${o?"total="+o+";":""}${a?"expire="+a+";":""}`}}{let e,s;for(const r of t)-1!==r.name.indexOf("剩余流量")&&(e=r.name),-1!==r.name.indexOf("过期时间")&&(s=r.name);return{key:"Raw-Subscription-Userinfo",value:`${e?"remains="+e+";":""}${s?"expire="+s+";":""}`}}}async function getSub(e,t){const{name:s}=e.params,r=$.read(SUBS_KEY)[s];r?t.json({status:"success",data:r}):t.status(404).json({status:"failed",message:`未找到订阅:${s}!`})}async function newSub(e,t){const s=e.body,r=$.read(SUBS_KEY);r[s.name]&&t.status(500).json({status:"failed",message:`订阅${s.name}已存在!`}),/^[\w-_]*$/.test(s.name)?(r[s.name]=s,$.write(r,SUBS_KEY),t.status(201).json({status:"success",data:s})):t.status(500).json({status:"failed",message:`订阅名称 ${s.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`})}async function updateSub(e,t){const{name:s}=e.params;$.info(`Updating subscription: ${s}`);let r=e.body;const n=$.read(SUBS_KEY);if(n[s]){const e={...n[s],...r};if(s!==r.name){const t=$.read(COLLECTIONS_KEY);for(const e of Object.keys(t)){const n=t[e].subscriptions.indexOf(s);-1!==n&&(t[e].subscriptions[n]=r.name)}delete n[s],n[r.name]=e}else n[s]=e;$.write(n,SUBS_KLine truncated
|
||||
const $=API("sub-store"),$app=express();$.http=HTTP({headers:{"User-Agent":"Quantumult%20X"}});const SUBS_KEY="subs",COLLECTIONS_KEY="collections",AVAILABLE_FILTERS={"Keyword Filter":KeywordFilter,"Useless Filter":UselessFilter,"Region Filter":RegionFilter,"Regex Filter":RegexFilter,"Type Filter":TypeFilter,"Script Filter":ScriptFilter},AVAILABLE_OPERATORS={"Set Property Operator":SetPropertyOperator,"Flag Operator":FlagOperator,"Sort Operator":SortOperator,"Keyword Sort Operator":KeywordSortOperator,"Keyword Rename Operator":KeywordRenameOperator,"Keyword Delete Operator":KeywordDeleteOperator,"Regex Rename Operator":RegexRenameOperator,"Regex Delete Operator":RegexDeleteOperator,"Script Operator":ScriptOperator};async function IP_API(e,t){const s=decodeURIComponent(e.params.server),r=await $.http.get(`http://ip-api.com/json/${s}?lang=zh-CN`).then(e=>JSON.parse(e.body));t.json(r)}async function downloadResource(e){let t=await $.http.get(e).then(e=>e.body).catch(t=>{res.status(500).json({status:"failed",message:`Cannot refresh remote resource: ${e}\n Reason: ${t}`})});const s=t.indexOf("proxies:");if(-1!==s){const e=t.lastIndexOf("}")+1;t=t.substring(s,e)}return t}async function refreshResource(e,t){const s=new Base64Code,{url:r}=e.body,n=await downloadResource(r);$.write(n,`#${s.safeEncode(r)}`),t.json({status:"success"})}async function downloadSub(e,t){const{name:s}=e.params,r=e.query.target||getPlatformFromHeaders(e.headers),n=$.read(SUBS_KEY);if(n[s]){const e=n[s];try{const s=await parseSub(e,r);t.send(s)}catch(e){t.status(500).json({status:"failed",message:e})}}else t.status(404).json({status:"failed",message:`订阅${s}不存在!`})}async function parseSub(e,t){let s;const r=(new Base64Code).safeEncode(e.url);if("Raw"===t||"URI"===t){const t=$.read(`#${r}`);t?(await $.wait(10),s=t):(s=await downloadResource(e.url),$.write(s,`#${r}`))}else s=await downloadResource(e.url),$.write(s,`#${e.url}`);$.info("======================================================================="),$.info(`Processing subscription: ${e.name}, target platform ==> ${t}.`),$.info("Initializing parsers...");const n=ProxyParser(t);n.addParsers([Clash_All,URI_SS,URI_SSR,URI_VMess,URI_Trojan,QX_SS,QX_SSR,QX_VMess,QX_Trojan,QX_Http,Loon_SS,Loon_SSR,Loon_VMess,Loon_Trojan,Loon_Http,Surge_SS,Surge_VMess,Surge_Trojan,Surge_Http]),$.info("Parsers initialized.");let o=n.parse(s);for(const t of e.process||[]){let e;if(-1!==t.type.indexOf("Script")){const{mode:s,content:r}=t.args;e="link"===s?await $.http.get(r).then(e=>e.body).catch(e=>{throw new Error(`Error when downloading remote script: ${t.args.content}.\n Reason: ${e}`)}):r}if(-1!==t.type.indexOf("Filter")){const s=AVAILABLE_FILTERS[t.type];if(s){$.info(`Applying filter "${t.type}" with arguments:\n >>> ${JSON.stringify(t.args)||"None"}`);try{o=-1!==t.type.indexOf("Script")?processFilter(s(e),o):processFilter(s(t.args),o)}catch(e){$.error(`Failed to apply filter "${t.type}"!\n REASON: ${e}`)}}}else if(-1!==t.type.indexOf("Operator")){const s=AVAILABLE_OPERATORS[t.type];if(s){$.info(`Applying operator "${t.type}" with arguments: \n >>> ${JSON.stringify(t.args)||"None"}`);try{o=-1!==t.type.indexOf("Script")?processOperator(s(e),o):processOperator(s(t.args),o)}catch(e){t.type}}}}return n.addProducers([QX_Producer,Loon_Producer,Surge_Producer,Raw_Producer,URI_Producer]),n.produce(o)}function getFlowHeaders(e,t){const s=Object.keys(e).filter(e=>/SUBSCRIPTION-USERINFO/i.test(e))[0];if(s){const t=e[s],r=Number(t.match(/upload=(\d+)/)[1]),n=Number(t.match(/download=(\d+)/)[1]),o=Number(t.match(/total=(\d+)/)[1]),a=(t.match(/expire=(\d+)/)||[])[1];return{key:"Subscription-Userinfo",value:`${r?"upload="+r+";":""}${n?"download="+n+";":""}${o?"total="+o+";":""}${a?"expire="+a+";":""}`}}{let e,s;for(const r of t)-1!==r.name.indexOf("剩余流量")&&(e=r.name),-1!==r.name.indexOf("过期时间")&&(s=r.name);return{key:"Raw-Subscription-Userinfo",value:`${e?"remains="+e+";":""}${s?"expire="+s+";":""}`}}}async function getSub(e,t){const{name:s}=e.params,r=$.read(SUBS_KEY)[s];r?t.json({status:"success",data:r}):t.status(404).json({status:"failed",message:`未找到订阅:${s}!`})}async function newSub(e,t){const s=e.body,r=$.read(SUBS_KEY);r[s.name]&&t.status(500).json({status:"failed",message:`订阅${s.name}已存在!`}),/^[\w-_]*$/.test(s.name)?(r[s.name]=s,$.write(r,SUBS_KEY),t.status(201).json({status:"success",data:s})):t.status(500).json({status:"failed",message:`订阅名称 ${s.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`})}async function updateSub(e,t){const{name:s}=e.params;$.info(`Updating subscription: ${s}`);let r=e.body;const n=$.read(SUBS_KEY);if(n[s]){const e={...n[s],...r};if(s!==r.name){const t=$.read(COLLECTIONS_KEY);for(const e of Object.keys(t)){const n=t[e].subscriptions.indexOf(s);-1!==n&&(t[e].subscriptions[n]=r.name)}delete n[s],n[r.name]=e}else n[s]=e;$.write(n,SUBS_KEY),t.json({status:"success",data:e})}else t.statuLine truncated
|
||||
Reference in new issue
Block a user