diff --git a/.gitignore b/.gitignore index cf8b8e6..802b3bb 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ logs/ # Database *.db *.sqlite3 +instance/ # Temporary files *.tmp @@ -80,6 +81,10 @@ coverage.xml temp_*.py temp_*.ps1 temp_*.sh +check_*.py +fix_*.py +parse_*.ps1 +find_*.py # AI-generated analysis docs AI_DOCS/ diff --git a/app/factory.py b/app/factory.py index 36ddc63..3a1e908 100644 --- a/app/factory.py +++ b/app/factory.py @@ -90,8 +90,10 @@ def create_app(settings_path=None): _setup_logging(settings) + db_host = settings['database']['host'] + db_config = { - 'host': settings['database']['host'], + 'host': db_host, 'port': settings['database']['port'], 'user': settings['database']['user'], 'password': settings['database']['password'], @@ -115,7 +117,7 @@ def create_app(settings_path=None): logging.warning(f"Could not create indexes: {e}") ssh_service = SSHService( - host=settings['server']['host'], + host=settings['server'].get('local_ip', settings['server']['host']), user=settings['server']['user'], ssh_key=settings['server'].get('ssh_key') ) @@ -133,7 +135,7 @@ def create_app(settings_path=None): admin_svc = AdminService(db_service, ssh_service) updater_svc = UpdateService(base_dir) director_svc = DirectorService( - host='127.0.0.1', + host=settings['server'].get('local_ip', '127.0.0.1'), node_port=settings.get('director', {}).get('port', 32479), k8s_service=k8s_service, ssh_service=ssh_service, diff --git a/check_bounds.py b/check_bounds.py deleted file mode 100644 index f8f8df2..0000000 --- a/check_bounds.py +++ /dev/null @@ -1,52 +0,0 @@ -import sys -sys.path.insert(0, '.') -from app.config import load_settings -from app.services.database import DatabaseService -import re - -settings = load_settings() -db_config = { - 'host': settings['database']['host'], - 'port': settings['database']['port'], - 'user': settings['database']['user'], - 'password': settings['database']['password'], - 'database': settings['database']['name'], -} -db = DatabaseService(db_config, 2, 10) - -# Get count by map -counts = db.query(''' - SELECT map, COUNT(*) as cnt - FROM dune.actors - WHERE transform IS NOT NULL - GROUP BY map - ORDER BY cnt DESC -''') - -print('Map coordinate counts:') -for c in counts: - print(f" {c['map']}: {c['cnt']} items") - -# Get bounds for HaggaBasin (seems to have more) -print('\n=== HaggaBasin bounds ===') -coords = db.query(''' - SELECT transform FROM dune.actors - WHERE map = 'HaggaBasin' AND transform IS NOT NULL - LIMIT 500 -''') - -points = [] -for a in coords: - t = a.get('transform') - if t: - pos_match = re.search(r'\(([0-9.e+-]+),([0-9.e+-]+),([0-9.e+-]+)\)', str(t)) - if pos_match: - points.append((float(pos_match.group(1)), float(pos_match.group(2)))) - -if points: - x_vals = [p[0] for p in points] - y_vals = [p[1] for p in points] - print(f'Min X: {min(x_vals):.1f}, Max X: {max(x_vals):.1f}') - print(f'Min Y: {min(y_vals):.1f}, Max Y: {max(y_vals):.1f}') - print(f'X range: {max(x_vals) - min(x_vals):.1f}') - print(f'Y range: {max(y_vals) - min(y_vals):.1f}') \ No newline at end of file diff --git a/clean-ca-certs.bat b/clean-ca-certs.bat deleted file mode 100644 index 07f13f7..0000000 --- a/clean-ca-certs.bat +++ /dev/null @@ -1,46 +0,0 @@ -@echo off -REM Clean up all Dune Dashboard CA certificates and reinstall the current one -REM Must be run as Administrator - -setlocal -set CERT_PATH=%~dp0ssl\ca.pem - -echo ============================================================ -echo Dune Dashboard - CA Certificate Cleanup -echo ============================================================ -echo. - -if not exist "%CERT_PATH%" ( - echo ERROR: CA certificate not found at %CERT_PATH% - echo Run setup.bat first to generate the CA certificate. - pause - exit /b 1 -) - -echo Removing all existing Dune Dashboard CA certificates... -echo. - -REM Remove from CurrentUser store -powershell -NoProfile -Command "$store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root','CurrentUser'); $store.Open('ReadWrite'); $certs = $store.Certificates | Where-Object { $_.Subject -like '*Dune Dashboard*' }; Write-Host \"Found $($certs.Count) in CurrentUser\"; foreach ($c in $certs) { $store.Remove($c); Write-Host \" Removed: $($c.Thumbprint)\" }; $store.Close()" - -REM Remove from LocalMachine store -powershell -NoProfile -Command "$store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root','LocalMachine'); $store.Open('ReadWrite'); $certs = $store.Certificates | Where-Object { $_.Subject -like '*Dune Dashboard*' }; Write-Host \"Found $($certs.Count) in LocalMachine\"; foreach ($c in $certs) { $store.Remove($c); Write-Host \" Removed: $($c.Thumbprint)\" }; $store.Close()" - -echo. -echo Installing fresh CA certificate... -echo. - -certutil -addstore -f "Root" "%CERT_PATH%" - -if %errorlevel% equ 0 ( - echo. - echo SUCCESS: CA certificate cleaned and reinstalled. - echo Restart your browser for the change to take effect. -) else ( - echo. - echo FAILED: Could not install certificate. - echo Make sure you are running this script as Administrator. -) - -echo. -pause diff --git a/debug_coords.py b/debug_coords.py deleted file mode 100644 index 7f9c7d3..0000000 --- a/debug_coords.py +++ /dev/null @@ -1,39 +0,0 @@ -import sys -sys.path.insert(0, '.') -from app.config import load_settings -from app.services.database import DatabaseService - -settings = load_settings() -db_config = { - 'host': settings['database']['host'], - 'port': settings['database']['port'], - 'user': settings['database']['user'], - 'password': settings['database']['password'], - 'database': settings['database']['name'], -} -db = DatabaseService(db_config, 2, 10) - -# Check sample transform data for players -sample = db.query(''' - SELECT id, class, map, transform FROM dune.actors - WHERE class LIKE '%DunePlayerCharacter_%' - LIMIT 3 -''') -for s in sample: - print('Class:', s['class']) - print('Map:', s['map']) - print('Transform:', s['transform'][:500] if s['transform'] else 'None') - print('---') - -# Also check vehicles -print('\n=== Vehicles ===') -veh = db.query(''' - SELECT id, class, map, transform FROM dune.actors - WHERE class LIKE '%Vehicle%' - LIMIT 2 -''') -for v in veh: - print('Class:', v['class']) - print('Map:', v['map']) - print('Transform:', v['transform'][:500] if v['transform'] else 'None') - print('---') \ No newline at end of file diff --git a/install-ca-cert.bat b/install-ca-cert.bat deleted file mode 100644 index 5904f56..0000000 --- a/install-ca-cert.bat +++ /dev/null @@ -1,33 +0,0 @@ -@echo off -REM Install Dune Dashboard CA certificate into Windows Trusted Root CA store -REM Must be run as Administrator - -setlocal -set CERT_PATH=%~dp0ssl\ca.pem - -if not exist "%CERT_PATH%" ( - echo ERROR: CA certificate not found at %CERT_PATH% - echo Run setup.bat first to generate the CA certificate. - pause - exit /b 1 -) - -echo Installing Dune Dashboard CA certificate... -echo. - -certutil -addstore -f "Root" "%CERT_PATH%" - -if %errorlevel% equ 0 ( - echo. - echo SUCCESS: CA certificate installed to Trusted Root Certification Authorities. - echo Browser warnings for https://localhost:5050 should now be gone. - echo. - echo Restart your browser for the change to take effect. -) else ( - echo. - echo FAILED: Could not install certificate. - echo Make sure you are running this script as Administrator. -) - -echo. -pause diff --git a/launcher.ps1 b/launcher.ps1 index 5709561..14e5920 100644 --- a/launcher.ps1 +++ b/launcher.ps1 @@ -25,11 +25,20 @@ function Show-Menu { Write-Host "" Write-Host " [2] Run Setup" -ForegroundColor White Write-Host " Configure the dashboard for the first time, or reconfigure." -ForegroundColor DarkGray + Write-Host " Sets up SSH, SSL, firewall rules, and saves settings.yaml." -ForegroundColor DarkGray Write-Host " WARNING: Re-running setup will wipe your current settings." -ForegroundColor Yellow Write-Host "" Write-Host " [3] Run Diagnostics" -ForegroundColor White Write-Host " Check your system for common issues that could block the dashboard." -ForegroundColor DarkGray Write-Host "" + Write-Host " [4] Install CA Certificate" -ForegroundColor White + Write-Host " Install the local CA certificate into Windows Trusted Root store." -ForegroundColor DarkGray + Write-Host " Removes browser SSL warnings on this machine only." -ForegroundColor DarkGray + Write-Host "" + Write-Host " [5] Clean & Reinstall CA Certificate" -ForegroundColor White + Write-Host " Remove all old Dune Dashboard CA certificates and install a fresh one." -ForegroundColor DarkGray + Write-Host " Use this if you have duplicate or expired CA certificates." -ForegroundColor DarkGray + Write-Host "" Write-Host " [Q] Quit" -ForegroundColor White Write-Host "" } @@ -409,9 +418,125 @@ print(json.dumps(s)) Read-Host " " } +function Install-CaCert { + $CaCertPath = Join-Path $ProjectRoot "ssl\ca.pem" + if (-not (Test-Path $CaCertPath)) { + Write-Host "" + Write-Host " [ERROR] CA certificate not found at ssl\ca.pem" -ForegroundColor Red + Write-Host "" + Write-Host " The CA certificate hasn't been generated yet." -ForegroundColor Yellow + Write-Host " Run Setup first to generate the local CA certificate." -ForegroundColor Yellow + return + } + + Write-Host "" + Write-Host " Installing Dune Dashboard CA certificate..." -ForegroundColor Yellow + Write-Host "" + + $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + if (-not $isAdmin) { + Write-Host " [WARN] Not running as Administrator." -ForegroundColor Yellow + Write-Host " Starting a new elevated PowerShell window to install the CA cert..." -ForegroundColor Yellow + Write-Host "" + $elevatedScript = "certutil -addstore -f 'Root' '$CaCertPath'; Write-Host ''; Read-Host 'Press Enter to close'" + Start-Process powershell -ArgumentList "-NoProfile", "-Command", $elevatedScript -Verb RunAs -Wait + Write-Host " CA certificate installation complete." -ForegroundColor Green + } else { + certutil -addstore -f "Root" $CaCertPath + if ($LASTEXITCODE -eq 0) { + Write-Host " CA certificate installed to Trusted Root Certification Authorities." -ForegroundColor Green + } else { + Write-Host " [WARN] certutil failed. Try running as Administrator." -ForegroundColor Yellow + } + } + Write-Host "" + Write-Host " Restart your browser for the change to take effect." -ForegroundColor Cyan + Write-Host "" + Write-Host " Press Enter to return to the main menu..." -ForegroundColor Cyan + Read-Host " " +} + +function Clean-CaCerts { + $CaCertPath = Join-Path $ProjectRoot "ssl\ca.pem" + if (-not (Test-Path $CaCertPath)) { + Write-Host "" + Write-Host " [ERROR] CA certificate not found at ssl\ca.pem" -ForegroundColor Red + Write-Host "" + Write-Host " The CA certificate hasn't been generated yet." -ForegroundColor Yellow + Write-Host " Run Setup first to generate the local CA certificate." -ForegroundColor Yellow + return + } + + Write-Host "" + Write-Host "============================================================" -ForegroundColor Cyan + Write-Host " Dune Dashboard - CA Certificate Cleanup" -ForegroundColor Cyan + Write-Host "============================================================" -ForegroundColor Cyan + Write-Host "" + + Write-Host " Removing all existing Dune Dashboard CA certificates..." -ForegroundColor Yellow + Write-Host "" + + try { + $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root', 'CurrentUser') + $store.Open('ReadWrite') + $certs = $store.Certificates | Where-Object { $_.Subject -like '*Dune Dashboard*' } + Write-Host " Found $($certs.Count) in CurrentUser store" -ForegroundColor DarkGray + foreach ($c in $certs) { + $store.Remove($c) + Write-Host " Removed: $($c.Thumbprint)" -ForegroundColor DarkGray + } + $store.Close() + } catch { + Write-Host " [WARN] Could not clean CurrentUser store: $_" -ForegroundColor Yellow + } + + try { + $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root', 'LocalMachine') + $store.Open('ReadWrite') + $certs = $store.Certificates | Where-Object { $_.Subject -like '*Dune Dashboard*' } + Write-Host " Found $($certs.Count) in LocalMachine store" -ForegroundColor DarkGray + foreach ($c in $certs) { + $store.Remove($c) + Write-Host " Removed: $($c.Thumbprint)" -ForegroundColor DarkGray + } + $store.Close() + } catch { + Write-Host " [WARN] Could not clean LocalMachine store (may require Administrator): $_" -ForegroundColor Yellow + } + + Write-Host "" + Write-Host " Installing fresh CA certificate..." -ForegroundColor Yellow + Write-Host "" + + $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + if (-not $isAdmin) { + Write-Host " [WARN] Not running as Administrator." -ForegroundColor Yellow + Write-Host " Starting a new elevated PowerShell window to install the CA cert..." -ForegroundColor Yellow + Write-Host "" + $elevatedScript = "certutil -addstore -f 'Root' '$CaCertPath'; Write-Host ''; Read-Host 'Press Enter to close'" + Start-Process powershell -ArgumentList "-NoProfile", "-Command", $elevatedScript -Verb RunAs -Wait + } else { + certutil -addstore -f "Root" $CaCertPath + } + + if ($LASTEXITCODE -eq 0) { + Write-Host " CA certificate cleaned and reinstalled successfully." -ForegroundColor Green + } else { + Write-Host " [WARN] Could not install certificate. Try running as Administrator." -ForegroundColor Yellow + } + + Write-Host "" + Write-Host " Restart your browser for the change to take effect." -ForegroundColor Cyan + Write-Host "" + Write-Host " Press Enter to return to the main menu..." -ForegroundColor Cyan + Read-Host " " +} + function Run-Setup { Write-Host "" - Write-Host " Starting setup..." -ForegroundColor Yellow + Write-Host "============================================================" -ForegroundColor Cyan + Write-Host " Dune Awakening Dashboard - Setup" -ForegroundColor Cyan + Write-Host "============================================================" -ForegroundColor Cyan Write-Host "" # Check if this is a re-run @@ -453,8 +578,748 @@ function Run-Setup { Write-Host "" } - # Run the actual setup script - & (Join-Path $ProjectRoot "setup.ps1") + # Check Python + Write-Host "[1/6] Checking Python..." -ForegroundColor Yellow + try { + $pythonVersion = python --version 2>&1 + Write-Host " Found: $pythonVersion" -ForegroundColor Green + } catch { + Write-Host " [ERROR] Python not found. Install Python 3.8+ first." -ForegroundColor Red + return + } + + # Install dependencies + Write-Host "" + Write-Host "[2/6] Installing dependencies..." -ForegroundColor Yellow + pip install -r (Join-Path $ProjectRoot "requirements.txt") --quiet + if ($LASTEXITCODE -eq 0) { + Write-Host " Dependencies installed" -ForegroundColor Green + } else { + Write-Host " [WARN] Some packages may have failed. Continuing..." -ForegroundColor Yellow + } + + # Configure SSH key + Write-Host "" + Write-Host "[3/6] Configuring SSH key..." -ForegroundColor Yellow + $TargetKey = Join-Path $ProjectRoot "internal-scripts\ssh\sshKey" + $SshKeyPaths = @( + "$env:LOCALAPPDATA\DuneAwakeningServer\sshKey", + "$env:TEMP\dune-tunnel-key", + "$env:TEMP\dune-awakening-server-sshKey", + $TargetKey, + (Join-Path (Split-Path $ProjectRoot) "internal-scripts\ssh\sshKey") + ) + $FoundKey = $null + foreach ($path in $SshKeyPaths) { + if (Test-Path $path) { + $FoundKey = $path + break + } + } + + function Fix-SshKeyPermissions($keyPath) { + if (-not (Test-Path $keyPath)) { return } + $acl = Get-Acl $keyPath + $acl.SetAccessRuleProtection($true, $false) + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow") + $acl.SetAccessRule($rule) + $adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators", "FullControl", "Allow") + $acl.SetAccessRule($adminRule) + Set-Acl -Path $keyPath -AclObject $acl + } + + if (-not $FoundKey) { + Write-Host " [INFO] No SSH key found in standard locations." -ForegroundColor Yellow + $userKey = Read-Host " Enter path to your SSH key file (or press Enter to skip)" + if ($userKey -and (Test-Path $userKey)) { + $targetDir = Join-Path $ProjectRoot "internal-scripts\ssh" + if (-not (Test-Path $targetDir)) { + New-Item -ItemType Directory -Path $targetDir -Force | Out-Null + } + Copy-Item $userKey $TargetKey -Force + Fix-SshKeyPermissions $TargetKey + $FoundKey = $TargetKey + Write-Host " Key copied to internal-scripts\ssh\sshKey" -ForegroundColor Green + } else { + Write-Host " Skipping SSH key configuration." -ForegroundColor Yellow + $FoundKey = $null + } + } else { + if ($FoundKey -notlike "*internal-scripts*") { + $targetDir = Join-Path $ProjectRoot "internal-scripts\ssh" + if (-not (Test-Path $targetDir)) { + New-Item -ItemType Directory -Path $targetDir -Force | Out-Null + } + Copy-Item $FoundKey $TargetKey -Force + Fix-SshKeyPermissions $TargetKey + $FoundKey = $TargetKey + Write-Host " Key copied to internal-scripts\ssh\sshKey for portability" -ForegroundColor Green + } else { + Write-Host " Found: $FoundKey" -ForegroundColor Green + } + } + + # Auto-detect server settings + Write-Host "" + Write-Host "[4/6] Detecting server settings..." -ForegroundColor Yellow + + $VmHost = "YOUR_SERVER_IP" + $HostIp = $null + $ServerUser = "dune" + $K8sNamespace = "" + $DashboardPort = "5050" + $DbPort = "15433" + $DbHost = "127.0.0.1" + $DirectorPort = "32479" + $FileBrowserPort = "18888" + $AuthUser = "admin" + $AuthPass = "" + + function Test-IsLocalIP($ip) { + if ($ip -match '^192\.168\.') { return $true } + if ($ip -match '^10\.') { return $true } + if ($ip -match '^172\.(1[6-9]|2[0-9]|3[0-1])\.') { return $true } + return $false + } + + $vmIp = $null + try { + $vmAdapter = Get-VMNetworkAdapter -VMName 'dune-awakening' -ErrorAction SilentlyContinue + if ($vmAdapter -and $vmAdapter.IPAddresses) { + $localIp = $vmAdapter.IPAddresses | Where-Object { Test-IsLocalIP $_ } + if ($localIp) { + $vmIp = $localIp[0] + Write-Host " Detected local Hyper-V VM IP: $vmIp" -ForegroundColor Green + } + } + } catch { } + + if (-not $vmIp) { + $KnownHosts = Join-Path $env:USERPROFILE ".ssh\known_hosts" + if (Test-Path $KnownHosts) { + $content = Get-Content $KnownHosts + $localIps = @() + $publicIps = @() + foreach ($line in $content) { + if ($line -match '\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b') { + $ip = $matches[1] + if (Test-IsLocalIP $ip) { + $localIps += $ip + } else { + $publicIps += $ip + } + } + } + if ($localIps.Count -gt 0) { + $vmIp = $localIps[-1] + Write-Host " Detected local IP from SSH history: $vmIp" -ForegroundColor Green + } elseif ($publicIps.Count -gt 0) { + $vmIp = $publicIps[-1] + Write-Host " Detected IP from SSH history: $vmIp (verify this is your local VM)" -ForegroundColor Yellow + } + } + } + + if ($vmIp) { $VmHost = $vmIp } + + try { + $HostIp = (Invoke-WebRequest -Uri "https://api.ipify.org" -UseBasicParsing -TimeoutSec 5).Content.Trim() + } catch { + $localIps = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike 'Loopback*' -and $_.PrefixOrigin -ne 'WellKnown' } | Select-Object -ExpandProperty IPAddress + if ($localIps.Count -gt 0) { $HostIp = $localIps[0] } + } + + Write-Host "" + Write-Host " VM External IP - used for SSH connection to your game server" -ForegroundColor Cyan + $val = Read-Host " VM Host [$VmHost]" + if ($val) { $VmHost = $val } + + Write-Host "" + Write-Host " This Machine's External IP - used for SSL certificate so remote access works" -ForegroundColor Cyan + if ($HostIp) { $hostIpHint = "$HostIp (auto-detected)" } else { $hostIpHint = "Your Windows machine's external IP" } + $val = Read-Host " Host IP [$hostIpHint]" + if ($val) { $HostIp = $val } + elseif (-not $HostIp) { $HostIp = $null } + + Write-Host "" + Write-Host " Domain Name (optional) - for a publicly trusted SSL certificate via Let's Encrypt." -ForegroundColor Cyan + Write-Host " This removes browser warnings for ALL visitors. Requires:" -ForegroundColor Cyan + Write-Host " - A domain (or subdomain) pointing to this machine's external IP" -ForegroundColor Cyan + Write-Host " - Port 80 accessible from the internet" -ForegroundColor Cyan + $val = Read-Host " Domain (leave blank to use local CA instead)" + $DomainName = $null + if ($val -and $val -match '\S') { $DomainName = $val } + + $LeEmail = $null + if ($DomainName) { + Write-Host "" + Write-Host " Email address is required by Let's Encrypt for expiry notifications." -ForegroundColor Cyan + $val = Read-Host " Email Address" + if ($val -and $val -match '\S') { $LeEmail = $val } + } + + # Try SSH with the confirmed VM IP to auto-detect namespace + if ($FoundKey -and $VmHost -ne "YOUR_SERVER_IP") { + Write-Host " Testing SSH connection..." -ForegroundColor Yellow + Write-Host " (Fresh VMs may need time to initialize SSH keys - will retry up to 60s)" -ForegroundColor DarkGray + Write-Host "" + + $sshOk = $false + for ($i = 0; $i -lt 12; $i++) { + try { + $testOut = ssh -i "$FoundKey" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 -o BatchMode=yes dune@$VmHost "echo ok" 2>$null + $testExit = $LASTEXITCODE + if ($testOut -match "ok" -and $testExit -eq 0) { + $sshOk = $true + break + } + } catch {} + Write-Host " Attempt $($i+1)/12 - SSH not ready yet, waiting 5s..." -ForegroundColor DarkGray + Start-Sleep -Seconds 5 + } + + if ($sshOk) { + Write-Host " SSH connection OK" -ForegroundColor Green + try { + $nsOut = ssh -i "$FoundKey" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes dune@$VmHost "sudo kubectl get namespaces -o name" 2>$null + } catch { $nsOut = $null } + if ($nsOut) { + foreach ($line in $nsOut -split "`n") { + $ns = $line -replace 'namespace/', '' + if ($ns -match '^funcom-seabass-') { + $K8sNamespace = $ns + Write-Host " Auto-detected namespace: $K8sNamespace" -ForegroundColor Green + break + } + } + } + } else { + Write-Host " SSH connection failed after 60s of retries" -ForegroundColor Yellow + Write-Host "" + Write-Host " This is normal on a fresh VM that hasn't finished initializing." -ForegroundColor Cyan + Write-Host " You can still continue setup - just enter the namespace manually below." -ForegroundColor Cyan + Write-Host "" + Write-Host " Once the VM is ready, copy your public key to enable SSH:" -ForegroundColor Cyan + Write-Host "" + $pubKeyContent = $null + try { $pubKeyContent = Get-Content "$FoundKey.pub" -ErrorAction SilentlyContinue } catch {} + if ($pubKeyContent) { + Write-Host " ssh dune@$VmHost 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo `"$pubKeyContent`" >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'" -ForegroundColor DarkGray + } else { + Write-Host " ssh dune@$VmHost 'mkdir -p ~/.ssh && chmod 700 ~/.ssh'" -ForegroundColor DarkGray + Write-Host " Then paste your public key into ~/.ssh/authorized_keys" -ForegroundColor DarkGray + } + Write-Host "" + } + } + + # Interactive review/edit + Write-Host "" + Write-Host " Review settings (press Enter to accept, or type new value):" -ForegroundColor Cyan + Write-Host "" + + Write-Host " VM Host: $VmHost (IP of your game server VM, used for SSH)" + if ($HostIp) { Write-Host " Host IP: $HostIp (This machine's external IP, used for SSL cert)" } + + $val = Read-Host " Server User [$ServerUser] (SSH username for the VM)" + if ($val) { $ServerUser = $val } + + Write-Host " SSH Key Path: $FoundKey (Path to your private SSH key)" + + if (-not $K8sNamespace) { + Write-Host " [INFO] To find your namespace, run: ssh dune@YOUR_IP 'sudo kubectl get namespaces'" -ForegroundColor Cyan + $K8sHint = "funcom-seabass- (Kubernetes cluster namespace)" + } else { + $K8sHint = "$K8sNamespace (Kubernetes cluster namespace)" + } + + $val = Read-Host " K8s Namespace [$K8sHint]" + if ($val) { $K8sNamespace = $val } + elseif (-not $K8sNamespace) { $K8sNamespace = "" } + + $val = Read-Host " Dashboard Port [$DashboardPort] (Local web access port)" + if ($val) { $DashboardPort = $val } + + $val = Read-Host " DB Local Port [$DbPort] (Local database tunnel port)" + if ($val) { $DbPort = $val } + + $val = Read-Host " Director Port [$DirectorPort] (Director API port)" + if ($val) { $DirectorPort = $val } + + $val = Read-Host " FileBrowser Port [$FileBrowserPort] (File manager port)" + if ($val) { $FileBrowserPort = $val } + + $val = Read-Host " Auth Username [$AuthUser] (Dashboard login name)" + if ($val) { $AuthUser = $val } + + $val = Read-Host " Auth Password (required)" -AsSecureString + $AuthPass = [System.Net.NetworkCredential]::new('', $val).Password + if ($AuthPass) { } + else { + Write-Host " [ERROR] Password is required. Setup cancelled." -ForegroundColor Red + return + } + + # SSL certificate (Let's Encrypt or local CA) + Write-Host "" + Write-Host " Configuring SSL certificate..." -ForegroundColor Yellow + $CertDir = Join-Path $ProjectRoot "ssl" + if (-not (Test-Path $CertDir)) { New-Item -ItemType Directory -Path $CertDir -Force | Out-Null } + $CaCertPath = Join-Path $CertDir "ca.pem" + $CaKeyPath = Join-Path $CertDir "ca-key.pem" + $SslCertPath = Join-Path $CertDir "cert.pem" + $SslKeyPath = Join-Path $CertDir "key.pem" + + $UseLetsEncrypt = $false + $LeCertPath = $null + $LeKeyPath = $null + + if ($DomainName) { + Write-Host " Attempting Let's Encrypt certificate for $DomainName..." -ForegroundColor Yellow + + $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue + if (-not $certbotCmd) { + Write-Host " Installing certbot..." -ForegroundColor Yellow + + try { + winget install --id Certbot.Certbot -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null + $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue + } catch {} + + if (-not $certbotCmd) { + Write-Host " winget failed, trying pip..." -ForegroundColor Yellow + pip install certbot 2>$null | Out-Null + $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue + } + + if (-not $certbotCmd) { + $testCertbot = python -m certbot --version 2>$null + if ($LASTEXITCODE -eq 0) { + $certbotCmd = @{ Source = "python -m certbot" } + } + } + } + + if ($certbotCmd) { + $httpRedirect = Get-NetTCPConnection -LocalPort 80 -ErrorAction SilentlyContinue | Where-Object { $_.State -eq 'Listen' } + if ($httpRedirect) { + Write-Host " Temporarily stopping services on port 80 for certificate validation..." -ForegroundColor Yellow + } + + $certbotUseModule = ($certbotCmd.Source -eq "python") + + if ($certbotUseModule) { + $certbotArgs = @("-m", "certbot", "certonly", "--standalone", "-d", $DomainName, "--non-interactive", "--agree-tos") + if ($LeEmail) { + $certbotArgs += "--email" + $certbotArgs += $LeEmail + } else { + $certbotArgs += "--register-unsafely-without-email" + } + $certbotResult = Start-Process python -ArgumentList $certbotArgs -Wait -NoNewWindow -PassThru -RedirectStandardOutput "$env:TEMP\certbot-out.txt" + } else { + $certbotArgs = @( + "certonly", "--standalone", + "-d", $DomainName, + "--non-interactive", + "--agree-tos" + ) + if ($LeEmail) { + $certbotArgs += "--email" + $certbotArgs += $LeEmail + } else { + $certbotArgs += "--register-unsafely-without-email" + } + $certbotResult = Start-Process certbot -ArgumentList $certbotArgs -Wait -NoNewWindow -PassThru -RedirectStandardOutput "$env:TEMP\certbot-out.txt" + } + $certbotOutput = Get-Content "$env:TEMP\certbot-out.txt" -ErrorAction SilentlyContinue + + if ($certbotResult.ExitCode -eq 0) { + $UseLetsEncrypt = $true + $LeCertPath = "C:\ProgramData\certbot\live\$DomainName\fullchain.pem" + $LeKeyPath = "C:\ProgramData\certbot\live\$DomainName\privkey.pem" + + if (-not (Test-Path $LeCertPath)) { + $LeCertPath = "C:\Certbot\live\$DomainName\fullchain.pem" + $LeKeyPath = "C:\Certbot\live\$DomainName\privkey.pem" + } + + if (Test-Path $LeCertPath) { + Write-Host " Let's Encrypt certificate obtained successfully!" -ForegroundColor Green + Write-Host " Certificate for $DomainName is publicly trusted." -ForegroundColor Green + } else { + Write-Host " [WARN] Let's Encrypt succeeded but cert files not found at expected path." -ForegroundColor Yellow + Write-Host " Falling back to local CA." -ForegroundColor Yellow + $UseLetsEncrypt = $false + } + } else { + Write-Host " [WARN] Let's Encrypt certificate failed." -ForegroundColor Yellow + if ($certbotOutput) { $certbotOutput | ForEach-Object { Write-Host " $_" -ForegroundColor Yellow } } + Write-Host " Falling back to local CA certificate." -ForegroundColor Yellow + Write-Host " To retry, run setup again after installing certbot: winget install Certbot.Certbot" -ForegroundColor Yellow + } + } else { + Write-Host " [WARN] certbot not available. Falling back to local CA certificate." -ForegroundColor Yellow + Write-Host " To install certbot later, run: winget install Certbot.Certbot" -ForegroundColor Yellow + Write-Host " Then re-run setup to get a Let's Encrypt certificate." -ForegroundColor Yellow + } + } + + if (-not $UseLetsEncrypt) { + $CaCertPathPy = $CaCertPath -replace '\\', '/' + $CaKeyPathPy = $CaKeyPath -replace '\\', '/' + $SslCertPathPy = $SslCertPath -replace '\\', '/' + $SslKeyPathPy = $SslKeyPath -replace '\\', '/' + + $SanIps = @('127.0.0.1') + if ($VmHost -ne "YOUR_SERVER_IP") { $SanIps += $VmHost } + if ($HostIp -and $HostIp -notin $SanIps) { $SanIps += $HostIp } + + if (-not (Test-Path $CaCertPath) -or -not (Test-Path $CaKeyPath)) { + Write-Host " Generating local CA..." -ForegroundColor Yellow + python -c "from app.utils.ssl import generate_ca; generate_ca('$CaCertPathPy', '$CaKeyPathPy')" + } + + $SanIpsArray = "['" + ($SanIps -join "', '") + "']" + if ($VmHost -ne "YOUR_SERVER_IP") { $CommonName = $VmHost } else { $CommonName = "localhost" } + python -c "from app.utils.ssl import generate_cert; generate_cert('$SslCertPathPy', '$SslKeyPathPy', ca_cert_path='$CaCertPathPy', ca_key_path='$CaKeyPathPy', common_name='$CommonName', san_ips=$SanIpsArray, san_dns=['localhost'])" + $SslCert = "'$SslCertPath'" + $SslKey = "'$SslKeyPath'" + + Write-Host "" + Write-Host " A local CA certificate was generated (Let's Encrypt was not used)." -ForegroundColor Cyan + Write-Host " Installing it into the Trusted Root store will remove browser warnings on THIS machine only." -ForegroundColor Cyan + Write-Host " Visitors from other machines will still see a warning unless they also install the CA cert." -ForegroundColor Cyan + Write-Host " To get a publicly trusted certificate, re-run setup and provide a domain name." -ForegroundColor Cyan + Write-Host "" + $InstallCa = Read-Host " Install CA certificate now? Requires Administrator (y/N)" + if ($InstallCa -eq 'y' -or $InstallCa -eq 'Y') { + $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + if (-not $isAdmin) { + Write-Host "" + Write-Host " [WARN] Not running as Administrator." -ForegroundColor Yellow + Write-Host " Starting a new elevated PowerShell window to install the CA cert..." -ForegroundColor Yellow + Write-Host "" + $elevatedScript = "certutil -addstore -f 'Root' '$CaCertPath'; Write-Host ''; Read-Host 'Press Enter to close'" + Start-Process powershell -ArgumentList "-NoProfile", "-Command", $elevatedScript -Verb RunAs -Wait + Write-Host " CA certificate installation complete." -ForegroundColor Green + } else { + certutil -addstore -f "Root" $CaCertPath + if ($LASTEXITCODE -eq 0) { + Write-Host " CA certificate installed to Trusted Root Certification Authorities." -ForegroundColor Green + } else { + Write-Host " [WARN] certutil failed. Try running setup as Administrator." -ForegroundColor Yellow + } + } + Write-Host " Restart your browser for the change to take effect." -ForegroundColor Cyan + } else { + Write-Host " Skipped. You can install it later from the launcher menu (option 4)." -ForegroundColor Yellow + } + } else { + $SslCert = "'$LeCertPath'" + $SslKey = "'$LeKeyPath'" + } + + # Remote access + Write-Host "" + $RemoteAccess = Read-Host " Enable remote access? (y/N)" + $EnableRemote = ($RemoteAccess -eq 'y' -or $RemoteAccess -eq 'Y') + + if ($EnableRemote) { + $DashHost = "0.0.0.0" + Write-Host " Remote access enabled. The dashboard will bind to 0.0.0.0 so localhost, 127.0.0.1, LAN IP, and public IP can all work." -ForegroundColor Green + Write-Host " Dedicated server: allow TCP $DashboardPort in Windows Firewall/cloud firewall." -ForegroundColor Cyan + Write-Host " Home network: port-forward TCP $DashboardPort from your router to this machine's LAN IP." -ForegroundColor Cyan + } else { + $DashHost = "127.0.0.1" + Write-Host " Local-only access enabled. Use http(s)://localhost:$DashboardPort or http(s)://127.0.0.1:$DashboardPort." -ForegroundColor Green + } + + $HttpRedirect = $false + $HttpRedirectPort = 80 + if ($EnableRemote -and ($SslCert -ne "null") -and ($SslKey -ne "null")) { + Write-Host "" + Write-Host " Optional HTTP to HTTPS redirect:" -ForegroundColor Cyan + Write-Host " - Not required if you visit https://HOST:$DashboardPort directly." -ForegroundColor Cyan + Write-Host " - Useful if you want http://HOST to redirect automatically." -ForegroundColor Cyan + Write-Host " - Requires TCP port 80 to be free and forwarded/open." -ForegroundColor Cyan + $RedirectAnswer = Read-Host " Enable HTTP redirect on port 80? (y/N)" + $HttpRedirect = ($RedirectAnswer -eq 'y' -or $RedirectAnswer -eq 'Y') + } + + # Firewall rules + Write-Host "" + if ($EnableRemote) { + $FirewallPorts = @($DashboardPort) + if ($HttpRedirect) { $FirewallPorts = @(80) + $FirewallPorts } + $FirewallPortList = ($FirewallPorts -join ',') + Write-Host " Windows Firewall needs to allow incoming TCP port(s): $FirewallPortList" -ForegroundColor Cyan + Write-Host " This requires Administrator privileges." -ForegroundColor Cyan + Write-Host "" + } else { + $FirewallPorts = @() + $FirewallPortList = "" + Write-Host " Skipping Windows Firewall rules because remote access is disabled." -ForegroundColor Cyan + } + Write-Host "" + if ($EnableRemote) { + $SetupFirewall = Read-Host " Create firewall rules? (Y/n)" + } else { + $SetupFirewall = "n" + } + if ($SetupFirewall -eq 'n' -or $SetupFirewall -eq 'N') { + if ($EnableRemote) { Write-Host " Skipped. Remote access may be blocked by firewall." -ForegroundColor Yellow } + } else { + $fwRuleName = "DuneDashboard" + $fwExists = $null + try { $fwExists = Get-NetFirewallRule -DisplayName $fwRuleName -ErrorAction SilentlyContinue } catch {} + if ($fwExists) { + Write-Host " Firewall rule already exists." -ForegroundColor Green + } else { + $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + if (-not $isAdmin) { + Write-Host " Starting elevated PowerShell to add firewall rules..." -ForegroundColor Yellow + $fwScript = "New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $FirewallPortList; Write-Host ''; Read-Host 'Press Enter to close'" + Start-Process powershell -ArgumentList "-NoProfile", "-Command", $fwScript -Verb RunAs -Wait + Write-Host " Firewall rules added." -ForegroundColor Green + } else { + New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $FirewallPorts + Write-Host " Firewall rules added for TCP port(s): $FirewallPortList." -ForegroundColor Green + } + } + } + + # Let's Encrypt auto-renewal + if ($UseLetsEncrypt) { + Write-Host "" + Write-Host " Setting up Let's Encrypt auto-renewal..." -ForegroundColor Yellow + $taskName = "DuneDashboard-LeRenewal" + $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue + if ($existingTask) { + Write-Host " Renewal task already exists." -ForegroundColor Green + } else { + $renewAction = New-ScheduledTaskAction -Execute "certbot" -Argument "renew --quiet" -RunLevel Highest + $renewTrigger = New-ScheduledTaskTrigger -Daily -At 2am + $renewSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries + Register-ScheduledTask -TaskName $taskName -Action $renewAction -Trigger $renewTrigger -Settings $renewSettings -Description "Auto-renew Let's Encrypt certificate for Dune Dashboard" -ErrorAction SilentlyContinue | Out-Null + Write-Host " Auto-renewal scheduled (daily at 2 AM). Certificates renew automatically." -ForegroundColor Green + } + } + + # Firewall security hardening + Write-Host "" + Write-Host "[SECURITY] Firewall Hardening" -ForegroundColor Cyan + Write-Host "" + Write-Host " Your game server has services exposed to the internet without authentication." -ForegroundColor Yellow + Write-Host " This is a SECURITY RISK - these services are vulnerable to automated attacks." -ForegroundColor Yellow + Write-Host " PostgreSQL was ALREADY exploited by cryptocurrency mining malware." -ForegroundColor Red + Write-Host "" + Write-Host " Recommended: Block these ports so only localhost/VPN can reach them:" -ForegroundColor Cyan + Write-Host "" + Write-Host " [1] PostgreSQL (port 15432) - VULNERABLE (ALREADY EXPLOITED - default postgres/postgres)" -ForegroundColor Red + Write-Host " [2] File Browser (port 18888) - VULNERABLE (no auth - exposes server files)" -ForegroundColor Yellow + Write-Host " [3] Battlegroup Director (port 31820) - VULNERABLE (no auth - exposes server API)" -ForegroundColor Yellow + Write-Host "" + Write-Host " You can change these anytime from the Server page in the dashboard." -ForegroundColor Cyan + Write-Host "" + + $val = Read-Host " Block PostgreSQL (15432)? (Y/n)" + $BlockPostgres = -not ($val -eq 'n' -or $val -eq 'N') + if ($BlockPostgres) { Write-Host " Will block port 15432 (PostgreSQL)" -ForegroundColor Green } + else { Write-Host " Skipped - port 15432 will remain open to the internet" -ForegroundColor Red } + + Write-Host "" + $val = Read-Host " Block File Browser (18888)? (Y/n)" + $BlockFileBrowser = -not ($val -eq 'n' -or $val -eq 'N') + if ($BlockFileBrowser) { Write-Host " Will block port 18888 (File Browser)" -ForegroundColor Green } + else { Write-Host " Skipped - port 18888 will remain open to the internet" -ForegroundColor Yellow } + + Write-Host "" + $val = Read-Host " Block Battlegroup Director (31820)? (Y/n)" + $BlockDirector = -not ($val -eq 'n' -or $val -eq 'N') + if ($BlockDirector) { Write-Host " Will block port 31820 (Director)" -ForegroundColor Green } + else { Write-Host " Skipped - port 31820 will remain open to the internet" -ForegroundColor Yellow } + + # Save settings + Write-Host "" + Write-Host "[5/6] Saving settings..." -ForegroundColor Yellow + + $secret = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 32 | ForEach-Object {[char]$_}) + + Write-Host " Hashing password with Argon2..." -ForegroundColor Yellow + $pwScript = @" +import sys +from argon2 import PasswordHasher +ph = PasswordHasher() +print(ph.hash(sys.argv[1])) +"@ + $pwScript | Out-File -FilePath "$env:TEMP\hash_pw.py" -Encoding utf8 -Force + $hashResult = python "$env:TEMP\hash_pw.py" $AuthPass 2>$null + Remove-Item "$env:TEMP\hash_pw.py" -Force -ErrorAction SilentlyContinue + if ($hashResult -and $hashResult -match '^\$argon2') { + $AuthHash = $hashResult.Trim() + Write-Host " Password hashed successfully." -ForegroundColor Green + } else { + Write-Host " [ERROR] Failed to hash password. Argon2 may not be installed." -ForegroundColor Red + Write-Host " Run: pip install argon2-cffi" -ForegroundColor Yellow + return + } + + $sshKeyPath = if ($FoundKey) { $FoundKey -replace '\\', '\\' } else { "null" } + $leDomain = if ($DomainName) { $DomainName } else { "null" } + $leEmail = if ($LeEmail) { $LeEmail } else { "null" } + $vmLocalIp = if ($vmIp) { $vmIp } else { "null" } + $settingsContent = @" +server: + host: '$VmHost' + local_ip: '$vmLocalIp' + user: $ServerUser + ssh_key: '$sshKeyPath' + +dashboard: + host: $DashHost + port: $DashboardPort + debug: false + secret_key: $secret + ssl_cert: $SslCert + ssl_key: $SslKey + ssl_domain: $leDomain + ssl_email: $leEmail + http_redirect: $HttpRedirect + http_redirect_port: $HttpRedirectPort + +database: + host: $DbHost + port: $DbPort + user: postgres + password: postgres + name: dune + schema: dune + min_connections: 2 + max_connections: 10 + +kubernetes: + namespace: '$K8sNamespace' + battlegroup_script: /home/dune/.dune/bin/battlegroup + +director: + port: $DirectorPort + +filebrowser: + port: $FileBrowserPort + +firewall: + block_filebrowser: $BlockFileBrowser + block_director: $BlockDirector + block_postgres: $BlockPostgres + +cache: + chat_pod_ttl: 60 + chat_messages_ttl: 10 + static_data_ttl: 300 + +auth: + enabled: true + username: $AuthUser + password_hash: '$AuthHash' + +logging: + level: INFO + file: logs/dashboard.log + max_bytes: 10485760 + backup_count: 5 +"@ + + [System.IO.File]::WriteAllText((Join-Path $ProjectRoot "settings.yaml"), $settingsContent, [System.Text.UTF8Encoding]::new($false)) + Write-Host " Settings saved to settings.yaml" -ForegroundColor Green + + New-Item -ItemType Directory -Path (Join-Path $ProjectRoot "logs") -Force | Out-Null + + Write-Host "" + Write-Host "[6/6] Verifying setup..." -ForegroundColor Yellow + $verifyResult = python -W ignore -c "from app.config import load_settings; s = load_settings(); print('OK - settings loaded')" 2>$null + if ($verifyResult -match "OK") { + Write-Host " $verifyResult" -ForegroundColor Green + } else { + Write-Host " [WARN] Verification skipped." -ForegroundColor Yellow + } + + Write-Host "" + Write-Host "============================================================" -ForegroundColor Cyan + Write-Host " Setup Complete!" -ForegroundColor Cyan + Write-Host "============================================================" -ForegroundColor Cyan + Write-Host "" + + # Check if SSH key is valid for the server + $SshValid = $false + if ($FoundKey -and (Test-Path $FoundKey) -and $VmHost -ne "YOUR_SERVER_IP") { + $testOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "${ServerUser}@${VmHost}" "echo ok" 2>$null + $SshValid = ($testOut -eq "ok") + } + + if (-not $SshValid) { + if ($VmHost -eq "YOUR_SERVER_IP") { + Write-Host " Remember to edit settings.yaml with your server IP before starting." -ForegroundColor Yellow + } else { + Write-Host " SSH key is not configured or failed to connect." -ForegroundColor Yellow + } + Write-Host "" + Write-Host " Then start the dashboard with:" -ForegroundColor Yellow + Write-Host "" + Write-Host " .\launcher.ps1 (option 1)" -ForegroundColor Cyan + } else { + Write-Host " SSH connection verified." -ForegroundColor Green + + $portsToBlock = @() + if ($BlockFileBrowser) { $portsToBlock += 18888 } + if ($BlockDirector) { $portsToBlock += 31820 } + if ($BlockPostgres) { $portsToBlock += 15432 } + + if ($portsToBlock.Count -gt 0) { + Write-Host "" + Write-Host " Applying firewall rules to block ports: $($portsToBlock -join ', ')..." -ForegroundColor Yellow + + $tmpFwScript = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".sh") + $fwContent = "for PORT in $($portsToBlock -join ' '); do`n" + $fwContent += " if iptables -t mangle -C PREROUTING -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n" + $fwContent += " echo 'Port `$PORT already blocked'`n" + $fwContent += " else`n" + $fwContent += " iptables -I INPUT 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" + $fwContent += " iptables -I INPUT 2 -p tcp --dport `$PORT -j DROP`n" + $fwContent += " iptables -I FORWARD 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" + $fwContent += " iptables -I FORWARD 2 -p tcp --dport `$PORT -j DROP`n" + $fwContent += " iptables -t mangle -I PREROUTING 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" + $fwContent += " iptables -t mangle -I PREROUTING 2 -p tcp --dport `$PORT -j DROP`n" + $fwContent += " echo 'Port `$PORT blocked'`n" + $fwContent += " fi`n" + $fwContent += "done`n" + $fwContent += "echo 'FIREWALL_DONE'`n" + $fwContent | Out-File -FilePath $tmpFwScript -Encoding ascii -NoNewline -Force + $bytes = [System.IO.File]::ReadAllBytes($tmpFwScript) + Remove-Item $tmpFwScript -Force -ErrorAction SilentlyContinue + $b64 = [Convert]::ToBase64String($bytes) + $fwOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 "${ServerUser}@${VmHost}" "echo '$b64' | base64 -d > /tmp/fw.sh && sudo bash /tmp/fw.sh && rm /tmp/fw.sh" 2>&1 + if ($fwOut -match "FIREWALL_DONE") { + Write-Host " Firewall rules applied successfully!" -ForegroundColor Green + } else { + Write-Host " [WARN] Firewall rules may not have applied correctly." -ForegroundColor Yellow + if ($fwOut.Length -gt 300) { + Write-Host " Output: $($fwOut.Substring(0, 300))" -ForegroundColor Yellow + } else { + Write-Host " Output: $fwOut" -ForegroundColor Yellow + } + } + } + + Write-Host "" + Write-Host " Start the dashboard with:" -ForegroundColor Yellow + Write-Host "" + Write-Host " .\launcher.ps1 (option 1)" -ForegroundColor Cyan + } + + Write-Host "" + Write-Host " Press Enter to return to the main menu..." -ForegroundColor Cyan + Read-Host " " } function Start-Dashboard { @@ -531,9 +1396,9 @@ print(json.dumps(s)) if (-not $SSHKeySrc -or -not (Test-Path $SSHKeySrc) -or -not (Test-SshKeyForStart $SSHKeySrc ($SSHUser + '@' + $ServerHost))) { $keyPaths = @( + "$env:LOCALAPPDATA\DuneAwakeningServer\sshKey", $LocalKey, $ParentKey, - "$env:LOCALAPPDATA\DuneAwakeningServer\sshKey", "$env:USERPROFILE\.ssh\id_ed25519", "$env:USERPROFILE\.ssh\id_rsa", "$env:USERPROFILE\.ssh\id_ecdsa", @@ -872,9 +1737,11 @@ while ($true) { "1" { Start-Dashboard; break } "2" { Run-Setup; break } "3" { Run-Diagnostics; break } + "4" { Install-CaCert; break } + "5" { Clean-CaCerts; break } "Q" { Write-Host ""; Write-Host " Goodbye!"; Write-Host ""; exit 0 } "q" { Write-Host ""; Write-Host " Goodbye!"; Write-Host ""; exit 0 } - default { Write-Host ""; Write-Host " Invalid choice. Please enter 1, 2, 3, or Q." -ForegroundColor Yellow; Write-Host "" } + default { Write-Host ""; Write-Host " Invalid choice. Please enter 1, 2, 3, 4, 5, or Q." -ForegroundColor Yellow; Write-Host "" } } Write-Host "" diff --git a/scripts/db_check.py b/scripts/db_check.py index 7e42eb7..a6bff6e 100644 --- a/scripts/db_check.py +++ b/scripts/db_check.py @@ -1,6 +1,15 @@ import sys, psycopg2 try: - c = psycopg2.connect(host='localhost', port=int(sys.argv[1]), user='postgres', password='postgres', dbname='dune', connect_timeout=3) + host = 'localhost' + port = int(sys.argv[1]) + i = 2 + while i < len(sys.argv): + if sys.argv[i] == '-host' and i + 1 < len(sys.argv): + host = sys.argv[i + 1] + i += 2 + else: + i += 1 + c = psycopg2.connect(host=host, port=port, user='postgres', password='postgres', dbname='dune', connect_timeout=3) c.close() print('ok') except: diff --git a/setup.bat b/setup.bat deleted file mode 100644 index ea25f65..0000000 --- a/setup.bat +++ /dev/null @@ -1,5 +0,0 @@ -@echo off -echo Starting Dune Awakening Dashboard Launcher... -echo. -powershell -ExecutionPolicy Bypass -File "%~dp0launcher.ps1" -pause diff --git a/setup.ps1 b/setup.ps1 deleted file mode 100644 index e8f3a20..0000000 --- a/setup.ps1 +++ /dev/null @@ -1,831 +0,0 @@ -# Dune Awakening Dashboard - Setup -# Run this ONCE per server. After that, use start.ps1 - -$ErrorActionPreference = "Stop" - -$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path -Set-Location -LiteralPath $ProjectRoot - -Write-Host "" -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host " Dune Awakening Dashboard - Setup" -ForegroundColor Cyan -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host "" - -# Check if this is a re-run (existing settings or logs) -$IsReRun = (Test-Path (Join-Path $ProjectRoot "settings.yaml")) -or (Test-Path (Join-Path $ProjectRoot "logs")) -or (Test-Path (Join-Path $ProjectRoot "instance")) -or (Test-Path (Join-Path $ProjectRoot "ssl")) - -if ($IsReRun) { - Write-Host " [WARNING] Existing dashboard data detected!" -ForegroundColor Red - Write-Host "" - Write-Host " This will WIPE the following and start fresh:" -ForegroundColor Yellow - Write-Host " - settings.yaml (configuration)" -ForegroundColor Yellow - Write-Host " - logs/ (all log files)" -ForegroundColor Yellow - Write-Host " - instance/ (SQLite database)" -ForegroundColor Yellow - Write-Host " - ssl/ (SSL certificates and CA)" -ForegroundColor Yellow - Write-Host " - __pycache__/ (Python cache)" -ForegroundColor Yellow - Write-Host "" - Write-Host " Are you sure you want to continue? (Y/N)" -ForegroundColor Red - - $confirmation = Read-Host " Type Y to confirm" - if ($confirmation -ne "Y" -and $confirmation -ne "y") { - Write-Host "" - Write-Host " Setup cancelled. No changes made." -ForegroundColor Yellow - exit 0 - } - - Write-Host "" - Write-Host " Cleaning existing data..." -ForegroundColor Yellow - - if (Test-Path (Join-Path $ProjectRoot "settings.yaml")) { - Remove-Item (Join-Path $ProjectRoot "settings.yaml") -Force - Write-Host " Removed settings.yaml" -ForegroundColor Green - } - if (Test-Path (Join-Path $ProjectRoot "logs")) { - Remove-Item (Join-Path $ProjectRoot "logs") -Recurse -Force - Write-Host " Removed logs/" -ForegroundColor Green - } - if (Test-Path (Join-Path $ProjectRoot "instance")) { - Remove-Item (Join-Path $ProjectRoot "instance") -Recurse -Force - Write-Host " Removed instance/" -ForegroundColor Green - } - if (Test-Path (Join-Path $ProjectRoot "ssl")) { - Remove-Item (Join-Path $ProjectRoot "ssl") -Recurse -Force - Write-Host " Removed ssl/" -ForegroundColor Green - } - if (Test-Path (Join-Path $ProjectRoot "__pycache__")) { - Remove-Item (Join-Path $ProjectRoot "__pycache__") -Recurse -Force - Write-Host " Removed __pycache__/" -ForegroundColor Green - } - - # Clean app/__pycache__ and routes/__pycache__ etc. - $cacheDirs = Get-ChildItem -Path (Join-Path $ProjectRoot "app") -Recurse -Directory -Filter "__pycache__" -ErrorAction SilentlyContinue - foreach ($dir in $cacheDirs) { - Remove-Item $dir.FullName -Recurse -Force - } - if ($cacheDirs.Count -gt 0) { - Write-Host " Removed $($cacheDirs.Count) Python cache folders" -ForegroundColor Green - } - - Write-Host " Clean complete!" -ForegroundColor Green - Write-Host "" -} - -# Check Python -Write-Host "[1/6] Checking Python..." -ForegroundColor Yellow -try { - $pythonVersion = python --version 2>&1 - Write-Host " Found: $pythonVersion" -ForegroundColor Green -} catch { - Write-Host " [ERROR] Python not found. Install Python 3.8+ first." -ForegroundColor Red - exit 1 -} - -# Install dependencies -Write-Host "" -Write-Host "[2/6] Installing dependencies..." -ForegroundColor Yellow -pip install -r (Join-Path $ProjectRoot "requirements.txt") --quiet -if ($LASTEXITCODE -eq 0) { - Write-Host " Dependencies installed" -ForegroundColor Green -} else { - Write-Host " [WARN] Some packages may have failed. Continuing..." -ForegroundColor Yellow -} - -# Configure SSH key -Write-Host "" -Write-Host "[3/6] Configuring SSH key..." -ForegroundColor Yellow -$TargetKey = Join-Path $ProjectRoot "internal-scripts\ssh\sshKey" -$SshKeyPaths = @( - "$env:TEMP\dune-tunnel-key", - "$env:TEMP\dune-awakening-server-sshKey", - $TargetKey, - (Join-Path (Split-Path $ProjectRoot) "internal-scripts\ssh\sshKey") -) -$FoundKey = $null -foreach ($path in $SshKeyPaths) { - if (Test-Path $path) { - $FoundKey = $path - break - } -} - -function Fix-SshKeyPermissions($keyPath) { - if (-not (Test-Path $keyPath)) { return } - $acl = Get-Acl $keyPath - $acl.SetAccessRuleProtection($true, $false) - $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow") - $acl.SetAccessRule($rule) - $adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators", "FullControl", "Allow") - $acl.SetAccessRule($adminRule) - Set-Acl -Path $keyPath -AclObject $acl -} - -if (-not $FoundKey) { - Write-Host " [INFO] No SSH key found in standard locations." -ForegroundColor Yellow - $userKey = Read-Host " Enter path to your SSH key file (or press Enter to skip)" - if ($userKey -and (Test-Path $userKey)) { - $targetDir = Join-Path $ProjectRoot "internal-scripts\ssh" - if (-not (Test-Path $targetDir)) { - New-Item -ItemType Directory -Path $targetDir -Force | Out-Null - } - Copy-Item $userKey $TargetKey -Force - Fix-SshKeyPermissions $TargetKey - $FoundKey = $TargetKey - Write-Host " Key copied to internal-scripts\ssh\sshKey" -ForegroundColor Green - } else { - Write-Host " Skipping SSH key configuration." -ForegroundColor Yellow - $FoundKey = $null - } -} else { - # If found in temp, copy to project for portability - if ($FoundKey -notlike "*internal-scripts*") { - $targetDir = Join-Path $ProjectRoot "internal-scripts\ssh" - if (-not (Test-Path $targetDir)) { - New-Item -ItemType Directory -Path $targetDir -Force | Out-Null - } - Copy-Item $FoundKey $TargetKey -Force - Fix-SshKeyPermissions $TargetKey - $FoundKey = $TargetKey - Write-Host " Key copied to internal-scripts\ssh\sshKey for portability" -ForegroundColor Green - } else { - Write-Host " Found: $FoundKey" -ForegroundColor Green - } -} - -# Auto-detect server settings -Write-Host "" -Write-Host "[4/6] Detecting server settings..." -ForegroundColor Yellow - -$VmHost = "YOUR_SERVER_IP" -$HostIp = $null -$ServerUser = "dune" -$K8sNamespace = "" -$DashboardPort = "5050" -$DbPort = "15433" -$DirectorPort = "32479" -$FileBrowserPort = "18888" -$AuthUser = "admin" -$AuthPass = "" - -# Try to detect VM IP - prioritize local Hyper-V, then local network IPs from known_hosts -function Test-IsLocalIP($ip) { - if ($ip -match '^192\.168\.') { return $true } - if ($ip -match '^10\.') { return $true } - if ($ip -match '^172\.(1[6-9]|2[0-9]|3[0-1])\.') { return $true } - return $false -} - -# First: try direct Hyper-V detection -$vmIp = $null -try { - $vmAdapter = Get-VMNetworkAdapter -VMName 'dune-awakening' -ErrorAction SilentlyContinue - if ($vmAdapter -and $vmAdapter.IPAddresses) { - $localIp = $vmAdapter.IPAddresses | Where-Object { Test-IsLocalIP $_ } - if ($localIp) { - $vmIp = $localIp[0] - Write-Host " Detected local Hyper-V VM IP: $vmIp" -ForegroundColor Green - } - } -} catch { } - -if (-not $vmIp) { - # Fallback: scan known_hosts and prioritize local IPs - $KnownHosts = Join-Path $env:USERPROFILE ".ssh\known_hosts" - if (Test-Path $KnownHosts) { - $content = Get-Content $KnownHosts - $localIps = @() - $publicIps = @() - foreach ($line in $content) { - if ($line -match '\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b') { - $ip = $matches[1] - if (Test-IsLocalIP $ip) { - $localIps += $ip - } else { - $publicIps += $ip - } - } - } - if ($localIps.Count -gt 0) { - $vmIp = $localIps[-1] - Write-Host " Detected local IP from SSH history: $vmIp" -ForegroundColor Green - } elseif ($publicIps.Count -gt 0) { - $vmIp = $publicIps[-1] - Write-Host " Detected IP from SSH history: $vmIp (verify this is your local VM)" -ForegroundColor Yellow - } - } -} - -if ($vmIp) { $VmHost = $vmIp } - -# Auto-detect external IP (try external service first, then local interfaces) -try { - $HostIp = (Invoke-WebRequest -Uri "https://api.ipify.org" -UseBasicParsing -TimeoutSec 5).Content.Trim() -} catch { - $localIps = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike 'Loopback*' -and $_.PrefixOrigin -ne 'WellKnown' } | Select-Object -ExpandProperty IPAddress - if ($localIps.Count -gt 0) { $HostIp = $localIps[0] } -} - -# Ask for VM IP (SSH) -Write-Host "" -Write-Host " VM External IP - used for SSH connection to your game server" -ForegroundColor Cyan -$val = Read-Host " VM Host [$VmHost]" -if ($val) { $VmHost = $val } - -# Ask for Host IP (cert SANs) -Write-Host "" -Write-Host " This Machine's External IP - used for SSL certificate so remote access works" -ForegroundColor Cyan -if ($HostIp) { $hostIpHint = "$HostIp (auto-detected)" } else { $hostIpHint = "Your Windows machine's external IP" } -$val = Read-Host " Host IP [$hostIpHint]" -if ($val) { $HostIp = $val } -elseif (-not $HostIp) { $HostIp = $null } - -# Ask for domain name (Let's Encrypt) -Write-Host "" -Write-Host " Domain Name (optional) - for a publicly trusted SSL certificate via Let's Encrypt." -ForegroundColor Cyan -Write-Host " This removes browser warnings for ALL visitors. Requires:" -ForegroundColor Cyan -Write-Host " - A domain (or subdomain) pointing to this machine's external IP" -ForegroundColor Cyan -Write-Host " - Port 80 accessible from the internet" -ForegroundColor Cyan -$val = Read-Host " Domain (leave blank to use local CA instead)" -$DomainName = $null -if ($val -and $val -match '\S') { $DomainName = $val } - -$LeEmail = $null -if ($DomainName) { - Write-Host "" - Write-Host " Email address is required by Let's Encrypt for expiry notifications." -ForegroundColor Cyan - $val = Read-Host " Email Address" - if ($val -and $val -match '\S') { $LeEmail = $val } -} - -# Try SSH with the confirmed VM IP to auto-detect namespace -if ($FoundKey -and $VmHost -ne "YOUR_SERVER_IP") { - Write-Host " Testing SSH connection..." -ForegroundColor Yellow - Write-Host " (Fresh VMs may need time to initialize SSH keys — will retry up to 60s)" -ForegroundColor DarkGray - Write-Host "" - - $sshOk = $false - for ($i = 0; $i -lt 12; $i++) { - $testOut = cmd /c "ssh -i `"$FoundKey`" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 -o BatchMode=yes dune@$VmHost echo ok" 2>&1 - $testExit = $LASTEXITCODE - if ($testOut -match "ok" -and $testExit -eq 0) { - $sshOk = $true - break - } - Write-Host " Attempt $($i+1)/12 — SSH not ready yet, waiting 5s..." -ForegroundColor DarkGray - Start-Sleep -Seconds 5 - } - - if ($sshOk) { - Write-Host " SSH connection OK" -ForegroundColor Green - $nsOut = cmd /c "ssh -i `"$FoundKey`" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes dune@$VmHost sudo kubectl get namespaces -o name" 2>$null - if ($nsOut) { - foreach ($line in $nsOut -split "`n") { - $ns = $line -replace 'namespace/', '' - if ($ns -match '^funcom-seabass-') { - $K8sNamespace = $ns - Write-Host " Auto-detected namespace: $K8sNamespace" -ForegroundColor Green - break - } - } - } - } else { - Write-Host " SSH connection failed after 60s of retries" -ForegroundColor Yellow - Write-Host "" - Write-Host " This is normal on a fresh VM that hasn't finished initializing." -ForegroundColor Cyan - Write-Host " You can still continue setup — just enter the namespace manually below." -ForegroundColor Cyan - Write-Host "" - Write-Host " Once the VM is ready, copy your public key to enable SSH:" -ForegroundColor Cyan - Write-Host "" - $pubKeyContent = $null - try { $pubKeyContent = Get-Content "$FoundKey.pub" -ErrorAction SilentlyContinue } catch {} - if ($pubKeyContent) { - Write-Host " ssh dune@$VmHost 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo `"$pubKeyContent`" >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'" -ForegroundColor DarkGray - } else { - Write-Host " ssh dune@$VmHost 'mkdir -p ~/.ssh && chmod 700 ~/.ssh'" -ForegroundColor DarkGray - Write-Host " Then paste your public key into ~/.ssh/authorized_keys" -ForegroundColor DarkGray - } - Write-Host "" - } -} - -# Interactive review/edit -Write-Host "" -Write-Host " Review settings (press Enter to accept, or type new value):" -ForegroundColor Cyan -Write-Host "" - -Write-Host " VM Host: $VmHost (IP of your game server VM, used for SSH)" -if ($HostIp) { Write-Host " Host IP: $HostIp (This machine's external IP, used for SSL cert)" } - -$val = Read-Host " Server User [$ServerUser] (SSH username for the VM)" -if ($val) { $ServerUser = $val } - -Write-Host " SSH Key Path: $FoundKey (Path to your private SSH key)" - -if (-not $K8sNamespace) { - Write-Host " [INFO] To find your namespace, run: ssh dune@YOUR_IP 'sudo kubectl get namespaces'" -ForegroundColor Cyan - $K8sHint = "funcom-seabass- (Kubernetes cluster namespace)" -} else { - $K8sHint = "$K8sNamespace (Kubernetes cluster namespace)" -} - -$val = Read-Host " K8s Namespace [$K8sHint]" -if ($val) { $K8sNamespace = $val } -elseif (-not $K8sNamespace) { $K8sNamespace = "" } - -$val = Read-Host " Dashboard Port [$DashboardPort] (Local web access port)" -if ($val) { $DashboardPort = $val } - -$val = Read-Host " DB Local Port [$DbPort] (Local database tunnel port)" -if ($val) { $DbPort = $val } - -$val = Read-Host " Director Port [$DirectorPort] (Director API port)" -if ($val) { $DirectorPort = $val } - -$val = Read-Host " FileBrowser Port [$FileBrowserPort] (File manager port)" -if ($val) { $FileBrowserPort = $val } - -$val = Read-Host " Auth Username [$AuthUser] (Dashboard login name)" -if ($val) { $AuthUser = $val } - -$val = Read-Host " Auth Password (required)" -if ($val) { $AuthPass = $val } -else { - Write-Host " [ERROR] Password is required. Setup cancelled." -ForegroundColor Red - exit 1 -} - -# SSL certificate (Let's Encrypt or local CA) -Write-Host "" -Write-Host " Configuring SSL certificate..." -ForegroundColor Yellow -$CertDir = Join-Path $ProjectRoot "ssl" -if (-not (Test-Path $CertDir)) { New-Item -ItemType Directory -Path $CertDir -Force | Out-Null } -$CaCertPath = Join-Path $CertDir "ca.pem" -$CaKeyPath = Join-Path $CertDir "ca-key.pem" -$SslCertPath = Join-Path $CertDir "cert.pem" -$SslKeyPath = Join-Path $CertDir "key.pem" - -$UseLetsEncrypt = $false -$LeCertPath = $null -$LeKeyPath = $null - -if ($DomainName) { - Write-Host " Attempting Let's Encrypt certificate for $DomainName..." -ForegroundColor Yellow - - # Check for certbot - $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue - if (-not $certbotCmd) { - Write-Host " Installing certbot..." -ForegroundColor Yellow - - # Try winget first - try { - winget install --id Certbot.Certbot -e --accept-source-agreements --accept-package-agreements --silent 2>$null | Out-Null - $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue - } catch {} - - # Try pip as fallback - if (-not $certbotCmd) { - Write-Host " winget failed, trying pip..." -ForegroundColor Yellow - pip install certbot 2>$null | Out-Null - $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue - } - - # Try running via python -m certbot - if (-not $certbotCmd) { - $testCertbot = python -m certbot --version 2>$null - if ($LASTEXITCODE -eq 0) { - $certbotCmd = @{ Source = "python -m certbot" } - } - } - } - - if ($certbotCmd) { - # Stop anything on port 80 for the challenge - $httpRedirect = Get-NetTCPConnection -LocalPort 80 -ErrorAction SilentlyContinue | Where-Object { $_.State -eq 'Listen' } - if ($httpRedirect) { - Write-Host " Temporarily stopping services on port 80 for certificate validation..." -ForegroundColor Yellow - } - - $certbotExe = if ($certbotCmd.Source -eq "python") { "python" } else { "certbot" } - $certbotUseModule = ($certbotCmd.Source -eq "python") - - if ($certbotUseModule) { - $certbotArgs = @("-m", "certbot", "certonly", "--standalone", "-d", $DomainName, "--non-interactive", "--agree-tos") - if ($LeEmail) { - $certbotArgs += "--email" - $certbotArgs += $LeEmail - } else { - $certbotArgs += "--register-unsafely-without-email" - } - $certbotResult = Start-Process python -ArgumentList $certbotArgs -Wait -NoNewWindow -PassThru -RedirectStandardOutput "$env:TEMP\certbot-out.txt" - } else { - $certbotArgs = @( - "certonly", "--standalone", - "-d", $DomainName, - "--non-interactive", - "--agree-tos" - ) - if ($LeEmail) { - $certbotArgs += "--email" - $certbotArgs += $LeEmail - } else { - $certbotArgs += "--register-unsafely-without-email" - } - $certbotResult = Start-Process certbot -ArgumentList $certbotArgs -Wait -NoNewWindow -PassThru -RedirectStandardOutput "$env:TEMP\certbot-out.txt" - } - $certbotOutput = Get-Content "$env:TEMP\certbot-out.txt" -ErrorAction SilentlyContinue - - if ($certbotResult.ExitCode -eq 0) { - $UseLetsEncrypt = $true - $LeCertPath = "C:\ProgramData\certbot\live\$DomainName\fullchain.pem" - $LeKeyPath = "C:\ProgramData\certbot\live\$DomainName\privkey.pem" - - # Fallback path check - if (-not (Test-Path $LeCertPath)) { - $LeCertPath = "C:\Certbot\live\$DomainName\fullchain.pem" - $LeKeyPath = "C:\Certbot\live\$DomainName\privkey.pem" - } - - if (Test-Path $LeCertPath) { - Write-Host " Let's Encrypt certificate obtained successfully!" -ForegroundColor Green - Write-Host " Certificate for $DomainName is publicly trusted." -ForegroundColor Green - } else { - Write-Host " [WARN] Let's Encrypt succeeded but cert files not found at expected path." -ForegroundColor Yellow - Write-Host " Falling back to local CA." -ForegroundColor Yellow - $UseLetsEncrypt = $false - } - } else { - Write-Host " [WARN] Let's Encrypt certificate failed." -ForegroundColor Yellow - if ($certbotOutput) { $certbotOutput | ForEach-Object { Write-Host " $_" -ForegroundColor Yellow } } - Write-Host " Falling back to local CA certificate." -ForegroundColor Yellow - Write-Host " To retry, run setup.ps1 again after installing certbot: winget install Certbot.Certbot" -ForegroundColor Yellow - } - } else { - Write-Host " [WARN] certbot not available. Falling back to local CA certificate." -ForegroundColor Yellow - Write-Host " To install certbot later, run: winget install Certbot.Certbot" -ForegroundColor Yellow - Write-Host " Then re-run setup.ps1 to get a Let's Encrypt certificate." -ForegroundColor Yellow - } -} - -if (-not $UseLetsEncrypt) { - # Use forward slashes for Python to avoid unicode escape issues - $CaCertPathPy = $CaCertPath -replace '\\', '/' - $CaKeyPathPy = $CaKeyPath -replace '\\', '/' - $SslCertPathPy = $SslCertPath -replace '\\', '/' - $SslKeyPathPy = $SslKeyPath -replace '\\', '/' - - # Auto-detect local Windows IP for cert SANs - $SanIps = @('127.0.0.1') - if ($VmHost -ne "YOUR_SERVER_IP") { $SanIps += $VmHost } - if ($HostIp -and $HostIp -notin $SanIps) { $SanIps += $HostIp } - - # Generate CA if it doesn't exist - if (-not (Test-Path $CaCertPath) -or -not (Test-Path $CaKeyPath)) { - Write-Host " Generating local CA..." -ForegroundColor Yellow - python -c "from app.utils.ssl import generate_ca; generate_ca('$CaCertPathPy', '$CaKeyPathPy')" - } - - # Generate server cert signed by CA - $SanIpsArray = "['" + ($SanIps -join "', '") + "']" - if ($VmHost -ne "YOUR_SERVER_IP") { $CommonName = $VmHost } else { $CommonName = "localhost" } - python -c "from app.utils.ssl import generate_cert; generate_cert('$SslCertPathPy', '$SslKeyPathPy', ca_cert_path='$CaCertPathPy', ca_key_path='$CaKeyPathPy', common_name='$CommonName', san_ips=$SanIpsArray, san_dns=['localhost'])" - $SslCert = "'$SslCertPath'" - $SslKey = "'$SslKeyPath'" - - # Offer to install CA cert into Windows Trusted Root store - Write-Host "" - Write-Host " A local CA certificate was generated (Let's Encrypt was not used)." -ForegroundColor Cyan - Write-Host " Installing it into the Trusted Root store will remove browser warnings on THIS machine only." -ForegroundColor Cyan - Write-Host " Visitors from other machines will still see a warning unless they also install the CA cert." -ForegroundColor Cyan - Write-Host " To get a publicly trusted certificate, re-run setup and provide a domain name." -ForegroundColor Cyan - Write-Host "" - $InstallCa = Read-Host " Install CA certificate now? Requires Administrator (y/N)" - if ($InstallCa -eq 'y' -or $InstallCa -eq 'Y') { - $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - if (-not $isAdmin) { - Write-Host "" - Write-Host " [WARN] Not running as Administrator." -ForegroundColor Yellow - Write-Host " Starting a new elevated PowerShell window to install the CA cert..." -ForegroundColor Yellow - Write-Host "" - $elevatedScript = "certutil -addstore -f 'Root' '$CaCertPath'; Write-Host ''; Read-Host 'Press Enter to close'" - Start-Process powershell -ArgumentList "-NoProfile", "-Command", $elevatedScript -Verb RunAs -Wait - Write-Host " CA certificate installation complete." -ForegroundColor Green - } else { - certutil -addstore -f "Root" $CaCertPath - if ($LASTEXITCODE -eq 0) { - Write-Host " CA certificate installed to Trusted Root Certification Authorities." -ForegroundColor Green - } else { - Write-Host " [WARN] certutil failed. Try running setup as Administrator." -ForegroundColor Yellow - } - } - Write-Host " Restart your browser for the change to take effect." -ForegroundColor Cyan - } else { - Write-Host " Skipped. You can install it later by running:" -ForegroundColor Yellow - Write-Host " .\install-ca-cert.bat" -ForegroundColor Cyan - } -} else { - $SslCert = "'$LeCertPath'" - $SslKey = "'$LeKeyPath'" -} - -# Remote access -Write-Host "" -$RemoteAccess = Read-Host " Enable remote access? (y/N)" -$EnableRemote = ($RemoteAccess -eq 'y' -or $RemoteAccess -eq 'Y') - -if ($EnableRemote) { - $DashHost = "0.0.0.0" - Write-Host " Remote access enabled. The dashboard will bind to 0.0.0.0 so localhost, 127.0.0.1, LAN IP, and public IP can all work." -ForegroundColor Green - Write-Host " Dedicated server: allow TCP $DashboardPort in Windows Firewall/cloud firewall." -ForegroundColor Cyan - Write-Host " Home network: port-forward TCP $DashboardPort from your router to this machine's LAN IP." -ForegroundColor Cyan -} else { - $DashHost = "127.0.0.1" - Write-Host " Local-only access enabled. Use http(s)://localhost:$DashboardPort or http(s)://127.0.0.1:$DashboardPort." -ForegroundColor Green -} - -$HttpRedirect = $false -$HttpRedirectPort = 80 -if ($EnableRemote -and ($SslCert -ne "null") -and ($SslKey -ne "null")) { - Write-Host "" - Write-Host " Optional HTTP to HTTPS redirect:" -ForegroundColor Cyan - Write-Host " - Not required if you visit https://HOST:$DashboardPort directly." -ForegroundColor Cyan - Write-Host " - Useful if you want http://HOST to redirect automatically." -ForegroundColor Cyan - Write-Host " - Requires TCP port 80 to be free and forwarded/open." -ForegroundColor Cyan - $RedirectAnswer = Read-Host " Enable HTTP redirect on port 80? (y/N)" - $HttpRedirect = ($RedirectAnswer -eq 'y' -or $RedirectAnswer -eq 'Y') -} - -# Firewall rules for optional HTTP redirect and dashboard port -Write-Host "" -if ($EnableRemote) { - $FirewallPorts = @($DashboardPort) - if ($HttpRedirect) { $FirewallPorts = @(80) + $FirewallPorts } - $FirewallPortList = ($FirewallPorts -join ',') - Write-Host " Windows Firewall needs to allow incoming TCP port(s): $FirewallPortList" -ForegroundColor Cyan - Write-Host " This requires Administrator privileges." -ForegroundColor Cyan - Write-Host "" -} else { - $FirewallPorts = @() - $FirewallPortList = "" - Write-Host " Skipping Windows Firewall rules because remote access is disabled." -ForegroundColor Cyan -} -Write-Host "" -if ($EnableRemote) { - $SetupFirewall = Read-Host " Create firewall rules? (Y/n)" -} else { - $SetupFirewall = "n" -} -if ($SetupFirewall -eq 'n' -or $SetupFirewall -eq 'N') { - if ($EnableRemote) { Write-Host " Skipped. Remote access may be blocked by firewall." -ForegroundColor Yellow } -} else { - $fwRuleName = "DuneDashboard" - $fwExists = $null - try { $fwExists = Get-NetFirewallRule -DisplayName $fwRuleName -ErrorAction SilentlyContinue } catch {} - if ($fwExists) { - Write-Host " Firewall rule already exists." -ForegroundColor Green - } else { - $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - if (-not $isAdmin) { - Write-Host " Starting elevated PowerShell to add firewall rules..." -ForegroundColor Yellow - $fwScript = "New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $FirewallPortList; Write-Host ''; Read-Host 'Press Enter to close'" - Start-Process powershell -ArgumentList "-NoProfile", "-Command", $fwScript -Verb RunAs -Wait - Write-Host " Firewall rules added." -ForegroundColor Green - } else { - New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $FirewallPorts - Write-Host " Firewall rules added for TCP port(s): $FirewallPortList." -ForegroundColor Green - } - } -} - -# Let's Encrypt auto-renewal scheduled task -if ($UseLetsEncrypt) { - Write-Host "" - Write-Host " Setting up Let's Encrypt auto-renewal..." -ForegroundColor Yellow - $taskName = "DuneDashboard-LeRenewal" - $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue - if ($existingTask) { - Write-Host " Renewal task already exists." -ForegroundColor Green - } else { - $renewAction = New-ScheduledTaskAction -Execute "certbot" -Argument "renew --quiet" -RunLevel Highest - $renewTrigger = New-ScheduledTaskTrigger -Daily -At 2am - $renewSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries - Register-ScheduledTask -TaskName $taskName -Action $renewAction -Trigger $renewTrigger -Settings $renewSettings -Description "Auto-renew Let's Encrypt certificate for Dune Dashboard" -ErrorAction SilentlyContinue | Out-Null - Write-Host " Auto-renewal scheduled (daily at 2 AM). Certificates renew automatically." -ForegroundColor Green - } -} - -# Firewall security hardening -Write-Host "" -Write-Host "[SECURITY] Firewall Hardening" -ForegroundColor Cyan -Write-Host "" -Write-Host " Your game server has services exposed to the internet without authentication." -ForegroundColor Yellow -Write-Host " This is a SECURITY RISK - these services are vulnerable to automated attacks." -ForegroundColor Yellow -Write-Host " PostgreSQL was ALREADY exploited by cryptocurrency mining malware." -ForegroundColor Red -Write-Host "" -Write-Host " Recommended: Block these ports so only localhost/VPN can reach them:" -ForegroundColor Cyan -Write-Host "" -Write-Host " [1] PostgreSQL (port 15432) - VULNERABLE (ALREADY EXPLOITED - default postgres/postgres)" -ForegroundColor Red -Write-Host " [2] File Browser (port 18888) - VULNERABLE (no auth - exposes server files)" -ForegroundColor Yellow -Write-Host " [3] Battlegroup Director (port 31820) - VULNERABLE (no auth - exposes server API)" -ForegroundColor Yellow -Write-Host "" -Write-Host " You can change these anytime from the Server page in the dashboard." -ForegroundColor Cyan -Write-Host "" -Write-Host " Block PostgreSQL (15432)? (Y/n)" -ForegroundColor Cyan -$val = Read-Host " Block PostgreSQL (15432)? (Y/n)" -$BlockPostgres = -not ($val -eq 'n' -or $val -eq 'N') -if ($BlockPostgres) { Write-Host " Will block port 15432 (PostgreSQL)" -ForegroundColor Green } -else { Write-Host " Skipped - port 15432 will remain open to the internet" -ForegroundColor Red } - -Write-Host "" -Write-Host " Block File Browser (18888)? (Y/n)" -ForegroundColor Cyan -$val = Read-Host " Block File Browser (18888)? (Y/n)" -$BlockFileBrowser = -not ($val -eq 'n' -or $val -eq 'N') -if ($BlockFileBrowser) { Write-Host " Will block port 18888 (File Browser)" -ForegroundColor Green } -else { Write-Host " Skipped - port 18888 will remain open to the internet" -ForegroundColor Yellow } - -Write-Host "" -Write-Host " Block Battlegroup Director (31820)? (Y/n)" -ForegroundColor Cyan -$val = Read-Host " Block Battlegroup Director (31820)? (Y/n)" -$BlockDirector = -not ($val -eq 'n' -or $val -eq 'N') -if ($BlockDirector) { Write-Host " Will block port 31820 (Director)" -ForegroundColor Green } -else { Write-Host " Skipped - port 31820 will remain open to the internet" -ForegroundColor Yellow } - -Write-Host "" -Write-Host "[5/6] Saving settings..." -ForegroundColor Yellow - -# Generate secret key -$secret = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 32 | ForEach-Object {[char]$_}) - -# Hash the password using Argon2 -Write-Host " Hashing password with Argon2..." -ForegroundColor Yellow -$pwScript = @" -import sys -from argon2 import PasswordHasher -ph = PasswordHasher() -print(ph.hash(sys.argv[1])) -"@ -$pwScript | Out-File -FilePath "$env:TEMP\hash_pw.py" -Encoding utf8 -Force -$hashResult = python "$env:TEMP\hash_pw.py" $AuthPass 2>$null -Remove-Item "$env:TEMP\hash_pw.py" -Force -ErrorAction SilentlyContinue -if ($hashResult -and $hashResult -match '^\$argon2') { - $AuthHash = $hashResult.Trim() - Write-Host " Password hashed successfully." -ForegroundColor Green -} else { - Write-Host " [ERROR] Failed to hash password. Argon2 may not be installed." -ForegroundColor Red - Write-Host " Run: pip install argon2-cffi" -ForegroundColor Yellow - exit 1 -} - -# Write settings.yaml (UTF-8 without BOM) -$sshKeyPath = if ($FoundKey) { $FoundKey -replace '\\', '\\' } else { "null" } -$leDomain = if ($DomainName) { $DomainName } else { "null" } -$leEmail = if ($LeEmail) { $LeEmail } else { "null" } -$settingsContent = @" -server: - host: '$VmHost' - user: $ServerUser - ssh_key: '$sshKeyPath' - -dashboard: - host: $DashHost - port: $DashboardPort - debug: false - secret_key: $secret - ssl_cert: $SslCert - ssl_key: $SslKey - ssl_domain: $leDomain - ssl_email: $leEmail - http_redirect: $HttpRedirect - http_redirect_port: $HttpRedirectPort - -database: - host: 127.0.0.1 - port: $DbPort - user: postgres - password: postgres - name: dune - schema: dune - min_connections: 2 - max_connections: 10 - -kubernetes: - namespace: '$K8sNamespace' - battlegroup_script: /home/dune/.dune/bin/battlegroup - -director: - port: $DirectorPort - -filebrowser: - port: $FileBrowserPort - -firewall: - block_filebrowser: $BlockFileBrowser - block_director: $BlockDirector - block_postgres: $BlockPostgres - -cache: - chat_pod_ttl: 60 - chat_messages_ttl: 10 - static_data_ttl: 300 - -auth: - enabled: true - username: $AuthUser - password_hash: '$AuthHash' - -logging: - level: INFO - file: logs/dashboard.log - max_bytes: 10485760 - backup_count: 5 -"@ - -[System.IO.File]::WriteAllText((Join-Path $ProjectRoot "settings.yaml"), $settingsContent, [System.Text.UTF8Encoding]::new($false)) -Write-Host " Settings saved to settings.yaml" -ForegroundColor Green - -# Create logs directory -New-Item -ItemType Directory -Path (Join-Path $ProjectRoot "logs") -Force | Out-Null - -Write-Host "" -Write-Host "[6/6] Verifying setup..." -ForegroundColor Yellow -$verifyResult = python -W ignore -c "from app.config import load_settings; s = load_settings(); print('OK - settings loaded')" 2>$null -if ($verifyResult -match "OK") { - Write-Host " $verifyResult" -ForegroundColor Green -} else { - Write-Host " [WARN] Verification skipped." -ForegroundColor Yellow -} - -Write-Host "" -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host " Setup Complete!" -ForegroundColor Cyan -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host "" - -# Check if SSH key is valid for the server -$SshValid = $false -if ($FoundKey -and (Test-Path $FoundKey) -and $VmHost -ne "YOUR_SERVER_IP") { - $testOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -o BatchMode=yes "${ServerUser}@${VmHost}" "echo ok" 2>$null - $SshValid = ($testOut -eq "ok") -} - -if (-not $SshValid) { - if ($VmHost -eq "YOUR_SERVER_IP") { - Write-Host " Remember to edit settings.yaml with your server IP before starting." -ForegroundColor Yellow - } else { - Write-Host " SSH key is not configured or failed to connect." -ForegroundColor Yellow - } - Write-Host "" - Write-Host " Then start the dashboard with:" -ForegroundColor Yellow - Write-Host "" - Write-Host " .\start.ps1" -ForegroundColor Cyan -} else { - Write-Host " SSH connection verified." -ForegroundColor Green - - # Apply firewall rules if requested - $portsToBlock = @() - if ($BlockFileBrowser) { $portsToBlock += 18888 } - if ($BlockDirector) { $portsToBlock += 31820 } - if ($BlockPostgres) { $portsToBlock += 15432 } - - if ($portsToBlock.Count -gt 0) { - Write-Host "" - Write-Host " Applying firewall rules to block ports: $($portsToBlock -join ', ')..." -ForegroundColor Yellow - - $tmpFwScript = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName() + ".sh") - $fwContent = "for PORT in $($portsToBlock -join ' '); do`n" - $fwContent += " if iptables -t mangle -C PREROUTING -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT 2>/dev/null; then`n" - $fwContent += " echo 'Port `$PORT already blocked'`n" - $fwContent += " else`n" - $fwContent += " iptables -I INPUT 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" - $fwContent += " iptables -I INPUT 2 -p tcp --dport `$PORT -j DROP`n" - $fwContent += " iptables -I FORWARD 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" - $fwContent += " iptables -I FORWARD 2 -p tcp --dport `$PORT -j DROP`n" - $fwContent += " iptables -t mangle -I PREROUTING 1 -p tcp --dport `$PORT -s 127.0.0.1 -j ACCEPT`n" - $fwContent += " iptables -t mangle -I PREROUTING 2 -p tcp --dport `$PORT -j DROP`n" - $fwContent += " echo 'Port `$PORT blocked'`n" - $fwContent += " fi`n" - $fwContent += "done`n" - $fwContent += "echo 'FIREWALL_DONE'`n" - $fwContent | Out-File -FilePath $tmpFwScript -Encoding ascii -NoNewline -Force - $bytes = [System.IO.File]::ReadAllBytes($tmpFwScript) - Remove-Item $tmpFwScript -Force -ErrorAction SilentlyContinue - $b64 = [Convert]::ToBase64String($bytes) - $fwOut = ssh -i $FoundKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 "${ServerUser}@${VmHost}" "echo '$b64' | base64 -d > /tmp/fw.sh && sudo bash /tmp/fw.sh && rm /tmp/fw.sh" 2>&1 - if ($fwOut -match "FIREWALL_DONE") { - Write-Host " Firewall rules applied successfully!" -ForegroundColor Green - } else { - Write-Host " [WARN] Firewall rules may not have applied correctly." -ForegroundColor Yellow - if ($fwOut.Length -gt 300) { - Write-Host " Output: $($fwOut.Substring(0, 300))" -ForegroundColor Yellow - } else { - Write-Host " Output: $fwOut" -ForegroundColor Yellow - } - } - } - - Write-Host "" - Write-Host " Start the dashboard with:" -ForegroundColor Yellow - Write-Host "" - Write-Host " .\start.ps1" -ForegroundColor Cyan -} - -Write-Host "" diff --git a/setup.sh b/setup.sh deleted file mode 100644 index f935e16..0000000 --- a/setup.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# Dune Awakening Dashboard - Legacy Setup Entry Point -# This now redirects to the unified launcher. - -PROJECT_ROOT="$(cd "$(dirname "$0")" && pwd)" -cd "$PROJECT_ROOT" - -echo "Starting Dune Awakening Dashboard Launcher..." -echo "" - -if [ -f "$PROJECT_ROOT/start.sh" ]; then - chmod +x "$PROJECT_ROOT/start.sh" - bash "$PROJECT_ROOT/start.sh" -else - echo "[ERROR] start.sh not found." - exit 1 -fi diff --git a/start.bat b/start.bat deleted file mode 100644 index 0994b98..0000000 --- a/start.bat +++ /dev/null @@ -1,3 +0,0 @@ -@echo off -powershell -ExecutionPolicy Bypass -File "%~dp0launcher.ps1" -pause diff --git a/start.ps1 b/start.ps1 deleted file mode 100644 index 7b8ff78..0000000 --- a/start.ps1 +++ /dev/null @@ -1,273 +0,0 @@ -# Dune Awakening Dashboard Launcher -# Starts SSH tunnel, DB port-forward, then runs the dashboard - -$ErrorActionPreference = "Stop" - -$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path -$SettingsFile = Join-Path $ProjectRoot "settings.yaml" - -if (-not (Test-Path $SettingsFile)) { - Write-Host "[WARN] settings.yaml not found. Running setup..." -ForegroundColor Yellow - & (Join-Path $ProjectRoot "setup.ps1") - if (-not (Test-Path $SettingsFile)) { - Write-Host "[ERROR] Setup failed." -ForegroundColor Red - exit 1 - } -} - -$readSettingsScript = @" -import yaml, json, sys, os -path = sys.argv[1] -if not os.path.exists(path): - print(json.dumps({})) - sys.exit(0) -with open(path, 'r', encoding='utf-8-sig') as f: - s = yaml.safe_load(f) or {} -print(json.dumps(s)) -"@ -$readSettingsScript | Out-File -FilePath "$env:TEMP\read_settings.py" -Encoding utf8 -Force -$settingsJson = python "$env:TEMP\read_settings.py" $SettingsFile 2>$null -if (-not $settingsJson) { - Write-Host "[ERROR] Failed to read settings.yaml" -ForegroundColor Red - exit 1 -} -$settings = $settingsJson | ConvertFrom-Json - -$ServerHost = $settings.server.host -$SSHUser = $settings.server.user -$LocalPort = [int]$settings.database.port -$Namespace = $settings.kubernetes.namespace -$DashboardPort = [int]$settings.dashboard.port -$DirectorPort = [int]$settings.director.port - -function Test-SshKey($keyPath, $targetServer) { - if (-not (Test-Path $keyPath)) { return $false } - $sshCmd = 'ssh -i "' + $keyPath + '" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 -o BatchMode=yes ' + $targetServer + ' "echo ok" 2>NUL' - $sshCmd | Out-File -FilePath "$env:TEMP\ssh_cmd.bat" -Encoding ascii - $out = cmd /c "$env:TEMP\ssh_cmd.bat" - return $out -eq "ok" -} - -$SSHKeySrc = $settings.server.ssh_key -$LocalKey = Join-Path $ProjectRoot "internal-scripts\ssh\sshKey" -$ParentKey = Join-Path (Split-Path $ProjectRoot) "internal-scripts\ssh\sshKey" - -if ($SSHKeySrc -and $SSHKeySrc -ne 'null' -and -not [string]::IsNullOrEmpty($SSHKeySrc)) { - if (Test-SshKey $SSHKeySrc ($SSHUser + '@' + $ServerHost)) { - Write-Host " Using key from settings.yaml" -ForegroundColor Green - } else { - Write-Host " Key in settings.yaml failed, searching..." -ForegroundColor Yellow - $SSHKeySrc = $null - } -} - -if (-not $SSHKeySrc -or -not (Test-Path $SSHKeySrc) -or -not (Test-SshKey $SSHKeySrc ($SSHUser + '@' + $ServerHost))) { - $keyPaths = @( - $LocalKey, - $ParentKey, - "$env:LOCALAPPDATA\DuneAwakeningServer\sshKey", - "$env:USERPROFILE\.ssh\id_ed25519", - "$env:USERPROFILE\.ssh\id_rsa", - "$env:USERPROFILE\.ssh\id_ecdsa", - "$env:TEMP\dune-tunnel-key", - "$env:TEMP\dune-awakening-server-sshKey" - ) - foreach ($kp in $keyPaths) { - if ($kp -ne $SSHKeySrc -and (Test-Path $kp)) { - if (Test-SshKey $kp ($SSHUser + '@' + $ServerHost)) { - $SSHKeySrc = $kp - Write-Host " Found working key: $SSHKeySrc" -ForegroundColor Green - break - } - } - } -} - -if (-not $SSHKeySrc -or -not (Test-Path $SSHKeySrc)) { - Write-Host "[ERROR] No working SSH key found." -ForegroundColor Red - Write-Host " Place a valid key in: $ProjectRoot\internal-scripts\ssh\sshKey" -ForegroundColor Yellow - Write-Host " Or update ssh_key path in settings.yaml" -ForegroundColor Yellow - exit 1 -} - -if ($SSHKeySrc -ne $LocalKey -and (Test-Path $LocalKey) -and (Get-Item $LocalKey).Length -eq 0) { - Copy-Item $SSHKeySrc $LocalKey -Force - Write-Host " Updated internal key file" -ForegroundColor Green -} - -$SSHKey = "$env:TEMP\dune-tunnel-key" -$ResolvedSrc = [System.IO.Path]::GetFullPath($SSHKeySrc) -$ResolvedDest = [System.IO.Path]::GetFullPath($SSHKey) -if ($ResolvedSrc -ne $ResolvedDest) { - Copy-Item $SSHKeySrc $SSHKey -Force -} -try { - $acl = Get-Acl $SSHKey -ErrorAction SilentlyContinue - if ($acl) { - $acl.SetAccessRuleProtection($true, $false) - $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow") - $acl.SetAccessRule($rule) - Set-Acl -Path $SSHKey -AclObject $acl -ErrorAction SilentlyContinue - } -} catch {} - -Write-Host "" -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host " Dune Awakening Dashboard" -ForegroundColor Cyan -Write-Host "============================================================" -ForegroundColor Cyan -Write-Host "" - -Get-NetTCPConnection -LocalPort $LocalPort -ErrorAction SilentlyContinue | ForEach-Object { - Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -eq 'ssh' } | Stop-Process -Force -ErrorAction SilentlyContinue -} -Start-Sleep 1 - -Write-Host "[1/4] Starting SSH tunnel (localhost:$LocalPort -> VM)..." -ForegroundColor Yellow -$sshArgs = @( - "-i", $SSHKey, - "-o", "StrictHostKeyChecking=accept-new", - "-o", "ServerAliveInterval=30", - "-o", "ServerAliveCountMax=3", - "-L", "${LocalPort}:localhost:${LocalPort}", - "-N", "${SSHUser}@${ServerHost}" -) -$sshTunnel = Start-Process ssh -ArgumentList $sshArgs -PassThru -WindowStyle Hidden - -$connected = $false -for ($i = 0; $i -lt 30; $i++) { - Start-Sleep -Seconds 1 - if ($sshTunnel.HasExited) { - Write-Host "[ERROR] SSH tunnel exited (code: $($sshTunnel.ExitCode))" -ForegroundColor Red - exit 1 - } - try { - $tcp = New-Object System.Net.Sockets.TcpClient - $tcp.Connect("127.0.0.1", $LocalPort) - $tcp.Close() - $connected = $true - break - } catch {} -} -if (-not $connected) { - Write-Host "[ERROR] SSH tunnel did not connect within 30s" -ForegroundColor Red - Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue - exit 1 -} -Write-Host "[OK] SSH tunnel up on localhost:$LocalPort" -ForegroundColor Green - -Write-Host "[2/4] Starting DB port-forward on VM..." -ForegroundColor Yellow - -if (-not $Namespace -or $Namespace -eq '') { - Write-Host "[ERROR] Kubernetes namespace is empty." -ForegroundColor Red - Write-Host " Edit settings.yaml and set kubernetes.namespace, or re-run setup.ps1 with SSH access." -ForegroundColor Yellow - Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue - exit 1 -} - -$DBService = "${Namespace}-db-dbdepl-svc" - -$pfCheck = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null -if ($pfCheck) { - $dbSvc = ($pfCheck -split "`n") | Where-Object { $_ -match 'db.*svc' -or $_ -match 'postgres' -or $_ -match 'pg' } | Select-Object -First 1 - if ($dbSvc) { - $DBService = $dbSvc -replace 'service/', '' - Write-Host " Found DB service: $DBService" -ForegroundColor Green - } -} - -$RemotePort = 15432 -ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ${SSHUser}@${ServerHost} "sudo pkill -9 -f port-forward 2>/dev/null; sleep 2" 2>$null -Start-Sleep -Seconds 2 - -$remoteCmd = 'nohup sudo kubectl port-forward -n ' + $Namespace + ' svc/' + $DBService + ' ' + $LocalPort + ':' + $RemotePort + ' > /tmp/pf.log 2>&1 &' -$pfRemote = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ' + $SSHUser + '@' + $ServerHost + ' "' + $remoteCmd + '"' -$pfRemote | Out-File -FilePath "$env:TEMP\ssh_pf_db.bat" -Encoding ascii -cmd /c "$env:TEMP\ssh_pf_db.bat" - -$bgdSvc = "${Namespace}-bgd-svc" -$pfCheckBgd = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "sudo kubectl get svc -n ${Namespace} -o name" 2>$null -if ($pfCheckBgd) { - $bgdMatch = ($pfCheckBgd -split "`n") | Where-Object { $_ -match 'bgd.*svc' } | Select-Object -First 1 - if ($bgdMatch) { $bgdSvc = $bgdMatch -replace 'service/', '' } -} - -$directorRemotePort = 11717 -$directorCmd = 'nohup sudo kubectl port-forward -n ' + $Namespace + ' svc/' + $bgdSvc + ' ' + $DirectorPort + ':' + $directorRemotePort + ' > /tmp/director_pf.log 2>&1 &' -$directorPf = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 ' + $SSHUser + '@' + $ServerHost + ' "' + $directorCmd + '"' -$directorPf | Out-File -FilePath "$env:TEMP\ssh_pf_director.bat" -Encoding ascii -cmd /c "$env:TEMP\ssh_pf_director.bat" - -Start-Sleep -Seconds 3 - -Write-Host "[3/4] Checking database..." -ForegroundColor Yellow -$dbTest = $false -$dbScript = Join-Path $ProjectRoot "scripts\db_check.py" -for ($i = 0; $i -lt 15; $i++) { - try { - $result = python $dbScript $LocalPort 2>$null - if ($result -match 'ok') { $dbTest = $true; break } - } catch {} - Start-Sleep -Seconds 1 -} -if (-not $dbTest) { - Write-Host "[ERROR] Database connection failed." -ForegroundColor Red - Write-Host "Checking port-forward log on VM..." -ForegroundColor Yellow - $pfLog = ssh -i $SSHKey -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 ${SSHUser}@${ServerHost} "cat /tmp/pf.log" 2>$null - if ($pfLog) { Write-Host $pfLog -ForegroundColor Red } - else { Write-Host "Log empty or unreadable." -ForegroundColor Yellow } - Write-Host "" - Write-Host "Verify DB service exists:" -ForegroundColor Yellow - Write-Host " ssh -i $SSHKey ${SSHUser}@${ServerHost} 'sudo kubectl get svc -n $Namespace'" -ForegroundColor Cyan - exit 1 -} -Write-Host "[OK] Database connected" -ForegroundColor Green - -$fwRuleName = "DuneDashboard" -$fwExists = $null -try { $fwExists = Get-NetFirewallRule -DisplayName $fwRuleName -ErrorAction SilentlyContinue } catch {} -$remoteDashboard = "$($settings.dashboard.host)" -eq "0.0.0.0" -if ($remoteDashboard -and -not $fwExists) { - Write-Host "[WARN] No firewall rule found. Remote access to the dashboard will be blocked." -ForegroundColor Yellow - $redirectEnabled = $settings.dashboard.http_redirect -eq $true - if ($redirectEnabled) { - $redirectPort = if ($settings.dashboard.http_redirect_port) { $settings.dashboard.http_redirect_port } else { 80 } - Write-Host " Ports needed: $redirectPort (optional HTTP redirect), $DashboardPort (dashboard)" -ForegroundColor Yellow - } else { - Write-Host " Port needed: $DashboardPort (dashboard)" -ForegroundColor Yellow - } - Write-Host "" - $SetupFirewall = Read-Host " Create firewall rules now? Requires Administrator (Y/n)" - if ($SetupFirewall -ne 'n' -and $SetupFirewall -ne 'N') { - $firewallPorts = @($DashboardPort) - if ($redirectEnabled) { $firewallPorts = @($redirectPort) + $firewallPorts } - $firewallPortList = ($firewallPorts -join ',') - $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - if (-not $isAdmin) { - Write-Host " Starting elevated PowerShell to add firewall rules..." -ForegroundColor Yellow - $fwScript = "New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $firewallPortList; Write-Host ''; Read-Host 'Press Enter to close'" - Start-Process powershell -ArgumentList "-NoProfile", "-Command", $fwScript -Verb RunAs -Wait - Write-Host "[OK] Firewall rules added." -ForegroundColor Green - } else { - New-NetFirewallRule -DisplayName DuneDashboard -Direction Inbound -Action Allow -Protocol TCP -LocalPort $firewallPorts - Write-Host "[OK] Firewall rules added for port(s): $firewallPortList." -ForegroundColor Green - } - } else { - Write-Host " Skipped. External access will be blocked." -ForegroundColor Yellow - } - Write-Host "" -} - -Write-Host "[4/4] Starting dashboard..." -ForegroundColor Yellow -$pipCheck = python -c "import flask_socketio, yaml, flask_login" 2>$null -if ($LASTEXITCODE -ne 0) { - Write-Host " Installing dependencies..." -ForegroundColor Yellow - pip install -r (Join-Path $ProjectRoot "requirements.txt") --quiet -} - -Set-Location -LiteralPath $ProjectRoot -python run.py - -Write-Host "Stopping tunnels..." -ForegroundColor Cyan -Stop-Process -Id $sshTunnel.Id -Force -ErrorAction SilentlyContinue -$pkillCmd = 'ssh -i "' + $SSHKey + '" -o StrictHostKeyChecking=accept-new ' + $SSHUser + '@' + $ServerHost + ' "pkill -f kubectl-port-forward"' -cmd /c $pkillCmd 2>$null