Exemple #1
0
        public async Task Connect()
        {
            try
            {
                ConnectionInfo = ConfigService.GetConnectionInfo();

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

                RegisterMessageHandlers();

                await HubConnection.StartAsync();
            }
            catch (Exception ex)
            {
                Logger.Write(ex, "Failed to connect to server.  Internet connection may be unavailable.", EventType.Warning);
                return;
            }

            try
            {
                var device = await DeviceInformation.Create(ConnectionInfo.DeviceID, ConnectionInfo.OrganizationID);

                var result = await HubConnection.InvokeAsync <bool>("DeviceCameOnline", device);

                if (!result)
                {
                    // Orgnanization ID wasn't found, or this device is already connected.
                    // The above can be caused by temporary issues on the server.  So we'll do
                    // nothing here and wait for it to get resolved.
                    Logger.Write("There was an issue registering with the server.  The server might be undergoing maintenance, or the supplied organization ID might be incorrect.");
                    await Task.Delay(TimeSpan.FromMinutes(1));

                    await HubConnection.StopAsync();

                    return;
                }

                if (string.IsNullOrWhiteSpace(ConnectionInfo.ServerVerificationToken))
                {
                    IsServerVerified = true;
                    ConnectionInfo.ServerVerificationToken = Guid.NewGuid().ToString();
                    await HubConnection.SendAsync("SetServerVerificationToken", ConnectionInfo.ServerVerificationToken);

                    ConfigService.SaveConnectionInfo(ConnectionInfo);
                }
                else
                {
                    await HubConnection.SendAsync("SendServerVerificationToken");
                }

                if (ConfigService.TryGetDeviceSetupOptions(out DeviceSetupOptions options))
                {
                    await HubConnection.SendAsync("DeviceSetupOptions", options, ConnectionInfo.DeviceID);
                }

                HeartbeatTimer?.Dispose();
                HeartbeatTimer          = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds);
                HeartbeatTimer.Elapsed += HeartbeatTimer_Elapsed;
                HeartbeatTimer.Start();
            }
            catch (Exception ex)
            {
                Logger.Write(ex, "Error starting websocket connection.", EventType.Error);
            }
        }
Exemple #2
0
 public AppLauncher(ConfigService configService)
 {
     ConnectionInfo = configService.GetConnectionInfo();
 }
Exemple #3
0
 public ScriptRunner(CommandExecutor commandExecutor, ConfigService configService)
 {
     CommandExecutor = commandExecutor;
     ConfigService   = configService;
 }
Exemple #4
0
 public AppLauncherLinux(ConfigService configService, IProcessInvoker processInvoker)
 {
     _processInvoker = processInvoker;
     _connectionInfo = configService.GetConnectionInfo();
 }
Exemple #5
0
        public async Task CheckForUpdates()
        {
            try
            {
                await CheckForUpdatesLock.WaitAsync();

                if (EnvironmentHelper.IsDebug)
                {
                    return;
                }

                if (PreviousUpdateFailed)
                {
                    Logger.Write("Skipping update check due to previous failure.  Restart the service to try again, or manually install the update.");
                    return;
                }


                var connectionInfo = ConfigService.GetConnectionInfo();
                var serverUrl      = ConfigService.GetConnectionInfo().Host;

                var platform = Environment.Is64BitOperatingSystem ? "x64" : "x86";
                var fileUrl  = serverUrl + $"/Downloads/Remotely-Win10-{platform}.zip";

                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
            {
                CheckForUpdatesLock.Release();
            }
        }
Exemple #6
0
 public UpdaterMac(ConfigService configService, IWebClientEx webClientEx)
 {
     _configService = configService;
     _webClientEx   = webClientEx;
     _webClientEx.SetRequestTimeout((int)_updateTimer.Interval);
 }
        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();
            }
        }
Exemple #8
0
 public UpdaterWin(ConfigService configService)
 {
     ConfigService = configService;
 }
Exemple #9
0
 public CommandExecutor(ConfigService configService)
 {
     ConfigService = configService;
 }
        public async Task InstallLatestVersion()
        {
            try
            {
                var connectionInfo = ConfigService.GetConnectionInfo();
                var serverUrl      = connectionInfo.Host;

                Logger.Write("Service Updater: Downloading install package.");

                using var wc = new WebClientEx((int)UpdateTimer.Interval);
                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();
                    }

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

                    Process.Start(installerPath, $"-install -quiet -path {zipPath} -serverurl {serverUrl} -organizationid {connectionInfo.OrganizationID}");
                }
                else if (EnvironmentHelper.IsLinux)
                {
                    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();
                    }

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

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

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

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

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

                    Process.Start("sudo", $"{installerPath} --path {zipPath} & disown");
                }
            }
            catch (WebException ex) when(ex.Status == WebExceptionStatus.Timeout)
            {
                Logger.Write("Timed out while waiting to downloaod update.", Shared.Enums.EventType.Warning);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
        }
