From 6e7a850ba2b4853f6bba8c3229abede9b02662c9 Mon Sep 17 00:00:00 2001 From: Reditus <33814724+ReditusDraco@users.noreply.github.com> Date: Tue, 19 May 2026 02:57:50 +0100 Subject: [PATCH] Fix setup flow: ask remote access first, auto-detect VM IP, validate local_ip - Ask 'Enable remote access?' before any IP questions - Default to local-only (n) for simpler setup - Skip SSL questions when local-only - Fix Hyper-V VM IP detection (handle single-element arrays properly) - Validate local_ip in config.py before using as host override - Fix factory.py to use settings['server']['host'] instead of local_ip - VM Host prompt shows '(detected)' when auto-detected IP is found --- app/config.py | 46 ++++++++++++++++++++++ app/factory.py | 2 +- launcher.ps1 | 103 ++++++++++++++++++++++++++++++------------------- 3 files changed, 110 insertions(+), 41 deletions(-) diff --git a/app/config.py b/app/config.py index 4c12bc7..a6ae03d 100644 --- a/app/config.py +++ b/app/config.py @@ -1,6 +1,7 @@ """Settings loader - reads settings.yaml with env var overrides""" import os +import socket import yaml import logging import copy @@ -13,6 +14,7 @@ DEFAULTS = { 'host': '127.0.0.1', 'user': 'dune', 'ssh_key': None, + 'local_ip': None, }, 'dashboard': { 'host': '127.0.0.1', @@ -121,6 +123,48 @@ def deep_merge(base, override): return result +def _detect_local_ip(): + """Detect local IP address for VM connectivity.""" + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(1) + s.connect(("8.8.8.8", 80)) + local_ip = s.getsockname()[0] + s.close() + if local_ip: + logger.debug(f"Detected local IP: {local_ip}") + return local_ip + except Exception as e: + logger.debug(f"Could not detect local IP: {e}") + return '127.0.0.1' + + +def _validate_server_host(settings): + """Validate and fix server.host if invalid or placeholder.""" + server = settings.setdefault('server', {}) + host = server.get('host', '') + + local_ip = server.get('local_ip') + if local_ip: + local_ip_str = str(local_ip).strip() + if local_ip_str and local_ip_str not in ('', 'null', 'None', 'YOUR_SERVER_IP', 'YOUR_VM_IP'): + if local_ip_str.count('.') == 3 and all(part.isdigit() and 0 <= int(part) <= 255 for part in local_ip_str.split('.')): + server['host'] = local_ip_str + logger.info(f"Using server.local_ip override: {server['host']}") + return settings + + host_str = str(host).strip() if host else '' + invalid_hosts = ('', 'YOUR_SERVER_IP', 'YOUR_VM_IP', 'null', 'None') + + if host_str in invalid_hosts or not host_str: + detected_ip = _detect_local_ip() + server['host'] = detected_ip + logger.warning(f"Invalid server.host detected, using auto-detected IP: {detected_ip}") + logger.warning("To fix: Edit settings.yaml and set server.host to your VM's IP address") + + return settings + + def _find_missing_keys(defaults, current, path=""): """Find keys present in defaults but missing in current.""" missing = [] @@ -226,4 +270,6 @@ def load_settings(settings_path=None): settings['dashboard']['secret_key'] = secrets.token_hex(32) logger.warning("Rotated secret_key to invalidate sessions after password migration") + settings = _validate_server_host(settings) + return settings diff --git a/app/factory.py b/app/factory.py index 49a9570..e60053f 100644 --- a/app/factory.py +++ b/app/factory.py @@ -175,7 +175,7 @@ def create_app(settings_path=None): logging.warning(f"Could not create indexes: {e}") ssh_service = SSHService( - host=settings['server'].get('local_ip', settings['server']['host']), + host=settings['server']['host'], user=settings['server']['user'], ssh_key=settings['server'].get('ssh_key') ) diff --git a/launcher.ps1 b/launcher.ps1 index 5322665..859a2a6 100644 --- a/launcher.ps1 +++ b/launcher.ps1 @@ -1071,9 +1071,9 @@ function Run-Setup { 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] + $validIps = $vmAdapter.IPAddresses | Where-Object { $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' -and (Test-IsLocalIP $_) } + if ($validIps -and $validIps.Count -gt 0) { + $vmIp = @($validIps)[0] Write-Host " Detected local Hyper-V VM IP: $vmIp" -ForegroundColor Green } } @@ -1096,11 +1096,17 @@ function Run-Setup { } } if ($localIps.Count -gt 0) { - $vmIp = $localIps[-1] - Write-Host " Detected local IP from SSH history: $vmIp" -ForegroundColor Green + $validLocal = $localIps | Where-Object { $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' } + if ($validLocal) { + $vmIp = @($validLocal)[-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 + $validPublic = $publicIps | Where-Object { $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' } + if ($validPublic) { + $vmIp = @($validPublic)[-1] + Write-Host " Detected IP from SSH history: $vmIp (verify this is your local VM)" -ForegroundColor Yellow + } } } } @@ -1115,32 +1121,54 @@ function Run-Setup { } Write-Host "" - Write-Host " VM External IP - used for SSH connection to your game server" -ForegroundColor Cyan - $val = Read-Host " VM Host [$VmHost]" + Write-Host " Access Mode - Choose how you want to access the dashboard:" -ForegroundColor Cyan + Write-Host " Local only (n) - Access from this machine only (recommended)" -ForegroundColor DarkGray + Write-Host " Remote (y) - Access from other devices on your network" -ForegroundColor DarkGray + $val = Read-Host " Enable remote access? (n)" + $EnableRemote = ($val -eq 'y' -or $val -eq 'Y') + + Write-Host "" + Write-Host " VM Host - IP of your game server (for SSH connection)" -ForegroundColor Cyan + if ($EnableRemote) { + Write-Host " Example: Your VM's external or LAN IP" -ForegroundColor DarkGray + } else { + Write-Host " Example: Your VM's local IP (e.g., 192.168.x.x, 10.x.x.x)" -ForegroundColor DarkGray + } + $vmHint = $VmHost + if ($vmIp -and $vmIp -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') { + $vmHint = "$VmHost (detected - press Enter to accept)" + } + $val = Read-Host " VM Host [$vmHint]" 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) { + if ($EnableRemote) { 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 } + Write-Host " This Machine's IP - used for SSL certificate (required for remote access)" -ForegroundColor Cyan + if ($HostIp) { $hostIpHint = "$HostIp (auto-detected)" } else { $hostIpHint = "Your machine's 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 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 } + } + } else { + $DomainName = $null + $LeEmail = $null + $HostIp = $null } # Try SSH with the confirmed VM IP to auto-detect namespace @@ -1258,8 +1286,11 @@ function Run-Setup { $UseLetsEncrypt = $false $LeCertPath = $null $LeKeyPath = $null + $SslCert = "null" + $SslKey = "null" - if ($DomainName) { + if ($EnableRemote) { + if ($DomainName) { Write-Host " Attempting Let's Encrypt certificate for $DomainName..." -ForegroundColor Yellow $certbotCmd = Get-Command certbot -ErrorAction SilentlyContinue @@ -1404,20 +1435,12 @@ function Run-Setup { $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