Skip to content

Commit

Permalink
Re-structure projects. Introduce WPF/Avalonia chat windows. Record se…
Browse files Browse the repository at this point in the history
…ssion from browser.
  • Loading branch information
bitbound committed Aug 1, 2020
1 parent 3cfed05 commit 1bb0d6f
Show file tree
Hide file tree
Showing 232 changed files with 15,583 additions and 9,527 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,5 @@ Server/wwwroot/Downloads/Win-x86/Remotely_Desktop.exe
/Server/ScaffoldingReadMe.txt
/Server/Remotely.db
/Desktop.Win.Wrapper/Remotely_Desktop.zip
/Server/Remotely.db-wal
/Server/Remotely.db-shm
62 changes: 5 additions & 57 deletions Agent.Installer.Win/Services/InstallerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class InstallerService
public event EventHandler<string> ProgressMessageChanged;
public event EventHandler<int> ProgressValueChanged;

public static string CoreRuntimeVersion => "3.1.3";
private string InstallPath => Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), "Program Files", "Remotely");
private string Platform => Environment.Is64BitOperatingSystem ? "x64" : "x86";
private JavaScriptSerializer Serializer { get; } = new JavaScriptSerializer();
Expand All @@ -44,8 +43,6 @@ public class InstallerService
return false;
}

//await InstallDesktpRuntimeIfNeeded();

StopService();

await StopProcesses();
Expand Down Expand Up @@ -120,7 +117,7 @@ public async Task<bool> Uninstall()
ClearInstallDirectory();
ProcessEx.StartHidden("cmd.exe", $"/c timeout 5 & rd /s /q \"{InstallPath}\"");

ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely ScreenCast\"").WaitForExit();
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop\"").WaitForExit();

GetRegistryBaseKey().DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Remotely", false);

Expand All @@ -135,9 +132,9 @@ public async Task<bool> Uninstall()

private void AddFirewallRule()
{
var screenCastPath = Path.Combine(InstallPath, "ScreenCast", "Remotely_ScreenCast.exe");
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely ScreenCast\"").WaitForExit();
ProcessEx.StartHidden("netsh", $"advfirewall firewall add rule name=\"Remotely ScreenCast\" program=\"{screenCastPath}\" protocol=any dir=in enable=yes action=allow profile=Private,Domain description=\"The agent that allows screen sharing and remote control for Remotely.\"").WaitForExit();
var screenCastPath = Path.Combine(InstallPath, "Desktop", "Remotely_Desktop.exe");
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop\"").WaitForExit();
ProcessEx.StartHidden("netsh", $"advfirewall firewall add rule name=\"Remotely Desktop\" program=\"{screenCastPath}\" protocol=any dir=in enable=yes action=allow profile=Private,Domain description=\"The agent that allows screen sharing and remote control for Remotely.\"").WaitForExit();
}

private void BackupDirectory()
Expand Down Expand Up @@ -328,55 +325,6 @@ private RegistryKey GetRegistryBaseKey()
}
}

private async Task InstallDesktpRuntimeIfNeeded()
{
Logger.Write("Checking for .NET Core runtime.");
var uninstallKeys = new List<RegistryKey>();
var runtimeInstalled = false;

foreach (var subkeyName in GetRegistryBaseKey().OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", false).GetSubKeyNames())
{
var subkey = GetRegistryBaseKey().OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + subkeyName, false);
if (subkey?.GetValue("DisplayName")?.ToString()?.Contains($"Microsoft Windows Desktop Runtime - {CoreRuntimeVersion}") == true)
{
runtimeInstalled = true;
break;
}
}

if (!runtimeInstalled)
{
Logger.Write("Downloading .NET Core runtime.");
ProgressMessageChanged.Invoke(this, "Downloading the .NET Core runtime.");
var client = new WebClient();
client.DownloadProgressChanged += (sender, args) =>
{
ProgressValueChanged?.Invoke(this, args.ProgressPercentage);
};
var downloadUrl = string.Empty;
if (Environment.Is64BitOperatingSystem)
{
downloadUrl = "https://download.visualstudio.microsoft.com/download/pr/5954c748-86a1-4823-9e7d-d35f6039317a/169e82cbf6fdeb678c5558c5d0a83834/windowsdesktop-runtime-3.1.3-win-x64.exe";
}
else
{
downloadUrl = "https://download.visualstudio.microsoft.com/download/pr/7cd5c874-5d11-4e72-81f0-4a005d956708/0eb310169770c893407169fc3abaac4f/windowsdesktop-runtime-3.1.3-win-x86.exe";
}
var targetFile = Path.Combine(Path.GetTempPath(), "windowsdesktop-runtime.exe");
await client.DownloadFileTaskAsync(downloadUrl, targetFile);

Logger.Write("Installing .NET Core runtime.");
ProgressMessageChanged?.Invoke(this, "Installing the .NET Core runtime.");
ProgressValueChanged?.Invoke(this, 0);

await Task.Run(() => { ProcessEx.StartHidden(targetFile, "/install /quiet /norestart").WaitForExit(); });
}
else
{
Logger.Write(".NET Core runtime already installed.");
}
}

