GSP-Agent-Windows/Installer/Configure-GSP-WindowsAgent.ps1
2026-06-10 19:54:11 -04:00

250 lines
7.5 KiB
PowerShell

# GSP Windows Agent Configuration Script
# This script processes Cfg/*.default files and creates proper Perl configs
param(
[string]$AgentPath = "C:\GSP\Agent",
[string]$InstallPath = "C:\GSP"
)
$Script:CfgDir = Join-Path $AgentPath "OGP64\OGP\Cfg"
$Script:LogsDir = Join-Path $InstallPath "logs"
$Script:ConfigLogFile = Join-Path $Script:LogsDir "config.log"
function Log-Message {
param([string]$Message, [switch]$IsError = $false)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logLine = "[$timestamp] $Message"
if ($IsError) {
Write-Host $logLine -ForegroundColor Red
} else {
Write-Host $logLine
}
Add-Content -Path $Script:ConfigLogFile -Value $logLine -Force
}
function Read-ConfigDefaults {
param([string]$FilePath)
if (-not (Test-Path $FilePath)) {
return @{}
}
$content = Get-Content $FilePath -Raw
$config = @{}
# Parse Perl hash syntax: key => 'value',
if ($content -match '%Cfg::\w+\s*=\s*\((.*)\);') {
$hashContent = $matches[1]
# Match key => 'value' or key => value patterns
$regex = "(\w+)\s*=>\s*['\"]?([^'\"]*)['\"]?(?:,|$)"
[regex]::Matches($hashContent, $regex) | ForEach-Object {
$key = $_.Groups[1].Value
$value = $_.Groups[2].Value.Trim()
$config[$key] = $value
}
}
return $config
}
function Ask-ConfigValue {
param(
[string]$Key,
[string]$DefaultValue,
[string]$Description = ""
)
$prompt = $Key
if ($DefaultValue) {
$prompt += " [default: $DefaultValue]"
}
$prompt += ": "
if ($Description) {
Write-Host $Description -ForegroundColor Cyan
}
$value = Read-Host $prompt
if ([string]::IsNullOrEmpty($value)) {
return $DefaultValue
}
return $value
}
function Configure-ConfigPm {
Log-Message "Configuring Config.pm..."
$defaultFile = Join-Path $Script:CfgDir "Config.pm.default"
$configFile = Join-Path $Script:CfgDir "Config.pm"
if (-not (Test-Path $defaultFile)) {
Log-Message "ERROR: Config.pm.default not found" -IsError $true
return $false
}
# If config already exists, preserve it
if (Test-Path $configFile) {
Log-Message "Config.pm already exists, preserving it"
return $true
}
Write-Host ""
Write-Host "=== Configuring Config.pm ===" -ForegroundColor Green
Write-Host "This file contains critical settings for agent communication"
Write-Host ""
# Read defaults
$defaults = Read-ConfigDefaults $defaultFile
# Ask for key values
$configs = @{}
$configs["listen_ip"] = Ask-ConfigValue "listen_ip" ($defaults["listen_ip"] ?? "0.0.0.0") "Agent listen IP address (0.0.0.0 = all interfaces)"
$configs["listen_port"] = Ask-ConfigValue "listen_port" ($defaults["listen_port"] ?? "12679") "Agent listen port"
$configs["key"] = Ask-ConfigValue "key" ($defaults["key"] ?? "CHANGE_ME_PANEL_AGENT_KEY") "Encryption key (must match Panel configuration)"
# Optional settings
$configs["logfile"] = "/OGP/ogp_agent.log"
$configs["version"] = $defaults["version"] ?? "v1.4"
$configs["steam_license"] = $defaults["steam_license"] ?? "Accept"
$configs["sudo_password"] = ""
$configs["web_admin_api_key"] = ""
$configs["web_api_url"] = ""
$configs["steam_dl_limit"] = $defaults["steam_dl_limit"] ?? "0"
# Write config file
try {
$content = "`%Cfg::Config = (`n"
foreach ($key in $configs.Keys | Sort-Object) {
$value = $configs[$key]
if ($key -eq "key") {
# Key is especially important - always show with quotes
$content += "`t$key => '$value',`n"
} else {
$content += "`t$key => '$value',`n"
}
}
$content += ");`n`n1;`n"
Set-Content -Path $configFile -Value $content -Encoding UTF8
Log-Message "Config.pm created successfully"
return $true
} catch {
Log-Message "ERROR creating Config.pm: $_" -IsError $true
return $false
}
}
function Configure-PreferencesPm {
Log-Message "Configuring Preferences.pm..."
$defaultFile = Join-Path $Script:CfgDir "Preferences.pm.default"
$configFile = Join-Path $Script:CfgDir "Preferences.pm"
if (-not (Test-Path $defaultFile)) {
Log-Message "WARNING: Preferences.pm.default not found"
return $false
}
# If preferences already exist, preserve them
if (Test-Path $configFile) {
Log-Message "Preferences.pm already exists, preserving it"
return $true
}
Write-Host ""
Write-Host "=== Configuring Preferences.pm ===" -ForegroundColor Green
Write-Host "This file contains optional agent preferences"
Write-Host ""
try {
Copy-Item -Path $defaultFile -Destination $configFile -Force
Log-Message "Preferences.pm created from defaults"
return $true
} catch {
Log-Message "ERROR creating Preferences.pm: $_" -IsError $true
return $false
}
}
function Configure-BashPrefs {
Log-Message "Configuring bash_prefs.cfg..."
$defaultFile = Join-Path $Script:CfgDir "bash_prefs.cfg.default"
$configFile = Join-Path $Script:CfgDir "bash_prefs.cfg"
if (-not (Test-Path $defaultFile)) {
Log-Message "WARNING: bash_prefs.cfg.default not found"
return $false
}
# If bash prefs already exist, preserve them
if (Test-Path $configFile) {
Log-Message "bash_prefs.cfg already exists, preserving it"
return $true
}
try {
Copy-Item -Path $defaultFile -Destination $configFile -Force
Log-Message "bash_prefs.cfg created from defaults"
return $true
} catch {
Log-Message "ERROR creating bash_prefs.cfg: $_" -IsError $true
return $false
}
}
function Main {
Log-Message "========================================="
Log-Message "GSP Windows Agent Configuration"
Log-Message "========================================="
Log-Message "Agent Path: $AgentPath"
Log-Message "Config Directory: $Script:CfgDir"
Log-Message ""
if (-not (Test-Path $Script:CfgDir)) {
Log-Message "ERROR: Configuration directory not found: $Script:CfgDir" -IsError $true
exit 1
}
# Check for necessary permissions
try {
$testFile = Join-Path $Script:CfgDir ".write_test"
"test" | Out-File $testFile
Remove-Item $testFile -Force
} catch {
Log-Message "ERROR: No write permission to config directory" -IsError $true
exit 1
}
# Configure each file
$configResults = @{
"Config.pm" = Configure-ConfigPm
"Preferences.pm" = Configure-PreferencesPm
"bash_prefs.cfg" = Configure-BashPrefs
}
# Summary
Log-Message ""
Log-Message "========================================="
Log-Message "Configuration Complete"
Log-Message "========================================="
$successCount = ($configResults.Values | Where-Object { $_ -eq $true }).Count
Write-Host "Successfully configured: $successCount/$($configResults.Count) files"
foreach ($file in $configResults.Keys) {
$status = $configResults[$file] ? "" : ""
Write-Host " $status $file"
}
Log-Message ""
Log-Message "Configuration log: $Script:ConfigLogFile"
}
Main