mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-09-12 18:23:14 +08:00
feat (backend): Add backend API to get flow info for subscriptions
This commit is contained in:
parent
79a8956f92
commit
7e1139528e
6
backend/dist/cron-sync-artifacts.min.js
vendored
6
backend/dist/cron-sync-artifacts.min.js
vendored
File diff suppressed because one or more lines are too long
6
backend/dist/sub-store-parser.loon.min.js
vendored
6
backend/dist/sub-store-parser.loon.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sub-store",
|
"name": "sub-store",
|
||||||
"version": "2.1.5",
|
"version": "2.2.0",
|
||||||
"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": {
|
||||||
|
@ -66,6 +66,7 @@ function getEnv(req, res) {
|
|||||||
if (isLoon) backend = 'Loon';
|
if (isLoon) backend = 'Loon';
|
||||||
if (isSurge) backend = 'Surge';
|
if (isSurge) backend = 'Surge';
|
||||||
res.json({
|
res.json({
|
||||||
|
status: 200,
|
||||||
backend,
|
backend,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ export default function register($app) {
|
|||||||
|
|
||||||
$app.get('/download/:name', downloadSubscription);
|
$app.get('/download/:name', downloadSubscription);
|
||||||
|
|
||||||
|
$app.get('/api/sub/flow/:name', getFlowInfo);
|
||||||
|
|
||||||
$app.route('/api/sub/:name')
|
$app.route('/api/sub/:name')
|
||||||
.get(getSubscription)
|
.get(getSubscription)
|
||||||
.patch(updateSubscription)
|
.patch(updateSubscription)
|
||||||
@ -72,6 +74,59 @@ async function downloadSubscription(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getFlowInfo(req, res) {
|
||||||
|
let { name } = req.params;
|
||||||
|
name = decodeURIComponent(name);
|
||||||
|
const sub = $.read(SUBS_KEY)[name];
|
||||||
|
|
||||||
|
if (!sub) {
|
||||||
|
res.status(404).json({
|
||||||
|
status: 'failed',
|
||||||
|
code: 'NOT_FOUND',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (sub.source === 'local') {
|
||||||
|
res.status(500).json({
|
||||||
|
status: 'failed',
|
||||||
|
code: 'IS_LOCAL_SUB',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const flowHeaders = await getFlowHeaders(sub.url);
|
||||||
|
if (!flowHeaders) {
|
||||||
|
res.status(500).json({
|
||||||
|
status: 'failed',
|
||||||
|
code: 'NO_FLOW_INFO',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// unit is KB
|
||||||
|
const upload = Number(flowHeaders.match(/upload=(\d+)/)[1]);
|
||||||
|
const download = Number(flowHeaders.match(/download=(\d+)/)[1]);
|
||||||
|
const total = Number(flowHeaders.match(/total=(\d+)/)[1]);
|
||||||
|
|
||||||
|
// optional expire timestamp
|
||||||
|
const expires = flowHeaders.match(/expire=(\d+)/);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: {
|
||||||
|
expires: expires ? Number(expires[1]) : undefined,
|
||||||
|
total,
|
||||||
|
usage: {
|
||||||
|
upload,
|
||||||
|
download,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({
|
||||||
|
status: 'failed',
|
||||||
|
code: 'NOT_AVAILABLE',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function createSubscription(req, res) {
|
function createSubscription(req, res) {
|
||||||
const sub = req.body;
|
const sub = req.body;
|
||||||
const allSubs = $.read(SUBS_KEY);
|
const allSubs = $.read(SUBS_KEY);
|
||||||
|
6
backend/sub-store.min.js
vendored
6
backend/sub-store.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user