Skip to content

Commit

Permalink
Revert frame diffing to original method. Add Clear Logs/Scripting His…
Browse files Browse the repository at this point in the history
…tory. Alert for outdated .NET Framework version.
  • Loading branch information
bitbound committed Jan 12, 2021
1 parent 6e1f951 commit 5291cb2
Show file tree
Hide file tree
Showing 29 changed files with 499 additions and 661 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CA1416: Validate platform compatibility
dotnet_diagnostic.CA1416.severity = none
2 changes: 1 addition & 1 deletion Agent.Installer.Win/Agent.Installer.Win.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>Remotely.Agent.Installer.Win</RootNamespace>
<AssemblyName>Remotely_Installer</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand Down
2 changes: 1 addition & 1 deletion Agent.Installer.Win/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
11 changes: 11 additions & 0 deletions Agent/Interfaces/IUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace Remotely.Agent.Interfaces
{
public interface IUpdater
{
Task BeginChecking();
Task CheckForUpdates();
Task InstallLatestVersion();
}
}
9 changes: 7 additions & 2 deletions Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ private static void BuildServices()
serviceCollection.AddTransient<PSCore>();
serviceCollection.AddTransient<WindowsPS>();
serviceCollection.AddScoped<ConfigService>();
serviceCollection.AddSingleton<Updater>();
serviceCollection.AddScoped<Uninstaller>();
serviceCollection.AddScoped<ScriptRunner>();
serviceCollection.AddScoped<CommandExecutor>();

if (EnvironmentHelper.IsWindows)
{
serviceCollection.AddScoped<IAppLauncher, AppLauncherWin>();
serviceCollection.AddSingleton<IUpdater, UpdaterWin>();
}
else if (EnvironmentHelper.IsLinux)
{
serviceCollection.AddScoped<IAppLauncher, AppLauncherLinux>();
serviceCollection.AddSingleton<IUpdater, UpdaterLinux>();
}
else if (EnvironmentHelper.IsMac)
{
Expand Down Expand Up @@ -105,10 +106,14 @@ private static async Task Init()
});
}

await Services.GetRequiredService<Updater>().BeginChecking();
await Services.GetRequiredService<IUpdater>().BeginChecking();

await Services.GetRequiredService<AgentSocket>().Connect();
}
catch (Exception ex)
{
Logger.Write(ex);
}
finally
{
await Services.GetRequiredService<AgentSocket>().HandleConnection();
Expand Down
21 changes: 18 additions & 3 deletions Agent/Services/AgentSocket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Remotely.Agent.Interfaces;
using Remotely.Agent.Utilities;
using Remotely.Shared.Enums;
using Remotely.Shared.Models;
using Remotely.Shared.Services;
Expand All @@ -27,7 +28,7 @@ public AgentSocket(ConfigService configService,
ScriptRunner scriptRunner,
ChatClientService chatService,
IAppLauncher appLauncher,
Updater updater)
IUpdater updater)
{
ConfigService = configService;
Uninstaller = uninstaller;
Expand All @@ -40,7 +41,6 @@ public AgentSocket(ConfigService configService,
public bool IsConnected => HubConnection?.State == HubConnectionState.Connected;
private IAppLauncher AppLauncher { get; }
private ChatClientService ChatService { get; }
private Updater Updater { get; }
private CommandExecutor CommandExecutor { get; }
private ConfigService ConfigService { get; }
private ConnectionInfo ConnectionInfo { get; set; }
Expand All @@ -49,6 +49,8 @@ public AgentSocket(ConfigService configService,
private bool IsServerVerified { get; set; }
private ScriptRunner ScriptRunner { get; }
private Uninstaller Uninstaller { get; }
private IUpdater Updater { get; }

public async Task Connect()
{
try
Expand Down Expand Up @@ -102,6 +104,14 @@ public async Task Connect()
HeartbeatTimer = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds);
HeartbeatTimer.Elapsed += HeartbeatTimer_Elapsed;
HeartbeatTimer.Start();

if (EnvironmentHelper.IsWindows &&
!RegistryHelper.CheckNetFrameworkVersion())
{
await SendDeviceAlert("The .NET Framework version on this device is outdated, and " +
"Remotely will no longer receive updates. Update the installed .NET Framework version " +
"to fix this.");
}
}
catch (Exception ex)
{
Expand All @@ -121,7 +131,7 @@ public async Task HandleConnection()
Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds...");
await Task.Delay(waitTime);
await Program.Services.GetRequiredService<AgentSocket>().Connect();
await Program.Services.GetRequiredService<Updater>().CheckForUpdates();
await Program.Services.GetRequiredService<IUpdater>().CheckForUpdates();
}
}
catch (Exception ex)
Expand All @@ -132,6 +142,10 @@ public async Task HandleConnection()
}
}

