mirror of
https://github.com/komari-monitor/komari.git
synced 2026-11-24 17:10:54 +00:00
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package api
|
||
|
||
import (
|
||
"net/http"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/komari-monitor/komari/database/accounts"
|
||
"github.com/komari-monitor/komari/database/dbcore"
|
||
"github.com/komari-monitor/komari/database/models"
|
||
v2 "github.com/komari-monitor/komari/protocol/v2"
|
||
agent_runtime "github.com/komari-monitor/komari/web/agent"
|
||
)
|
||
|
||
func GetClients(c *gin.Context) {
|
||
// 升级到ws
|
||
if !IsWebSocketUpgrade(c) {
|
||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Require WebSocket upgrade"})
|
||
return
|
||
}
|
||
// Upgrade the HTTP connection to a WebSocket connection
|
||
conn, err := UpgradeSafeConn(c, EnableWebSocketCompression)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Failed to upgrade to WebSocket." + err.Error()})
|
||
return
|
||
}
|
||
defer conn.Close()
|
||
|
||
session, _ := c.Cookie("session_token")
|
||
_, err = accounts.GetUserBySession(session)
|
||
isLogin := err == nil
|
||
hiddenMap := map[string]bool{}
|
||
|
||
// 仅在未登录时需要 Hidden 信息做过滤
|
||
if !isLogin {
|
||
var hiddenClients []models.Client
|
||
db := dbcore.GetDBInstance()
|
||
_ = db.Select("uuid").Where("hidden = ?", true).Find(&hiddenClients).Error
|
||
for _, cli := range hiddenClients {
|
||
hiddenMap[cli.UUID] = true
|
||
}
|
||
}
|
||
|
||
// 请求
|
||
for {
|
||
resp := struct {
|
||
Online []string `json:"online"` // 已建立连接的客户端uuid列表
|
||
Data map[string]v2.Report `json:"data"` // 最后上报的数据
|
||
}{Online: []string{}, Data: map[string]v2.Report{}}
|
||
|
||
_, data, err := conn.ReadMessage()
|
||
if err != nil {
|
||
return
|
||
}
|
||
message := string(data)
|
||
|
||
uuID := ""
|
||
if message != "get" { // 非请求全部内容
|
||
if strings.HasPrefix(message, "get ") {
|
||
uuID = strings.TrimSpace(strings.TrimPrefix(message, "get "))
|
||
} else {
|
||
conn.WriteJSON(gin.H{"status": "error", "error": "Invalid message"})
|
||
continue
|
||
}
|
||
}
|
||
|
||
// 在线客户端uuid列表(WebSocket 与非 WebSocket)
|
||
for _, key := range agent_runtime.GetAllOnlineUUIDs() {
|
||
if !isLogin && hiddenMap[key] {
|
||
continue
|
||
}
|
||
if uuID != "" && key != uuID {
|
||
continue
|
||
}
|
||
resp.Online = append(resp.Online, key)
|
||
}
|
||
|
||
//过往节点数据信息
|
||
for key, report := range agent_runtime.GetLatestReport() {
|
||
if !isLogin && hiddenMap[key] {
|
||
continue
|
||
}
|
||
if uuID != "" && key != uuID {
|
||
continue
|
||
}
|
||
|
||
report.UUID = "" // 不暴露 uuid
|
||
if report.CPU.Usage == 0 {
|
||
report.CPU.Usage = 0.01
|
||
}
|
||
resp.Data[key] = *report
|
||
}
|
||
|
||
if err := conn.WriteJSON(gin.H{"status": "success", "data": resp}); err != nil {
|
||
return
|
||
}
|
||
}
|
||
}
|