Exemple #1
0
        /// <summary>
        /// Checks in the client with the server
        /// </summary>
        /// <returns>Whether or not the call was successful</returns>
        public static bool CheckIn()
        {
            try
            {
                if (!(appSettings.ServerSettings.ServerConnectionEnabled) || associatedBuilder == null)
                {
                    return(false);
                }

                httpLogger.Information("Client is attempting to check in with the server");


                var workerID = appSettings.ServerSettings.HTTPGuid;

                if (workerID != Guid.Empty)
                {
                    httpLogger.Information("Requesting to check in with the server");
                    var client  = new WebClient();
                    var content = client.DownloadString("https://localhost:44377/api/Workers/CheckIn?workerID=" + workerID + "&engineBusy=" + Core.Client.EngineBusy);
                    httpLogger.Information("Received /api/Workers/CheckIn response: " + content);
                    var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject <CheckInResponse>(content);

                    if (deserialized.ScheduledTask != null)
                    {
                        associatedBuilder.Invoke(new MethodInvoker(delegate()
                        {
                            if (deserialized.ScheduledTask.ExecutionType == "Local")
                            {
                                UI.Forms.frmScriptEngine newEngine = new UI.Forms.frmScriptEngine(deserialized.PublishedScript.ScriptData, null);
                                newEngine.remoteTask      = deserialized.ScheduledTask;
                                newEngine.serverExecution = true;
                                newEngine.Show();
                            }
                            else
                            {
                                UI.Forms.frmScriptEngine newEngine = new UI.Forms.frmScriptEngine();
                                newEngine.xmlData         = deserialized.PublishedScript.ScriptData;
                                newEngine.remoteTask      = deserialized.ScheduledTask;
                                newEngine.serverExecution = true;
                                newEngine.Show();
                            }
                        }));
                    }


                    return(true);
                }
                else
                {
                    httpLogger.Information("Client unable to check in because the client does not have a GUID");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                httpLogger.Information("Error Checking In: " + ex.ToString());
                return(false);
            }
        }
 private static void RunXMLScript(string scriptData)
 {
     associatedBuilder.Invoke(new MethodInvoker(delegate()
     {
         UI.Forms.frmScriptEngine newEngine = new UI.Forms.frmScriptEngine();
         newEngine.xmlData      = scriptData;
         newEngine.callBackForm = null;
         newEngine.Show();
     }));
 }
Exemple #3
0
        private static void ProcessRequest(string data, string[] messageContent, NetworkStream stream)
        {
            if ((data.StartsWith("POST /ExecuteScript")) || (data.StartsWith("POST /AwaitScript")))
            {
                automationLogger.Information($"Client Requests Script Execution");

                //locate the body content
                string dataParameter  = "";
                bool   isFileLocation = false;

                //find the script parameter
                foreach (var item in messageContent)
                {
                    if (item.StartsWith("ScriptData: "))
                    {
                        dataParameter  = item.Replace("ScriptData: ", "");
                        isFileLocation = false;

                        break;
                    }

                    else if (item.StartsWith("ScriptLocation: "))
                    {
                        dataParameter  = item.Replace("ScriptLocation: ", "");
                        isFileLocation = true;
                        break;
                    }
                }

                //check to see if nothing was provided
                if (string.IsNullOrEmpty(dataParameter))
                {
                    automationLogger.Information($"Client Script Data Not Found");
                    return;
                }


                if (dataParameter.TryParseBase64(out var base64SourceString))
                {
                    automationLogger.Information($"Client Passed Base64 String: {base64SourceString}");
                    dataParameter = base64SourceString;
                }
                else
                {
                    automationLogger.Information($"Client Did Not Pass Base64 String");
                }


                //check if data parameter references file location
                if (isFileLocation)
                {
                    if (File.Exists(dataParameter))
                    {
                        //file was found at path provided
                        dataParameter = File.ReadAllText(dataParameter);
                    }
                    else if (File.Exists(Path.Combine(IO.Folders.GetFolder(IO.Folders.FolderType.ScriptsFolder), dataParameter)))
                    {
                        //file was found at fallback to scripts folder
                        dataParameter = Path.Combine(IO.Folders.GetFolder(IO.Folders.FolderType.ScriptsFolder), dataParameter);
                        dataParameter = File.ReadAllText(dataParameter);
                    }
                    else
                    {
                        //file not found
                        automationLogger.Information($"Client Script Location Not Found: {dataParameter}");
                        SendResponse(ResponseCode.InternalServerError, $"Client Script Location Not Found: {dataParameter}", stream);
                        return;
                    }
                }

                //log execution
                automationLogger.Information($"Executing Script: {dataParameter}");


                //invoke builder and pass it script data to execute
                associatedBuilder.Invoke(new MethodInvoker(delegate()
                {
                    UI.Forms.frmScriptEngine newEngine = new UI.Forms.frmScriptEngine();
                    newEngine.xmlData      = dataParameter;
                    newEngine.callBackForm = null;
                    //instance = newEngine.engineInstance;
                    newEngine.Show();
                }));


                if (data.StartsWith("POST /AwaitScript"))
                {
                    //reset result value
                    TasktResult = "";

                    //add reference to script finished event
                    automationInstance.ScriptFinishedEvent += AutomationInstance_ScriptFinishedEvent;

                    //wait for script to finish before returning
                    do
                    {
                        Thread.Sleep(1000);
                    } while (TasktResult == string.Empty);

                    //send response back to client
                    SendResponse(ResponseCode.OK, automationInstance.TasktResult, stream);
                }
                else
                {
                    //return success immediately
                    SendResponse(ResponseCode.OK, "Script Launched Successfully", stream);
                }
            }
            else if (data.StartsWith("POST /ExecuteCommand"))
            {
                automationLogger.Information($"Client Requests Command Execution");

                //locate the body content
                string dataParameter = "";

                //find the script parameter
                foreach (var item in messageContent)
                {
                    if (item.StartsWith("CommandData: "))
                    {
                        dataParameter = item.Replace("CommandData: ", "");
                        break;
                    }
                }

                //check to see if nothing was provided
                if (string.IsNullOrEmpty(dataParameter))
                {
                    automationLogger.Information($"Client Command Data Not Found");
                    return;
                }


                if (dataParameter.TryParseBase64(out var base64SourceString))
                {
                    automationLogger.Information($"Client Passed Base64 String: {base64SourceString}");
                    dataParameter = base64SourceString;
                }
                else
                {
                    automationLogger.Information($"Client Did Not Pass Base64 String");
                }

                try
                {
                    //deserialize command
                    var command = Newtonsoft.Json.JsonConvert.DeserializeObject(dataParameter, new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
                    });

                    //log execution
                    automationLogger.Information($"Executing Command: {dataParameter}");

                    //define script action
                    var scriptAction = new Script.ScriptAction()
                    {
                        ScriptCommand = (Core.Automation.Commands.ScriptCommand)command
                    };

                    //execute command
                    ExecuteCommandEngine.ExecuteCommand(scriptAction);

                    //send back response
                    SendResponse(ResponseCode.OK, "Command Executed Successfully", stream);
                }
                catch (Exception ex)
                {
                    SendResponse(ResponseCode.InternalServerError, $"An error occured: {ex.ToString()}", stream);
                }
            }

            else if (data.StartsWith("POST /EngineStatus"))
            {
                automationLogger.Information($"Returning Engine Status: {Client.ClientStatus}");
                SendResponse(ResponseCode.OK, Core.Client.ClientStatus, stream);
            }
            else if (data.StartsWith("POST /RestartTaskt"))
            {
                automationLogger.Information($"Restarting taskt");
                SendResponse(ResponseCode.OK, "taskt is being Restarted", stream);
                Application.Restart();
            }
            else
            {
                automationLogger.Information($"Invalid Client Request");
                SendResponse(ResponseCode.InternalServerError, "Invalid Client Request", stream);
            }
        }