Haven/Start Haven.bat
ancsemi 623ef079e1 v2.8.5 — noise gate fix, screen share rejoin, setup script improvements
- Fix noise gate/AI suppression lost after mic device switch (#212)
- Fix screen share + webcam not renegotiated on voice-rejoin (#213)
- Start Haven.bat / start.sh now read PORT from .env (#214)
- SSL cert generation errors are no longer suppressed (#214)
- Desktop download links updated to v1.1.7
2026-03-29 14:45:38 -04:00

247 lines
7.8 KiB
Batchfile

@echo off
title Haven Server
color 0A
echo.
echo ========================================
echo HAVEN - Private Chat Server
echo ========================================
echo.
:: ── Data directory (%APPDATA%\Haven) ──────────────────────
set "HAVEN_DATA=%APPDATA%\Haven"
if not exist "%HAVEN_DATA%" mkdir "%HAVEN_DATA%"
:: Read PORT from .env (default 3000)
set "HAVEN_PORT=3000"
if exist "%HAVEN_DATA%\.env" (
for /f "tokens=1,* delims==" %%A in ('findstr /B /I "PORT=" "%HAVEN_DATA%\.env"') do (
set "HAVEN_PORT=%%B"
)
)
:: Kill any existing Haven server on the configured port
echo [*] Checking for existing server...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%HAVEN_PORT%" ^| findstr "LISTENING"') do (
echo [!] Killing existing process on port %HAVEN_PORT% (PID: %%a)
taskkill /PID %%a /F >nul 2>&1
)
:: Check Node.js is installed
where node >nul 2>&1
if %ERRORLEVEL% EQU 0 goto :NODE_OK
color 0E
echo.
echo [!] Node.js is not installed or not in PATH.
echo.
echo You have two options:
echo.
echo 1) Press Y below to install it automatically (downloads ~30 MB)
echo.
echo 2) Or download it manually from https://nodejs.org
echo.
set /p "AUTOINSTALL= Would you like to install Node.js automatically now? [Y/N]: "
if /i "%AUTOINSTALL%" NEQ "Y" goto :NODE_SKIP
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0install-node.ps1"
if %ERRORLEVEL% NEQ 0 (
color 0C
echo.
echo [ERROR] Automatic install failed. Please install manually from https://nodejs.org
echo.
pause
exit /b 1
)
echo.
echo [OK] Node.js installed! Close this window and double-click Start Haven again.
echo Node.js needs a fresh terminal to be recognized.
echo.
pause
exit /b 0
:NODE_SKIP
echo.
echo [*] No problem. Install Node.js from https://nodejs.org and try again.
echo.
pause
exit /b 1
:NODE_OK
for /f "tokens=1 delims=v." %%v in ('node -v 2^>nul') do set "NODE_MAJOR=%%v"
echo [OK] Node.js found: & node -v
:: Warn if Node major version is too new (native modules won't have prebuilts)
if defined NODE_MAJOR (
if %NODE_MAJOR% GEQ 24 (
color 0E
echo.
echo [!] WARNING: Node.js v%NODE_MAJOR% detected. Haven requires Node 18-22.
echo Native modules like better-sqlite3 may not have prebuilt
echo binaries yet, causing build failures.
echo.
echo Please install Node.js 22 LTS from https://nodejs.org
echo.
pause
exit /b 1
)
)
:: Always install/update dependencies (fast when already up-to-date)
cd /d "%~dp0"
echo [*] Checking dependencies...
call npm install --no-audit --no-fund 2>&1
if %ERRORLEVEL% NEQ 0 (
color 0C
echo.
echo [ERROR] npm install failed. Check the errors above.
echo.
pause
exit /b 1
)
echo [OK] Dependencies ready
echo.
:: Check .env exists in APPDATA data directory
if not exist "%HAVEN_DATA%\.env" (
if exist "%~dp0.env.example" (
echo [*] Creating .env in %HAVEN_DATA% from template...
copy "%~dp0.env.example" "%HAVEN_DATA%\.env" >nul
)
echo [!] IMPORTANT: Edit %HAVEN_DATA%\.env and change your settings before going live!
echo.
)
:: Detect local IP for SSL certificate SAN (Subject Alternative Name)
set "LOCAL_IP=127.0.0.1"
for /f "tokens=2 delims=:" %%A in ('ipconfig ^| findstr /R /C:"IPv4 Address"') do (
for /f "tokens=*" %%B in ("%%A") do (
if not "%%B"=="" set "LOCAL_IP=%%B"
)
)
:: Generate self-signed SSL certs in data directory if missing (skip if FORCE_HTTP=true)
if /I "%FORCE_HTTP%"=="true" (
echo [*] FORCE_HTTP=true -- skipping SSL certificate generation
echo.
) else if not exist "%HAVEN_DATA%\certs\cert.pem" (
echo [*] Generating self-signed SSL certificate...
if not exist "%HAVEN_DATA%\certs" mkdir "%HAVEN_DATA%\certs"
:: Try to find openssl on PATH first, then check common install locations
set "OPENSSL_CMD="
where openssl >nul 2>&1
if not errorlevel 1 (
set "OPENSSL_CMD=openssl"
) else (
for %%D in (
"C:\Program Files\OpenSSL-Win64\bin"
"C:\Program Files\OpenSSL\bin"
"C:\Program Files (x86)\OpenSSL-Win32\bin"
"C:\OpenSSL-Win64\bin"
"C:\OpenSSL-Win32\bin"
"C:\OpenSSL\bin"
) do (
if exist "%%~D\openssl.exe" (
if not defined OPENSSL_CMD (
set "OPENSSL_CMD=%%~D\openssl.exe"
echo [*] Found OpenSSL at %%~D
)
)
)
)
if not defined OPENSSL_CMD (
echo [!] OpenSSL not found on PATH or in common install directories.
echo Haven will run in HTTP mode. See README for details.
echo To enable HTTPS, install OpenSSL or add it to your PATH.
echo Common install location: C:\Program Files\OpenSSL-Win64\bin
) else (
:: SAN (Subject Alternative Name) is required by modern browsers for HTTPS
"%OPENSSL_CMD%" req -x509 -newkey rsa:2048 -keyout "%HAVEN_DATA%\certs\key.pem" -out "%HAVEN_DATA%\certs\cert.pem" -days 3650 -nodes -subj "/CN=Haven" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1,IP:%LOCAL_IP%"
if exist "%HAVEN_DATA%\certs\cert.pem" (
echo [OK] SSL certificate generated in %HAVEN_DATA%\certs
) else (
echo [!] SSL certificate generation failed.
echo Haven will run in HTTP mode. See README for details.
)
)
echo.
)
echo [*] Data directory: %HAVEN_DATA%
echo [*] Starting Haven server...
echo.
:: Start server in background
cd /d "%~dp0"
start /B node server.js
:: Wait for server to be ready
echo [*] Waiting for server to start...
set RETRIES=0
:WAIT_LOOP
timeout /t 1 /nobreak >nul
set /a RETRIES+=1
netstat -ano | findstr ":%HAVEN_PORT%" | findstr "LISTENING" >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
if %RETRIES% GEQ 15 (
color 0C
echo [ERROR] Server failed to start after 15 seconds.
echo Check the output above for errors.
pause
exit /b 1
)
goto WAIT_LOOP
)
:: Detect protocol based on whether certs exist and server can use them
set "HAVEN_PROTO=http"
if /I "%FORCE_HTTP%"=="true" (
set "HAVEN_PROTO=http"
) else if exist "%HAVEN_DATA%\certs\cert.pem" (
if exist "%HAVEN_DATA%\certs\key.pem" (
set "HAVEN_PROTO=https"
)
)
echo.
if "%HAVEN_PROTO%"=="https" (
echo ========================================
echo Haven is LIVE on port %HAVEN_PORT% ^(HTTPS^)
echo ========================================
echo.
echo Local: https://localhost:%HAVEN_PORT%
echo LAN: https://YOUR_LOCAL_IP:%HAVEN_PORT%
echo Remote: https://YOUR_PUBLIC_IP:%HAVEN_PORT%
echo.
echo First time? Your browser will show a security
echo warning ^(self-signed cert^). Click "Advanced"
echo then "Proceed" to continue.
) else (
echo ========================================
echo Haven is LIVE on port %HAVEN_PORT% ^(HTTP^)
echo ========================================
echo.
echo Local: http://localhost:%HAVEN_PORT%
echo LAN: http://YOUR_LOCAL_IP:%HAVEN_PORT%
echo Remote: http://YOUR_PUBLIC_IP:%HAVEN_PORT%
echo.
echo NOTE: Running without SSL. Voice chat and
echo remote connections work best with HTTPS.
echo See README for how to enable HTTPS.
)
echo.
:: ── Open browser automatically ──────────────────────────────
echo [*] Opening browser...
start %HAVEN_PROTO%://localhost:%HAVEN_PORT%
echo.
echo ----------------------------------------
echo Server is running. Close this window
echo or press Ctrl+C to stop the server.
echo ----------------------------------------
echo.
:: Keep window open so server stays alive
:KEEPALIVE
timeout /t 3600 /nobreak >nul
goto KEEPALIVE