diff --git a/MCGalaxy/Network/Heartbeat/ClassiCube.cs b/MCGalaxy/Network/Heartbeat/ClassiCube.cs index 05e511613..73432ea33 100644 --- a/MCGalaxy/Network/Heartbeat/ClassiCube.cs +++ b/MCGalaxy/Network/Heartbeat/ClassiCube.cs @@ -38,8 +38,7 @@ void CheckAddress() { try { hostUrl = GetHost(); - IPAddress[] addresses = Dns.GetHostAddresses(hostUrl); - EnsureIPv4Url(addresses); + proxyUrl = EnsureIPv4Url(hostUrl); } catch (Exception ex) { Logger.LogError("Error retrieving DNS information for " + hostUrl, ex); } @@ -49,27 +48,6 @@ void CheckAddress() { hostUrl = hostUrl.Replace("www.", ""); Logger.Log(LogType.SystemActivity, "Finding " + hostUrl + " url.."); } - - // classicube.net only supports ipv4 servers, so we need to make - // sure we are using its ipv4 address when POSTing heartbeats - void EnsureIPv4Url(IPAddress[] addresses) { - bool hasIPv6 = false; - IPAddress firstIPv4 = null; - - // proxying doesn't work properly with https:// URLs - if (URL.CaselessStarts("https://")) return; - - foreach (IPAddress ip in addresses) { - AddressFamily family = ip.AddressFamily; - if (family == AddressFamily.InterNetworkV6) - hasIPv6 = true; - if (family == AddressFamily.InterNetwork && firstIPv4 == null) - firstIPv4 = ip; - } - - if (!hasIPv6 || firstIPv4 == null) return; - proxyUrl = "http://" + firstIPv4 + ":80"; - } protected override string GetHeartbeatData() { string name = Server.Config.Name; diff --git a/MCGalaxy/Network/Heartbeat/Heartbeat.cs b/MCGalaxy/Network/Heartbeat/Heartbeat.cs index 25159c730..7e2ade58f 100644 --- a/MCGalaxy/Network/Heartbeat/Heartbeat.cs +++ b/MCGalaxy/Network/Heartbeat/Heartbeat.cs @@ -19,6 +19,7 @@ permissions and limitations under the Licenses. using System.Collections.Generic; using System.Net; using System.Net.Cache; +using System.Net.Sockets; using System.Text; using MCGalaxy.Authentication; using MCGalaxy.Tasks; @@ -133,5 +134,28 @@ internal static void ReloadDefault() { Register(beat); } } + + + // e.g. classicube.net only supports ipv4 servers, so we need to make + // sure we are using its ipv4 address when POSTing heartbeats there + protected string EnsureIPv4Url(string hostUrl) { + bool hasIPv6 = false; + IPAddress firstIPv4 = null; + + // proxying doesn't work properly with https:// URLs + if (URL.CaselessStarts("https://")) return null; + IPAddress[] addresses = Dns.GetHostAddresses(hostUrl); + + foreach (IPAddress ip in addresses) { + AddressFamily family = ip.AddressFamily; + if (family == AddressFamily.InterNetworkV6) + hasIPv6 = true; + if (family == AddressFamily.InterNetwork && firstIPv4 == null) + firstIPv4 = ip; + } + + if (!hasIPv6 || firstIPv4 == null) return null; + return "http://" + firstIPv4 + ":80"; + } } } diff --git a/MCGalaxy/Network/Utils/HttpUtil.cs b/MCGalaxy/Network/Utils/HttpUtil.cs index 6d4c38fab..2d261715d 100644 --- a/MCGalaxy/Network/Utils/HttpUtil.cs +++ b/MCGalaxy/Network/Utils/HttpUtil.cs @@ -213,5 +213,15 @@ static string DescribeError(Exception ex) { return null; } } + + + public static string LookupExternalIP() { + HttpWebRequest req = CreateRequest("http://classicube.net/api/myip/"); + + using (WebResponse response = req.GetResponse()) + { + return GetResponseText(response); + } + } } } \ No newline at end of file diff --git a/MCGalaxy/Server/Authentication/LoginAuthenticator.cs b/MCGalaxy/Server/Authentication/LoginAuthenticator.cs index 43a245342..a04d89ed2 100644 --- a/MCGalaxy/Server/Authentication/LoginAuthenticator.cs +++ b/MCGalaxy/Server/Authentication/LoginAuthenticator.cs @@ -126,12 +126,7 @@ static void UpdateExternalIP() { if (externalIP != null) return; try { - HttpWebRequest req = HttpUtil.CreateRequest("http://classicube.net/api/myip/"); - - using (WebResponse response = req.GetResponse()) - { - externalIP = HttpUtil.GetResponseText(response); - } + externalIP = HttpUtil.LookupExternalIP(); } catch (Exception ex) { Logger.LogError("Retrieving external IP", ex); }