Changed the way HG client verification is done: now transforming local and LAN client IPs into external IPs. This addresses some issues related to running both the user agents service and the viewer in the same machine/LAN, which then presents a problem when the user agent goes to an external network.

This commit is contained in:
Diva Canto
2010-07-30 14:04:13 -07:00
parent b83910f12a
commit 8ab7d80b09
4 changed files with 84 additions and 9 deletions

View File

@@ -31,6 +31,7 @@ using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using log4net;
namespace OpenSim.Framework
@@ -180,10 +181,14 @@ namespace OpenSim.Framework
throw new ArgumentException("[NetworkUtil] Unable to resolve defaultHostname to an IPv4 address for an IPv4 client");
}
static IPAddress externalIPAddress;
static NetworkUtil()
{
try
{
externalIPAddress = GetExternalIP();
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses)
@@ -244,5 +249,72 @@ namespace OpenSim.Framework
}
return defaultHostname;
}
public static IPAddress GetExternalIPOf(IPAddress user)
{
// Check if we're accessing localhost.
foreach (IPAddress host in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (host.Equals(user) && host.AddressFamily == AddressFamily.InterNetwork)
{
m_log.Info("[NetworkUtil] Localhost user detected, sending '" + externalIPAddress + "' instead of '" + user + "'");
return externalIPAddress;
}
}
// Check for same LAN segment
foreach (KeyValuePair<IPAddress, IPAddress> subnet in m_subnets)
{
byte[] subnetBytes = subnet.Value.GetAddressBytes();
byte[] localBytes = subnet.Key.GetAddressBytes();
byte[] destBytes = user.GetAddressBytes();
if (subnetBytes.Length != destBytes.Length || subnetBytes.Length != localBytes.Length)
return user;
bool valid = true;
for (int i = 0; i < subnetBytes.Length; i++)
{
if ((localBytes[i] & subnetBytes[i]) != (destBytes[i] & subnetBytes[i]))
{
valid = false;
break;
}
}
if (subnet.Key.AddressFamily != AddressFamily.InterNetwork)
valid = false;
if (valid)
{
m_log.Info("[NetworkUtil] Local LAN user detected, sending '" + externalIPAddress + "' instead of '" + user + "'");
return externalIPAddress;
}
}
// Otherwise, return user address
return user;
}
private static IPAddress GetExternalIP()
{
string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
m_log.Info("[NetworkUtil]: Exception in GetExternalIP: " + we.ToString());
}
IPAddress externalIp = IPAddress.Parse(requestHtml);
return externalIp;
}
}
}