Files
web-vnc/scripts/autostart-run.bat
T
Codex f315618bfc fix(autostart): wait for the VNC server before starting web-vnc
autostart-run.bat checked 5900 once at startup; at boot the UltraVNC service may
not be up yet, so web-vnc either spawned a 2nd VNC server (conflict) or started
serving on :8080 before the VNC server was ready, breaking browser connections.

Now autostart-run.bat polls 127.0.0.1:5900 for up to ~30s (one PowerShell loop:
TcpClient + Start-Sleep 1, exits on first accepted connection) and only then
launches web-vnc.exe -- without --spawn when 5900 already listens, or spawning a
local server (portable vnc\winvnc.exe / tvnserver.exe, else auto-detect) if it
never came up. Docs (AGENTS.md, README.md) updated.
2026-08-05 16:14:39 +03:00

71 lines
2.7 KiB
Batchfile

@echo off
REM ============================================================
REM autostart-run.bat - non-interactive launcher used by the
REM scheduled task created by install-autostart.bat.
REM Reads the password from webvnc.conf, WAITS for the VNC server
REM on 127.0.0.1:5900 (the UltraVNC service may still be starting
REM at boot), and only then starts web-vnc.exe in the foreground.
REM If no VNC server comes up, it spawns a local one (portable
REM vnc\winvnc.exe / tvnserver.exe, or auto-detected) so web-vnc
REM does not serve before the VNC server is ready.
REM Exit codes: 0 = server ran and stopped; 1 = no binary;
REM 2 = password not configured; other = web-vnc error.
REM ============================================================
cd /d "%~dp0"
REM --- 1. Binary (build if Go is available, else fail) ---
if not exist "web-vnc.exe" (
where go >nul 2>nul
if not errorlevel 1 (
go build -o web-vnc.exe .\cmd\web-vnc >nul 2>nul
)
)
if not exist "web-vnc.exe" exit /b 1
REM --- 2. Stored password ---
if not exist "webvnc.conf" exit /b 2
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%"=="" exit /b 2
if "%VNC_PW%"=="" exit /b 2
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 the UltraVNC service (uvnc_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 task host keeps it alive) ---
web-vnc.exe --password-hash "%HASH%" %SPAWNARGS% %SPAWNFLAG% --listen :8080
exit /b %errorlevel%