+ Added simple HttpListener for the integrated RunUO status page.

* HttpListener will serve the status page to a URI in the format of http://<shard-ip>/status/
* Port 80 may need to be white-listed for external requests to be served.
This commit is contained in:
Vorspire 2015-08-23 01:34:04 +01:00
parent d093acfb2e
commit 5d50a5a37c

View file

@ -1,30 +1,89 @@
#region References
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Net;
using System.Text; using System.Text;
using Server;
using Server.Network;
using Server.Guilds; using Server.Guilds;
using Server.Network;
#endregion
namespace Server.Misc namespace Server.Misc
{ {
public class StatusPage : Timer public class StatusPage : Timer
{ {
public static bool Enabled = false; public static readonly bool Enabled = true;
private static HttpListener _Listener;
private static string _StatusPage = String.Empty;
private static byte[] _StatusBuffer = new byte[0];
private static readonly object _StatusLock = new object();
public static void Initialize() public static void Initialize()
{ {
if ( Enabled ) if (!Enabled)
new StatusPage().Start(); {
return;
} }
public StatusPage() : base( TimeSpan.FromSeconds( 5.0 ), TimeSpan.FromSeconds( 60.0 ) ) new StatusPage().Start();
Listen();
}
private static void Listen()
{ {
Priority = TimerPriority.FiveSeconds; if (!HttpListener.IsSupported)
{
return;
}
if (_Listener == null)
{
_Listener = new HttpListener();
_Listener.Prefixes.Add("http://*:80/status/");
_Listener.Start();
}
else if (!_Listener.IsListening)
{
_Listener.Start();
}
if (_Listener.IsListening)
{
_Listener.BeginGetContext(ListenerCallback, null);
}
}
private static void ListenerCallback(IAsyncResult result)
{
try
{
var context = _Listener.EndGetContext(result);
byte[] buffer;
lock (_StatusLock)
{
buffer = _StatusBuffer;
}
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
}
catch
{ }
Listen();
} }
private static string Encode(string input) private static string Encode(string input)
{ {
StringBuilder sb = new StringBuilder( input ); var sb = new StringBuilder(input);
sb.Replace("&", "&amp;"); sb.Replace("&", "&amp;");
sb.Replace("<", "&lt;"); sb.Replace("<", "&lt;");
@ -35,46 +94,57 @@ namespace Server.Misc
return sb.ToString(); return sb.ToString();
} }
public StatusPage()
: base(TimeSpan.FromSeconds(5.0), TimeSpan.FromSeconds(60.0))
{
Priority = TimerPriority.FiveSeconds;
}
protected override void OnTick() protected override void OnTick()
{ {
if (!Directory.Exists("web")) if (!Directory.Exists("web"))
Directory.CreateDirectory( "web" );
using ( StreamWriter op = new StreamWriter( "web/status.html" ) )
{ {
Directory.CreateDirectory("web");
}
using (var op = new StreamWriter("web/status.html"))
{
op.WriteLine("<!DOCTYPE html>");
op.WriteLine("<html>"); op.WriteLine("<html>");
op.WriteLine(" <head>"); op.WriteLine(" <head>");
op.WriteLine( " <title>RunUO Server Status</title>"); op.WriteLine(" <title>" + ServerList.ServerName + " Server Status</title>");
op.WriteLine(" </head>"); op.WriteLine(" </head>");
op.WriteLine( " <body bgcolor=\"white\">" ); op.WriteLine(" <style type=\"text/css\">");
op.WriteLine(" body { background: #999; }");
op.WriteLine(" table { width: 100%; }");
op.WriteLine(" tr.ruo-header td { background: #000; color: #FFF; }");
op.WriteLine(" tr.odd td { background: #222; color: #DDD; }");
op.WriteLine(" tr.even td { background: #DDD; color: #222; }");
op.WriteLine(" </style>");
op.WriteLine(" <body>");
op.WriteLine(" <h1>RunUO Server Status</h1>"); op.WriteLine(" <h1>RunUO Server Status</h1>");
op.WriteLine( " Online clients:<br>" ); op.WriteLine(" <h3>Online clients</h3>");
op.WriteLine( " <table width=\"100%\">" ); op.WriteLine(" <table cellpadding=\"0\" cellspacing=\"0\">");
op.WriteLine( " <tr>" ); op.WriteLine(" <tr class=\"ruo-header\"><td>Name</td><td>Location</td><td>Kills</td><td>Karma/Fame</td></tr>");
op.WriteLine( " <td bgcolor=\"black\"><font color=\"white\">Name</font></td><td bgcolor=\"black\"><font color=\"white\">Location</font></td><td bgcolor=\"black\"><font color=\"white\">Kills</font></td><td bgcolor=\"black\"><font color=\"white\">Karma / Fame</font></td>" );
op.WriteLine( " </tr>" );
foreach ( NetState state in NetState.Instances ) var index = 0;
foreach (var m in NetState.Instances.Where(state => state.Mobile != null).Select(state => state.Mobile))
{ {
Mobile m = state.Mobile; ++index;
if ( m != null ) var g = m.Guild as Guild;
{
Guild g = m.Guild as Guild;
op.Write( " <tr><td>" ); op.Write(" <tr class=\"ruo-result " + (index % 2 == 0 ? "even" : "odd") + "\"><td>");
if (g != null) if (g != null)
{ {
op.Write(Encode(m.Name)); op.Write(Encode(m.Name));
op.Write(" ["); op.Write(" [");
string title = m.GuildTitle; var title = m.GuildTitle;
if ( title != null ) title = title != null ? title.Trim() : String.Empty;
title = title.Trim();
else
title = "";
if (title.Length > 0) if (title.Length > 0)
{ {
@ -107,13 +177,18 @@ namespace Server.Misc
op.Write(m.Fame); op.Write(m.Fame);
op.WriteLine("</td></tr>"); op.WriteLine("</td></tr>");
} }
}
op.WriteLine(" <tr>"); op.WriteLine(" <tr>");
op.WriteLine(" </table>"); op.WriteLine(" </table>");
op.WriteLine(" </body>"); op.WriteLine(" </body>");
op.WriteLine("</html>"); op.WriteLine("</html>");
} }
lock (_StatusLock)
{
_StatusPage = File.ReadAllText("web/status.html");
_StatusBuffer = Encoding.UTF8.GetBytes(_StatusPage);
}
} }
} }
} }