new masterserver code
git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@4656 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
@@ -22,15 +22,17 @@ using AHSoftware.CubeMon.Games.ActionCube.v094;
|
||||
/// </summary>
|
||||
public class CubeMsRequestHandler : IHttpHandler
|
||||
{
|
||||
Log log = new Log();
|
||||
CubeMS master;
|
||||
CubeMasterDAL masterDal;
|
||||
ServerBlacklist blacklist;
|
||||
int serverListCacheMinutes;
|
||||
|
||||
CubeMsRequestHandler()
|
||||
public CubeMsRequestHandler()
|
||||
{
|
||||
master = new CubeMS();
|
||||
master.ConnectionString = WebConfigurationManager.ConnectionStrings["CubeMS"].ConnectionString;
|
||||
masterDal = new CubeMasterDAL();
|
||||
masterDal.ConnectionString = WebConfigurationManager.ConnectionStrings["CubeMS"].ConnectionString;
|
||||
serverListCacheMinutes = int.Parse(WebConfigurationManager.AppSettings["ServerListCacheMinutes"] ?? "0");
|
||||
|
||||
blacklist = new ServerBlacklist(HttpContext.Current.Server.MapPath("/Blacklist.xml"));
|
||||
}
|
||||
|
||||
#region IHttpHandler Members
|
||||
@@ -42,14 +44,18 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
IPEndPoint address = new IPEndPoint(IPAddress.Parse(context.Request.UserHostAddress), 0);
|
||||
|
||||
string serverAddress = context.Request.UserHostAddress;
|
||||
|
||||
IPEndPoint address = new IPEndPoint(IPAddress.Parse(serverAddress), 0);
|
||||
string requestFile = Path.GetFileName(context.Request.FilePath);
|
||||
|
||||
try
|
||||
{
|
||||
if(requestFile == "register.do" && context.Request.QueryString["action"] == "add")
|
||||
{
|
||||
context.Response.ContentType = "text/plain";
|
||||
// fixme
|
||||
//context.Response.ContentType = "text/plain";
|
||||
|
||||
try
|
||||
{
|
||||
@@ -58,28 +64,65 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
catch
|
||||
{
|
||||
context.Response.Write("Server not registered, invalid request. Maybe you use an older server version?");
|
||||
log.AddEvent(Log.EventType.RegistrationFailed, "invalid request", address);
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RegistrationFailed, "invalid request", address);
|
||||
return;
|
||||
}
|
||||
|
||||
if(master.Register(address))
|
||||
if (blacklist.IsBlacklisted(serverAddress) || context.Request.QueryString["bansample"] == "1")
|
||||
{
|
||||
/*
|
||||
context.Response.Write(RickRoll.RickRollMeNow());
|
||||
context.Response.Flush();
|
||||
*/
|
||||
|
||||
|
||||
string lyrics = RickRoll.RickRollMeNow();
|
||||
|
||||
using(StringReader reader = new StringReader(lyrics))
|
||||
{
|
||||
string line = reader.ReadLine();
|
||||
while(line != null)
|
||||
{
|
||||
// rick roll them.. line by line
|
||||
context.Response.Write(line);
|
||||
context.Response.Write(Environment.NewLine);
|
||||
context.Response.Flush();
|
||||
|
||||
//System.Threading.Thread.Sleep(250);
|
||||
line = reader.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
context.Response.Write(Environment.NewLine);
|
||||
context.Response.Write("Behave and try again in a few days..");
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RegistrationFailed, "blacklisted", address);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
masterDal.Register(address);
|
||||
|
||||
// TODO: *DOS safety
|
||||
context.Response.Write("Registration successful. Due to caching it might take a few minutes to see the your server in the serverlist");
|
||||
log.AddEvent(Log.EventType.RegistrationSuceeeded, null, address);
|
||||
}
|
||||
else
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RegistrationSuceeeded, null, address);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
context.Response.Write("Server not registered, could not ping you. Make sure your server is accessible from the internet.");
|
||||
log.AddEvent(Log.EventType.RegistrationFailed, "ping", address);
|
||||
context.Response.Write("Server not registered: " + ex.Message + ex.StackTrace);
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RegistrationFailed, null, address);
|
||||
}
|
||||
}
|
||||
else if(requestFile == "retrieve.do")
|
||||
{
|
||||
|
||||
string type = context.Request.QueryString["item"];
|
||||
if(type == "list")
|
||||
{
|
||||
context.Response.ContentType = "text/plain";
|
||||
// fixme
|
||||
context.Response.ContentType = "text/plain";
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
@@ -88,13 +131,13 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
string servers = context.Cache[type] as string;
|
||||
if(servers == null || servers.Length == 0) // empty cache, or empty server list
|
||||
{
|
||||
servers = master.GetServers(false);
|
||||
servers = masterDal.GetServers(false);
|
||||
if(serverListCacheMinutes > 0) context.Cache.Add(type, servers, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, serverListCacheMinutes, 0), CacheItemPriority.Normal, null);
|
||||
}
|
||||
|
||||
context.Response.Write(servers);
|
||||
context.Response.Write(servers);
|
||||
context.Response.Flush();
|
||||
log.AddEvent(Log.EventType.RetrieveServers, "list", address);
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RetrieveServers, "list", address);
|
||||
|
||||
// append client script if its an older or unknown client
|
||||
if(bool.Parse((WebConfigurationManager.AppSettings["EnableClientScripts"] ?? "false")) && context.Request.UserAgent != WebConfigurationManager.AppSettings["ClientAgent"])
|
||||
@@ -102,7 +145,7 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
string script = context.Cache["script"] as string;
|
||||
if(script == null)
|
||||
{
|
||||
if((script = master.GetClientScript(context.Request.UserAgent)) != null)
|
||||
if((script = masterDal.GetClientScript(context.Request.UserAgent)) != null)
|
||||
{
|
||||
int cacheminutes = int.Parse(WebConfigurationManager.AppSettings["ClientScriptCacheMinutes"] ?? "0");
|
||||
context.Cache.Add("script", script, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, cacheminutes, 0), CacheItemPriority.Normal, null);
|
||||
@@ -113,10 +156,11 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
{
|
||||
context.Response.Write(script);
|
||||
context.Response.Flush();
|
||||
log.AddEvent(Log.EventType.RetrieveServers, "script", address);
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RetrieveServers, "script", address);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
catch
|
||||
{
|
||||
// send error to the client
|
||||
@@ -124,6 +168,10 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
context.Response.Flush();
|
||||
throw; // log errors globally
|
||||
}
|
||||
*/
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
else if(context.Request.QueryString["item"] == "xml")
|
||||
{
|
||||
@@ -132,17 +180,18 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
string serversXml = context.Cache[type] as string;
|
||||
if(serversXml == null || serversXml.Length == 0) // empty cache, or empty server list
|
||||
{
|
||||
serversXml = master.GetServers(true);
|
||||
serversXml = masterDal.GetServers(true);
|
||||
if(serverListCacheMinutes > 0) context.Cache.Add(type, serversXml, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, serverListCacheMinutes, 0), CacheItemPriority.Normal, null);
|
||||
}
|
||||
|
||||
context.Response.Write(serversXml);
|
||||
context.Response.Flush();
|
||||
|
||||
log.AddEvent(Log.EventType.RetrieveServers, "xml", address);
|
||||
masterDal.AddEvent(CubeMasterDAL.EventType.RetrieveServers, "xml", address);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
catch(Exception ex)
|
||||
{
|
||||
try
|
||||
@@ -151,7 +200,10 @@ public class CubeMsRequestHandler : IHttpHandler
|
||||
// log.AddEvent(Log.EventType.Exception, ex.Message, address);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}*/
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user