public async Task CheckForUpdates() { try { if (EnvironmentHelper.IsDebug) { return; } await UpdateLock.WaitAsync(); var connectionInfo = ConfigService.GetConnectionInfo(); var serverUrl = ConfigService.GetConnectionInfo().Host; string fileUrl; if (EnvironmentHelper.IsWindows) { var platform = Environment.Is64BitOperatingSystem ? "x64" : "x86"; fileUrl = serverUrl + $"/Downloads/Remotely-Win10-{platform}.zip"; } else if (EnvironmentHelper.IsLinux) { fileUrl = serverUrl + $"/Downloads/Remotely-Linux.zip"; } else { throw new PlatformNotSupportedException(); } var lastEtag = string.Empty; if (File.Exists("etag.txt")) { lastEtag = await File.ReadAllTextAsync("etag.txt"); } try { var wr = WebRequest.CreateHttp(fileUrl); wr.Method = "Head"; wr.Headers.Add("If-None-Match", lastEtag); using var response = (HttpWebResponse) await wr.GetResponseAsync(); if (response.StatusCode == HttpStatusCode.NotModified) { Logger.Write("Service Updater: Version is current."); return; } } catch (WebException ex) when((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotModified) { Logger.Write("Service Updater: Version is current."); return; } Logger.Write("Service Updater: Update found."); await InstallLatestVersion(); } catch (Exception ex) { Logger.Write(ex); } finally { UpdateLock.Release(); } }
public async Task CheckForUpdates() { try { await UpdateLock.WaitAsync(); var hc = new HttpClient(); var wc = new WebClient(); var connectionInfo = ConfigService.GetConnectionInfo(); var response = await hc.GetAsync(connectionInfo.Host + $"/API/AgentUpdate/CurrentVersion"); var latestVersion = response.Content.ReadAsStringAsync().Result; var thisVersion = FileVersionInfo.GetVersionInfo("Remotely_Agent.dll").FileVersion.ToString().Trim(); if (thisVersion != latestVersion) { Logger.Write($"Service Updater: Update found. Current Version: {thisVersion}. Latest Version: {latestVersion}."); var updateWindow = int.Parse(wc.DownloadString(connectionInfo.Host + $"/API/AgentUpdate/UpdateWindow")); var waitTime = new Random().Next(1, updateWindow); Logger.Write($"Waiting {waitTime} seconds before updating."); await Task.Delay(TimeSpan.FromSeconds(waitTime)); Logger.Write($"Service Updater: Downloading installer."); if (OSUtils.IsWindows) { var filePath = Path.Combine(Path.GetTempPath(), "Remotely_Installer.exe"); wc.DownloadFile( ConfigService.GetConnectionInfo().Host + $"/Downloads/Remotely_Installer.exe", filePath); foreach (var proc in Process.GetProcessesByName("Remotely_Installer")) { proc.Kill(); } Process.Start(filePath, $"-install -quiet -serverurl {connectionInfo.Host} -organizationid {connectionInfo.OrganizationID}"); } else if (OSUtils.IsLinux) { var filePath = Path.Combine(Path.GetTempPath(), "RemotelyUpdate.sh"); wc.DownloadFile( ConfigService.GetConnectionInfo().Host + $"/API/ClientDownloads/{connectionInfo.OrganizationID}/Linux-x64", filePath); Process.Start("sudo", $"chmod +x {filePath}").WaitForExit(); Process.Start("sudo", $"{filePath} & disown"); } } } catch (Exception ex) { Logger.Write(ex); } finally { UpdateLock.Release(); } }