Fixed async issues in certain cases.

This commit is contained in:
NobyDa 2021-01-24 18:32:46 +08:00
parent fc7f2ad211
commit 358dcdcad9
1 changed files with 41 additions and 36 deletions

View File

@ -2,7 +2,7 @@
Sub-Store外置流量查询脚本
该脚本基于Sub-Store, 可解决APP使用Sub-Store链接后, 没有流量通知的问题. 使用前需确认您的机场订阅是否支持流量信息.
该脚本基于Sub-Store, 可解决APP使用Sub-Store链接后, 没有流量通知的问题. 使用前需确认您的机场订阅是否支持流量信息. (:节点名流量信息暂不支持)
Sub-Store订阅管理器: https://github.com/Peng-YM/Sub-Store
测试兼容: QuantumultX, Surge, Loon.
@ -14,47 +14,52 @@ Sub-Store订阅管理器: https://github.com/Peng-YM/Sub-Store
******************************/
function operator(proxies) {
async function operator(proxies) {
try {
if ($request.headers["User-Agent"].match(/Quant|Surge|Loon|Decar/)) {
function AllSubs(subsUrl, subsName) {
$.http.get(subsUrl).then(resp => {
try { //reference to https://github.com/KOP-XIAO/QuantumultX/blob/master/Scripts/resource-parser.js
var sinfo = JSON.stringify(resp.headers).replace(/ /g, "").toLowerCase();
var total = (parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)).toFixed(0);
var usd = ((parseFloat(sinfo.indexOf("upload") != -1 ? sinfo.split("upload=")[1].split(",")[0] : "0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3)).toFixed(2);
var left = ((parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)) - ((parseFloat(sinfo.indexOf("upload") != -1 ? sinfo.split("upload=")[1].split(",")[0] : "0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3))).toFixed(2);
if (sinfo.indexOf("expire=") != -1) {
var epr = new Date(parseFloat(sinfo.split("expire=")[1].split(",")[0]) * 1000);
var year = epr.getFullYear();
var mth = epr.getMonth() + 1 < 10 ? '0' + (epr.getMonth() + 1) : (epr.getMonth() + 1);
var day = epr.getDate() < 10 ? "0" + (epr.getDate()) : epr.getDate();
var epr = `🔹 过期时间:「 ${year}-${mth}-${day}`
} else {
var epr = "";
}
$.notify(`🔹 订阅昵称:「 ${subsName}`, epr, `🔸 已用流量:「 ${usd} GB 」\n🔸 剩余流量:「 ${left} GB 」`)
} catch (errInfo) {
console.log(`\n🔹 订阅昵称:「 ${subsName}\n🔺 查询失败: ${errInfo}`)
}
}, er => $.notify(`🔹 订阅昵称:「 ${subsName}`, ``, `🔺 查询失败: ${er}`))
}
const subtag = $request.url.match(/download\/(collection\/)?([\w-_]*)/)[2]
const rawInfo = $.read('subs')
if ($request.url.match(/\/collection\//)) {
const readName = $.read('collections')
if ($request.headers["User-Agent"].match(/Quantumult|Surge|Loon|Decar/) && !$.raw_Name) {
const rawInfo = $.read('subs');
const readName = $.read('collections');
const subtag = $request.url.match(/download\/(collection\/)?([\w-_]*)/)[2];
if ($request.url.match(/\/collection\//)) { //collection subscription.
const isOpen = readName[subtag].process.map(o => o.type).indexOf("Script Operator") != -1;
for (var i = 0; i < readName[subtag].subscriptions.length; i++) {
const rawUrl = rawInfo[readName[subtag].subscriptions[i]].url;
const rawName = readName[subtag].subscriptions[i];
AllSubs(rawUrl, rawName);
try {
$.raw_Name = readName[subtag].subscriptions[i];
if (!isOpen) break; //prevent queries in certain cases.
const query = await $.http.get(rawInfo[readName[subtag].subscriptions[i]].url);
AllSubs(query, $.raw_Name);
} catch (er) {
$.notify(`🔹 订阅昵称:「 ${$.raw_Name||'未知'}`, ``, `🔺 查询失败:「 ${er.message||er}`);
}
}
} else {
AllSubs(rawInfo[subtag].url, rawInfo[subtag].name);
} else { //single subscription.
$.raw_Name = rawInfo[subtag].name;
const query = await $.http.get(rawInfo[subtag].url);
AllSubs(query, $.raw_Name);
}
}
} catch (err) {
console.log(`\n🔺 查询失败: \n${err.message}`)
$.notify(`🔹 订阅昵称:「 ${$.raw_Name||'未知'}`, ``, `🔺 查询失败:「 ${err.message||err}`);
} finally {
return proxies
return proxies;
}
}
function AllSubs(resp, subsName) { //reference to https://github.com/KOP-XIAO/QuantumultX/blob/master/Scripts/resource-parser.js
var sinfo = JSON.stringify(resp.headers || '').replace(/ /g, "").toLowerCase();
if (sinfo.indexOf("total=") != -1 && sinfo.indexOf("download=") != -1) {
var total = (parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)).toFixed(0);
var usd = ((parseFloat(sinfo.indexOf("upload") != -1 ? sinfo.split("upload=")[1].split(",")[0] : "0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3)).toFixed(2);
var left = ((parseFloat(sinfo.split("total=")[1].split(",")[0]) / (1024 ** 3)) - ((parseFloat(sinfo.indexOf("upload") != -1 ? sinfo.split("upload=")[1].split(",")[0] : "0") + parseFloat(sinfo.split("download=")[1].split(",")[0])) / (1024 ** 3))).toFixed(2);
if (sinfo.indexOf("expire=") != -1) {
var epr = new Date(parseFloat(sinfo.split("expire=")[1].split(",")[0]) * 1000);
var year = epr.getFullYear();
var mth = epr.getMonth() + 1 < 10 ? '0' + (epr.getMonth() + 1) : (epr.getMonth() + 1);
var day = epr.getDate() < 10 ? "0" + (epr.getDate()) : epr.getDate();
var epr = `🔹 过期时间:「 ${year}-${mth}-${day}`;
} else {
var epr = "";
}
$.notify(`🔹 订阅昵称:「 ${subsName}`, epr, `🔸 已用流量:「 ${usd} GB 」\n🔸 剩余流量:「 ${left} GB 」`);
} else $.error(`\n🔹 订阅昵称:「 ${subsName}\n🔺 查询失败:「 该订阅不包含流量信息 」`);
}