feat: 管理后台仪表盘统计卡片接入真实数据

新增 /api/admin/stats 聚合接口,填充运行时长、活跃设备、今日流量、在线节点四项卡片:
- VpnService 记录启动时间,提供 StartedAt/TotalLiveTraffic 方法
- 新增 TrafficStat 模型,客户端断开时 upsert 当日 rx/tx 流量(持久化)
- 前端 AdminView 调用 stats 接口并每 30 秒自动刷新
This commit is contained in:
2026-07-07 15:43:29 +08:00
parent 570dc82125
commit c14fe5ec06
7 changed files with 160 additions and 27 deletions
+32 -13
View File
@@ -6,6 +6,7 @@ import (
"log"
"net"
"sync"
"time"
"lmvpn/internal/model"
@@ -13,21 +14,22 @@ import (
)
type VpnService struct {
mu sync.RWMutex
settings model.VpnSetting
net *net.IPNet
serverIP net.IP
prefix int
alloc *AllocationManager
net6 *net.IPNet
mu sync.RWMutex
settings model.VpnSetting
net *net.IPNet
serverIP net.IP
prefix int
alloc *AllocationManager
net6 *net.IPNet
serverIP6 net.IP
prefix6 int
alloc6 *AllocationManager
switchx *PacketSwitch
tun *TUNInterface
tunDone chan struct{}
running bool
clients map[*tunnelConn]struct{}
alloc6 *AllocationManager
switchx *PacketSwitch
tun *TUNInterface
tunDone chan struct{}
running bool
startedAt time.Time
clients map[*tunnelConn]struct{}
}
func NewVpnService() *VpnService {
@@ -42,6 +44,22 @@ func (s *VpnService) Running() bool {
return s.running
}
func (s *VpnService) StartedAt() time.Time {
s.mu.RLock()
defer s.mu.RUnlock()
return s.startedAt
}
func (s *VpnService) TotalLiveTraffic() (rx, tx int64) {
s.mu.RLock()
for c := range s.clients {
rx += c.rxBytes.Load()
tx += c.txBytes.Load()
}
s.mu.RUnlock()
return
}
func (s *VpnService) Settings() model.VpnSetting {
s.mu.RLock()
defer s.mu.RUnlock()
@@ -133,6 +151,7 @@ func (s *VpnService) ApplySettings(settings model.VpnSetting, reservations4, res
s.tun = tun
s.tunDone = make(chan struct{})
s.running = true
s.startedAt = time.Now()
s.mu.Unlock()
go s.serveTUN()