From 29f01e192548d1568f48dbb47b3f8653ddefae90 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Sun, 23 Feb 2020 11:28:08 -0800 Subject: [PATCH] Add organization name to chat service. --- .../Services/InstallerService.cs | 5 ++ Agent/Services/AppLauncher.cs | 8 +-- Agent/Services/ChatClientService.cs | 4 +- Agent/Services/DeviceSocket.cs | 4 +- ScreenCast.Core/Conductor.cs | 63 ++++++++++++++----- ScreenCast.Core/Services/ChatHostService.cs | 60 ++++++++++++++++-- ScreenCast.Linux/Program.cs | 5 +- ScreenCast.Win/Program.cs | 4 +- .../Services/WinClipboardService.cs | 10 ++- Server/Services/BrowserSocketHub.cs | 3 +- 10 files changed, 128 insertions(+), 38 deletions(-) diff --git a/Agent.Installer.Win/Services/InstallerService.cs b/Agent.Installer.Win/Services/InstallerService.cs index 75482982..5124ea45 100644 --- a/Agent.Installer.Win/Services/InstallerService.cs +++ b/Agent.Installer.Win/Services/InstallerService.cs @@ -385,6 +385,11 @@ namespace Remotely.Agent.Installer.Win.Services Logger.Write("Restoring backup."); ClearInstallDirectory(); ZipFile.ExtractToDirectory(backupPath, InstallPath); + var serv = ServiceController.GetServices().FirstOrDefault(ser => ser.ServiceName == "Remotely_Service"); + if (serv?.Status != ServiceControllerStatus.Running) + { + serv?.Start(); + } } } catch (Exception ex) diff --git a/Agent/Services/AppLauncher.cs b/Agent/Services/AppLauncher.cs index fe7bd168..8e5a84b6 100644 --- a/Agent/Services/AppLauncher.cs +++ b/Agent/Services/AppLauncher.cs @@ -21,7 +21,7 @@ namespace Remotely.Agent.Services private ConnectionInfo ConnectionInfo { get; } - public async Task LaunchChatService(string requesterID, HubConnection hubConnection) + public async Task LaunchChatService(string orgName, string requesterID, HubConnection hubConnection) { try { @@ -39,11 +39,11 @@ namespace Remotely.Agent.Services if (Program.IsDebug) { - return Process.Start("conhost.exe", $"{rcBinaryPath} -mode Chat -requester {requesterID}").Id; + return Process.Start(rcBinaryPath, $"-mode Chat -requester {requesterID} -organization \"{orgName}\"").Id; } else { - var result = Win32Interop.OpenInteractiveProcess($"{rcBinaryPath} -mode Chat -requester {requesterID}", "default", false, out var procInfo); + var result = Win32Interop.OpenInteractiveProcess($"{rcBinaryPath} -mode Chat -requester {requesterID} -organization \"{orgName}\"", "default", false, out var procInfo); if (!result) { await hubConnection.InvokeAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID); @@ -56,7 +56,7 @@ namespace Remotely.Agent.Services } else if (OSUtils.IsLinux) { - var args = $"xterm -e {rcBinaryPath} -mode Chat -requester {requesterID} & disown"; + var args = $"xterm -e {rcBinaryPath} -mode Chat -requester {requesterID} -organization \"{orgName}\" & disown"; return StartLinuxScreenCaster(args); } } diff --git a/Agent/Services/ChatClientService.cs b/Agent/Services/ChatClientService.cs index 1dec4d72..fe1f875a 100644 --- a/Agent/Services/ChatClientService.cs +++ b/Agent/Services/ChatClientService.cs @@ -35,7 +35,7 @@ namespace Remotely.Agent.Services }; private MemoryCache ChatClients { get; } = new MemoryCache("ChatClients"); - public async Task SendMessage(string message, string senderConnectionID, HubConnection hubConnection) + public async Task SendMessage(string message, string orgName, string senderConnectionID, HubConnection hubConnection) { try { @@ -45,7 +45,7 @@ namespace Remotely.Agent.Services if (!ChatClients.Contains(senderConnectionID)) { var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", OSUtils.ScreenCastExecutableFileName); - var procID = await AppLauncher.LaunchChatService(senderConnectionID, hubConnection); + var procID = await AppLauncher.LaunchChatService(orgName, senderConnectionID, hubConnection); var clientPipe = new NamedPipeClientStream(".", "Remotely_Chat" + senderConnectionID, PipeDirection.InOut, PipeOptions.Asynchronous); clientPipe.Connect(15000); diff --git a/Agent/Services/DeviceSocket.cs b/Agent/Services/DeviceSocket.cs index e9847a62..548e00dc 100644 --- a/Agent/Services/DeviceSocket.cs +++ b/Agent/Services/DeviceSocket.cs @@ -109,8 +109,8 @@ namespace Remotely.Agent.Services private void RegisterMessageHandlers() { - HubConnection.On("Chat", async (string message, string senderConnectionID) => { - await ChatService.SendMessage(message, senderConnectionID, HubConnection); + HubConnection.On("Chat", async (string message, string orgName, string senderConnectionID) => { + await ChatService.SendMessage(message, orgName, senderConnectionID, HubConnection); }); HubConnection.On("ExecuteCommand", (async (string mode, string command, string commandID, string senderConnectionID) => { diff --git a/ScreenCast.Core/Conductor.cs b/ScreenCast.Core/Conductor.cs index e52bfd76..cd3fdf94 100644 --- a/ScreenCast.Core/Conductor.cs +++ b/ScreenCast.Core/Conductor.cs @@ -34,9 +34,11 @@ namespace Remotely.ScreenCast.Core public string DeviceID { get; private set; } public string Host { get; private set; } public AppMode Mode { get; private set; } + public string OrganizationName { get; private set; } public string RequesterID { get; private set; } public string ServiceID { get; private set; } public ConcurrentDictionary Viewers { get; } = new ConcurrentDictionary(); + public async Task Connect() { await CasterSocket.Connect(Host); @@ -48,35 +50,66 @@ namespace Remotely.ScreenCast.Core for (var i = 0; i < args.Length; i += 2) { - var key = args?[i]; - if (key != null) + try { - key = key.Trim().Replace("-", "").ToLower(); - var value = args?[i + 1]; - if (value != null) + var key = args?[i]; + if (key != null) { - ArgDict[key] = args[i + 1].Trim(); + if (!key.Contains("-")) + { + Logger.Write("Command line arguments are invalid."); + i -= 1; + continue; + } + key = key.Trim().Replace("-", "").ToLower(); + if (i + 1 == args.Length) + { + ArgDict.Add(key, "true"); + continue; + } + var value = args?[i + 1]; + if (value != null) + { + if (value.StartsWith("-")) + { + ArgDict.Add(key, "true"); + i -= 1; + } + else + { + ArgDict.Add(key, args[i + 1].Trim()); + } + } } } + catch (Exception ex) + { + Logger.Write(ex); + } } Mode = (AppMode)Enum.Parse(typeof(AppMode), ArgDict["mode"], true); - if (Mode == AppMode.Normal || Mode == AppMode.Unattended) + if (ArgDict.TryGetValue("host", out var host)) { - Host = ArgDict["host"]; + Host = host; } - - if (Mode == AppMode.Chat || Mode == AppMode.Unattended) + if (ArgDict.TryGetValue("requester", out var requester)) { - RequesterID = ArgDict["requester"]; + RequesterID = requester; } - - if (Mode == AppMode.Unattended) + if (ArgDict.TryGetValue("serviceid", out var serviceID)) { - ServiceID = ArgDict["serviceid"]; - DeviceID = ArgDict["deviceid"]; + ServiceID = serviceID; + } + if (ArgDict.TryGetValue("deviceid", out var deviceID)) + { + DeviceID = deviceID; + } + if (ArgDict.TryGetValue("organization", out var orgName)) + { + OrganizationName = orgName; } } diff --git a/ScreenCast.Core/Services/ChatHostService.cs b/ScreenCast.Core/Services/ChatHostService.cs index 262f5e0d..5dabd42a 100644 --- a/ScreenCast.Core/Services/ChatHostService.cs +++ b/ScreenCast.Core/Services/ChatHostService.cs @@ -26,10 +26,9 @@ namespace Remotely.ScreenCast.Core.Services "; private NamedPipeServerStream NamedPipeStream { get; set; } - private StreamWriter Writer { get; set; } private StreamReader Reader { get; set; } - - public async Task StartChat(string requesterID) + private StreamWriter Writer { get; set; } + public async Task StartChat(string requesterID, string organizationName) { NamedPipeStream = new NamedPipeServerStream("Remotely_Chat" + requesterID, PipeDirection.InOut, 10, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); Writer = new StreamWriter(NamedPipeStream); @@ -38,7 +37,14 @@ namespace Remotely.ScreenCast.Core.Services Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Cyan; Console.Title = "Remotely Chat"; - Console.Write(AsciiLogo); + if (string.IsNullOrWhiteSpace(organizationName)) + { + Console.Write(AsciiLogo); + } + else + { + WriteOrgnanizationName(organizationName); + } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.WriteLine("Your IT administrator would like to chat!"); @@ -103,5 +109,51 @@ namespace Remotely.ScreenCast.Core.Services Console.Write("You: "); Console.ForegroundColor = ConsoleColor.White; } + + private void WriteOrgnanizationName(string organizationName) + { + Console.WriteLine(); + Console.WriteLine(); + + Console.Write(" "); + for (var i = 0; i < organizationName.Length + 10; i++) + { + Console.Write("/"); + } + Console.WriteLine(); + + + Console.Write(" ///"); + for (var i = 0; i < organizationName.Length + 4; i++) + { + Console.Write(" "); + } + Console.Write("///"); + Console.WriteLine(); + + + Console.WriteLine($" /// {organizationName} ///"); + + + Console.Write(" ///"); + for (var i = 0; i < organizationName.Length + 4; i++) + { + Console.Write(" "); + } + Console.Write("///"); + Console.WriteLine(); + + + Console.Write(" "); + for (var i = 0; i < organizationName.Length + 10; i++) + { + Console.Write("/"); + } + Console.WriteLine(); + + + Console.WriteLine(); + Console.WriteLine(); + } } } diff --git a/ScreenCast.Linux/Program.cs b/ScreenCast.Linux/Program.cs index f8540f14..49a001df 100644 --- a/ScreenCast.Linux/Program.cs +++ b/ScreenCast.Linux/Program.cs @@ -8,6 +8,7 @@ using Remotely.ScreenCast.Core.Communication; using Remotely.ScreenCast.Core.Interfaces; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Linq; namespace Remotely.ScreenCast.Linux { @@ -26,11 +27,11 @@ namespace Remotely.ScreenCast.Linux Conductor = Services.GetRequiredService(); - Conductor.ProcessArgs(args); + Conductor.ProcessArgs(Environment.GetCommandLineArgs().Skip(1).ToArray()); if (Conductor.Mode == Core.Enums.AppMode.Chat) { - Services.GetRequiredService().StartChat(Conductor.RequesterID).Wait(); + Services.GetRequiredService().StartChat(Conductor.RequesterID, Conductor.OrganizationName).Wait(); } else { diff --git a/ScreenCast.Win/Program.cs b/ScreenCast.Win/Program.cs index e52abbd9..94a163f0 100644 --- a/ScreenCast.Win/Program.cs +++ b/ScreenCast.Win/Program.cs @@ -42,11 +42,11 @@ namespace Remotely.ScreenCast.Win BuildServices(); Conductor = Services.GetRequiredService(); - Conductor.ProcessArgs(args); + Conductor.ProcessArgs(Environment.GetCommandLineArgs().Skip(1).ToArray()); if (Conductor.Mode == Core.Enums.AppMode.Chat) { - Services.GetRequiredService().StartChat(Conductor.RequesterID).Wait(); + Services.GetRequiredService().StartChat(Conductor.RequesterID, Conductor.OrganizationName).Wait(); } else { diff --git a/ScreenCast.Win/Services/WinClipboardService.cs b/ScreenCast.Win/Services/WinClipboardService.cs index c8a095f7..e91e54c2 100644 --- a/ScreenCast.Win/Services/WinClipboardService.cs +++ b/ScreenCast.Win/Services/WinClipboardService.cs @@ -46,22 +46,20 @@ namespace Remotely.ScreenCast.Win.Services private void ClipboardWatcher_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - Win32Interop.SwitchToInputDesktop(); - var thread = new Thread(() => { try { + Win32Interop.SwitchToInputDesktop(); + + if (Clipboard.ContainsText() && Clipboard.GetText() != ClipboardText) { ClipboardText = Clipboard.GetText(); ClipboardTextChanged.Invoke(this, ClipboardText); } } - catch (Exception ex) - { - Logger.Write(ex); - } + catch { } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); diff --git a/Server/Services/BrowserSocketHub.cs b/Server/Services/BrowserSocketHub.cs index 90cdaec3..3cc74ddd 100644 --- a/Server/Services/BrowserSocketHub.cs +++ b/Server/Services/BrowserSocketHub.cs @@ -53,7 +53,8 @@ namespace Remotely.Server.Services { deviceIDs = DataService.FilterDeviceIDsByUserPermission(deviceIDs, RemotelyUser); var connections = GetActiveClientConnections(deviceIDs); - return DeviceHub.Clients.Clients(connections.Select(x => x.Key).ToList()).SendAsync("Chat", $"{RemotelyUser.UserName}: {message}", Context.ConnectionId); + var organizationName = DataService.GetOrganizationName(RemotelyUser.UserName); + return DeviceHub.Clients.Clients(connections.Select(x => x.Key).ToList()).SendAsync("Chat", $"{RemotelyUser.UserName}: {message}", organizationName, Context.ConnectionId); } public Task DeployScript(string fileID, string mode, string[] deviceIDs)