protected override void DoWork()
        {
            var serverVersion = Communication.GetRawResponse("/service/getversion.php?client");
            var localVersion  = RegistryHandler.GetSystemSetting("Version");

            try
            {
                var server = int.Parse(serverVersion.Replace(".", ""));
                var local  = int.Parse(localVersion.Replace(".", ""));

                if (server <= local)
                {
                    return;
                }

                if (File.Exists(string.Format("{0}\\tmp\\FOGService.msi", AppDomain.CurrentDomain.BaseDirectory)))
                {
                    File.Delete(string.Format("{0}\\tmp\\FOGService.msi", AppDomain.CurrentDomain.BaseDirectory));
                }

                Communication.DownloadFile("/client/FOGService.msi",
                                           AppDomain.CurrentDomain.BaseDirectory + @"\tmp\FOGService.msi");

                PrepareUpdateHelpers();
                Power.Updating = true;
            }
            catch (Exception ex)
            {
                Log.Error(Name, "Unable to parse versions");
                Log.Error(Name, ex);
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            Thread.Sleep(15 * 1000);
            //Initialize everything
            Log.FilePath = (Environment.ExpandEnvironmentVariables("%userprofile%") + @"\fog_user.log");
            AppDomain.CurrentDomain.UnhandledException += Log.UnhandledException;
            Eager.Initalize();

            Log.Entry(LogName, "Initializing");

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\updating.info"))
            {
                Log.Entry(LogName, "Update.info found, exiting program");
                Power.SpawnUpdateWaiter(Assembly.GetExecutingAssembly().Location);
                Environment.Exit(0);
            }

            _fogService = new FOGUserService();
            _fogService.Start();

            if (RegistryHandler.GetSystemSetting("Tray").Equals("1"))
            {
                StartTray();
            }
        }
Example #3
0
        private static void ApplyUpdates()
        {
            const string logName = "Update Helper";

            var useTray = RegistryHandler.GetSystemSetting("Tray");
            var https   = RegistryHandler.GetSystemSetting("HTTPS");
            var webRoot = RegistryHandler.GetSystemSetting("WebRoot");
            var server  = RegistryHandler.GetSystemSetting("Server");

            var process = new Process
            {
                StartInfo =
                {
                    Arguments = string.Format("/i \"{0}\" /quiet USETRAY=\"{1}\" HTTPS=\"{2}\" WEBADDRESS=\"{3}\" WEBROOT=\"{4}\"",
                                              (AppDomain.CurrentDomain.BaseDirectory + "FOGService.msi"),
                                              useTray, https, server, webRoot)
                }
            };

            process.StartInfo.CreateNoWindow  = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WindowStyle     = ProcessWindowStyle.Hidden;

            process.StartInfo.FileName = "msiexec";


            Log.Entry(logName, "--> " + process.StartInfo.FileName + " " + process.StartInfo.Arguments);
            process.Start();
            process.WaitForExit();
        }
Example #4
0
        protected override void Load()
        {
            // Kill the sub-processes
            foreach (var process in Process.GetProcessesByName("FOGUserService"))
            {
                process.Kill();
            }

            foreach (var process in Process.GetProcessesByName("FOGTray"))
            {
                process.Kill();
            }

            dynamic json = new JObject();

            json.action = "load";
            Bus.Emit(Bus.Channel.Status, json, true);

            Log.NewLine();
            Log.PaddedHeader("Authentication");
            Log.Entry("Client-Info", string.Format("Version: {0}", RegistryHandler.GetSystemSetting("Version")));
            if (!Authentication.HandShake())
            {
                return;
            }
            Log.NewLine();
        }
Example #5
0
        protected override void DoWork()
        {
            //Get task info
            var response = Communication.GetResponse("/service/jobs.php", true);

            //Shutdown if a task is avaible and the user is logged out or it is forced
            if (response.Error)
            {
                return;
            }

            Log.Entry(Name, "Restarting computer for task");

            if (!UserHandler.IsUserLoggedIn() || response.GetField("#force").Equals("1"))
            {
                Power.Restart(Name);
            }

            else if (!response.Error && !_notifiedUser)
            {
                Log.Entry(Name, "User is currently logged in, will try again later");

                var notification = new Notification("Please log off",
                                                    string.Format(
                                                        "{0} is attemping to service your computer, please log off at the soonest available time",
                                                        RegistryHandler.GetSystemSetting("Company")), 60);

                Bus.Emit(Bus.Channel.Notification, notification.GetJson(), true);
                _notifiedUser = true;
            }
        }
        /// <summary>
        ///     Load the server information from the registry and apply it
        ///     <returns>True if settings were updated</returns>
        /// </summary>
        public static bool GetAndSetServerAddress()
        {
            if (RegistryHandler.GetSystemSetting("HTTPS") == null || RegistryHandler.GetSystemSetting("WebRoot") == null ||
                string.IsNullOrEmpty(RegistryHandler.GetSystemSetting("Server")))
            {
                Log.Error(LogName, "Invalid parameters");
                return(false);
            }

            ServerAddress  = (RegistryHandler.GetSystemSetting("HTTPS").Equals("1") ? "https://" : "http://");
            ServerAddress += RegistryHandler.GetSystemSetting("Server") +
                             RegistryHandler.GetSystemSetting("WebRoot");
            return(true);
        }
        /// <summary>
        /// Loop through all the modules until an update or shutdown is pending
        /// </summary>
        protected virtual void ModuleLooper()
        {
            // Only run the service if there isn't a shutdown or update pending
            while (!Power.ShuttingDown && !Power.Updating)
            {
                // Stop looping as soon as a shutdown or update pending
                foreach (var module in _modules.TakeWhile(module => !Power.ShuttingDown && !Power.Updating))
                {
                    // Entry file formatting
                    Log.NewLine();
                    Log.PaddedHeader(module.Name);
                    Log.Entry("Client-Info", string.Format("Version: {0}", RegistryHandler.GetSystemSetting("Version")));

                    try
                    {
                        module.Start();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(Name, ex);
                    }

                    // Entry file formatting
                    Log.Divider();
                    Log.NewLine();

                    if (Power.Requested)
                    {
                        break;
                    }
                }

                while (Power.Requested)
                {
                    Log.Entry(Name, "Power operation being requested, checking back in 30 seconds");
                    Thread.Sleep(30 * 1000);
                }

                // Skip checking for sleep time if there is a shutdown or update pending
                if (Power.ShuttingDown || Power.Updating)
                {
                    break;
                }

                // Once all modules have been run, sleep for the set time
                var sleepTime = GetSleepTime() ?? DefaultSleepTime;
                Log.Entry(Name, string.Format("Sleeping for {0} seconds", sleepTime));
                Thread.Sleep(sleepTime * 1000);
            }
        }
Example #8
0
        //Rename the computer and remove it from active directory
        private void RenameComputer(Response response)
        {
            Log.Entry(Name, "Checking Hostname");
            if (!response.IsFieldValid("#hostname"))
            {
                Log.Error(Name, "Hostname is not specified");
                return;
            }
            if (Environment.MachineName.ToLower().Equals(response.GetField("#hostname").ToLower()))
            {
                Log.Entry(Name, "Hostname is correct");
                return;
            }

            Log.Entry(Name, string.Format("Renaming host to {0}", response.GetField("#hostname")));

            if (!UserHandler.IsUserLoggedIn() || response.GetField("#force").Equals("1"))
            {
                Log.Entry(Name, "Unregistering computer");
                //First unjoin it from active directory
                UnRegisterComputer(response);

                Log.Entry(Name, "Updating registry");

                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "NV Hostname",
                                                 response.GetField("#hostname"));
                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName",
                                                 response.GetField("#hostname"));
                RegistryHandler.SetRegistryValue(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName",
                                                 response.GetField("#hostname"));

                Power.Restart(RegistryHandler.GetSystemSetting("Company") + " needs to rename your computer", Power.FormOption.Delay);
            }
            else if (!_notifiedUser)
            {
                Log.Entry(Name, "User is currently logged in, will try again later");

                var notification = new Notification("Please log off",
                                                    string.Format(
                                                        "{0} is attemping to service your computer, please log off at the soonest available time",
                                                        RegistryHandler.GetSystemSetting("Company")), 120);

                Bus.Emit(Bus.Channel.Notification, notification.GetJson(), true);

                _notifiedUser = true;
            }
        }
