@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM Vérifier + relancer en admin si nécessaire
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Lancement en tant qu'Administrateur...
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

REM Paramètres
set "LEN=32"
set "CHARS=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$?_-=#"
REM Caractères spéciaux seulement
set "SPECIAL=$?_-=#"

REM Tailles
set "TOTAL_LEN=69"   REM 26+26+10+7
set "SPEC_LEN=7"     REM longueur de SPECIAL

:GEN_PW
set "rustdesk_pw="

for /L %%b in (1,1,%LEN%) do (
    set /A rnd_num=!RANDOM! %% %TOTAL_LEN%
    for %%c in (!rnd_num!) do (
        set "rustdesk_pw=!rustdesk_pw!!CHARS:~%%c,1!"
    )
)

REM Test si au moins un caractère spécial est présent
set "hasSpecial=0"
for /L %%i in (0,1,%LEN%) do (
    set "ch=!rustdesk_pw:~%%i,1!"
    if defined ch (
        if "!SPECIAL:%ch%=!" NEQ "!SPECIAL!" (
            set "hasSpecial=1"
        )
    )
)

if !hasSpecial! EQU 0 (
    REM Pas de spécial : on en force un à une position aléatoire
    set /A pos=!RANDOM! %% %LEN%
    set /A spIdx=!RANDOM! %% %SPEC_LEN%

    set "before=!rustdesk_pw:~0,%pos%!"
    set "after=!rustdesk_pw:~%pos%+1!"
    set "sp=!SPECIAL:~%spIdx%,1!"

    set "rustdesk_pw=!before!!sp!!after!"
)

REM Get your config string from your Web portal and Fill Below
set rustdesk_cfg="9JSPB9markHaTxGZXVTciB3Y5UFMJJjaWR1K2YjU4AnQthjVGhUWOlVRU1USIJiOikXZrJCLiIiOikGchJCLiIiOikXYsVmciwiIl1mL5NHajVGdus2clRGdzVnciojI0N3boJye"

REM ############################### Please Do Not Edit Below This Line #########################################

if not exist C:\Temp_RustDesk\ md C:\Temp_RustDesk\
cd C:\Temp_RustDesk\

echo.
echo Telechargement de RustDesk


powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri 'https://techsy.me/rustdesk/rustdesk-latest-x86_64.exe' -OutFile 'rustdesk.exe' -UseBasicParsing"

rustdesk.exe --silent-install
timeout /t 20

cd "C:\Program Files\RustDesk\"
rustdesk.exe --install-service
timeout /t 20

for /f "delims=" %%i in ('rustdesk.exe --get-id ^| more') do set rustdesk_id=%%i

rustdesk.exe --config %rustdesk_cfg%
rustdesk.exe --password %rustdesk_pw%

set "pw_safe=%rustdesk_pw:%%=%%%%%"
set "pw_safe=%pw_safe:! =^!%"

echo.
echo ================================================

powershell -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $postData='rustdesk_id=%rustdesk_id%&mdp=!rustdesk_pw!'; (Invoke-WebRequest -UseBasicParsing -Uri 'https://myrustdesk.techsy.me/registration.php' -Method Post -Body $postData -ContentType 'application/x-www-form-urlencoded').Content"

REM Show the value of the ID Variable
echo RustDesk ID: %rustdesk_id%

REM Show the value of the Password Variable
echo Password: !rustdesk_pw!

echo ================================================
echo.

endlocal
pause

del "C:\Users\Public\Desktop\RustDesk.lnk"
del /s /f /q "C:\Temp_RustDesk\*"  >nul 2>&1
rd "C:\Temp_RustDesk" /s /q
del /f /q "%~f0" >nul 2>&1