initial attempt at serverlist autoconfiguration, please let me know in irc if any issues are encountered.

This commit is contained in:
mark 2007-08-27 22:29:58 +00:00
parent e22e859827
commit 2533ee486a
4 changed files with 96 additions and 32 deletions

View file

@ -13,7 +13,7 @@ namespace Server.Misc
public static bool Enabled = true; public static bool Enabled = true;
public static bool SocketBlock = true; // true to block at connection, false to block at login request public static bool SocketBlock = true; // true to block at connection, false to block at login request
public const int MaxAddresses = 10; public static int MaxAddresses = 10;
public static IPAddress[] Exemptions = new IPAddress[] //For hosting services where there are cases where IPs can be proxied public static IPAddress[] Exemptions = new IPAddress[] //For hosting services where there are cases where IPs can be proxied
{ {
@ -42,7 +42,7 @@ namespace Server.Misc
for ( int i = 0; i < netStates.Count; ++i ) for ( int i = 0; i < netStates.Count; ++i )
{ {
NetState compState = (NetState)netStates[i]; NetState compState = netStates[i];
if ( ourAddress.Equals( compState.Address ) ) if ( ourAddress.Equals( compState.Address ) )
{ {

View file

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Server; using Server;
@ -20,27 +21,47 @@ namespace Server.Misc
* private const string Address = "shard.host.com"; * private const string Address = "shard.host.com";
*/ */
public static readonly string Address = null; public static readonly string Address = null;
public static readonly string ServerName = "RunUO TC";
public const string ServerName = "RunUO TC"; public static readonly bool AutoDetect = true;
public static void Initialize() public static void Initialize()
{ {
Listener.Port = 2593; Listener.Port = 2593;
if ( Address == null ) {
if ( AutoDetect )
AutoDetection();
}
else {
Resolve( Address, out m_PublicAddress );
}
EventSink.ServerList += new ServerListEventHandler( EventSink_ServerList ); EventSink.ServerList += new ServerListEventHandler( EventSink_ServerList );
} }
public static void EventSink_ServerList( ServerListEventArgs e ) private static IPAddress m_PublicAddress;
private static void EventSink_ServerList( ServerListEventArgs e )
{ {
try try
{ {
IPAddress ipAddr; NetState ns = e.State;
Socket s = ns.Socket;
if ( Resolve( Address != null && !IsLocalMachine( e.State ) ? Address : Dns.GetHostName(), out ipAddr ) ) IPEndPoint ipep = (IPEndPoint)s.LocalEndPoint;
e.AddServer( ServerName, new IPEndPoint( ipAddr, Listener.Port ) );
else IPAddress localAddress = ipep.Address;
e.Rejected = true; int localPort = ipep.Port;
if ( IsPrivateNetwork( localAddress ) ) {
ipep = (IPEndPoint)s.RemoteEndPoint;
if ( !IsPrivateNetwork( ipep.Address ) && m_PublicAddress != null )
localAddress = m_PublicAddress;
}
e.AddServer( ServerName, new IPEndPoint( localAddress, localPort ) );
} }
catch catch
{ {
@ -48,45 +69,88 @@ namespace Server.Misc
} }
} }
public static bool Resolve( string addr, out IPAddress outValue ) private static void AutoDetection()
{ {
if ( !HasPublicIPAddress() ) {
Console.Write( "ServerList: Auto-detecting public IP address..." );
m_PublicAddress = FindPublicAddress();
if ( m_PublicAddress != null )
Console.WriteLine( "done ({0})", m_PublicAddress.ToString() );
else
Console.WriteLine( "failed" );
}
}
private static bool Resolve( string addr, out IPAddress outValue )
{
if ( IPAddress.TryParse( addr, out outValue ) ) if ( IPAddress.TryParse( addr, out outValue ) )
return true; return true;
try try {
{
IPHostEntry iphe = Dns.GetHostEntry( addr ); IPHostEntry iphe = Dns.GetHostEntry( addr );
if ( iphe.AddressList.Length > 0 ) if ( iphe.AddressList.Length > 0 ) {
{
outValue = iphe.AddressList[iphe.AddressList.Length - 1]; outValue = iphe.AddressList[iphe.AddressList.Length - 1];
return true; return true;
} }
} }
catch catch {}
{
}
outValue = IPAddress.None;
return false; return false;
} }
private static bool IsLocalMachine( NetState state ) private static bool HasPublicIPAddress()
{ {
IPAddress theirAddress = state.Address;
if ( IPAddress.IsLoopback( theirAddress ) )
return true;
bool contains = false;
IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() ); IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() );
for ( int i = 0; !contains && i < iphe.AddressList.Length; ++i ) IPAddress[] ips = iphe.AddressList;
contains = theirAddress.Equals( iphe.AddressList[i] );
return contains; for ( int i = 0; i < ips.Length; ++i )
if ( !IsPrivateNetwork( ips[i] ) )
return true;
return false;
}
private static bool IsPrivateNetwork( IPAddress ip )
{
// 10.0.0.0/8
// 172.16.0.0/12
// 192.168.0.0/16
if ( Utility.IPMatch( "192.168.*", ip ) )
return true;
else if ( Utility.IPMatch( "10.*", ip ) )
return true;
else if ( Utility.IPMatch( "172.16-31.*", ip ) )
return true;
else
return false;
}
private static IPAddress FindPublicAddress()
{
try {
WebRequest req = HttpWebRequest.Create( "http://www.runuo.com/ip.php" );
req.Timeout = 15000;
WebResponse res = req.GetResponse();
Stream s = res.GetResponseStream();
StreamReader sr = new StreamReader( s );
IPAddress ip = IPAddress.Parse( sr.ReadLine() );
sr.Close();
s.Close();
res.Close();
return ip;
} catch {
return null;
}
} }
} }
} }

View file

@ -346,7 +346,7 @@ namespace Server
if( SocketPool.Created ) if( SocketPool.Created )
SocketPool.Destroy(); SocketPool.Destroy();
Timer.Set(); Timer.TimerThread.Set();
Console.WriteLine( "done" ); Console.WriteLine( "done" );
} }

View file

@ -70,7 +70,7 @@ namespace Server.Network
IPAddress[] ip = iphe.AddressList; IPAddress[] ip = iphe.AddressList;
for ( int i = 0; i < ip.Length; ++i ) for ( int i = 0; i < ip.Length; ++i )
Console.WriteLine( "Address: {0}:{1}", ip[i], port ); Console.WriteLine( "Address: {0}:{1}", ip[i], port );
} }
catch catch
{ {