RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

ServerList.cs with AutoIP and AutoLocalLan

Status
Not open for further replies.

krazeykow

Sorceror
Rakshasas said:
I've been using this for a while and it's been doing the trick. But after 20 mins ago or so something very strange happened on the console.



It not working is one thing, but how the heck did that happen?



-This is because the script could not somehow, interact with the site, put a "//" infront of the ip sites that do this to you... this should fix the problem... it picks a site from a random list, and when it picks one that has a dead link it just can't get the ip... I hope this helps you..

-krazeykow
 

smokeem

Wanderer
Sorry I know your very busy but....

mrfixit said:
Summary:
ServerList.cs is a script used by RunUO to send the inbuildnt serverlist to the uo client.

Because so many have problems with the default ServerList.cs, I created a new version that automaticly finds you internet ip and finds out if you are connecting localy.

Description:
You only need this custom ServerList.cs if your behind any NAT devices like a router. If you are behind a router your runuo console will show a ip in one of these ip ranges:
10.0.0.0 -> 10.255.255.255
192.168.0.0 -> 192.168.255.255
172.16.0.0 -> 172.32.255.255
169.254.0.0 -> 169.254.255.255

This scripts fixes two problems with the defualt script:
1. RunUO does not know the internet ip of your connection when you are behind a router. This script gets the internet ip from a website (Multiple mirrors).
2. When you specify a internet ip in the default script RunUO will send this to everyone, even if you are connecting localy causing that you loose connecting after click on your shards name in the uo clients serverlist. This script will automaticly check if you are connecting localy and give you the correct local lan ip (Fixed in RunUO 1.0 betas).

Installation:
1. Download ServerList.cs.
2. Replace the origional in the runuo\scripts\misc folder with the new file.
-----------------------------------------------------------------------------------
Ok if i replace your serverlist.cs with mine do I have to fill in where it says [YOUR SERVER NAME] [YOUR IP ADRESS] and all that with mine before copy pasting your file inplace of mine.
I just dont want to lose all the work ive done so far on my server.
 

Racetrack0

Wanderer
I assume your reference to "your serverlist.cs" means that was addressed for Mrfixit, but since it's gone a few days, I figured I'd get back to you to the best of my ability.

Where it says [YOUR SERVER NAME], it won't really care what you put in there (within reason - no illegal control characters, but you shouldn't use one by accident), so long as it is within quotations (this is the case whenever you assign a value to a String-type variable). This will be what your shard is called in the shard list. If it's just "YOUR SERVER NAME" (followed by a semi-colon to end the line), that'll work fine, but that will be the name players see for your shard.

As for the IP address, there are two options. If you use his clever method to automatically retrieve your WAN IP address, then set it equal to null (this is one case where you don't enclose it in quotes, as null is a reserved word, but do follow it with a semi-colon). If you choose to enter the IP, as I did since mine is static, then put the IP/DNS name in quotes, followed by a semi-colon (and set auto-detect to false). But yes, this one you should change this to one of these two.

Hope this helps :)

(Incidentally, I don't know if it will just ignore the IP address value - given it's a valid string assignment - when the auto-detect is set to true, but setting it null is one way to guarantee it really is auto-detecting.)
 

petrock

Wanderer
Nice script, but it didn't work for me without modification, unfortunately. I am behind a netgear router and have set my computer as the DMZ. Even as the DMZ neither the distro nor this script would let anyone connect to my shard from the internet. I solved this by commenting out the line with FindMachine(e) at around line 180 and replacing it with this: "IPAddress ipAddr = IPAddress.Parse( Address );". Apparently the FindMachine function didn't work correctly on my network for some reason. I have had very bad luck with RunUO working correctly with routers in the last few months, so hopefully this info can help someone with similar problems.
 

evilfreak

Wanderer
i installed it worked great got me on

uogateway server which it would never show up there before so i thought cool got it open to outside internet. so i thought i better shut it down and change the server name in the serverlist.cs and started it back up no more public server listing for me ive tried going in and modifying the server name back and yet no results any clues guys? here is my current serverlist.cs and im all the time connected through a dsl modem with no firewalls.



