fix(autostart): run from repo root + visible console at logon

autostart-run.bat did 'cd /d %~dp0' -> scripts/, but web-vnc.exe/webvnc.conf/
go.mod live in the repo root, so the binary was never found and web-vnc never
started after reboot. Now it cd's to %~dp0.. (repo root).

The task was ONSTART/RU SYSTEM (session 0) so it could not show a window, making
failures invisible. install-autostart.bat now registers ONLOGON /RL HIGHEST as the
current user -> a visible console opens at logon. autostart-run.bat sets a window
title, prints status, stays open while web-vnc serves, and pauses on fatal errors
(no binary / no webvnc.conf) so the reason is visible. Docs updated.
This commit is contained in:
Codex
2026-08-06 01:09:47 +03:00
parent f315618bfc
commit 02fefa1e4e
5 changed files with 98 additions and 43 deletions
+56 -22
View File
@@ -1,42 +1,55 @@
@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 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 ============================================================
cd /d "%~dp0"
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 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
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" exit /b 2
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%"=="" exit /b 2
if "%VNC_PW%"=="" exit /b 2
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 the UltraVNC service (uvnc_service) may still be starting.
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.
@@ -66,6 +79,27 @@ set "SPAWNARGS="
set "SPAWNFLAG="
:launch
REM --- 4. Launch (foreground; the task host keeps it alive) ---
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
exit /b %errorlevel%
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