@echo off
setlocal EnableExtensions
title Vazin Remote Setup
fltmc >nul 2>&1
if not "%errorlevel%"=="0" (
  powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)
set "REMOTE_HOST=remote.vazin.online"
set "REMOTE_RELAY=remote.vazin.online:21117"
set "REMOTE_KEY=xGaNvHYu8vsosqJP0t22T4DF2lSC48stZB8Fd0sb3AY="
set "RUSTDESK_VERSION=1.4.7"
set "DOWNLOAD_FILE=%TEMP%\vazin-remote-rustdesk-%RUSTDESK_VERSION%.exe"
set "DOWNLOAD_URL=https://github.com/rustdesk/rustdesk/releases/download/%RUSTDESK_VERSION%/rustdesk-%RUSTDESK_VERSION%-x86_64.exe"
taskkill /F /IM rustdesk.exe >nul 2>&1
echo [1/5] Checking Vazin Remote server...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; Resolve-DnsName '%REMOTE_HOST%' -Type A|Out-Null; if(-not(Test-NetConnection '%REMOTE_HOST%' -Port 21116 -InformationLevel Quiet)){throw 'Server unreachable'}"
if not "%errorlevel%"=="0" goto :failed
echo [2/5] Downloading client...
curl.exe -fL --retry 3 --connect-timeout 20 -o "%DOWNLOAD_FILE%" "%DOWNLOAD_URL%"
if not "%errorlevel%"=="0" goto :failed
echo [3/5] Installing Windows service...
"%DOWNLOAD_FILE%" --silent-install
if not "%errorlevel%"=="0" goto :failed
timeout /t 5 /nobreak >nul
set "CLIENT_EXE=%ProgramFiles%\RustDesk\rustdesk.exe"
if not exist "%CLIENT_EXE%" set "CLIENT_EXE=%ProgramFiles%\RustDesk\RustDesk.exe"
if not exist "%CLIENT_EXE%" goto :failed
echo [4/5] Applying private server settings...
"%CLIENT_EXE%" --option custom-rendezvous-server "%REMOTE_HOST%"
"%CLIENT_EXE%" --option relay-server "%REMOTE_RELAY%"
"%CLIENT_EXE%" --option api-server ""
"%CLIENT_EXE%" --option key "%REMOTE_KEY%"
timeout /t 3 /nobreak >nul
echo [5/5] Verifying configuration...
set "VERIFY_FILE=%TEMP%\vazin-remote-server.txt"
"%CLIENT_EXE%" --option custom-rendezvous-server > "%VERIFY_FILE%"
set "ACTUAL_SERVER="
set /p ACTUAL_SERVER=<"%VERIFY_FILE%"
del /q "%VERIFY_FILE%" >nul 2>&1
if /I not "%ACTUAL_SERVER%"=="%REMOTE_HOST%" goto :failed
del /q "%DOWNLOAD_FILE%" >nul 2>&1
start "" "%CLIENT_EXE%"
echo Vazin Remote is ready.
timeout /t 4 /nobreak >nul
exit /b 0
:failed
echo Setup stopped because automatic installation could not be verified.
pause
exit /b 1
