private void Update()
        {
            UpdateLock.Reset();

            var watch = Stopwatch.StartNew();

            try
            {
                while (!UpdateToken.IsCancellationRequested)
                {
                    string input;
                    if (!string.IsNullOrEmpty((input = Console.ReadLine())))
                    {
                        if (input.StartsWith("/") && !ExecuteCommand(input))
                        {
                            Logger.Log(LogType.Command, "Invalid command!");
                        }
                    }

                    if (UpdateToken.IsCancellationRequested || Server == null)
                    {
                        break;
                    }

                    if (watch.ElapsedMilliseconds < 10)
                    {
                        var time = (int)(10 - watch.ElapsedMilliseconds);
                        if (time < 0)
                        {
                            time = 0;
                        }
                        Thread.Sleep(time);
                    }

                    watch.Reset();
                    watch.Start();
                }
            }
            finally
            {
                UpdateLock.Set();
            }

            Logger.Log(LogType.Warning, "Update loop stopped!");
        }
        public override bool Stop()
        {
            Logger.Log(LogType.Debug, "Stopping UpdateThread...");
            if (UpdateToken?.IsCancellationRequested == false)
            {
                UpdateToken.Cancel();
                UpdateLock.Wait();
            }
            Logger.Log(LogType.Debug, "Stopped UpdateThread.");

            Logger.Log(LogType.Debug, "Stopping Modules...");
            foreach (var module in Modules)
            {
                module.Stop();
            }
            Logger.Log(LogType.Debug, "Stopped Modules.");

            return(true);
        }
Example #3
0
        public static void LockUI(bool Enable)
        {
            try
            {
                Form       form = Application.OpenForms["FormManual"];
                TabControl Tab;
                if (form == null)
                {
                    return;
                }

                Tab = form.Controls.Find("tbcManual", true).FirstOrDefault() as TabControl;
                if (Tab == null)
                {
                    return;
                }

                if (Tab.InvokeRequired)
                {
                    UpdateLock ph = new UpdateLock(LockUI);
                    Tab.BeginInvoke(ph, Enable);
                }
                else
                {
                    Tab.Enabled = !Enable;
                    if (!Enable)
                    {
                        form.Cursor = Cursors.Default;
                    }
                    else
                    {
                        form.Cursor = Cursors.WaitCursor;
                    }
                }
            }
            catch
            {
                logger.Error("LockUI: Update fail.");
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                if (disposing)
                {
                    AppDomain.CurrentDomain.AssemblyResolve -= AppDomain_AssemblyResolve;

                    ClientJoined?.Dispose();
                    ClientLeaved?.Dispose();

                    if (UpdateToken?.IsCancellationRequested == false)
                    {
                        UpdateToken.Cancel();
                        UpdateLock.Wait();
                    }
                }


                IsDisposed = true;
            }
            base.Dispose(disposing);
        }
Example #5
0
        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();
            }
        }
Example #6
0
        public static UpdateLock <T> Bind <T>(PropertyLink <T> property1, EventLink obj1ChangeEvent, PropertyLink <T> property2, EventLink obj2ChangeEvent)
        {
            UpdateLock <T> binding = new UpdateLock <T>(property1, obj1ChangeEvent, property2, obj2ChangeEvent);

            return(binding);
        }
Example #7
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();
            }
        }