Files
komari/web/api/ws.go
T
Akizon77 7da616ce4c feat(security): add configurable CORS and origin checks
Add settings for allowed CORS origins and WebSocket origin validation so
deployments can restrict cross-origin access instead of using a global
wildcard policy.

Move CORS handling into the security middleware and update report cache
usage to use the dedicated report package.
2026-06-08 20:45:00 +08:00

112 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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"
"github.com/komari-monitor/komari/protocol/v1"
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 := UpgradeWebSocket(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Failed to upgrade to WebSocket." + err.Error()})
return
}
defer conn.Close()
// 初始化用户信息
var (
isLogin = false
hiddenMap = map[string]bool{}
session, _ = c.Cookie("session_token")
)
// 登录状态检查
_, err = accounts.GetUserBySession(session)
if err == nil {
isLogin = true
}
// 仅在未登录时需要 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 {
var resp struct {
Online []string `json:"online"` // 已建立连接的客户端uuid列表
Data map[string]v1.Report `json:"data"` // 最后上报的数据
}
resp.Online = []string{}
resp.Data = map[string]v1.Report{}
_, data, err := conn.ReadMessage()
if err != nil {
//log.Println("Error reading message:", err)
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
}
err = conn.WriteJSON(gin.H{"status": "success", "data": resp})
if err != nil {
return
}
}
}