public async Task SendDeviceAlert(string alertMessage)
{
await HubConnection.SendAsync("AddDeviceAlert", alertMessage);
}
public async Task SendHeartbeat()
{
var currentInfo = await DeviceInformation.Create(ConnectionInfo.DeviceID, ConnectionInfo.OrganizationID);
Expand Down Expand Up @@ -326,5 +340,6 @@ private void RegisterMessageHandlers()
}
});
}

}
}
98 changes: 30 additions & 68 deletions Agent/Services/Updater.cs → Agent/Services/UpdaterLinux.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using Remotely.Shared.Utilities;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Remotely.Agent.Services
{
public class Updater
public class UpdaterLinux : IUpdater
{
public Updater(ConfigService configService)
public UpdaterLinux(ConfigService configService)
{
ConfigService = configService;
}
Expand Down Expand Up @@ -56,21 +60,7 @@ public async Task CheckForUpdates()
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 fileUrl = serverUrl + $"/Downloads/Remotely-Linux.zip";

var lastEtag = string.Empty;

Expand Down Expand Up @@ -127,66 +117,38 @@ public async Task InstallLatestVersion()
var downloadId = Guid.NewGuid().ToString();
var zipPath = Path.Combine(Path.GetTempPath(), "RemotelyUpdate.zip");

if (EnvironmentHelper.IsWindows)
{
var installerPath = Path.Combine(Path.GetTempPath(), "Remotely_Installer.exe");
var platform = Environment.Is64BitOperatingSystem ? "x64" : "x86";

await wc.DownloadFileTaskAsync(
serverUrl + $"/Downloads/Remotely_Installer.exe",
installerPath);

await wc.DownloadFileTaskAsync(
serverUrl + $"/api/AgentUpdate/DownloadPackage/win-{platform}/{downloadId}",
zipPath);

(await WebRequest.CreateHttp(serverUrl + $"/api/AgentUpdate/ClearDownload/{downloadId}").GetResponseAsync()).Dispose();


foreach (var proc in Process.GetProcessesByName("Remotely_Installer"))
{
proc.Kill();
}
var installerPath = Path.Combine(Path.GetTempPath(), "RemotelyUpdate.sh");

Logger.Write("Launching installer to perform update.");
string platform;

Process.Start(installerPath, $"-install -quiet -path {zipPath} -serverurl {serverUrl} -organizationid {connectionInfo.OrganizationID}");
if (RuntimeInformation.OSDescription.Contains("Ubuntu", StringComparison.OrdinalIgnoreCase))
{
platform = "Ubuntu-x64";
}
else if (EnvironmentHelper.IsLinux)
else if (RuntimeInformation.OSDescription.Contains("Manjaro", StringComparison.OrdinalIgnoreCase))
{
var installerPath = Path.Combine(Path.GetTempPath(), "RemotelyUpdate.sh");

string platform;

if (RuntimeInformation.OSDescription.Contains("Ubuntu", StringComparison.OrdinalIgnoreCase))
{
platform = "Ubuntu-x64";
}
else if (RuntimeInformation.OSDescription.Contains("Manjaro", StringComparison.OrdinalIgnoreCase))
{
platform = "Manjaro-x64";
}
else
{
throw new PlatformNotSupportedException();
}
platform = "Manjaro-x64";
}
else
{
throw new PlatformNotSupportedException();
}

await wc.DownloadFileTaskAsync(
serverUrl + $"/API/ClientDownloads/{connectionInfo.OrganizationID}/{platform}",
installerPath);
await wc.DownloadFileTaskAsync(
serverUrl + $"/API/ClientDownloads/{connectionInfo.OrganizationID}/{platform}",
installerPath);

await wc.DownloadFileTaskAsync(
serverUrl + $"/API/AgentUpdate/DownloadPackage/linux/{downloadId}",
zipPath);
await wc.DownloadFileTaskAsync(
serverUrl + $"/API/AgentUpdate/DownloadPackage/linux/{downloadId}",
zipPath);

(await WebRequest.CreateHttp(serverUrl + $"/api/AgentUpdate/ClearDownload/{downloadId}").GetResponseAsync()).Dispose();
(await WebRequest.CreateHttp(serverUrl + $"/api/AgentUpdate/ClearDownload/{downloadId}").GetResponseAsync()).Dispose();

Logger.Write("Launching installer to perform update.");
Logger.Write("Launching installer to perform update.");

Process.Start("sudo", $"chmod +x {installerPath}").WaitForExit();
Process.Start("sudo", $"chmod +x {installerPath}").WaitForExit();

Process.Start("sudo", $"{installerPath} --path {zipPath} & disown");
}
Process.Start("sudo", $"{installerPath} --path {zipPath} & disown");
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.Timeout)
{
Expand Down
Loading

0 comments on commit 5291cb2

Please sign in to comment.