feat: add authorization to websocket
This commit is contained in:
parent
e7e06cf646
commit
51557d616e
2 changed files with 12 additions and 2 deletions
|
|
@ -11,4 +11,7 @@ UPTIME_KUMA_ENABLED=false
|
|||
# URL for Uptime Kuma
|
||||
UPTIME_KUMA_URL="http://localhost:3001/metrics"
|
||||
# API key for Uptime Kuma
|
||||
UPTIME_KUMA_API_KEY="API_KEY_GOES_HERE"
|
||||
UPTIME_KUMA_API_KEY="API_KEY_GOES_HERE"
|
||||
|
||||
# The password for the computer WebSocket
|
||||
WEBSOCKET_PASSWORD="PASSWORD_GOES_HERE"
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
|
@ -18,6 +19,12 @@ var upgrader = websocket.Upgrader{
|
|||
}
|
||||
|
||||
func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != os.Getenv("WEBSOCKET_PASSWORD") {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("Unauthorized"))
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
slog.Error("Error when upgrading websocket connection", slog.Any("error", err))
|
||||
|
|
@ -25,7 +32,7 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
defer conn.Close()
|
||||
|
||||
slog.Info("Websocket connection established!")
|
||||
slog.Info("WebSocket connection established!")
|
||||
service.ComputerData.Online = true
|
||||
|
||||
// Read messages
|
||||
|
|
|
|||
Loading…
Reference in a new issue