feat: autostart as a service + separate password setup (set-password.bat)

- set-password.bat: one-time password setup -> writes webvnc.conf (HASH + VNC_PASSWORD), syncs UltraVNC via ensure-vnc-password.ps1; if the 'web-vnc' task is installed, restarts it (elevated) to reload the new password, skipping restart when the password is unchanged.
- run.bat: no longer prompts for the password; reads it from webvnc.conf (asks to run set-password.bat first if absent). Also clears --spawn-command (not just --spawn) when 5900 is already listening, to avoid spawning a 2nd VNC server.
- scripts/autostart-run.bat: non-interactive launcher for the scheduled task (reads webvnc.conf, finds only a LOCAL VNC server, never downloads at boot).
- scripts/install-autostart.bat / uninstall-autostart.bat: register/remove a 'web-vnc' scheduled task (ONSTART, SYSTEM, HIGHEST) via schtasks.
- scripts/restart-autostart.bat: restart the task to apply a new password; checks whether web-vnc.exe is running (stop+start, or just start).
- .gitignore: ignore webvnc.conf (secret).
- AGENTS.md / README.md: document the autostart flow, set-password.bat, the new scripts, and that the running gateway must be restarted to apply a password change.
This commit is contained in:
Codex
2026-08-05 15:23:58 +03:00
parent 0779b4327c
commit 754f77a8d0
9 changed files with 427 additions and 62 deletions
+52
View File
@@ -0,0 +1,52 @@
@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, finds a LOCAL VNC server
REM (does NOT download anything), and starts web-vnc.exe in the
REM foreground. The task keeps the process alive and restarts it
REM on next boot.
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. VNC server (detect local only; never download at boot) ---
set "SPAWNARGS="
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 set "SPAWNARGS=--spawn-command "%SPAWN_CMD%""
set "SPAWNFLAG=--spawn"
powershell -NoProfile -Command "try{$c=New-Object Net.Sockets.TcpClient('127.0.0.1',5900);$c.Close();exit 0}catch{exit 1}"
if not errorlevel 1 (
set "SPAWNFLAG="
set "SPAWNARGS="
)
REM --- 4. Launch (foreground; the task host keeps it alive) ---
web-vnc.exe --password-hash "%HASH%" %SPAWNARGS% %SPAWNFLAG% --listen :8080
exit /b %errorlevel%
+48
View File
@@ -0,0 +1,48 @@
@echo off
REM ============================================================
REM install-autostart.bat - register web-vnc as a startup task.
REM Creates a scheduled task "web-vnc" that runs at system startup
REM (as SYSTEM, highest privileges) and launches
REM scripts\autostart-run.bat in the foreground.
REM Requires administrator rights (auto-elevates).
REM Run set-password.bat first to configure the password.
REM ============================================================
cd /d "%~dp0"
REM Admin check + auto-elevate.
net session >nul 2>nul
if errorlevel 1 (
echo Requesting administrator rights ...
powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
if not exist "webvnc.conf" (
echo [error] Password is not configured. Run set-password.bat first.
pause
exit /b 1
)
if not exist "scripts\autostart-run.bat" (
echo [error] scripts\autostart-run.bat not found.
pause
exit /b 1
)
set "TASK=web-vnc"
set "CMD=%~dp0scripts\autostart-run.bat"
echo Creating scheduled task "%TASK%" (startup, SYSTEM, highest) ...
schtasks /Create /TN "%TASK%" /TR "cmd /c \"%CMD%\"" /SC ONSTART /RU SYSTEM /RL HIGHEST /F
if errorlevel 1 (
echo [error] Could not create the task.
pause
exit /b 1
)
echo.
echo Done. web-vnc will start automatically on every boot.
echo Start it now (without rebooting):
echo schtasks /Run /TN "%TASK%"
echo Remove it with: scripts\uninstall-autostart.bat
echo.
pause
+58
View File
@@ -0,0 +1,58 @@
@echo off
REM ============================================================
REM restart-autostart.bat - restart the "web-vnc" task so the
REM running gateway reloads webvnc.conf (e.g. after set-password.bat
REM changed the password). web-vnc.exe reads the hash and the VNC
REM password only once at startup, so a restart is the only way to
REM apply a new password to the live gateway.
REM
REM It first checks whether web-vnc.exe is actually running:
REM - running -> stop the task (schtasks /End) and start it again;
REM - not running -> just start it (no /End).
REM Requires administrator rights (the task runs as SYSTEM), so it
REM auto-elevates. Called from set-password.bat, or run manually.
REM ============================================================
cd /d "%~dp0"
net session >nul 2>nul
if errorlevel 1 (
echo Requesting administrator rights ...
powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
set "TASK=web-vnc"
schtasks /Query /TN "%TASK%" >nul 2>nul
if errorlevel 1 (
echo [error] Task "%TASK%" is not installed. Run scripts\install-autostart.bat first.
pause
exit /b 1
)
REM --- Is web-vnc.exe currently running? ---
set "RUNNING=0"
tasklist /FI "IMAGENAME eq web-vnc.exe" 2>nul | findstr /I /B /C:"web-vnc.exe" >nul
if not errorlevel 1 set "RUNNING=1"
if "%RUNNING%"=="1" (
echo web-vnc.exe is running - restarting task "%TASK%" ...
schtasks /End /TN "%TASK%" >nul 2>nul
REM give the previous process a moment to release port 8080
timeout /t 2 /nobreak >nul
) else (
echo web-vnc.exe is NOT running - just starting task "%TASK%" ...
)
schtasks /Run /TN "%TASK%"
if errorlevel 1 (
echo [error] Could not start the task.
pause
exit /b 1
)
echo.
echo Done. web-vnc reloaded the password from webvnc.conf.
echo Check it at http://localhost:8080
echo.
pause
+26
View File
@@ -0,0 +1,26 @@
@echo off
REM ============================================================
REM uninstall-autostart.bat - remove the web-vnc startup task.
REM Deletes the scheduled task "web-vnc" created by
REM install-autostart.bat. Requires administrator rights
REM (auto-elevates).
REM ============================================================
cd /d "%~dp0"
net session >nul 2>nul
if errorlevel 1 (
echo Requesting administrator rights ...
powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
set "TASK=web-vnc"
echo Removing scheduled task "%TASK%" ...
schtasks /Delete /TN "%TASK%" /F
if errorlevel 1 (
echo [error] Could not remove the task (maybe it does not exist).
pause
exit /b 1
)
echo Done.
pause