/// <summary> /// Handles a HTTP request /// </summary> /// <param name="Request">HTTP Request</param> /// <param name="Response">HTTP Response object to use</param> protected void HandleRequest(HttpListenerRequest Request, HttpListenerResponse Response) { Response.Headers["Server"] = MasterServer.Settings.WebServerServerHeader; // Check the IP ban list to see whether the remote host is banned if (banManager.IsBanned(Request.RemoteEndPoint.Address)) { Response.ContentType = "text/html"; Response.StatusCode = 401; StreamWriter Writer = new StreamWriter(Response.OutputStream); Writer.WriteLine("<html><head><title>401: Forbidden</title></head><body>Your IP is banned</body></html>"); Writer.Close(); } else { // Loop through active request handlers and see whether any of them can handle the request foreach (IRequestHandler requestHandler in requestHandlers) { if (requestHandler.HandleRequest(Request, Response)) { if (log != null) { log.Write(this, requestHandler.GetType().Name, Response.StatusCode, Request.HttpMethod, Request.Url.PathAndQuery, Request.RemoteEndPoint.Address, Request.LocalEndPoint.Address, Request.UserHostName, Request.UserAgent, (Request.UrlReferrer != null) ? Request.UrlReferrer.OriginalString : "-"); } // Request was handled by the handler return; } } // Provide some basic failover behaviour in case no handler handled the request (even the 404 handler!) Response.ContentType = "text/html"; Response.StatusCode = 404; StreamWriter Writer = new StreamWriter(Response.OutputStream); Writer.WriteLine("<html><head><title>404: Not Found</title></head><body><h2>404: Not Found</h2></body></html>"); Writer.Close(); } if (log != null) { log.Write(this, "None", Response.StatusCode, Request.HttpMethod, Request.Url.PathAndQuery, Request.RemoteEndPoint.Address, Request.LocalEndPoint.Address, Request.UserHostName, Request.UserAgent, (Request.UrlReferrer != null) ? Request.UrlReferrer.OriginalString : "-"); } }
/// <summary> /// Create a new connection instance to handle an inbound connection /// </summary> /// <param name="socket">TCP socket for communicating with the remote server</param> /// <param name="connectionLogWriter">Log writer module</param> /// <param name="serverList">Server List object</param> /// <param name="geoIP">GeoIP resolver</param> /// <param name="md5Manager">MD5 database manager</param> /// <param name="banManager">IP ban manager</param> /// <param name="cdKeyValidator">CD key validator</param> /// <param name="gameStats">Game stats module</param> public Connection(Socket socket, IConnectionLogWriter logWriter, ServerList serverList, GeoIP geoIP, MD5Manager md5Manager, IPBanManager banManager, ICDKeyValidator cdKeyValidator, IGameStatsLog gameStats) : this(socket, logWriter, serverList, geoIP) { // Raise the NewConnection event for connected packet analysers OnNewConnection(); // Check whether the remote host is banned if (banManager.IsBanned((socket.RemoteEndPoint as IPEndPoint).Address)) { ConnectionLog("BANNED"); socket.Close(); return; } this.md5Manager = md5Manager; this.cdKeyValidator = cdKeyValidator; this.gameStats = gameStats; // Handle this connection in a new thread ConnectionThreadManager.CreateStart(new ThreadStart(Handle)); }