private void InstallService()
{
Logger.Write("Installing service.");
Expand Down Expand Up @@ -437,7 +385,7 @@ private void RestoreBackup()
private async Task StopProcesses()
{
ProgressMessageChanged?.Invoke(this, "Stopping Remotely processes.");
var procs = Process.GetProcessesByName("Remotely_Agent").Concat(Process.GetProcessesByName("Remotely_ScreenCast"));
var procs = Process.GetProcessesByName("Remotely_Agent").Concat(Process.GetProcessesByName("Remotely_Desktop"));

foreach (var proc in procs)
{
Expand Down
32 changes: 15 additions & 17 deletions Agent/Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.1.6" />
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.4" />
<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="7.0.1" />
<PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="7.0.1" />
<PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="7.0.1" />
<PackageReference Include="Microsoft.PowerShell.CoreCLR.Eventing" Version="7.0.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.1" />
<PackageReference Include="Microsoft.PowerShell.Security" Version="7.0.1" />
<PackageReference Include="Microsoft.WSMan.Management" Version="7.0.1" />
<PackageReference Include="Microsoft.WSMan.Runtime" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.6" />
<PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="7.0.3" />
<PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="7.0.3" />
<PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="7.0.3" />
<PackageReference Include="Microsoft.PowerShell.CoreCLR.Eventing" Version="7.0.3" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.3" />
<PackageReference Include="Microsoft.PowerShell.Security" Version="7.0.3" />
<PackageReference Include="Microsoft.WSMan.Management" Version="7.0.3" />
<PackageReference Include="Microsoft.WSMan.Runtime" Version="7.0.3" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="System.Management.Automation" Version="7.0.1" />
<PackageReference Include="System.Management.Automation" Version="7.0.3" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.7.0" />
</ItemGroup>

Expand All @@ -52,9 +52,7 @@


<ItemGroup>
<Compile Update="Services\WindowsService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Update="Services\WindowsService.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static void BuildServices()
{
builder.AddConsole().AddDebug();
});
serviceCollection.AddSingleton<DeviceSocket>();
serviceCollection.AddSingleton<AgentSocket>();
serviceCollection.AddScoped<ChatClientService>();
serviceCollection.AddTransient<Bash>();
serviceCollection.AddTransient<CMD>();
Expand Down Expand Up @@ -79,11 +79,11 @@ private static async Task Init()

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

await Services.GetRequiredService<DeviceSocket>().Connect();
await Services.GetRequiredService<AgentSocket>().Connect();
}
finally
{
await Services.GetRequiredService<DeviceSocket>().HandleConnection();
await Services.GetRequiredService<AgentSocket>().HandleConnection();
}
}

Expand Down
12 changes: 6 additions & 6 deletions Agent/Services/DeviceSocket.cs → Agent/Services/AgentSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