Exemple #11
0
 public UpdaterLinux(ConfigService configService)
 {
     ConfigService = configService;
 }
Exemple #12
0
        public async Task InstallLatestVersion()
        {
            try
            {
                await InstallLatestVersionLock.WaitAsync();

                var connectionInfo = ConfigService.GetConnectionInfo();
                var serverUrl      = connectionInfo.Host;

                Logger.Write("Service Updater: Downloading install package.");

                using var wc = new WebClientEx((int)UpdateTimer.Interval);
                var downloadId = Guid.NewGuid().ToString();
                var zipPath    = Path.Combine(Path.GetTempPath(), "RemotelyUpdate.zip");

                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();
                }

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

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

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

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

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

                Process.Start("sudo", $"{installerPath} --path {zipPath} & disown");
            }
            catch (WebException ex) when(ex.Status == WebExceptionStatus.Timeout)
            {
                Logger.Write("Timed out while waiting to download update.", Shared.Enums.EventType.Warning);
                PreviousUpdateFailed = true;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                PreviousUpdateFailed = true;
            }
            finally
            {
                InstallLatestVersionLock.Release();
            }
        }
Exemple #13
0
        private static void RegisterMessageHandlers(HubConnection hubConnection)
        {
            hubConnection.On("ExecuteCommand", (async(string mode, string command, string commandID, string senderConnectionID) =>
            {
                await ExecuteCommand(mode, command, commandID, senderConnectionID);
            }));
            hubConnection.On("TransferFiles", async(string transferID, List <string> fileIDs, string requesterID) =>
            {
                Logger.Write($"File transfer started by {requesterID}.");
                var connectionInfo = ConfigService.GetConnectionInfo();
                var sharedFilePath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "RemotelySharedFiles")).FullName;

                foreach (var fileID in fileIDs)
                {
                    var url      = $"{connectionInfo.Host}/API/FileSharing/{fileID}";
                    var wr       = WebRequest.CreateHttp(url);
                    var response = await wr.GetResponseAsync();
                    var cd       = response.Headers["Content-Disposition"];
                    var filename = cd
                                   .Split(";")
                                   .FirstOrDefault(x => x.Trim()
                                                   .StartsWith("filename"))
                                   .Split("=")[1];

                    var legalChars = filename.ToCharArray().Where(x => !Path.GetInvalidFileNameChars().Any(y => x == y));

                    filename = new string(legalChars.ToArray());

                    using (var rs = response.GetResponseStream())
                    {
                        using (var fs = new FileStream(Path.Combine(sharedFilePath, filename), FileMode.Create))
                        {
                            rs.CopyTo(fs);
                        }
                    }
                }
                await HubConnection.InvokeAsync("TransferCompleted", transferID, requesterID);
            });
            hubConnection.On("DeployScript", async(string mode, string fileID, string commandContextID, string requesterID) => {
                var connectionInfo = ConfigService.GetConnectionInfo();
                var sharedFilePath = Directory.CreateDirectory(Path.Combine(
                                                                   Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                                                                   "Remotely",
                                                                   "SharedFiles"
                                                                   )).FullName;
                var webClient = new WebClient();

                var url      = $"{connectionInfo.Host}/API/FileSharing/{fileID}";
                var wr       = WebRequest.CreateHttp(url);
                var response = await wr.GetResponseAsync();
                var cd       = response.Headers["Content-Disposition"];
                var filename = cd.Split(";").FirstOrDefault(x => x.Trim().StartsWith("filename")).Split("=")[1];
                using (var rs = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(rs))
                    {
                        var result = await sr.ReadToEndAsync();
                        await ExecuteCommand(mode, result, commandContextID, requesterID);
                    }
                }
            });
            hubConnection.On("UninstallClient", () =>
            {
                Uninstaller.UninstallAgent();
            });

            hubConnection.On("RemoteControl", async(string requesterID, string serviceID) =>
            {
                if (!IsServerVerified)
                {
                    Logger.Write("Remote control attempted before server was verified.");
                    Uninstaller.UninstallAgent();
                    return;
                }
                try
                {
                    var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", OSUtils.ScreenCastExecutableFileName);
                    if (!File.Exists(rcBinaryPath))
                    {
                        await hubConnection.InvokeAsync("DisplayMessage", "Remote control executable not found on target device.", "Executable not found on device.", requesterID);
                        return;
                    }


                    // Start ScreenCast.
                    await hubConnection.InvokeAsync("DisplayMessage", $"Starting remote control...", "Starting remote control.", requesterID);
                    if (OSUtils.IsWindows)
                    {
                        if (Program.IsDebug)
                        {
                            Process.Start(rcBinaryPath, $"-mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host}");
                        }
                        else
                        {
                            var result = Win32Interop.OpenInteractiveProcess(rcBinaryPath + $" -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host}", "default", true, out _);
                            if (!result)
                            {
                                await hubConnection.InvokeAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID);
                            }
                        }
                    }
                    else if (OSUtils.IsLinux)
                    {
                        var users    = OSUtils.StartProcessWithResults("users", "");
                        var username = users?.Split()?.FirstOrDefault()?.Trim();
                        var homeDir  = OSUtils.StartProcessWithResults("sudo", $"-u {username} env | grep HOME")?.Split('=')?.Last();
                        var psi      = new ProcessStartInfo()
                        {
                            FileName  = "sudo",
                            Arguments = $"-u {username} {rcBinaryPath} -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} & disown"
                        };
                        psi.Environment.Add("DISPLAY", ":0");
                        psi.Environment.Add("XAUTHORITY", $"{homeDir}/.Xauthority");
                        var casterProc = Process.Start(psi);
                        casterProc.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    await hubConnection.InvokeAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID);
                    throw;
                }
            });
            hubConnection.On("RestartScreenCaster", async(List <string> viewerIDs, string serviceID, string requesterID) =>
            {
                if (!IsServerVerified)
                {
                    Logger.Write("Remote control attempted before server was verified.");
                    Uninstaller.UninstallAgent();
                    return;
                }
                try
                {
                    var rcBinaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenCast", OSUtils.ScreenCastExecutableFileName);
                    // Start ScreenCast.
                    if (OSUtils.IsWindows)
                    {
                        Logger.Write("Restarting screen caster.");
                        if (Program.IsDebug)
                        {
                            Process.Start(rcBinaryPath, $"-mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} -relaunch true -viewers {String.Join(",", viewerIDs)}");
                        }
                        else
                        {
                            // Give a little time for session changing, etc.
                            await Task.Delay(1000);

                            var result = Win32Interop.OpenInteractiveProcess(rcBinaryPath + $" -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} -relaunch true -viewers {String.Join(",", viewerIDs)}", "default", true, out _);

                            if (!result)
                            {
                                await Task.Delay(1000);
                                // Try one more time.
                                result = Win32Interop.OpenInteractiveProcess(rcBinaryPath + $" -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} -relaunch true -viewers {String.Join(",", viewerIDs)}", "default", true, out _);

                                if (!result)
                                {
                                    Logger.Write("Failed to relaunch screen caster.");
                                    await hubConnection.InvokeAsync("SendConnectionFailedToViewers", viewerIDs);
                                    await hubConnection.InvokeAsync("DisplayMessage", "Remote control failed to start on target device.", "Failed to start remote control.", requesterID);
                                }
                            }
                        }
                    }
                    else if (OSUtils.IsLinux)
                    {
                        var users    = OSUtils.StartProcessWithResults("users", "");
                        var username = users?.Split()?.FirstOrDefault()?.Trim();
                        var homeDir  = OSUtils.StartProcessWithResults("sudo", $"-u {username} env | grep HOME")?.Split('=')?.Last();
                        var psi      = new ProcessStartInfo()
                        {
                            FileName  = "sudo",
                            Arguments = $"-u {username} {rcBinaryPath} -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} -relaunch true -viewers {String.Join(",", viewerIDs)} & disown"
                        };
                        psi.Environment.Add("DISPLAY", ":0");
                        psi.Environment.Add("XAUTHORITY", $"{homeDir}/.Xauthority");
                        var casterProc = Process.Start(psi);
                        casterProc.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    await hubConnection.InvokeAsync("SendConnectionFailedToViewers", viewerIDs);
                    Logger.Write(ex);
                    throw;
                }
            });
            hubConnection.On("CtrlAltDel", () =>
            {
                User32.SendSAS(false);
            });

            hubConnection.On("ServerVerificationToken", (string verificationToken) =>
            {
                if (verificationToken == ConfigService.GetConnectionInfo().ServerVerificationToken)
                {
                    IsServerVerified = true;
                    if (!Program.IsDebug)
                    {
                        Updater.CheckForCoreUpdates();
                    }
                }
                else
                {
                    Logger.Write($"Server sent an incorrect verification token.  Token Sent: {verificationToken}.");
                    Uninstaller.UninstallAgent();
                    return;
                }
            });
        }
Exemple #14
0
        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();
            }
        }
Exemple #15
0
 public ScriptExecutor(ConfigService configService)
 {
     ConfigService = configService;
 }