Add subscription info refresh endpoint and UI stats
This commit is contained in:
@@ -4,7 +4,7 @@ import path from "node:path";
|
||||
import crypto from "node:crypto";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { settings } from "./config.js";
|
||||
import { fetchSubscription } from "./subscription.js";
|
||||
import { fetchSubscription, fetchSubscriptionInfo } from "./subscription.js";
|
||||
import os from "node:os";
|
||||
import {
|
||||
buildGatewayConfig,
|
||||
@@ -1424,6 +1424,19 @@ async function handleApi(req, res) {
|
||||
return sendJson(res, 200, { success: true, ...parsed });
|
||||
}
|
||||
|
||||
if (req.method === "POST" && req.url === "/api/subscription/refresh-info") {
|
||||
const prevState = readJson(settings.statePath, {});
|
||||
if (!prevState.subscriptionUrl) {
|
||||
return sendJson(res, 400, { success: false, error: "Подписка не настроена" });
|
||||
}
|
||||
|
||||
const info = await fetchSubscriptionInfo(prevState.subscriptionUrl);
|
||||
writeJson(settings.statePath, { ...prevState, ...info });
|
||||
const cached = readJson(settings.subscriptionCachePath, null);
|
||||
if (cached) writeJson(settings.subscriptionCachePath, { ...cached, ...info });
|
||||
return sendJson(res, 200, { success: true, ...info });
|
||||
}
|
||||
|
||||
if (req.method === "DELETE" && req.url === "/api/subscription") {
|
||||
if (fs.existsSync(settings.subscriptionCachePath))
|
||||
fs.rmSync(settings.subscriptionCachePath);
|
||||
|
||||
@@ -136,7 +136,7 @@ export function parseSubscriptionBody(body) {
|
||||
return { config: parsedConfig, servers };
|
||||
}
|
||||
|
||||
export async function fetchSubscription(url) {
|
||||
async function requestSubscription(url) {
|
||||
let parsedUrl;
|
||||
try {
|
||||
parsedUrl = new URL(url);
|
||||
@@ -157,6 +157,21 @@ export async function fetchSubscription(url) {
|
||||
throw new Error(`Subscription request failed: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function fetchSubscriptionInfo(url) {
|
||||
const response = await requestSubscription(url);
|
||||
await response.body?.cancel();
|
||||
return {
|
||||
userInfo: parseUserInfo(response.headers.get('subscription-userinfo')),
|
||||
fetchedAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchSubscription(url) {
|
||||
const response = await requestSubscription(url);
|
||||
|
||||
const body = await response.text();
|
||||
const userInfo = parseUserInfo(response.headers.get('subscription-userinfo'));
|
||||
const parsed = parseSubscriptionBody(body);
|
||||
|
||||
Reference in New Issue
Block a user