Example #9
0
        protected override int?GetSleepTime()
        {
            try
            {
                var sleepTimeStr = RegistryHandler.GetSystemSetting("Sleep");
                var sleepTime    = int.Parse(sleepTimeStr);
                if (sleepTime >= DefaultSleepTime)
                {
                    return(sleepTime);
                }

                Log.Entry(Name, string.Format("Sleep time set on the server is below the minimum of {0}", DefaultSleepTime));
            }
            catch (Exception ex)
            {
                Log.Error(Name, "Unable to parse sleep time");
                Log.Error(Name, ex);
            }

            return(null);
        }
Example #10
0
        private static void QueueShutdown(string parameters, FormOption options = FormOption.Abort, string message = null, int gracePeriod = -1)
        {
            if (_timer != null && _timer.Enabled)
            {
                Log.Entry(LogName, "Power task already in-progress");
                return;
            }

            delayed = false;

            try
            {
                if (gracePeriod == -1)
                {
                    gracePeriod = (!string.IsNullOrEmpty(RegistryHandler.GetSystemSetting("gracePeriod")))
                        ? int.Parse(RegistryHandler.GetSystemSetting("gracePeriod"))
                        : DefaultGracePeriod;
                }
            }
            catch (Exception)
            {
                gracePeriod = DefaultGracePeriod;
            }

            Log.Entry(LogName, string.Format("Creating shutdown command in {0} seconds", gracePeriod));

            requestData         = new JObject();
            requestData.action  = "request";
            requestData.period  = gracePeriod;
            requestData.options = options;
            requestData.command = parameters;
            requestData.message = message ?? "This computer needs to perform maintance.";

            Bus.Emit(Bus.Channel.Power, requestData, true);
            _timer          = new Timer(gracePeriod * 1000);
            _timer.Elapsed += TimerElapsed;
            _timer.Start();
        }