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
90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type ConfigManager struct {
|
|
cfgDir string
|
|
}
|
|
|
|
func NewConfigManager(cfgDir string) *ConfigManager {
|
|
return &ConfigManager{cfgDir: cfgDir}
|
|
}
|
|
|
|
type ConfigInfo struct {
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func (cm *ConfigManager) List() ([]ConfigInfo, error) {
|
|
entries, err := os.ReadDir(cm.cfgDir)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return []ConfigInfo{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
var configs []ConfigInfo
|
|
for _, e := range entries {
|
|
if !e.IsDir() && strings.HasSuffix(e.Name(), ".cfg") {
|
|
info, _ := e.Info()
|
|
configs = append(configs, ConfigInfo{
|
|
Name: strings.TrimSuffix(e.Name(), ".cfg"),
|
|
Size: info.Size(),
|
|
})
|
|
}
|
|
}
|
|
return configs, nil
|
|
}
|
|
|
|
func (cm *ConfigManager) Get(name string) (string, error) {
|
|
path := cm.path(name)
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return "", fmt.Errorf("config not found")
|
|
}
|
|
return "", err
|
|
}
|
|
return string(data), nil
|
|
}
|
|
|
|
func (cm *ConfigManager) Create(name, content string) error {
|
|
path := cm.path(name)
|
|
if _, err := os.Stat(path); err == nil {
|
|
return fmt.Errorf("config already exists")
|
|
}
|
|
return writeFileAtomic(path, []byte(content))
|
|
}
|
|
|
|
func (cm *ConfigManager) Update(name, content string) error {
|
|
return writeFileAtomic(cm.path(name), []byte(content))
|
|
}
|
|
|
|
func (cm *ConfigManager) Delete(name string) error {
|
|
return os.Remove(cm.path(name))
|
|
}
|
|
|
|
func (cm *ConfigManager) Duplicate(name, newName string) error {
|
|
content, err := cm.Get(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return cm.Create(newName, content)
|
|
}
|
|
|
|
func (cm *ConfigManager) path(name string) string {
|
|
return filepath.Join(cm.cfgDir, sanitizeConfigName(name)+".cfg")
|
|
}
|
|
|
|
func sanitizeConfigName(name string) string {
|
|
name = filepath.Base(name)
|
|
name = strings.TrimSuffix(name, ".cfg")
|
|
return name
|
|
}
|