public ProxiesController(ILoggerService logger, IProxyQuery proxyQuery, IProxyCommand proxyCommand) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _proxyQuery = proxyQuery ?? throw new ArgumentNullException(nameof(proxyQuery)); _proxyCommand = proxyCommand ?? throw new ArgumentNullException(nameof(proxyCommand)); }
/// <summary> /// Parses, validates and dispatches messages from the client /// </summary> /// <param name="s">The raw message sent from the client</param> /// <returns>the response to be returned to the client</returns> private string HandleMessage(string s) { //parse the request from the client into an XML document XmlDocument requestDocument = new XmlDocument(); //load the message into the document requestDocument.LoadXml(s); //get a reference to the command element node. XmlNode commandNode = requestDocument.SelectSingleNode("/command"); //check that there is a command element if (commandNode == null) { //if not, the message is formatted wrong and we cant recognize it. //return an error message to send to the client return(CreateErrorDocument("Input not recognized").OuterXml); } //create an XMLDocument to hold to response to be sent back to the client XmlDocument responseDocument = new XmlDocument(); //create the top level response element XmlNode responseElement = responseDocument.CreateNode(XmlNodeType.Element, "response", ""); //add the response element to the response document responseDocument.AppendChild(responseElement); //get the id for the command string id = ExtractId(commandNode); //create a new attribute named id XmlAttribute idAttribute = responseDocument.CreateAttribute("id"); //set its value to the request id idAttribute.Value = id; //add the new id element to the response id responseElement.Attributes.SetNamedItem(idAttribute); //get child nodes from the request document. Each one should //represent a command (there should be only one) XmlNodeList children = commandNode.ChildNodes; //IProxyCommand that contains the command sent from the client IProxyCommand command = null; //attribute tha holds response type XmlAttribute typeAttribute = responseDocument.CreateAttribute("type"); //loop through all of the command nodes (usually only one) foreach (XmlNode node in children) { //look at the node name. This will indicate the command switch (node.Name) { //exececute command case "exec": { //create a new ExecCommand instance command = new ExecCommand(); //try and run the command try { //pass in the response and request document, capture the response document //with output from the command included responseDocument = command.Exec(requestDocument, responseDocument); //set the type attribute to success typeAttribute.Value = "success"; //add the type attribute to the response document responseDocument.FirstChild.Attributes.SetNamedItem(typeAttribute); //return a string of XML representing the response document to send to //the client return(responseDocument.OuterXml); } catch (Exception e) { //something went wrong, create error document and send its xml to the client return(CreateErrorDocument(e.Message, id).OuterXml); } } //screenshot command case "screenshot": { //create the screenshot command command = new ScreenshotCommand(); try { //pass in the response and request document, capture the response document //with output from the command included responseDocument = command.Exec(requestDocument, responseDocument); //set the type attribute to success typeAttribute.Value = "success"; //add the type attribute to the response document responseDocument.FirstChild.Attributes.SetNamedItem(typeAttribute); //return a string of XML representing the response document to send to //the client return(responseDocument.OuterXml); } catch (Exception e) { //something went wrong, create error document and send its xml to the client return(CreateErrorDocument(e.Message, id).OuterXml); } } } } //if we get to here we didnt recognize the command, so we just return null //todo: should this move to switch defaul //todo: should be through an error? return(null); }