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
This commit is contained in:
asmtron 2019-08-13 00:12:21 +00:00 committed by GitHub
commit 71289ea6e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ REM -------------------------------------------------------------------
REM REM
REM autoupdate.bat REM autoupdate.bat
REM 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 -------------------------------------------------------------------
REM Options: REM Options:
REM -log = redirect display output to the file autoupdate.log 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 rdpwrap_new_ini="%~dp0rdpwrap_new.ini"
set github_location=1 set github_location=1
echo ___________________________________________ echo ___________________________________________
echo Automatic RDP Wrapper installer and updater echo Automatic RDP Wrapper installer and updater
echo. echo.
@ -56,12 +55,12 @@ if /i "%~1"=="-log" (
goto :finish goto :finish
) )
if /i "%~1"=="-taskadd" ( if /i "%~1"=="-taskadd" (
echo [+] add autorun of %autoupdate_bat% on startup in the schedule task 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 schtasks /create /f /sc ONSTART /tn "RDP Wrapper Autoupdate" /tr "cmd.exe /C \"%~dp0autoupdate.bat\" -log" /ru SYSTEM /delay 0000:10
goto :finish goto :finish
) )
if /i "%~1"=="-taskremove" ( if /i "%~1"=="-taskremove" (
echo [-] remove autorun of %autoupdate_bat% on startup in the schedule task echo [-] remove autorun of %autoupdate_bat% on startup in the schedule task^^!
schtasks /delete /f /tn "RDP Wrapper Autoupdate" schtasks /delete /f /tn "RDP Wrapper Autoupdate"
goto :finish goto :finish
) )
@ -78,13 +77,13 @@ if not exist %RDPWInst_exe% goto :error_install
goto :start_check goto :start_check
:not_admin :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 ^<Please use 'right click' on this batch file and select "Run As Administrator"^> echo ^<Please use 'right click' on this batch file and select "Run As Administrator"^>
echo. echo.
goto :finish goto :finish
:error_install :error_install
echo [-] RDP Wrapper installer executable (RDPWInst.exe) not found! 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 Please extract all files from the downloaded RDP Wrapper package or check your Antivirus.
echo. echo.
goto :finish goto :finish
@ -94,60 +93,76 @@ REM ----------------------------------
REM 1) check if TermService is running REM 1) check if TermService is running
REM ---------------------------------- REM ----------------------------------
sc queryex "TermService"|find "STATE"|find /v "RUNNING" >nul&&( sc queryex "TermService"|find "STATE"|find /v "RUNNING" >nul&&(
echo [-] TermService not running! echo [-] TermService NOT running^^!
call :install call :install
)||( )||(
echo [+] TermService running. 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 -----------------------------------------
REM 2) check if rdpwrap.dll exist in registry REM 3) check if rdpwrap.dll exist in registry
REM ----------------------------------------- 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 [+] 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" ( if %rdpwrap_installed%=="0" (
call :install call :install
) )
) )
REM ------------------------------ REM ------------------------------
REM 3) check if rdpwrap.ini exists REM 4) check if rdpwrap.ini exists
REM ------------------------------ REM ------------------------------
if exist %rdpwrap_ini% ( if exist %rdpwrap_ini% (
echo [+] Found file: %rdpwrap_ini% echo [+] Found file: %rdpwrap_ini%.
) else ( ) else (
echo [-] File NOT found: %rdpwrap_ini%! echo [-] File NOT found: %rdpwrap_ini%^^!
if %rdpwrap_installed%=="0" ( if %rdpwrap_installed%=="0" (
call :install call :install
) )
) )
REM --------------------------------------------------------------- REM ---------------------------------------------------------------
REM 4) get file version of %windir%\System32\termsrv.dll REM 5) get file version of %windir%\System32\termsrv.dll
REM --------------------------------------------------------------- REM ---------------------------------------------------------------
for /f "tokens=* usebackq" %%a in ( 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 ( ) do (
set termsrv_dll_ver=%%a set termsrv_dll_ver=%%a
) )
if "%termsrv_dll_ver%"=="" ( 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 goto :finish
) else ( ) else (
echo [+] Installed "termsrv.dll" version: %termsrv_dll_ver% echo [+] Installed "termsrv.dll" version: %termsrv_dll_ver%.
) )
REM ---------------------------------------------------------------------------------------- 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 ---------------------------------------------------------------------------------------- REM ----------------------------------------------------------------------------------------
echo [*] Read last "termsrv.dll" version from the windows registry... echo [*] Read last "termsrv.dll" version from the windows registry...
for /f "tokens=2*" %%a in ( for /f "tokens=2* usebackq" %%a in (
'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" 2^>nul' `reg query "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" 2^>nul`
) do ( ) do (
set last_termsrv_dll_ver=%%b set last_termsrv_dll_ver=%%b
) )
if "%last_termsrv_dll_ver%"=="%termsrv_dll_ver%" ( 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 ( ) 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... 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 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\RDP-Wrapper\Autoupdate" /v "termsrv.dll" /t REG_SZ /d "%termsrv_dll_ver%" /f
if %rdpwrap_installed%=="0" ( if %rdpwrap_installed%=="0" (
@ -155,16 +170,16 @@ if "%last_termsrv_dll_ver%"=="%termsrv_dll_ver%" (
) )
) )
REM --------------------------------------------------------------- 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 --------------------------------------------------------------- REM ---------------------------------------------------------------
:check_update :check_update
if exist %rdpwrap_ini_check% ( 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&&( findstr /c:"[%termsrv_dll_ver%]" %rdpwrap_ini_check% >nul&&(
echo [+] Found "termsrv.dll" version entry [%termsrv_dll_ver%] in file %rdpwrap_ini_check% 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 [*] 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%!" == "" ( if not "!rdpwrap_ini_update_github_%github_location%!" == "" (
set rdpwrap_ini_url=!rdpwrap_ini_update_github_%github_location%! set rdpwrap_ini_url=!rdpwrap_ini_update_github_%github_location%!
call :update call :update
@ -173,8 +188,8 @@ if exist %rdpwrap_ini_check% (
goto :finish goto :finish
) )
) else ( ) else (
echo [-] File NOT found: %rdpwrap_ini_check% echo [-] File NOT found: %rdpwrap_ini_check%.
echo [*] Give up - Please check if Antivirus/Firewall blocks the file %rdpwrap_ini_check%! echo [*] Give up - Please check if Antivirus/Firewall blocks the file %rdpwrap_ini_check%^^!
goto :finish goto :finish
) )
goto :finish goto :finish
@ -192,15 +207,38 @@ set rdpwrap_installed="1"
goto :eof goto :eof
REM ------------------- REM -------------------
REM Restart RDP-Wrapper REM Restart RDP Wrapper
REM ------------------- REM -------------------
:restart :restart
if %rdpwrap_installed%=="0" ( 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% ( if exist %rdpwrap_new_ini% (
echo [*] Start copy %rdpwrap_new_ini% to %rdpwrap_ini% ... echo [*] Start streaming %rdpwrap_new_ini% to %rdpwrap_ini%...
copy /y %rdpwrap_new_ini% %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.
echo [*] Restart RDP Wrapper... echo [*] Restart RDP Wrapper...
@ -214,7 +252,7 @@ REM --------------------------------------------------------------------
:update :update
set /a github_location=github_location+1 set /a github_location=github_location+1
echo. echo.
echo [*] Download latest version of rdpwrap.ini from GitHub echo [*] Download latest version of rdpwrap.ini from GitHub...
echo -^> %rdpwrap_ini_url% echo -^> %rdpwrap_ini_url%
for /f "tokens=* usebackq" %%a in ( for /f "tokens=* usebackq" %%a in (
`cscript //nologo "%~f0?.wsf" //job:fileDownload %rdpwrap_ini_url% %rdpwrap_new_ini%` `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" set "download_status=%%a"
) )
if "%download_status%"=="-1" ( 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% set rdpwrap_ini_check=%rdpwrap_new_ini%
call :restart call :restart
) else ( ) else (
echo [-] FAILED to download from GitHub latest version to %rdpwrap_new_ini% echo [-] FAILED to download from GitHub latest version to %rdpwrap_new_ini%^^!
echo [*] Please check you internet connection/firewall and try again! echo [*] Please check you internet connection/firewall and try again^^!
) )
goto :eof goto :eof