// ==================================================================================================
// ServerList.cs
// ==================================================================================================
// 1.0 RunUO Beta 36 Initial version
// 1.1 Mr Fixit Now automaticly detects if you are connecting localy and uses the
// servers local ip in the client serverlist.
// 1.2 Mr Fixit Internet IP autodetection using www.whatismyip.com.
// 1.3 Mr Fixit If script fails to find the internet ip, keep the old one and try
// again in # minutes.
// 1.4 Mr Fixit You can now add AutoIP mirrors. Added whatismyip.com and myip.com.
// 1.5 Mr Fixit Adjusted the AutoIP mirror engine so it supports more mirrors.
// Added findmyip.com and ipaddy.com.
// 1.6 Mr Fixit IP is now trimmed (Just in case). Added simflex.com, edpsciences.com,
// ipid.shat.net and checkip.dyndns.org.
// 1.7 Mr Fixit Removed www.whatismyip.com is it seems to be out of buisness.
// ==================================================================================================

using System;
using System.Net;
using System.Net.Sockets;
using Server;
using Server.Network;

namespace Server.Misc
{
public class ServerList
{

// ==================================================================================
// YOUR SERVERS NAME
// ==================================================================================
public const string ServerName = "blah blah";

// ==================================================================================
// YOUR INTERNET IP OR DNS NAME
// Here you can select to autodetect your internet ip, or manualy specify
// Examples:
// public static string Address = "12.34.56.78";
// public static string Address = "shard.host.com";
// ==================================================================================
public const bool InternetIPAutodetect = true;
public const int MinutesBetweenIPAutodetect = 30;
public static string Address = null;

// ==================================================================================
// Here are some values stored
// ==================================================================================
private static LocalLanIPRange[] LocalLanIPRanges = new LocalLanIPRange[10];
private static UInt32 LocalLanIPRangesCount;
private static AutoIPMirror[] AutoIPMirrors = new AutoIPMirror[10];
private static UInt32 AutoIPMirrorsCount;
private static DateTime InternetIPAutodetectLast;


// ==================================================================================
// Initialization
// ==================================================================================
public static void Initialize()
{
// ----------------------------------------------------
// Select what port to use
// ----------------------------------------------------
Listener.Port = 2593;

// ----------------------------------------------------
// Load the local LAN ip ranges
// ----------------------------------------------------
AddLocalLANIPRange("10.0.0.0", "10.255.255.255");
AddLocalLANIPRange("192.168.0.0", "192.168.255.255");
AddLocalLANIPRange("172.16.0.0", "172.32.255.255");
AddLocalLANIPRange("169.254.0.0", "169.254.255.255");

// ----------------------------------------------------
// Load the Auto IP mirros
// ----------------------------------------------------
AddAutoIPMirror("http://www.myip.com/", "<html>Your ip is ", "<p></html>");
AddAutoIPMirror("http://www.findmyip.com/", "Your IP address is:<br>", "</FONT>");
AddAutoIPMirror("http://www.ipaddy.com/", "Your IP address is: ", "<br>");
AddAutoIPMirror("http://checkip.dyndns.org/", "Current IP Address: ", "</body>");
AddAutoIPMirror("http://ipid.shat.net/iponly/", "<title>", "</title>");
AddAutoIPMirror("http://www.edpsciences.com/htbin/ipaddress", "Your IP address is <B> ", " </B>");
AddAutoIPMirror("http://www2.simflex.com/ip.shtml", "Your IP address is <BR>", "<BR>");

// ----------------------------------------------------
// Create the event
// ----------------------------------------------------
EventSink.ServerList += new ServerListEventHandler( EventSink_ServerList );
}


// ==================================================================================
// Add a range of local lan ips
// ==================================================================================
private static void AddLocalLANIPRange(string RangeFrom, string RangeTo)
{
LocalLanIPRanges[LocalLanIPRangesCount] = new LocalLanIPRange();
LocalLanIPRanges[LocalLanIPRangesCount].RangeFrom = StringIPToUInt32IP(RangeFrom);
LocalLanIPRanges[LocalLanIPRangesCount].RangeTo = StringIPToUInt32IP(RangeTo);
LocalLanIPRangesCount = LocalLanIPRangesCount + 1;
}


// ==================================================================================
// Convert a ip string to a binary unsigned int
// ==================================================================================
private static UInt32 StringIPToUInt32IP(string addr)
{
byte[] byteArray1 = IPAddress.Parse(addr).GetAddressBytes();
byte[] byteArray2 = IPAddress.Parse(addr).GetAddressBytes();
byteArray1[0] = byteArray2[3];
byteArray1[1] = byteArray2[2];
byteArray1[2] = byteArray2[1];
byteArray1[3] = byteArray2[0];
return BitConverter.ToUInt32( byteArray1, 0);
}


// ==================================================================================
// Used to store the local lan ip ranges
// ==================================================================================
private class LocalLanIPRange
{
public UInt32 RangeFrom;
public UInt32 RangeTo;
}


// ==================================================================================
// Add a AutoIP mirror
// ==================================================================================
private static void AddAutoIPMirror(string sURL, string sStart, string sEnd)
{
AutoIPMirrors[AutoIPMirrorsCount] = new AutoIPMirror();
AutoIPMirrors[AutoIPMirrorsCount].sURL = sURL;
AutoIPMirrors[AutoIPMirrorsCount].sStart = sStart;
AutoIPMirrors[AutoIPMirrorsCount].sEnd = sEnd;
AutoIPMirrorsCount = AutoIPMirrorsCount + 1;
}


// ==================================================================================
// Used to store the Auto IP mirrors
// ==================================================================================
private class AutoIPMirror
{
public string sURL;
public string sStart;
public string sEnd;
public UInt32 iFailures;
}


// ==================================================================================
// The serverlist event
// ==================================================================================
public static void EventSink_ServerList( ServerListEventArgs e )
{
try
{
// ----------------------------------------------------
// Autodetect the Internet IP every 30 minutes
// ----------------------------------------------------
if (InternetIPAutodetect) {
DateTime UpdateTime = InternetIPAutodetectLast;
UpdateTime = UpdateTime.AddMinutes(MinutesBetweenIPAutodetect);
if (UpdateTime<DateTime.Now) {
string NewAddress = null;
NewAddress = FindInternetIP();
InternetIPAutodetectLast = DateTime.Now;
if (NewAddress!=null)
{
Address = NewAddress;
}
}
}

// ----------------------------------------------------
// Find the server ip to use for this user
// ----------------------------------------------------
IPAddress ipAddr = FindMachineIP(e);

// ----------------------------------------------------
// Send serverlist
// ----------------------------------------------------
if (ipAddr != null)
{
e.AddServer( ServerName, new IPEndPoint( ipAddr, Listener.Port ) );
} else {
e.Rejected = true;
}
}
catch
{
e.Rejected = true;
}
}


// ==================================================================================
// Connects to a webserver that gives you your internet ip
// ==================================================================================
public static string FindInternetIP( )
{

// ----------------------------------------------------
// Pick a random mirror
// ----------------------------------------------------
Random rnd = new Random();
int UseMirror = (int)( rnd.NextDouble() * AutoIPMirrorsCount);
string MyIP = "";

// ----------------------------------------------------
// Catch if the mirror is down
// ----------------------------------------------------
try
{
// ----------------------------------------------------
// Get the webpage
// ----------------------------------------------------
WebClient client = new WebClient();
byte[] pageData = client.DownloadData(AutoIPMirrors[UseMirror].sURL);
MyIP = System.Text.Encoding.ASCII.GetString(pageData);

// ----------------------------------------------------
// Find the string
// ----------------------------------------------------
int iStart = MyIP.LastIndexOf(AutoIPMirrors[UseMirror].sStart);
int iEnd = MyIP.IndexOf(AutoIPMirrors[UseMirror].sEnd, iStart+AutoIPMirrors[UseMirror].sStart.Length);
MyIP = MyIP.Substring(iStart+AutoIPMirrors[UseMirror].sStart.Length, iEnd-iStart-AutoIPMirrors[UseMirror].sStart.Length );
MyIP = MyIP.Trim();

// ----------------------------------------------------
// Return value
// ----------------------------------------------------
Console.WriteLine("Internet IP: {0} ({1})", MyIP, AutoIPMirrors[UseMirror].sURL);
return MyIP;
}
catch
{
Console.WriteLine("Unable to autoupdate the Internet IP from {0}!", AutoIPMirrors[UseMirror].sURL);
Console.WriteLine("----------------------------------------------------------------------");
Console.WriteLine(MyIP);
Console.WriteLine("----------------------------------------------------------------------");
return null;
}
}


// ==================================================================================
// Calculates what server IP to use
// ==================================================================================
public static IPAddress FindMachineIP( ServerListEventArgs e )
{
// ----------------------------------------------------
// Find the IP of the connecting user
// ----------------------------------------------------
Socket sock = e.State.Socket;
IPAddress theirAddress = ((IPEndPoint)sock.RemoteEndPoint).Address;
IPAddress serverAddress;

// ----------------------------------------------------
// Is it Loopback?
// ----------------------------------------------------
if ( IPAddress.IsLoopback( theirAddress ) )
{
return IPAddress.Parse( "127.0.0.1" );
}

// ----------------------------------------------------
// Local
// ----------------------------------------------------
UInt32 uint32Address = StringIPToUInt32IP(theirAddress.ToString());
for (UInt32 LocalLanIPRangesLoop = 0 ; LocalLanIPRangesLoop < LocalLanIPRangesCount; LocalLanIPRangesLoop++)
{
if ( (LocalLanIPRanges[LocalLanIPRangesLoop].RangeFrom <= uint32Address) && (LocalLanIPRanges[LocalLanIPRangesLoop].RangeTo >= uint32Address) )
{
Resolve(Dns.GetHostName(), out serverAddress);
return serverAddress;
}
}

// ----------------------------------------------------
// Internet addresses
// ----------------------------------------------------
if (Address!=null)
{
Resolve(Address, out serverAddress);
} else {
Resolve(Dns.GetHostName(), out serverAddress);
}
return serverAddress;
}


// ==================================================================================
// Resolves dns names
// ==================================================================================
public static bool Resolve( string addr, out IPAddress outValue )
{
try
{
outValue = IPAddress.Parse( addr );
return true;
}
catch
{
try
{
IPHostEntry iphe = Dns.Resolve( addr );

if ( iphe.AddressList.Length > 0 )
{
outValue = iphe.AddressList[iphe.AddressList.Length - 1];
return true;
}
}
catch
{
}
}
outValue = IPAddress.None;
return false;
}

}
}
thanx for anyone who can help me out :D
 

