All checks were successful
Test Asgard Runner / test (push) Successful in 3s
Wire gorilla/websocket into the Go companion agent to send arbitrary console commands (e.g. oxide.reload BetterLoot) to the Rust Dedicated Server's WebRCON endpoint. Adds RCON_PORT and RCON_PASSWORD env vars, a new "command" action on the existing cmd.server NATS subject, and the internal/rcon package that handles the JSON-over-WebSocket protocol. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
115 lines
3.3 KiB
Go
115 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
"github.com/vigilcyber/corrosion-companion/internal/app"
|
|
"github.com/vigilcyber/corrosion-companion/internal/client"
|
|
)
|
|
|
|
// Config holds all environment-based configuration
|
|
type Config struct {
|
|
// NATS connection
|
|
NATSUrl string `envconfig:"NATS_URL" required:"true"`
|
|
NATSToken string `envconfig:"NATS_TOKEN" default:""`
|
|
|
|
// License identification
|
|
LicenseID string `envconfig:"LICENSE_ID" required:"true"`
|
|
|
|
// Game server configuration
|
|
SteamCMDPath string `envconfig:"STEAMCMD_PATH" default:"/usr/games/steamcmd"`
|
|
GameServerPath string `envconfig:"GAME_SERVER_PATH" default:""`
|
|
GameServerArgs string `envconfig:"GAME_SERVER_ARGS" default:"-batchmode"`
|
|
|
|
// Install directory for deployment
|
|
InstallDir string `envconfig:"INSTALL_DIR" default:""`
|
|
|
|
// RCON configuration
|
|
RconPort int `envconfig:"RCON_PORT" default:"28016"`
|
|
RconPassword string `envconfig:"RCON_PASSWORD" default:""`
|
|
|
|
// Optional settings
|
|
HeartbeatInterval int `envconfig:"HEARTBEAT_INTERVAL" default:"60"`
|
|
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
|
|
}
|
|
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
log.Printf("Corrosion Companion Agent v%s starting...", version)
|
|
|
|
// Load configuration from environment
|
|
var cfg Config
|
|
if err := envconfig.Process("", &cfg); err != nil {
|
|
log.Fatalf("Failed to load configuration: %v", err)
|
|
}
|
|
|
|
// Set default InstallDir based on OS if not configured
|
|
if cfg.InstallDir == "" {
|
|
if runtime.GOOS == "windows" {
|
|
cfg.InstallDir = `C:\RustServer`
|
|
} else {
|
|
cfg.InstallDir = "/opt/rustserver"
|
|
}
|
|
}
|
|
|
|
log.Printf("Configuration loaded:")
|
|
log.Printf(" NATS URL: %s", cfg.NATSUrl)
|
|
log.Printf(" License ID: %s", cfg.LicenseID)
|
|
log.Printf(" Game Server Path: %s", cfg.GameServerPath)
|
|
log.Printf(" SteamCMD Path: %s", cfg.SteamCMDPath)
|
|
log.Printf(" Install Dir: %s", cfg.InstallDir)
|
|
log.Printf(" RCON Port: %d", cfg.RconPort)
|
|
log.Printf(" Heartbeat Interval: %ds", cfg.HeartbeatInterval)
|
|
|
|
// Create context with signal handling for graceful shutdown
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
// Connect to NATS
|
|
log.Println("Connecting to NATS...")
|
|
nc, err := client.Connect(cfg.NATSUrl, cfg.NATSToken)
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to NATS: %v", err)
|
|
}
|
|
defer nc.Close()
|
|
|
|
log.Println("NATS connection established")
|
|
|
|
// Initialize daemon configuration
|
|
daemonCfg := &app.DaemonConfig{
|
|
LicenseID: cfg.LicenseID,
|
|
HeartbeatInterval: time.Duration(cfg.HeartbeatInterval) * time.Second,
|
|
SteamCMDPath: cfg.SteamCMDPath,
|
|
GameServerPath: cfg.GameServerPath,
|
|
GameServerArgs: cfg.GameServerArgs,
|
|
Version: version,
|
|
InstallDir: cfg.InstallDir,
|
|
RconPort: cfg.RconPort,
|
|
RconPassword: cfg.RconPassword,
|
|
}
|
|
|
|
// Start daemon
|
|
daemon, err := app.NewDaemon(nc, daemonCfg)
|
|
if err != nil {
|
|
log.Fatalf("Failed to initialize daemon: %v", err)
|
|
}
|
|
|
|
log.Println("Daemon initialized, starting main loop...")
|
|
|
|
// Run daemon until context is cancelled
|
|
if err := daemon.Run(ctx); err != nil {
|
|
log.Fatalf("Daemon error: %v", err)
|
|
}
|
|
|
|
log.Println("Companion agent shutdown complete")
|
|
}
|