+ 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,118 +1,193 @@
#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;
}
new StatusPage().Start();
Listen();
} }
public StatusPage() : base( TimeSpan.FromSeconds( 5.0 ), TimeSpan.FromSeconds( 60.0 ) ) 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 string Encode( string input ) private static void ListenerCallback(IAsyncResult result)
{ {
StringBuilder sb = new StringBuilder( input ); try
{
var context = _Listener.EndGetContext(result);
sb.Replace( "&", "&amp;" ); byte[] buffer;
sb.Replace( "<", "&lt;" );
sb.Replace( ">", "&gt;" ); lock (_StatusLock)
sb.Replace( "\"", "&quot;" ); {
sb.Replace( "'", "&apos;" ); 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)
{
var sb = new StringBuilder(input);
sb.Replace("&", "&amp;");
sb.Replace("<", "&lt;");
sb.Replace(">", "&gt;");
sb.Replace("\"", "&quot;");
sb.Replace("'", "&apos;");
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" ) )
{ {
op.WriteLine( "<html>" ); Directory.CreateDirectory("web");
op.WriteLine( " <head>" ); }
op.WriteLine( " <title>RunUO Server Status</title>");
op.WriteLine( " </head>" );
op.WriteLine( " <body bgcolor=\"white\">" );
op.WriteLine( " <h1>RunUO Server Status</h1>" );
op.WriteLine( " Online clients:<br>" );
op.WriteLine( " <table width=\"100%\">" );
op.WriteLine( " <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 ) using (var op = new StreamWriter("web/status.html"))
{
op.WriteLine("<!DOCTYPE html>");
op.WriteLine("<html>");
op.WriteLine(" <head>");
op.WriteLine(" <title>" + ServerList.ServerName + " Server Status</title>");
op.WriteLine(" </head>");
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(" <h3>Online clients</h3>");
op.WriteLine(" <table cellpadding=\"0\" cellspacing=\"0\">");
op.WriteLine(" <tr class=\"ruo-header\"><td>Name</td><td>Location</td><td>Kills</td><td>Karma/Fame</td></tr>");
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;
op.Write(" <tr class=\"ruo-result " + (index % 2 == 0 ? "even" : "odd") + "\"><td>");
if (g != null)
{ {
Guild g = m.Guild as Guild; op.Write(Encode(m.Name));
op.Write(" [");
op.Write( " <tr><td>" ); var title = m.GuildTitle;
if ( g != null ) title = title != null ? title.Trim() : String.Empty;
if (title.Length > 0)
{ {
op.Write( Encode( m.Name ) ); op.Write(Encode(title));
op.Write( " [" ); op.Write(", ");
string title = m.GuildTitle;
if ( title != null )
title = title.Trim();
else
title = "";
if ( title.Length > 0 )
{
op.Write( Encode( title ) );
op.Write( ", " );
}
op.Write( Encode( g.Abbreviation ) );
op.Write( ']' );
}
else
{
op.Write( Encode( m.Name ) );
} }
op.Write( "</td><td>" ); op.Write(Encode(g.Abbreviation));
op.Write( m.X );
op.Write( ", " ); op.Write(']');
op.Write( m.Y );
op.Write( ", " );
op.Write( m.Z );
op.Write( " (" );
op.Write( m.Map );
op.Write( ")</td><td>" );
op.Write( m.Kills );
op.Write( "</td><td>" );
op.Write( m.Karma );
op.Write( " / " );
op.Write( m.Fame );
op.WriteLine( "</td></tr>" );
} }
else
{
op.Write(Encode(m.Name));
}
op.Write("</td><td>");
op.Write(m.X);
op.Write(", ");
op.Write(m.Y);
op.Write(", ");
op.Write(m.Z);
op.Write(" (");
op.Write(m.Map);
op.Write(")</td><td>");
op.Write(m.Kills);
op.Write("</td><td>");
op.Write(m.Karma);
op.Write(" / ");
op.Write(m.Fame);
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);
} }
} }
} }