private HTTPResult HTTPResultConfig() { XmlDocument GCXMLResultConfig = (XmlDocument)GCXMLConfig.Clone(); GCXMLResultConfig.SelectSingleNode("/Configs/WebServer/Login").InnerText = "***************"; GCXMLResultConfig.SelectSingleNode("/Configs/WebServer/Password").InnerText = "***************"; HTTPResult result = new HTTPResult("application/xml", GCXMLResultConfig.OuterXml); GCXMLResultConfig = null; return(result); }
private HTTPResult HTTPResultGameStart(string gameGUID) { XmlNode GameXMLNode; XmlDocument XMLResult = new XmlDocument(); XmlNode XMLResultNode = XMLResult.AppendChild(XMLResult.CreateNode(XmlNodeType.Element, "Result", null)); XmlNode tmpNode; HTTPResult result; if (gameGUID == null) { tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "State", null); tmpNode.InnerText = "Error"; XMLResultNode.AppendChild(tmpNode); tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "Error", null); tmpNode.InnerText = "GUID Not defined"; XMLResultNode.AppendChild(tmpNode); result = new HTTPResult(400, "application/xml", XMLResult.OuterXml); } else { GameXMLNode = GamesXMLNode.SelectSingleNode("./Game[@guid='" + gameGUID + "']"); string[] ctrlReturn = ProcessCtrlBeforeStart(GameXMLNode); if (ctrlReturn != null) { tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "State", null); tmpNode.InnerText = ctrlReturn[1]; XMLResultNode.AppendChild(tmpNode); tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "Error", null); tmpNode.InnerText = ctrlReturn[0]; XMLResultNode.AppendChild(tmpNode); result = new HTTPResult(409, "application/xml", XMLResult.OuterXml); } else { Process process = ProcessStart(GameXMLNode); GameProcess.Add(gameGUID, process); tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "State", null); tmpNode.InnerText = "Starting"; XMLResultNode.AppendChild(tmpNode); tmpNode = XMLResult.CreateNode(XmlNodeType.Element, "GUID", null); tmpNode.InnerText = gameGUID; XMLResultNode.AppendChild(tmpNode); result = new HTTPResult(202, "application/xml", XMLResult.OuterXml); } } return(result); }
// ********************************************************* // HTTP functions private async Task HTTPHandleIncomingConnections() { // While a user hasn't visited the `shutdown` url, keep on handling requests while (true) { // Will wait here until we hear from a connection HttpListenerContext ctx = await listener.GetContextAsync(); // Peel out the requests and response objects HttpListenerRequest req = ctx.Request; HttpListenerResponse resp = ctx.Response; // Print out some info about the request Console.WriteLine(req.Url.ToString()); Console.WriteLine(req.HttpMethod); Console.WriteLine(req.UserHostName); Console.WriteLine(req.UserAgent); Console.WriteLine(); // Check authentification string authorization = req.Headers["Authorization"]; string userInfo; string username = ""; string password = ""; if (authorization != null) { byte[] tempConverted = Convert.FromBase64String(authorization.Replace("Basic ", "").Trim()); userInfo = System.Text.Encoding.UTF8.GetString(tempConverted); string[] usernamePassword = userInfo.Split(':'); username = usernamePassword[0]; password = usernamePassword[1]; } // Reply HTTPResult result; if ( username == GCXMLConfig.SelectSingleNode("/Configs/WebServer/Login").InnerText&& password == GCXMLConfig.SelectSingleNode("/Configs/WebServer/Password").InnerText ) { result = new HTTPResult(404, "text/html", "<!DOCTYPE><html><head><title>Gameserver Control</title></head><body>Not found</body></html>"); if ((req.HttpMethod == "GET") && (req.Url.AbsolutePath == "/api/v1/config")) { result = HTTPResultConfig(); } Match GameUriMatch = Regex.Match(req.Url.AbsolutePath, "/api/v1/game/(?<guid>[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?)/(?<action>status|start|stop)", RegexOptions.IgnoreCase); if (GameUriMatch.Success == true) { if (req.HttpMethod == "GET") { if (GameUriMatch.Groups["action"].Value == "status") { result = HTTPResultGameStatus(GameUriMatch.Groups["guid"].Value); } } if (req.HttpMethod == "POST") { if (GameUriMatch.Groups["action"].Value == "start") { result = HTTPResultGameStart(GameUriMatch.Groups["guid"].Value); } if (GameUriMatch.Groups["action"].Value == "stop") { result = HTTPResultGameStop(GameUriMatch.Groups["guid"].Value); } } } } else { result = new HTTPResult(401, "text/html", "<!DOCTYPE><html><head><title>Gameserver Control</title></head><body>Access denied</body></html>"); resp.AddHeader("WWW-Authenticate", "Basic realm=\"Gameserver Control\""); } // Write the response info byte[] data = Encoding.UTF8.GetBytes(String.Format(result.Content)); resp.ContentType = result.ContentType; resp.ContentEncoding = Encoding.UTF8; resp.ContentLength64 = data.LongLength; resp.StatusCode = result.StatusCode; // Write out to the response stream (asynchronously), then close it await resp.OutputStream.WriteAsync(data, 0, data.Length); resp.Close(); } }