Fix connection logs: add background status.log sync task

ConnLogs were never populated because AppendConnLog was never called.
Added StartStatusSync() goroutine that runs every 10 seconds:

1. Iterates all running instances
2. Reads status.log via ParseStatus (status-version 3)
3. For each CLIENT_LIST entry:
   - If no active ConnLog exists → AppendConnLog (new connection)
   - If active ConnLog exists → UpdateActiveConnStats (traffic)
4. For active ConnLogs with no matching CLIENT_LIST → CloseActiveConn

Store additions:
  ListActiveConns(instanceID)  — returns all unclosed connections
  UpdateActiveConnStats()      — updates bytes in/out without flush

StartStatusSync is called from main.go right after Service creation.
UpdateActiveConnStats deliberately skips flush() to avoid writing
db.json every 10 seconds; stats are persisted on disconnect.
This commit is contained in:
cnbugs
2026-08-09 23:52:17 +08:00
parent f3cda67d9f
commit 19c0b190ea
3 changed files with 83 additions and 0 deletions
+1
View File
@@ -56,6 +56,7 @@ func main() {
log.Printf("warn: ensure CA: %v", err)
}
svc := service.New(cfg, st, ovm)
svc.StartStatusSync() // 后台定时同步连接日志
addr := cfg.Host + ":" + itoa(cfg.Port)
log.Printf("openvpn-manager listening on %s, data=%s, dist=%s", addr, cfg.DataDir, *distDir)