From d649a37d9769a4df551c7e4bee91eef959dca6da Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Mon, 11 Mar 2019 22:43:12 -0700 Subject: [PATCH] Linux auto-update working. --- Remotely_Agent/Services/DeviceSocket.cs | 4 -- Remotely_Agent/Services/Updater.cs | 49 ++++++++++--------- .../wwwroot/Downloads/CurrentAgentVersion.txt | 1 + .../wwwroot/Downloads/Install-Linux-x64.sh | 2 +- Utilities/Publish.ps1 | 2 + 5 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 Remotely_Server/wwwroot/Downloads/CurrentAgentVersion.txt diff --git a/Remotely_Agent/Services/DeviceSocket.cs b/Remotely_Agent/Services/DeviceSocket.cs index 91fb79ef..ba657586 100644 --- a/Remotely_Agent/Services/DeviceSocket.cs +++ b/Remotely_Agent/Services/DeviceSocket.cs @@ -49,10 +49,6 @@ namespace Remotely_Agent.Services ConnectionInfo.ServerVerificationToken = Guid.NewGuid().ToString().Replace("-",""); await HubConnection.InvokeAsync("SetServerVerificationToken", ConnectionInfo.ServerVerificationToken); Utilities.SaveConnectionInfo(ConnectionInfo); - if (!Program.IsDebug) - { - Updater.CheckForCoreUpdates(); - } } else { diff --git a/Remotely_Agent/Services/Updater.cs b/Remotely_Agent/Services/Updater.cs index 489a1a3a..3471503e 100644 --- a/Remotely_Agent/Services/Updater.cs +++ b/Remotely_Agent/Services/Updater.cs @@ -54,8 +54,8 @@ namespace Remotely_Agent.Services } var wc = new WebClient(); - var latestVersion = wc.DownloadString(Utilities.GetConnectionInfo().Host + $"/API/CoreVersion/{platform}"); - var thisVersion = FileVersionInfo.GetVersionInfo("Remotely_Agent.dll").FileVersion.ToString(); + var latestVersion = wc.DownloadString(Utilities.GetConnectionInfo().Host + $"/Downloads/CurrentAgentVersion.txt").Trim(); + var thisVersion = FileVersionInfo.GetVersionInfo("Remotely_Agent.dll").FileVersion.ToString().Trim(); if (thisVersion != latestVersion) { Logger.Write($"Service Updater: Downloading update. Current Version: {thisVersion}. Latest Version: {latestVersion}."); @@ -75,6 +75,7 @@ namespace Remotely_Agent.Services } else if (OSUtils.IsLinux) { + Process.Start("sudo", $"rm -r {Path.Combine(Path.GetTempPath(), "Remotely_Update")}"); Process.Start("sudo", "apt-get install unzip").WaitForExit(); Process.Start("sudo", $"unzip -o {tempFile} -d {Path.Combine(Path.GetTempPath(), "Remotely_Update")}").WaitForExit(); Process.Start("sudo", $"chmod -R 777 {Path.Combine(Path.GetTempPath(), "Remotely_Update")}").WaitForExit(); @@ -120,13 +121,13 @@ namespace Remotely_Agent.Services Process.Start("sudo", "systemctl stop remotely-agent"); } - foreach (var proc in Process.GetProcesses().Where(x => + foreach (var proc in Process.GetProcesses().Where(x => x.ProcessName.Contains("Remotely_Agent") && x.Id != Process.GetCurrentProcess().Id)) { proc.Kill(); } - + Logger.Write("Service Updater: Gathering files."); string targetDir = ""; @@ -139,15 +140,32 @@ namespace Remotely_Agent.Services targetDir = "/usr/local/bin/Remotely"; } - var subdirList = Directory.GetDirectories(Path.Combine(Path.GetTempPath(), "Remotely_Update")); - var fileList = Directory.GetFiles(Path.Combine(Path.GetTempPath(), "Remotely_Update")); Logger.Write("Service Updater: Copying new files."); + + var fileList = Directory.GetFiles(Path.Combine(Path.GetTempPath(), "Remotely_Update")); + foreach (var file in fileList) + { + try + { + var targetPath = Path.Combine(targetDir, Path.GetFileName(file)); + File.Copy(file, targetPath, true); + } + catch (Exception ex) + { + Logger.Write(ex); + continue; + } + } + + + var subdirList = Directory.GetDirectories(Path.Combine(Path.GetTempPath(), "Remotely_Update")); + foreach (var subdir in subdirList) { try { - var targetPath = Path.Combine(targetDir, Path.GetDirectoryName(subdir)); + var targetPath = Path.Combine(targetDir, Path.GetFileName(subdir.TrimEnd(Path.DirectorySeparatorChar))); if (Directory.Exists(targetPath)) { Directory.Delete(targetPath, true); @@ -157,25 +175,10 @@ namespace Remotely_Agent.Services catch (Exception ex) { Logger.Write(ex); + continue; } } - foreach (var file in fileList) - { - try - { - var targetPath = Path.Combine(targetDir, Path.GetFileName(file)); - if (File.Exists(targetPath)) - { - File.Delete(targetPath); - } - File.Move(file, targetPath); - } - catch (Exception ex) - { - Logger.Write(ex); - } - } Logger.Write("Service Updater: Update completed."); } catch (Exception ex) diff --git a/Remotely_Server/wwwroot/Downloads/CurrentAgentVersion.txt b/Remotely_Server/wwwroot/Downloads/CurrentAgentVersion.txt new file mode 100644 index 00000000..a784ccb3 --- /dev/null +++ b/Remotely_Server/wwwroot/Downloads/CurrentAgentVersion.txt @@ -0,0 +1 @@ +2019.03.11.2236 diff --git a/Remotely_Server/wwwroot/Downloads/Install-Linux-x64.sh b/Remotely_Server/wwwroot/Downloads/Install-Linux-x64.sh index d0133e16..53a9aea4 100644 --- a/Remotely_Server/wwwroot/Downloads/Install-Linux-x64.sh +++ b/Remotely_Server/wwwroot/Downloads/Install-Linux-x64.sh @@ -4,7 +4,7 @@ GUID=$(cat /proc/sys/kernel/random/uuid) systemctl stop remotely-client rm -r -f /usr/local/bin/Remotely -rm -f /etc/systemd/system/remotely-client.service +rm -f /etc/systemd/system/remotely-agent.service systemctl daemon-reload if [ "$1" = "--uninstall" ]; then diff --git a/Utilities/Publish.ps1 b/Utilities/Publish.ps1 index 043bdf72..58a5cf89 100644 --- a/Utilities/Publish.ps1 +++ b/Utilities/Publish.ps1 @@ -56,6 +56,8 @@ Set-Location -Path (Get-Item -Path $PSScriptRoot).Parent.FullName if ($ArgList.Contains("c")) { + # Add Current Version file to Downloads folder for clients to check. + Set-Content -Path ".\Remotely_Server\wwwroot\Downloads\CurrentAgentVersion.txt" -Value $CurrentVersion.Trim() -Force # Copy .NET Framework ScreenCaster to Agent resources for embedding. if ((Test-Path -Path ".\Remotely_Agent\Resources") -eq $false) { New-Item -Path ".\Remotely_Agent\Resources" -ItemType Directory