#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_UseUpx=y #AutoIt3Wrapper_Change2CUI=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include-once #Region ##### INCLUDES ##### #include #include #include #include #include "ServiceWrapper.au3" ;##### Required SC.exe rig. #EndRegion ##### INCLUDES ##### #Region ##### GLOBALS ##### Global $g_AppName = "Rust Desk Deployment" Global $g_DebugPath = @DesktopDir & "\WTS_RD_LOG.txt" Global $g_TempPath = @TempDir & "\WTS" Global $g_RustDesk_RegPath = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk" Global $g_RustDesk_RegKey = "Version" Global $g_RustDesk_TempInstallerPath = $g_TempPath & "\RD_Installer.exe" Global $g_RustDesk_Tags = "https://github.com/rustdesk/rustdesk/tags.atom" Global $g_RustDesk_CurrentVersion = _GetLocalVersion() Global $g_RustDesk_LatestVersion = _GetLatestVersion() Global $g_RustDesk_Download_URL = "https://github.com/rustdesk/rustdesk/releases/download/" & $g_RustDesk_LatestVersion & "/rustdesk-" & $g_RustDesk_LatestVersion & "-x86_64.msi" Global $g_RustDesk_InstalledPath = @ProgramFilesDir & "\RustDesk\RustDesk.exe" Global $g_RustDesk_InstalledPathDIR = @ProgramFilesDir & "\RustDesk" #EndRegion ##### GLOBALS ##### #Region ##### ENTRY POINT ##### _DebugInit() _Debug("+> UDFs Loaded! <+") DirCreate($g_TempPath) #EndRegion ##### ENTRY POINT ##### Func _Deploy_RustDesk() _Debug(">> _Deploy_RustDesk() : Init. <<") Local $iTimeout = 0, $iTimeout_Max = 10 _Download($g_RustDesk_Download_URL, $g_RustDesk_TempInstallerPath, "Rust Desk...") ShellExecuteWait("msiexec.exe", ' /i "' & $g_RustDesk_TempInstallerPath & '" /passive', $g_TempPath) While Not _ServiceExists("RustDesk") ; Or Not _ServiceStatus("RustDesk") = "RUNNING" Sleep(1000) $iTimeout += 1 If $iTimeout >= $iTimeout_Max Then ShellExecuteWait($g_RustDesk_InstalledPath, "--install-service", $g_RustDesk_InstalledPathDIR, Default, @SW_HIDE) _Debug("!> _Deploy_RustDesk(): TIMEOUT. Initiated manual service creation. = $iTimeout_Max Then _Debug("!> _Deploy_RustDesk(): TIMEOUT. Manual create/start failed!") _Error(10) EndIf WEnd _Debug("+> _Deploy_RustDesk(): SUCCESS!") EndFunc ;==>_Deploy_RustDesk Func _Start_RustDesk() Local $iTimer = 0 Local $iTimerTotal = 30 _Debug(">> _Start_RustDesk(): Initialization. <<") If Not _ServiceExists("RustDesk") Then _Debug("!> _Start_RustDesk(): Service does not exist! _Start_RustDesk(): Attempted to install service. <+") ElseIf Not (_ServiceStatus("RustDesk") = "RUNNING") Then _Debug("+> _Start_RustDesk(): Service exists, but isn't RUNNING <+") _Run("sc.exe", "start RustDesk", @SystemDir) _Debug("+> _Start_RustDesk(): Attempted to start service. <+") EndIf While Not _ServiceExists("RustDesk") Or Not _ServiceStatus("RustDesk") = "RUNNING" $iTimer += 5 Sleep(5000) If $iTimer >= $iTimerTotal Then ;TIMEOUT _Debug("!> _Start_RustDesk(): TIMEOUT waiting for RustDesk Services! _Start_RustDesk Func _GetLocalVersion() Local $sVersion = RegRead($g_RustDesk_RegPath, $g_RustDesk_RegKey) If @error Then _Debug("-> _GetLocalVersion(): " & "Not Installed! <-") Return False Else _Debug("+> _GetLocalVersion(): " & $sVersion & " <-") Return $sVersion EndIf EndFunc ;==>_GetLocalVersion Func _GetLatestVersion() Local $sRAW, $aVersion $sRAW = _INetGetSource($g_RustDesk_Tags) If @error Or $sRAW = "" Then _Error(4) $aVersion = StringRegExp($sRAW, "\n+.+\n+.+\n+.+\n.+(.+)<\/title>", 1) If IsArray($aVersion) Then _Debug("+> _GetLatestVersion(): " & $aVersion[0] & " <+") Return $aVersion[0] Else _Error(5) EndIf EndFunc ;==>_GetLatestVersion Func _isInstalled() RegRead($g_RustDesk_RegPath, $g_RustDesk_RegKey) If @error Then _Debug("-> _isInstalled(): " & "False <-") Return False Else _Debug("+> _isInstalled(): " & "True <+") Return True EndIf EndFunc ;==>_isInstalled Func _Get_RustDesk_ID() Local $lReturn $lReturn = _Run($g_RustDesk_InstalledPath, "--get-id", $g_RustDesk_InstalledPathDIR) If @error Then _Error(9) EndIf $lReturn = StringStripWS($lReturn, $STR_STRIPALL) _Debug(">> _Get_RustDesk_ID(): " & $lReturn & " <<") Return $lReturn EndFunc ;==>_Get_RustDesk_ID Func _Run($pExe, $pArguments = Default, $pPath = Default, $pHide = True) _Debug(">> _Run(" & $pExe & "," & $pArguments & "," & $pPath & "," & String($pHide) & ") <<") If $pHide = True Then $pHide = @SW_HIDE Else $pHide = @SW_SHOW EndIf Local $hProcess, $sOutput = "", $sLine If $pArguments = Default Then $hProcess = Run($pExe, $pPath, $pHide) If @error Then _Error(6) Else $hProcess = Run($pExe & " " & $pArguments, $pPath, $pHide, $STDERR_CHILD & $STDOUT_CHILD) If @error Then _Error(7) EndIf While 1 $sLine = StdoutRead($hProcess) If @error Then ExitLoop ; Exit loop if there's no more data $sOutput &= $sLine WEnd ProcessClose($hProcess) _Debug("-> _Run(): $sOutput = " & $sOutput & " <-") Return $sOutput EndFunc ;==>_Run Func _Set_RustDesk_Password($pPassword) _Debug(">> _Set_RustDesk_Password(" & $pPassword & ") >>") Local $lReturn = _Run($g_RustDesk_InstalledPath, "--password " & $pPassword, $g_RustDesk_InstalledPathDIR) If Not $lReturn = "Done!" Then _Error(8) Else Return True EndIf EndFunc ;==>_Set_RustDesk_Password Func _Download($pURL, $pPath, $pText = "Data...") _Debug(">> _Download(" & $pURL & "," & $pPath & ") <<") Local $lTempFile, $lBytesReceived, $lPercentage, $lFileSize, $lINet_HNDL ProgressOn("Download", "Downloading " & $pText, "0%") $lINet_HNDL = InetGet($pURL, $pPath, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND) If @error Then _Error(1) $lFileSize = InetGetSize($pURL) If @error Then _Error(2) While Not InetGetInfo($lINet_HNDL, 2) Sleep(500) $lBytesReceived = InetGetInfo($lINet_HNDL, 0) If @error Then _Error(3) $lPercentage = Int($lBytesReceived / $lFileSize * 100) ProgressSet($lPercentage, $lPercentage & "%") WEnd ProgressOff() EndFunc ;==>_Download Func _DebugInit() If FileExists($g_DebugPath) Then FileDelete($g_DebugPath) Else _Debug("----- DEBUG LOG BEGIN -----") EndIf EndFunc ;==>_DebugInit Func _Debug($pData) ConsoleWrite($pData & @CRLF) FileWriteLine($g_DebugPath, $pData) EndFunc ;==>_Debug Func _Error($pError) Local $sSupport_Info = "Please call (502)-509-7162 for support!" Switch $pError Case 1 _Debug("!> ERROR: _Download() Failed to get INetGet HNDL! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Download Failed!" & @CRLF & "Check your internet connection!" & @CRLF & $sSupport_Info) Case 2 _Debug("!> ERROR: _Download() Can't get filesize to predict progress! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Download Failed!" & @CRLF & "Check your internet connection!" & @CRLF & $sSupport_Info) Case 3 _Debug("!> ERROR: _Download() interrupted! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Download Failed!" & @CRLF & "Check your internet connection!" & @CRLF & $sSupport_Info) Case 4 _Debug("!> ERROR: _GetRemoteVersion() Failed to retrieve TAGS page! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Could not find latest version of Rust Desk!" & @CRLF & $sSupport_Info) Case 5 _Debug("!> ERROR: _GetRemoteVersion() RegEx failed to find version number! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Could not find latest version of Rust Desk!" & @CRLF & $sSupport_Info) Case 6 _Debug("!> ERROR: _Run(): Failed to run! No Args. <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Failed to Launch Application!" & @CRLF & $sSupport_Info) Case 7 _Debug("!> ERROR: _Run(): Failed to run! With Args. <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Failed to Launch Application!" & @CRLF & $sSupport_Info) Case 8 _Debug("!> ERROR: _Set_RustDesk_Password(): Failed set RustDesk Password! <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Failed to set RustDesk Password!" & @CRLF & $sSupport_Info) Case 9 _Debug("!> ERROR: _Get_RustDesk_ID(): Failed to acquire RustDesk ID. <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Failed to get RustDeskID!" & @CRLF & $sSupport_Info) Case 10 _Debug("!> ERROR: _Deploy_RustDesk(): TIMEOUTs waiting for service RUNNING. <!") MsgBox(16, "ERROR", $g_AppName & @CRLF & "Failed to properly launch RustDesk!" & @CRLF & $sSupport_Info) EndSwitch Exit EndFunc ;==>_Error