Critical fixes: - Fix Dockerfile: reorder stages so frontend assets embed into Go binary - Fix Go version 1.25 (nonexistent) to 1.24 across Dockerfile, go.mod, CI - Add graceful game server shutdown on SIGTERM/SIGINT - Order startup tasks: updates complete before auto-start - Fix TOCTOU race in UpdateSettings with atomic Update() method Security: - Add optional AUTH_TOKEN bearer auth middleware on API/WS routes - Fix path traversal in DeleteMod using filepath.Rel instead of HasPrefix - Add input validation for IPPort, ServerParameters, ScheduledUpdate Memory safety: - Cap RPT buffer allocation to 64KB to prevent OOM on large logs - Cap GetLog file read to 10MB - Fix context cancel leak in SteamCmdManager.run() - Remove data-raced cancel field in steamcmd.go - Atomic file writes (write-temp-then-rename) across all managers Reliability: - Log save errors in ProcessManager.Stop() - Atomic file writes prevent corruption on crash Tests: - Add mod_manager_test.go (12 tests: ListWorkshopMods, ListLocalMods, BuildUsageMap, RemoveMod, dirSize) - Add scheduler_test.go (6 tests: Start/Stop, Refresh with empty, invalid, valid, and replaced cron expressions) - Add TestRestart to server_process_test.go CI/Docs: - Add -race flag to go test in CI and Makefile - Add npm lint step to CI - Add Go/npm module caching to CI - Update README: prerequisites, AUTH_TOKEN/GIN_MODE/SERVERS_DIR docs, fix manual quickstart to use make build
139 lines
3.9 KiB
Go
139 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"arma3-web-server/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
settings *services.SettingsManager
|
|
configs *services.ConfigManager
|
|
modlists *services.ModlistManager
|
|
process *services.ProcessManager
|
|
steamcmd *services.SteamCmdManager
|
|
scheduler *services.Scheduler
|
|
streamer *services.LogStreamer
|
|
dataDir string
|
|
serverfileDir string
|
|
modsDir string
|
|
cfgDir string
|
|
profilesDir string
|
|
frontendServed bool
|
|
authToken string
|
|
}
|
|
|
|
func New(
|
|
settings *services.SettingsManager,
|
|
configs *services.ConfigManager,
|
|
modlists *services.ModlistManager,
|
|
process *services.ProcessManager,
|
|
steamcmd *services.SteamCmdManager,
|
|
scheduler *services.Scheduler,
|
|
streamer *services.LogStreamer,
|
|
dataDir, serverfileDir, modsDir, cfgDir, profilesDir string,
|
|
frontendServed bool,
|
|
) *Handler {
|
|
return &Handler{
|
|
settings: settings,
|
|
configs: configs,
|
|
modlists: modlists,
|
|
process: process,
|
|
steamcmd: steamcmd,
|
|
scheduler: scheduler,
|
|
streamer: streamer,
|
|
dataDir: dataDir,
|
|
serverfileDir: serverfileDir,
|
|
modsDir: modsDir,
|
|
cfgDir: cfgDir,
|
|
profilesDir: profilesDir,
|
|
frontendServed: frontendServed,
|
|
authToken: os.Getenv("AUTH_TOKEN"),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) authMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if h.authToken == "" {
|
|
c.Next()
|
|
return
|
|
}
|
|
auth := c.GetHeader("Authorization")
|
|
if !strings.HasPrefix(auth, "Bearer ") {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing or invalid authorization header"})
|
|
return
|
|
}
|
|
token := strings.TrimPrefix(auth, "Bearer ")
|
|
if !secureCompare(token, h.authToken) {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func secureCompare(a, b string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := 0; i < len(a); i++ {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (h *Handler) SetupRoutes(r *gin.Engine) {
|
|
api := r.Group("/api", h.authMiddleware())
|
|
{
|
|
api.GET("/server/settings", h.GetSettings)
|
|
api.PUT("/server/settings", h.UpdateSettings)
|
|
api.POST("/server/start", h.StartServer)
|
|
api.POST("/server/stop", h.StopServer)
|
|
api.POST("/server/restart", h.RestartServer)
|
|
api.GET("/server/status", h.ServerStatus)
|
|
api.GET("/server/steamcmd", h.SteamCMDSettings)
|
|
api.POST("/server/steamcmd/update-game", h.SteamCMDUpdateGame)
|
|
api.POST("/server/steamcmd/download-mod", h.SteamCMDDownloadMod)
|
|
api.GET("/server/steamcmd/status", h.SteamCMDStatus)
|
|
api.GET("/server/paths", h.ServerPaths)
|
|
api.GET("/server/health", h.Health)
|
|
|
|
api.GET("/server/logs", h.ListLogs)
|
|
api.GET("/server/logs/:file", h.GetLog)
|
|
|
|
api.GET("/configs", h.ListConfigs)
|
|
api.POST("/configs", h.CreateConfig)
|
|
api.GET("/configs/:name", h.GetConfig)
|
|
api.PUT("/configs/:name", h.UpdateConfig)
|
|
api.DELETE("/configs/:name", h.DeleteConfig)
|
|
api.POST("/configs/:name/duplicate", h.DuplicateConfig)
|
|
|
|
api.GET("/modlists", h.ListModlists)
|
|
api.POST("/modlists", h.CreateModlist)
|
|
api.GET("/modlists/:id", h.GetModlist)
|
|
api.PUT("/modlists/:id", h.UpdateModlist)
|
|
api.DELETE("/modlists/:id", h.DeleteModlist)
|
|
api.POST("/modlists/:id/duplicate", h.DuplicateModlist)
|
|
api.POST("/modlists/import", h.ImportModlist)
|
|
api.GET("/modlists/:id/check", h.CheckModlistMods)
|
|
api.POST("/modlists/:id/download-missing", h.DownloadMissingMods)
|
|
api.POST("/modlists/:id/update-all", h.UpdateAllMods)
|
|
api.GET("/modlists/:id/export", h.ExportModlist)
|
|
|
|
api.GET("/mods", h.ListMods)
|
|
api.DELETE("/mods", h.DeleteMod)
|
|
api.POST("/mods/cleanup", h.CleanupMods)
|
|
}
|
|
|
|
ws := r.Group("/ws", h.authMiddleware())
|
|
ws.GET("/server/logs", h.StreamLogs)
|
|
ws.GET("/steamcmd/logs", h.StreamSteamCMDLogs)
|
|
ws.GET("/server/rpt", h.StreamRPTLogs)
|
|
}
|