mirror of
https://github.com/komari-monitor/komari.git
synced 2026-11-22 16:00:46 +00:00
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/komari-monitor/komari/internal/config"
|
|
"github.com/komari-monitor/komari/web/security"
|
|
)
|
|
|
|
type WebSocketUpgradeOption func(*websocket.Upgrader)
|
|
|
|
func IsWebSocketUpgrade(c *gin.Context) bool {
|
|
return websocket.IsWebSocketUpgrade(c.Request)
|
|
}
|
|
|
|
func EnableWebSocketCompression(upgrader *websocket.Upgrader) {
|
|
upgrader.EnableCompression = true
|
|
}
|
|
|
|
func UpgradeWebSocket(c *gin.Context, options ...WebSocketUpgradeOption) (*websocket.Conn, error) {
|
|
if !IsWebSocketUpgrade(c) {
|
|
return nil, fmt.Errorf("require websocket upgrade")
|
|
}
|
|
upgrader := websocket.Upgrader{
|
|
CheckOrigin: CheckWebSocketOrigin,
|
|
}
|
|
for _, option := range options {
|
|
option(&upgrader)
|
|
}
|
|
return upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
}
|
|
|
|
func CheckWebSocketOrigin(r *http.Request) bool {
|
|
origin := r.Header.Get("Origin")
|
|
if strings.EqualFold(os.Getenv("KOMARI_WS_DISABLE_ORIGIN"), "true") {
|
|
return true
|
|
}
|
|
if security.IsAPIKeyRequest(r) {
|
|
return true
|
|
}
|
|
if origin == "" && r.URL.Query().Get("token") != "" {
|
|
return true
|
|
}
|
|
enabled, _ := config.GetAs[bool](config.WsOriginCheckEnabledKey, true)
|
|
if !enabled {
|
|
return true
|
|
}
|
|
if origin == "" {
|
|
return false
|
|
}
|
|
if security.OriginMatchesHost(origin, r.Host) {
|
|
return true
|
|
}
|
|
allowlist, _ := config.GetAs[string](config.WsAllowedOriginsKey, "")
|
|
return security.OriginInAllowlist(origin, allowlist)
|
|
}
|