namespace Remotely.Agent.Services
{
public class DeviceSocket
public class AgentSocket
{
public DeviceSocket(ConfigService configService,
public AgentSocket(ConfigService configService,
Uninstaller uninstaller,
CommandExecutor commandExecutor,
ScriptRunner scriptRunner,
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task Connect()
ConnectionInfo = ConfigService.GetConnectionInfo();

HubConnection = new HubConnectionBuilder()
.WithUrl(ConnectionInfo.Host + "/DeviceHub")
.WithUrl(ConnectionInfo.Host + "/AgentHub")
.Build();

RegisterMessageHandlers();
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task HandleConnection()
var waitTime = new Random().Next(1000, 30000);
Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds...");
await Task.Delay(waitTime);
await Program.Services.GetRequiredService<DeviceSocket>().Connect();
await Program.Services.GetRequiredService<AgentSocket>().Connect();
await Program.Services.GetRequiredService<Updater>().CheckForUpdates();
}
}
Expand Down Expand Up @@ -149,8 +149,8 @@ private void RegisterMessageHandlers()
// TODO: Remove possibility for circular dependencies in the future
// by emitting these events so other services can listen for them.

HubConnection.On("Chat", async (string message, string orgName, string senderConnectionID) => {
await ChatService.SendMessage(message, orgName, senderConnectionID, HubConnection);
HubConnection.On("Chat", async (string senderName, string message, string orgName, bool disconnected, string senderConnectionID) => {
await ChatService.SendMessage(senderName, message, orgName, disconnected, senderConnectionID, HubConnection);
});
HubConnection.On("DownloadFile", async (string filePath, string senderConnectionID) =>
{
Expand Down
30 changes: 17 additions & 13 deletions Agent/Services/AppLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public async Task<int> LaunchChatService(string orgName, string requesterID, Hub
{
try
{
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", EnvironmentHelper.ScreenCastExecutableFileName);
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Desktop", EnvironmentHelper.DesktopExecutableFileName);
if (!File.Exists(rcBinaryPath))
{
await hubConnection.SendAsync("DisplayMessage", "Chat executable not found on target device.", "Executable not found on device.", requesterID);
}


// Start ScreenCast.
// Start Desktop app.
await hubConnection.SendAsync("DisplayMessage", $"Starting chat service...", "Starting chat service.", requesterID);
if (EnvironmentHelper.IsWindows)
{
Expand All @@ -50,7 +50,7 @@ public async Task<int> LaunchChatService(string orgName, string requesterID, Hub
out var procInfo);
if (!result)
{
await hubConnection.SendAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID);
await hubConnection.SendAsync("DisplayMessage", "Chat service failed to start on target device.", "Failed to start chat service.", requesterID);
}
else
{
Expand All @@ -60,14 +60,14 @@ public async Task<int> LaunchChatService(string orgName, string requesterID, Hub
}
else if (EnvironmentHelper.IsLinux)
{
var args = $"xterm -e {rcBinaryPath} -mode Chat -requester \"{requesterID}\" -organization \"{orgName}\" & disown";
return StartLinuxScreenCaster(args);
var args = $"{rcBinaryPath} -mode Chat -requester \"{requesterID}\" -organization \"{orgName}\" & disown";
return StartLinuxDesktopApp(args);
}
}
catch (Exception ex)
{
Logger.Write(ex);
await hubConnection.SendAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID);
await hubConnection.SendAsync("DisplayMessage", "Chat service failed to start on target device.", "Failed to start chat service.", requesterID);
}
return -1;
}
Expand All @@ -76,21 +76,23 @@ public async Task LaunchRemoteControl(int targetSessionId, string requesterID, s
{
try
{
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", EnvironmentHelper.ScreenCastExecutableFileName);
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Desktop", EnvironmentHelper.DesktopExecutableFileName);
if (!File.Exists(rcBinaryPath))
{
await hubConnection.SendAsync("DisplayMessage", "Remote control executable not found on target device.", "Executable not found on device.", requesterID);
return;
}


// Start ScreenCast.
// Start Desktop app.
await hubConnection.SendAsync("DisplayMessage", $"Starting remote control...", "Starting remote control.", requesterID);
if (EnvironmentHelper.IsWindows)
{

if (EnvironmentHelper.IsDebug)
{
// SignalR Connection IDs might start with a hyphen. We surround them
// with quotes so the command line will be parsed correctly.
Process.Start(rcBinaryPath, $"-mode Unattended -requester \"{requesterID}\" -serviceid \"{serviceID}\" -deviceid {ConnectionInfo.DeviceID} -host {ConnectionInfo.Host}");
}
else
Expand All @@ -110,7 +112,7 @@ public async Task LaunchRemoteControl(int targetSessionId, string requesterID, s
else if (EnvironmentHelper.IsLinux)
{
var args = $"{rcBinaryPath} -mode Unattended -requester \"{requesterID}\" -serviceid \"{serviceID}\" -deviceid {ConnectionInfo.DeviceID} -host {ConnectionInfo.Host} & disown";
StartLinuxScreenCaster(args);
StartLinuxDesktopApp(args);
}
}
catch (Exception ex)
Expand All @@ -123,13 +125,15 @@ public async Task RestartScreenCaster(List<string> viewerIDs, string serviceID,
{
try
{
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", EnvironmentHelper.ScreenCastExecutableFileName);
// Start ScreenCast.
var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Desktop", EnvironmentHelper.DesktopExecutableFileName);
// Start Desktop app.
if (EnvironmentHelper.IsWindows)
{
Logger.Write("Restarting screen caster.");
if (EnvironmentHelper.IsDebug)
{
// SignalR Connection IDs might start with a hyphen. We surround them
// with quotes so the command line will be parsed correctly.
Process.Start(rcBinaryPath, $"-mode Unattended -requester \"{requesterID}\" -serviceid \"{serviceID}\" -deviceid {ConnectionInfo.DeviceID} -host {ConnectionInfo.Host} -relaunch true -viewers {String.Join(",", viewerIDs)}");
}
else
Expand All @@ -156,7 +160,7 @@ public async Task RestartScreenCaster(List<string> viewerIDs, string serviceID,
else if (EnvironmentHelper.IsLinux)
{
var args = $"{rcBinaryPath} -mode Unattended -requester \"{requesterID}\" -serviceid \"{serviceID}\" -deviceid {ConnectionInfo.DeviceID} -host {ConnectionInfo.Host} -relaunch true -viewers {string.Join(",", viewerIDs)} & disown";
StartLinuxScreenCaster(args);
StartLinuxDesktopApp(args);
}
}
catch (Exception ex)
Expand All @@ -167,7 +171,7 @@ public async Task RestartScreenCaster(List<string> viewerIDs, string serviceID,
}
}

private int StartLinuxScreenCaster(string args)
private int StartLinuxDesktopApp(string args)
{
var xauthority = string.Empty;

Expand Down

0 comments on commit 1bb0d6f

Please sign in to comment.