//============================================================================================================== // Log update //============================================================================================================== public static void OnPilotLog(HttpListenerRequest request, HttpListenerResponse response) { string responseString = ""; try { // Parsing arguments Dictionary <string, string> postArguments = WebReader.GetPostArgs(request); string sessionKey = postArguments["sessionKey"]; // Looking for player session Player player = Authorization.FindBySession(sessionKey); // Session found - send the log if (player != null) { responseString = WebConstructor.GetShipLog(player.ship); } // No session found else { responseString = "<p>Authorization error.</p>"; } } // Something went wrong (typically, argument parsing error) catch (Exception ex) { responseString = WebConstructor.defaultHeader; responseString += "<h1>Unknown error:</h1><h2>" + ex.Message + "</h2>"; responseString += WebConstructor.defaultFooter; } // Send the message WebWriter.Reply(response, responseString); }
public static void ThreadMain() { while (threadState != ThreadState.Stopping) { List <PlayerResponsePair> servedPlayers = new List <PlayerResponsePair>(); // Serving foreach (PlayerResponsePair data in waitingPlayers) { if (data.player.isWaitingForUpdate) { WebWriter.Reply(data.response, WebConstructor.GetShipLog(data.player.ship)); data.player.NotifyOnFlush(); servedPlayers.Add(data); } } if (servedPlayers.Count > 0) { ConsoleEx.Log("Served updates to " + servedPlayers.Count + " player(s)."); } // Cleaning up foreach (PlayerResponsePair data in servedPlayers) { waitingPlayers.Remove(data); } // Waiting System.Threading.Thread.Sleep(1000); } }
public static void ListenerCallback(IAsyncResult result) { try { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. string requestedPage = ""; foreach (string s in request.Url.Segments) { requestedPage += s; } if (requestedPage == "/") { requestedPage = "/index.html"; } ConsoleEx.Log("(" + request.RemoteEndPoint.Address.ToString() + ") " + request.HttpMethod.ToString() + " " + requestedPage); if (!request.IsSecureConnection) { WebWriter.SendFile(response, "www/forceSSL.html"); } else if (File.Exists("www" + requestedPage)) { WebWriter.SendFile(response, "www" + requestedPage); } else { if (requestedPage == "/hero") { WebRequest.OnHero(request, response); } else if (requestedPage == "/pilotLog") { WebRequest.OnPilotLog(request, response); } else if (requestedPage == "/register") { WebRequest.OnRegister(request, response); } else if (requestedPage == "/login") { WebRequest.OnLogin(request, response); } else if (requestedPage == "/update") { WebRequest.OnUpdate(request, response); } } } catch (Exception) { } }
//============================================================================================================== // Registration //============================================================================================================== public static void OnRegister(HttpListenerRequest request, HttpListenerResponse response) { string responseString = WebConstructor.defaultHeader; Dictionary <string, string> postArguments = WebReader.GetPostArgs(request); string username, password, passwordRepeat, shipName; try { // Parsing arguments username = postArguments["username"]; password = postArguments["password"]; passwordRepeat = postArguments["passwordRepeat"]; shipName = postArguments["shipName"]; // If the passwords do match, create an account if (password == passwordRepeat) { bool accountCreated = Authorization.CreateAccount(username, password); // If successful, login the player and create the ship if (accountCreated) { string sessionKey = Authorization.Login(username, password); Player player = Authorization.FindBySession(sessionKey); Ship ship = Universe.AddNewPlayerShip(shipName, player); player.startingShipName = shipName; // Redirect player to hero page responseString = WebConstructor.headOpener; responseString += "</head><body onload=\"sendPostRequest('hero', {sessionKey: '" + sessionKey + "'});\">"; } // Something failed within authorization module (typically username already taken) else { responseString += "<h1>" + ConsoleEx.lastErrorMessage + "</h1>"; } } // Password mismatch else if (password != passwordRepeat) { responseString += "<h1>Account creation failed. Passwords do not match.</h1>"; } // Finish the message responseString += WebConstructor.defaultFooter; } // Something went wrong (typically, argument parsing error) catch (Exception ex) { responseString = WebConstructor.defaultHeader; responseString += "<h1>Unknown error:</h1><h2>" + ex.Message + "</h2>"; responseString += WebConstructor.defaultFooter; } WebWriter.Reply(response, responseString); }
//============================================================================================================== // Login //============================================================================================================== public static void OnLogin(HttpListenerRequest request, HttpListenerResponse response) { string responseString = WebConstructor.defaultHeader; try { // Parsing arguments Dictionary <string, string> postArguments = WebReader.GetPostArgs(request); string username = postArguments["username"]; string password = postArguments["password"]; // A little easter egg if (username == "admin" && password == "admin") { responseString = WebConstructor.defaultHeader; responseString += "<h1>This is not a router bro.</h1>"; } // Actual username and password else if (username != null && password != null) { // Authorize a player string sessionKey = Authorization.Login(username, password); // Check if the authorization failed if (sessionKey == "") { throw new Exception(ConsoleEx.lastErrorMessage); } // Check if the ship is fine Player player = Authorization.FindBySession(sessionKey); // If not - create a new one if (player.ship == null) { Universe.AddNewPlayerShip(player.startingShipName, player); } // If authorization is successful, redirect to hero page if (sessionKey != "") { responseString = WebConstructor.headOpener; responseString += "</head><body onload=\"sendPostRequest('hero', {sessionKey: '" + sessionKey + "'});\">"; //responseString += "<h1>Login successful. Session key: " + sessionKey + "</h1>"; } // If not - show an error message else { responseString += "<h1>" + ConsoleEx.lastErrorMessage + "</h1>"; } } // Finish the message responseString += WebConstructor.defaultFooter; } // Something went wrong (typically, argument parsing error) catch (Exception ex) { responseString = WebConstructor.defaultHeader; responseString += "<h1>An error occured:</h1><h2>"; responseString += ex.Message + "</h2>"; responseString += WebConstructor.defaultFooter; } // Send message WebWriter.Reply(response, responseString); }