diff --git a/server/proxy/service/subscription-server-v2.js b/server/proxy/service/subscription-server-v2.js index 28c04564..8d1eb5fe 100644 --- a/server/proxy/service/subscription-server-v2.js +++ b/server/proxy/service/subscription-server-v2.js @@ -152,16 +152,28 @@ function buildStaticNodes() { // ── 生成subscription-userinfo头 ────────────── // 共享流量池模型: total=池配额(2000GB),upload+download=池总用量 +// 使用缓存的池状态文件(traffic-monitor-v2每5分钟更新)以减少计算开销 function generateUserInfoHeader(user) { const nextMonth = new Date(); nextMonth.setMonth(nextMonth.getMonth() + 1); nextMonth.setDate(1); nextMonth.setHours(0, 0, 0, 0); - // 获取流量池总用量 (所有用户汇总) - const poolStatus = userManager.getPoolStatus(); + // 优先从缓存文件读取池状态 (traffic-monitor-v2每5分钟写入) + let poolUpload = 0; + let poolDownload = 0; + try { + const cached = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'pool-quota-status.json'), 'utf8')); + poolUpload = cached.pool_upload_bytes || 0; + poolDownload = cached.pool_download_bytes || 0; + } catch { + // 缓存不可用时实时计算 + const poolStatus = userManager.getPoolStatus(); + poolUpload = poolStatus.pool_upload_bytes; + poolDownload = poolStatus.pool_download_bytes; + } - return `upload=${poolStatus.pool_upload_bytes}; download=${poolStatus.pool_download_bytes}; total=${userManager.POOL_QUOTA_BYTES}; expire=${Math.floor(nextMonth.getTime() / 1000)}`; + return `upload=${poolUpload}; download=${poolDownload}; total=${userManager.POOL_QUOTA_BYTES}; expire=${Math.floor(nextMonth.getTime() / 1000)}`; } // ── 生成VLESS URI (Shadowrocket · 多节点) ───── diff --git a/server/proxy/service/traffic-monitor-v2.js b/server/proxy/service/traffic-monitor-v2.js index 2a739497..cbb6f5d9 100644 --- a/server/proxy/service/traffic-monitor-v2.js +++ b/server/proxy/service/traffic-monitor-v2.js @@ -101,7 +101,7 @@ function checkPoolAlerts(poolStatus) { alertState.alerts_sent = { p80: false, p90: false, p100: false }; } - const pct = poolStatus.pool_percentage; + const pct = Math.min(poolStatus.pool_percentage, 100); if (pct >= 100 && !alertState.alerts_sent.p100) { console.log(`[V2流量监控] ⚠️ 流量池已用完! ${poolStatus.pool_used_gb.toFixed(2)}GB / ${poolStatus.pool_total_gb}GB`); diff --git a/server/proxy/service/user-manager.js b/server/proxy/service/user-manager.js index 6ab49f96..a98ab936 100644 --- a/server/proxy/service/user-manager.js +++ b/server/proxy/service/user-manager.js @@ -137,7 +137,7 @@ function addUser(email, options = {}) { uuid: generateUUID(), token: generateToken(), label: options.label || email.split('@')[0], - quota_bytes: POOL_QUOTA_BYTES, // 共享流量池配额(展示用·实际按池总量控制) + quota_bytes: POOL_QUOTA_BYTES, // 存储池配额引用·实际配额由池级别统一控制(getPoolStatus)·非独立限额 enabled: true, created_at: now.toISOString(), traffic: {