Exemple #1
0
        public static C.TaskInfo StopService(string name)
        {
            C.TaskInfo result = C.TaskInfo.Fail("Init");
            try
            {
                ServiceController service = new ServiceController(name);

                //Start the service
                if (service.Status == ServiceControllerStatus.Running)
                {
                    service.Stop();
                    try
                    {
                        service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10.0));
                        result = C.TaskInfo.Success("Service stopped!");
                    }
                    catch (System.ServiceProcess.TimeoutException ex)
                    {
                        result = C.TaskInfo.Fail("Timout while waiting for service.");
                    }
                }
                else
                {
                    result = C.TaskInfo.Fail("Service not in running mode! Mode: " + service.Status.ToString());
                }
            }
            catch (Exception ex)
            {
                result = C.TaskInfo.Fail(ex.Message);
            }

            return(result);
        }
Exemple #2
0
        public static C.TaskInfo ChangeUserPassword(string username, string newPassword)
        {
            C.TaskInfo result = C.TaskInfo.Fail("Init");

            try
            {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry grp;
                grp = AD.Children.Find(username, schemaClassName: "user");
                if (grp != null)
                {
                    grp.Invoke("SetPassword", new object[] { newPassword });
                    grp.CommitChanges();
                    result = C.TaskInfo.Success("User password changed!");
                }
                else
                {
                    result = C.TaskInfo.Fail("Can't find username.");
                }
            }
            catch (Exception ex)
            {
                result = C.TaskInfo.Fail(ex.Message);
            }

            return(result);
        }
        async public static void HandleClient(TcpClient client)
        {
            Logger log = new Logger("c-" + ((IPEndPoint)client.Client.RemoteEndPoint).Port);

            client.ReceiveTimeout = 1000;
            client.SendTimeout    = 1000;

            try
            {
                log.i("Getting header");
                C.TaskInfo task = await C.RecieveCommandHeader(client);

                if (task)
                {
                    C.CommandInfo cmdInfo = (task as C.TaskInfoResult <C.CommandInfo>).result;
                    if (cmdInfo.dataLength < 0)
                    {
                        throw new Exception("Task is corrupted (data length is -1)");
                    }

                    if (cmdInfo.dataLength > 1024)
                    {
                        log.e("Command data is more than 1KB\n Got:" + cmdInfo.dataLength);
                    }
                    else
                    {
                        log.i("Getting data");
                        C.TaskInfo dataTask = await C.RecieveCommandData(client, cmdInfo);

                        if (dataTask)
                        {
                            log.i("[OK] " + cmdInfo.cmd.ToString() + ": " + cmdInfo.data);
                            string command_result = Scenarios.HandleCommand(cmdInfo.cmd)(cmdInfo);
                            await C.SendCommand(cmdInfo.cmd, command_result, client);

                            await Task.Delay(5000); // wait for client to read response;
                        }
                        else
                        {
                            log.e("Can't read command data\n" + dataTask.eventReason);
                        }
                    }
                }
                else
                {
                    log.e("Can't read header\n" + task.eventReason);
                }
            }
            catch (Exception ex)
            {
                log.e("Client had errors", ex);
            }
            finally
            {
                client.Close();
            }
        }
Exemple #4
0
        public static C.TaskInfo RunProcessInfo(string name, string process, string args)
        {
            C.TaskInfo result = C.TaskInfo.Fail("Init");
            try
            {
                runCmdProcess(process, args);
                result = C.TaskInfo.Success("Completed '" + name + "' without errors");
            }
            catch (Exception ex)
            {
                result = C.TaskInfo.Fail(ex.Message);
            }

            return(result);
        }
Exemple #5
0
        public static C.TaskInfo isServiceRunning(string name, out bool isRunning)
        {
            C.TaskInfo result = C.TaskInfo.Fail("Init");
            isRunning = false;

            try
            {
                ServiceController service = new ServiceController(name);
                isRunning =
                    (service.Status == ServiceControllerStatus.Running) ||
                    (service.Status == ServiceControllerStatus.StartPending) ||
                    (service.Status == ServiceControllerStatus.ContinuePending)
                ;
                result = C.TaskInfo.Success("got status");
            }
            catch (Exception ex)
            {
                result = C.TaskInfo.Fail(ex.Message);
            }

            return(result);
        }