feat: 为脚本操作增加流量信息操作 flowUtils

This commit is contained in:
xream 2023-11-07 16:42:28 +08:00
parent a91f9d7728
commit 6d51774d36
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
4 changed files with 35 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.81", "version": "2.14.82",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -8,6 +8,7 @@ import $ from '@/core/app';
import { hex_md5 } from '@/vendor/md5'; import { hex_md5 } from '@/vendor/md5';
import { ProxyUtils } from '@/core/proxy-utils'; import { ProxyUtils } from '@/core/proxy-utils';
import env from '@/utils/env'; import env from '@/utils/env';
import { getFlowHeaders, parseFlowHeaders, flowTransfer } from '@/utils/flow';
/** /**
The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows: The rule "(name CONTAINS "🇨🇳") AND (port IN [80, 443])" can be expressed as follows:
@ -668,6 +669,7 @@ function removeFlag(str) {
} }
function createDynamicFunction(name, script, $arguments) { function createDynamicFunction(name, script, $arguments) {
const flowUtils = { getFlowHeaders, parseFlowHeaders, flowTransfer };
if ($.env.isLoon) { if ($.env.isLoon) {
return new Function( return new Function(
'$arguments', '$arguments',
@ -678,6 +680,7 @@ function createDynamicFunction(name, script, $arguments) {
'$notification', '$notification',
'ProxyUtils', 'ProxyUtils',
'scriptResourceCache', 'scriptResourceCache',
'flowUtils',
`${script}\n return ${name}`, `${script}\n return ${name}`,
)( )(
$arguments, $arguments,
@ -691,6 +694,7 @@ function createDynamicFunction(name, script, $arguments) {
$notification, $notification,
ProxyUtils, ProxyUtils,
scriptResourceCache, scriptResourceCache,
flowUtils,
); );
} else { } else {
return new Function( return new Function(
@ -699,7 +703,9 @@ function createDynamicFunction(name, script, $arguments) {
'lodash', 'lodash',
'ProxyUtils', 'ProxyUtils',
'scriptResourceCache', 'scriptResourceCache',
'flowUtils',
`${script}\n return ${name}`, `${script}\n return ${name}`,
)($arguments, $, lodash, ProxyUtils, scriptResourceCache); )($arguments, $, lodash, ProxyUtils, scriptResourceCache, flowUtils);
} }
} }

View File

@ -6,7 +6,7 @@ import {
} from './errors'; } from './errors';
import { deleteByName, findByName, updateByName } from '@/utils/database'; import { deleteByName, findByName, updateByName } from '@/utils/database';
import { SUBS_KEY, COLLECTIONS_KEY, ARTIFACTS_KEY } from '@/constants'; import { SUBS_KEY, COLLECTIONS_KEY, ARTIFACTS_KEY } from '@/constants';
import { getFlowHeaders } from '@/utils/flow'; import { getFlowHeaders, parseFlowHeaders } from '@/utils/flow';
import { success, failed } from './response'; import { success, failed } from './response';
import $ from '@/core/app'; import $ from '@/core/app';
@ -68,20 +68,7 @@ async function getFlowInfo(req, res) {
return; return;
} }
// unit is KB success(res, parseFlowHeaders(flowHeaders));
const uploadMatch = flowHeaders.match(/upload=(-?)(\d+)/);
const upload = Number(uploadMatch[1] + uploadMatch[2]);
const downloadMatch = flowHeaders.match(/download=(-?)(\d+)/);
const download = Number(downloadMatch[1] + downloadMatch[2]);
const total = Number(flowHeaders.match(/total=(\d+)/)[1]);
// optional expire timestamp
const match = flowHeaders.match(/expire=(\d+)/);
const expires = match ? Number(match[1]) : undefined;
success(res, { expires, total, usage: { upload, download } });
} catch (err) { } catch (err) {
failed( failed(
res, res,

View File

@ -13,3 +13,28 @@ export async function getFlowHeaders(url) {
)[0]; )[0];
return headers[subkey]; return headers[subkey];
} }
export function parseFlowHeaders(flowHeaders) {
if (!flowHeaders) return;
// unit is KB
const uploadMatch = flowHeaders.match(/upload=(-?)(\d+)/);
const upload = Number(uploadMatch[1] + uploadMatch[2]);
const downloadMatch = flowHeaders.match(/download=(-?)(\d+)/);
const download = Number(downloadMatch[1] + downloadMatch[2]);
const total = Number(flowHeaders.match(/total=(\d+)/)[1]);
// optional expire timestamp
const match = flowHeaders.match(/expire=(\d+)/);
const expires = match ? Number(match[1]) : undefined;
return { expires, total, usage: { upload, download } };
}
export function flowTransfer(flow, unit = 'B') {
const unitList = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
let unitIndex = unitList.indexOf(unit);
return flow < 1024
? { value: flow.toFixed(1), unit: unit }
: flowTransfer(flow / 1024, unitList[++unitIndex]);
}