mjw1201

Wanderer
Uo Gateway

I've already registered with uogateway but i've changed to this new serverlist.cs thing so will i need to add anythign on uogateway because the ip i registered mite not be the same as the serverlist.cs ip adress that comes up

Thanx Guys :D
 

Nagash

Sorceror
Same here....running behind a router on windows xp. Never had this problem before with my cable provider, it's this new one. I used this servelist, tried petrock's advice, and even changes this(line 266):
Code:
if ( IPAddress.IsLoopback( theirAddress ) )
			{
				return IPAddress.Parse( "127.0.0.1" );
			}

to this:
Code:
if ( IPAddress.IsLoopback( theirAddress ) )
			{
				return IPAddress.Parse( Address /*"127.0.0.1"*/ );
			}
And of course, I tired the plain RunUO original serverlist and this one. Nothing would work. Any clue?
 

Rl-2

Wanderer
router

if your running behind a router and dont know what ip 2 use what is it, the 192.168.xx.xxx or the 127.0.0.1?
 

el_scrubo

Wanderer
hey, i'm having problems with this script. it compiles just fine, but it's not detecting my external IP address. it only shows my LAN and loopback address. can you provide some assistance for this behaviour?

thanks.
 

Miral007

Wanderer
el_scrubo said:
hey, i'm having problems with this script. it compiles just fine, but it's not detecting my external IP address. it only shows my LAN and loopback address. can you provide some assistance for this behaviour?

