@echo off REM ============================================================ REM autostart-run.bat - launcher used by the scheduled task REM created by install-autostart.bat. Runs in a VISIBLE console REM window (install-autostart.bat registers it at logon, as the REM current user, so the window is visible - unlike a hidden REM SYSTEM/boot task). It reads the password from webvnc.conf, REM WAITS for the VNC server on 127.0.0.1:5900 (the UltraVNC REM service may still be starting), and only then starts REM web-vnc.exe in the foreground. If no VNC server comes up, REM it spawns a local one. On any fatal error it pauses so the REM window stays open and you can read why it did not start. REM REM This script lives in scripts/ but operates relative to the REM REPO ROOT (web-vnc.exe, webvnc.conf, vnc\, ./cmd/web-vnc are REM there), so it cd's to %~dp0.. first. REM Exit codes: 0 = server ran and stopped; 1 = no binary; REM 2 = password not configured; other = web-vnc error. REM ============================================================ title web-vnc autostart cd /d "%~dp0.." echo. echo === web-vnc autostart === echo Working dir: %CD% echo. REM --- 1. Binary (build if Go is available, else fail) --- if exist "web-vnc.exe" goto havebin where go >nul 2>nul if errorlevel 1 goto nobinary echo Building web-vnc.exe ... go build -o web-vnc.exe .\cmd\web-vnc if errorlevel 1 goto nobinary :havebin if not exist "web-vnc.exe" goto nobinary REM --- 2. Stored password --- if not exist "webvnc.conf" goto noconf set "HASH=" set "VNC_PW=" for /f "tokens=1,* delims==" %%a in (webvnc.conf) do ( if /i "%%a"=="HASH" set "HASH=%%b" if /i "%%a"=="VNC_PASSWORD" set "VNC_PW=%%b" ) if "%HASH%"=="" goto noconf if "%VNC_PW%"=="" goto noconf set "WEBVNC_VNC_PASSWORD=%VNC_PW%" set "VNC_PW=" REM --- 3. Wait for the VNC server on 127.0.0.1:5900 --- REM At boot/logon the UltraVNC service may still be starting. REM Poll up to ~30 s (one PowerShell process loops internally) so we do REM NOT spawn a 2nd server and do NOT serve before the VNC server is REM ready. Exits 0 as soon as 5900 accepts a connection, 1 on timeout. echo Waiting for VNC server on 127.0.0.1:5900 (up to 30 s) ... powershell -NoProfile -Command "for($i=0;$i -lt 30;$i++){try{$c=New-Object Net.Sockets.TcpClient('127.0.0.1',5900);$c.Close();exit 0}catch{};Start-Sleep -Seconds 1};exit 1" if not errorlevel 1 goto vnc_up :vnc_down echo VNC server is not up after the wait. set "SPAWN_CMD=" if exist "vnc\winvnc.exe" set "SPAWN_CMD=vnc\winvnc.exe -run" if exist "vnc\tvnserver.exe" if not defined SPAWN_CMD set "SPAWN_CMD=vnc\tvnserver.exe -run" if defined SPAWN_CMD goto have_spawn set "SPAWNARGS=" set "SPAWNFLAG=--spawn" echo No local VNC server; web-vnc will auto-detect and spawn one. goto launch :have_spawn set "SPAWNARGS=--spawn-command "%SPAWN_CMD%"" set "SPAWNFLAG=--spawn" echo Starting local VNC server: %SPAWN_CMD% goto launch :vnc_up echo VNC server is up - connecting to it without --spawn. set "SPAWNARGS=" set "SPAWNFLAG=" :launch REM --- 4. Launch (foreground; the console window stays open while it serves) --- echo. echo Starting web-vnc on http://localhost:8080 echo Close this window to stop it. echo. web-vnc.exe --password-hash "%HASH%" %SPAWNARGS% %SPAWNFLAG% --listen :8080 echo. echo Server stopped (exit code %errorlevel%). pause exit /b :nobinary echo. echo [error] web-vnc.exe not found and could not be built. echo Build it once with: go build -o web-vnc.exe .\cmd\web-vnc echo or run run.bat interactively. pause exit /b 1 :noconf echo. echo [error] webvnc.conf not found. Run set-password.bat first to set the password. pause exit /b 2