From 71289ea6e78a2b424b5ab5f1d200a456950c196e Mon Sep 17 00:00:00 2001 From: asmtron <49522124+asmtron@users.noreply.github.com> Date: Tue, 13 Aug 2019 00:12:21 +0000 Subject: [PATCH] Update autoupdater.bat (**bugfix) bug: normal copy with admin right of the file "rdpwrap_new.ini" to "rdpwrap.ini" will not work properly (file locked) fix: we need to stream the data line by line from "rdpwrap_new.ini" to "rdpwrap.ini" add: listener check --- bin/autoupdate.bat | 148 ++++++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 55 deletions(-) diff --git a/bin/autoupdate.bat b/bin/autoupdate.bat index 150604f..798dbaf 100644 --- a/bin/autoupdate.bat +++ b/bin/autoupdate.bat @@ -7,7 +7,7 @@ REM ------------------------------------------------------------------- REM REM autoupdate.bat REM -REM Automatic RDP Wrapper installer and updater // asmtron (13-08-2019) +REM Automatic RDP Wrapper installer and updater // asmtron (14-08-2019) REM ------------------------------------------------------------------- REM Options: REM -log = redirect display output to the file autoupdate.log @@ -40,7 +40,6 @@ set rdpwrap_ini_check=%rdpwrap_ini% set rdpwrap_new_ini="%~dp0rdpwrap_new.ini" set github_location=1 - echo ___________________________________________ echo Automatic RDP Wrapper installer and updater echo. @@ -51,40 +50,40 @@ fsutil dirty query %systemdrive% >nul if not %errorlevel% == 0 goto :not_admin REM check for arguments if /i "%~1"=="-log" ( - echo %autoupdate_bat% output from %date% at %time% > %autoupdate_log% - call %autoupdate_bat% >> %autoupdate_log% - goto :finish + echo %autoupdate_bat% output from %date% at %time% > %autoupdate_log% + call %autoupdate_bat% >> %autoupdate_log% + goto :finish ) if /i "%~1"=="-taskadd" ( - echo [+] add autorun of %autoupdate_bat% on startup in the schedule task - schtasks /create /f /sc ONSTART /tn "RDP Wrapper Autoupdate" /tr "cmd.exe /C \"%~dp0autoupdate.bat\" -log" /ru SYSTEM /delay 0000:10 - goto :finish + echo [+] add autorun of %autoupdate_bat% on startup in the schedule task. + schtasks /create /f /sc ONSTART /tn "RDP Wrapper Autoupdate" /tr "cmd.exe /C \"%~dp0autoupdate.bat\" -log" /ru SYSTEM /delay 0000:10 + goto :finish ) if /i "%~1"=="-taskremove" ( - echo [-] remove autorun of %autoupdate_bat% on startup in the schedule task - schtasks /delete /f /tn "RDP Wrapper Autoupdate" - goto :finish + echo [-] remove autorun of %autoupdate_bat% on startup in the schedule task^^! + schtasks /delete /f /tn "RDP Wrapper Autoupdate" + goto :finish ) if /i not "%~1"=="" ( - echo [x] Unknown argument specified: "%~1" - echo [*] Supported argments/options are: - echo -log = redirect display output to the file autoupdate.log - echo -taskadd = add autorun of autoupdate.bat on startup in the schedule task - echo -taskremove = remove autorun of autoupdate.bat on startup in the schedule task - goto :finish + echo [x] Unknown argument specified: "%~1" + echo [*] Supported argments/options are: + echo -log = redirect display output to the file autoupdate.log + echo -taskadd = add autorun of autoupdate.bat on startup in the schedule task + echo -taskremove = remove autorun of autoupdate.bat on startup in the schedule task + goto :finish ) REM check if file "RDPWInst.exe" exist if not exist %RDPWInst_exe% goto :error_install goto :start_check :not_admin -echo [-] This script must be run as administrator to work properly! +echo [-] This script must be run as administrator to work properly^^! echo ^ echo. goto :finish :error_install -echo [-] RDP Wrapper installer executable (RDPWInst.exe) not found! -echo Please extract all files from the downloaded RDP-Wrapper package or check your Antivirus. +echo [-] RDP Wrapper installer executable (RDPWInst.exe) not found^^! +echo Please extract all files from the downloaded RDP Wrapper package or check your Antivirus. echo. goto :finish @@ -94,60 +93,76 @@ REM ---------------------------------- REM 1) check if TermService is running REM ---------------------------------- sc queryex "TermService"|find "STATE"|find /v "RUNNING" >nul&&( - echo [-] TermService not running! + echo [-] TermService NOT running^^! call :install )||( echo [+] TermService running. ) +REM ------------------------------------------ +REM 2) check if listener session rdp-tcp exist +REM ------------------------------------------ +set rdp_tcp_session="" +for /f "tokens=1-2* usebackq" %%a in ( + `query session rdp-tcp` +) do ( + set rdp_tcp_session=%%a + set rdp_tcp_session_id=%%b +) +if %rdp_tcp_session%=="" ( + echo [-] Listener session rdp-tcp NOT found^^! + call :install +) else ( + echo [+] Found listener session: %rdp_tcp_session% ^(ID: %rdp_tcp_session_id%^). +) REM ----------------------------------------- -REM 2) check if rdpwrap.dll exist in registry +REM 3) check if rdpwrap.dll exist in registry REM ----------------------------------------- -reg query HKLM\SYSTEM\CurrentControlSet\Services\TermService\Parameters /f "rdpwrap.dll" >nul&&( +reg query "HKLM\SYSTEM\CurrentControlSet\Services\TermService\Parameters" /f "rdpwrap.dll" >nul&&( echo [+] Found windows registry entry for "rdpwrap.dll". )||( - echo [-] NOT found windows registry entry for "rdpwrap.dll"! + echo [-] NOT found windows registry entry for "rdpwrap.dll"^^! if %rdpwrap_installed%=="0" ( call :install ) ) REM ------------------------------ -REM 3) check if rdpwrap.ini exists +REM 4) check if rdpwrap.ini exists REM ------------------------------ if exist %rdpwrap_ini% ( - echo [+] Found file: %rdpwrap_ini% + echo [+] Found file: %rdpwrap_ini%. ) else ( - echo [-] File NOT found: %rdpwrap_ini%! + echo [-] File NOT found: %rdpwrap_ini%^^! if %rdpwrap_installed%=="0" ( call :install ) ) REM --------------------------------------------------------------- -REM 4) get file version of %windir%\System32\termsrv.dll +REM 5) get file version of %windir%\System32\termsrv.dll REM --------------------------------------------------------------- for /f "tokens=* usebackq" %%a in ( - `cscript //nologo "%~f0?.wsf" //job:fileVersion %windir%\System32\termsrv.dll` + `cscript //nologo "%~f0?.wsf" //job:fileVersion "%windir%\System32\termsrv.dll"` ) do ( set termsrv_dll_ver=%%a ) if "%termsrv_dll_ver%"=="" ( - echo [x] Error on getting the file version of %windir%\System32\termsrv.dll + echo [x] Error on getting the file version of "%windir%\System32\termsrv.dll"^^! goto :finish ) else ( - echo [+] Installed "termsrv.dll" version: %termsrv_dll_ver% + echo [+] Installed "termsrv.dll" version: %termsrv_dll_ver%. ) REM ---------------------------------------------------------------------------------------- -REM 5) check if installed fileversion is different to the last saved fileversion in registry +REM 6) check if installed fileversion is different to the last saved fileversion in registry REM ---------------------------------------------------------------------------------------- echo [*] Read last "termsrv.dll" version from the windows registry... -for /f "tokens=2*" %%a in ( - 'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" 2^>nul' +for /f "tokens=2* usebackq" %%a in ( + `reg query "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" 2^>nul` ) do ( set last_termsrv_dll_ver=%%b ) if "%last_termsrv_dll_ver%"=="%termsrv_dll_ver%" ( - echo [+] Current "termsrv.dll v.%termsrv_dll_ver%" same as last "termsrv.dll v.%last_termsrv_dll_ver%" + echo [+] Current "termsrv.dll v.%termsrv_dll_ver%" same as last "termsrv.dll v.%last_termsrv_dll_ver%". ) else ( - echo [-] Current "termsrv.dll v.%termsrv_dll_ver%" different from last "termsrv.dll v.%last_termsrv_dll_ver%"! + echo [-] Current "termsrv.dll v.%termsrv_dll_ver%" different from last "termsrv.dll v.%last_termsrv_dll_ver%"^^! echo [*] Update current "termsrv.dll" version to the windows registry... reg add "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" /t REG_SZ /d "%termsrv_dll_ver%" /f if %rdpwrap_installed%=="0" ( @@ -155,26 +170,26 @@ if "%last_termsrv_dll_ver%"=="%termsrv_dll_ver%" ( ) ) REM --------------------------------------------------------------- -REM 6) check if installed termsrv.dll version exists in rdpwrap.ini +REM 7) check if installed termsrv.dll version exists in rdpwrap.ini REM --------------------------------------------------------------- :check_update if exist %rdpwrap_ini_check% ( - echo [*] Start searching [%termsrv_dll_ver%] version entry in file %rdpwrap_ini_check% ... + echo [*] Start searching [%termsrv_dll_ver%] version entry in file %rdpwrap_ini_check%... findstr /c:"[%termsrv_dll_ver%]" %rdpwrap_ini_check% >nul&&( - echo [+] Found "termsrv.dll" version entry [%termsrv_dll_ver%] in file %rdpwrap_ini_check% - echo [*] RDP-Wrapper seems to be up-to-date and working... + echo [+] Found "termsrv.dll" version entry [%termsrv_dll_ver%] in file %rdpwrap_ini_check%. + echo [*] RDP Wrapper seems to be up-to-date and working... )||( - echo [-] NOT found "termsrv.dll" version entry [%termsrv_dll_ver%] in file %rdpwrap_ini_check%! + echo [-] NOT found "termsrv.dll" version entry [%termsrv_dll_ver%] in file %rdpwrap_ini_check%^^! if not "!rdpwrap_ini_update_github_%github_location%!" == "" ( set rdpwrap_ini_url=!rdpwrap_ini_update_github_%github_location%! - call :update - goto :check_update + call :update + goto :check_update ) goto :finish ) ) else ( - echo [-] File NOT found: %rdpwrap_ini_check% - echo [*] Give up - Please check if Antivirus/Firewall blocks the file %rdpwrap_ini_check%! + echo [-] File NOT found: %rdpwrap_ini_check%. + echo [*] Give up - Please check if Antivirus/Firewall blocks the file %rdpwrap_ini_check%^^! goto :finish ) goto :finish @@ -184,7 +199,7 @@ REM Install RDP Wrapper (exactly uninstall and reinstall) REM ----------------------------------------------------- :install echo. -echo [*] Uninstall and reinstall RDP Wrapper ... +echo [*] Uninstall and reinstall RDP Wrapper... echo. set rdpwrap_installed="1" %RDPWInst_exe% -u @@ -192,18 +207,41 @@ set rdpwrap_installed="1" goto :eof REM ------------------- -REM Restart RDP-Wrapper +REM Restart RDP Wrapper REM ------------------- :restart if %rdpwrap_installed%=="0" ( - call :install + call :install ) +REM NOTE normal copy of the file "rdpwrap_new.ini" to "rdpwrap.ini" will not work (file locked) +REM we need to stream the data line by line from "rdpwrap_new.ini" to "rdpwrap.ini" if exist %rdpwrap_new_ini% ( - echo [*] Start copy %rdpwrap_new_ini% to %rdpwrap_ini% ... - copy /y %rdpwrap_new_ini% %rdpwrap_ini% + echo [*] Start streaming %rdpwrap_new_ini% to %rdpwrap_ini%... + set firstline="0" + for /f "usebackq delims=" %%a in ( + `findstr /n "^" %rdpwrap_new_ini%` + ) do ( + set "line=!%%a!" + set "line=!line:*:=!" + if !firstline!=="0" ( + set firstline="1" + if "!line!"=="*:=" ( + echo.>%rdpwrap_ini% + ) else ( + echo !line!>%rdpwrap_ini% + ) + ) else ( + if "!line!"=="*:=" ( + echo.>>%rdpwrap_ini% + ) else ( + echo !line!>>%rdpwrap_ini% + ) + ) + ) + echo [+] Update of %rdpwrap_ini% finished successfully. ) echo. -echo [*] Restart RDP Wrapper ... +echo [*] Restart RDP Wrapper... echo. %RDPWInst_exe% -r goto :eof @@ -214,7 +252,7 @@ REM -------------------------------------------------------------------- :update set /a github_location=github_location+1 echo. -echo [*] Download latest version of rdpwrap.ini from GitHub +echo [*] Download latest version of rdpwrap.ini from GitHub... echo -^> %rdpwrap_ini_url% for /f "tokens=* usebackq" %%a in ( `cscript //nologo "%~f0?.wsf" //job:fileDownload %rdpwrap_ini_url% %rdpwrap_new_ini%` @@ -222,12 +260,12 @@ for /f "tokens=* usebackq" %%a in ( set "download_status=%%a" ) if "%download_status%"=="-1" ( - echo [+] Successfully download from GitHhub latest version to %rdpwrap_new_ini% + echo [+] Successfully download from GitHhub latest version to %rdpwrap_new_ini%. set rdpwrap_ini_check=%rdpwrap_new_ini% call :restart ) else ( - echo [-] FAILED to download from GitHub latest version to %rdpwrap_new_ini% - echo [*] Please check you internet connection/firewall and try again! + echo [-] FAILED to download from GitHub latest version to %rdpwrap_new_ini%^^! + echo [*] Please check you internet connection/firewall and try again^^! ) goto :eof