thanks.
I got the same problem. I edited the getmachine(e) line and it still doesnt work. Never echoes anything to the console beyond RUnUO's normal startup and worldsave messages...
 

Oppiet30

Wanderer
calebbreen said:
it wont pick up my WAN IP from my Router,

i dont use static ip, i have a netgear router and ive tried with port forwarding and still no luck

when i load the runuo server console it comes up with the normal 127.0.0.1 and then my lan address but no WAN IP

Please help


It isn't going to show your ISP IP when it starts up. But it will show your 192.168.x.x address. It iwll still work with his scrit. :)
 

Hosebomber

Wanderer
For anyone else whom may be having a spam of html coding when the first person logs on to the server with this script. Simple remove this mirror site from the script and it will stop.

AddAutoIPMirror("http://www.myip.com/", "<html>Your ip is ", "<p></html>");

Great work Mr. Fixit. I just hated that seeing that spam of html tags stream across my server like that. :D
 

Kamron

Knight
or modify

Code:
				Console.WriteLine("Internet IP: {0} ({1})", MyIP, AutoIPMirrors[UseMirror].sURL);
to
Code:
				Console.WriteLine("Internet IP: {0}", MyIP);
 

Quantos

Lord
MastaKavlin said:
So I"m just supposed to download it and I can connect? I THINK NOT!

Then you need to learn to read. It works because it is written well.

<edit>Actually it's not just downloading it, you actually need to replace the distro serverlist.cs with it, then change the shard name.</edit>
 
Status
Not open for further replies.
Top