@echo off
rem PixelPup Remote Support - point an already-installed RustDesk client at
rem pixelpup.net. Self-elevates, writes the server config where the background
rem service (LocalSystem) actually reads it, plus any existing user profiles,
rem then restarts so it takes effect.
Title PixelPup Client Setup

rem Self-elevate - writing the service profile and restarting the service need admin.
net session >nul 2>&1
if %errorlevel% NEQ 0 (
    echo Requesting administrator privileges...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo Configuring RustDesk to use pixelpup.net...

rem 1. Service profile config. The service reads from one of two locations
rem depending on install history (systemprofile on fresh 1.4.x installs,
rem ServiceProfiles\LocalService on older/upgraded installs) - write both.
for %%D in ("C:\Windows\System32\config\systemprofile\AppData\Roaming\RustDesk\config" "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config") do (
    if not exist "%%~D" mkdir "%%~D"
    > "%%~D\RustDesk2.toml" echo [options]
    >> "%%~D\RustDesk2.toml" echo custom-rendezvous-server = 'pixelpup.net'
    >> "%%~D\RustDesk2.toml" echo relay-server = 'pixelpup.net'
    >> "%%~D\RustDesk2.toml" echo api-server = 'https://console.pixelpup.net'
    >> "%%~D\RustDesk2.toml" echo key = 'VYJnhkZ4XuVVCOLt6L1DR4Q5ySF6y97TBScSBcIDIc0='
    >> "%%~D\RustDesk2.toml" echo allow-websocket = 'N'
    >> "%%~D\RustDesk2.toml" echo enable-terminal = 'Y'
)

rem 2. Every user profile that already has a RustDesk folder (covers portable use).
for /d %%U in (C:\Users\*) do (
    if exist "%%U\AppData\Roaming\RustDesk" (
        if not exist "%%U\AppData\Roaming\RustDesk\config" mkdir "%%U\AppData\Roaming\RustDesk\config"
        > "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo [options]
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo custom-rendezvous-server = 'pixelpup.net'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo relay-server = 'pixelpup.net'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo api-server = 'https://console.pixelpup.net'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo key = 'VYJnhkZ4XuVVCOLt6L1DR4Q5ySF6y97TBScSBcIDIc0='
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo allow-websocket = 'N'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo enable-terminal = 'Y'
    )
)

rem 3. Firewall rules (idempotent - replaces any previous PixelPup rules). The
rem program-scoped inbound rule covers incoming sessions on 21118 AND the
rem ephemeral ports peer connections actually arrive on.
set "RDEXE=C:\Program Files\RustDesk\RustDesk.exe"
if exist "%RDEXE%" (
    echo Adding firewall rules...
    netsh advfirewall firewall delete rule name="PixelPup RustDesk - Client (Inbound)" >nul 2>&1
    netsh advfirewall firewall add rule name="PixelPup RustDesk - Client (Inbound)" dir=in action=allow program="%RDEXE%" enable=yes profile=any >nul
    netsh advfirewall firewall delete rule name="PixelPup RustDesk - Server & API (Outbound TCP)" >nul 2>&1
    netsh advfirewall firewall add rule name="PixelPup RustDesk - Server & API (Outbound TCP)" dir=out action=allow program="%RDEXE%" protocol=TCP remoteport=443,21114-21119 profile=any >nul
    netsh advfirewall firewall delete rule name="PixelPup RustDesk - Registration (Outbound UDP)" >nul 2>&1
    netsh advfirewall firewall add rule name="PixelPup RustDesk - Registration (Outbound UDP)" dir=out action=allow program="%RDEXE%" protocol=UDP remoteport=21116 profile=any >nul
)

rem 4. Apply: restart the service if present, otherwise restart the app.
sc query RustDesk >nul 2>&1
if %errorlevel%==0 (
    echo Restarting RustDesk service...
    net stop RustDesk >nul 2>&1
    net start RustDesk >nul 2>&1
) else (
    echo Restarting RustDesk app...
    taskkill /f /im rustdesk.exe >nul 2>&1
    timeout /t 2 >nul
    if exist "C:\Program Files\RustDesk\rustdesk.exe" start "" "C:\Program Files\RustDesk\rustdesk.exe"
)

echo.
echo Configuration complete. Your computer should appear online in the console shortly.
pause
