Skip to content

Commit

Permalink
Move EnsureIPv4Url to base Heartbeat class and make external IP looku…
Browse files Browse the repository at this point in the history
…p method public per request
  • Loading branch information
UnknownShadow200 committed Mar 28, 2024
1 parent d885b41 commit eea7969
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
24 changes: 1 addition & 23 deletions MCGalaxy/Network/Heartbeat/ClassiCube.cs
Expand Up @@ -38,8 +38,7 @@ public sealed class ClassiCubeBeat : Heartbeat

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);
}
Expand All @@ -49,27 +48,6 @@ public sealed class ClassiCubeBeat : Heartbeat
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;
Expand Down
24 changes: 24 additions & 0 deletions MCGalaxy/Network/Heartbeat/Heartbeat.cs
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Cache;
using System.Net.Sockets;
using System.Text;
using MCGalaxy.Authentication;
using MCGalaxy.Tasks;
Expand Down Expand Up @@ -133,5 +134,28 @@ public abstract class Heartbeat
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";
}
}
}
10 changes: 10 additions & 0 deletions MCGalaxy/Network/Utils/HttpUtil.cs
Expand Up @@ -213,5 +213,15 @@ class CustomWebClient : WebClient {
return null;
}
}


public static string LookupExternalIP() {
HttpWebRequest req = CreateRequest("http://classicube.net/api/myip/");

using (WebResponse response = req.GetResponse())
{
return GetResponseText(response);
}
}
}
}
7 changes: 1 addition & 6 deletions MCGalaxy/Server/Authentication/LoginAuthenticator.cs
Expand Up @@ -126,12 +126,7 @@ public class MojangAuthenticator : LoginAuthenticator
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);
}
Expand Down

0 comments on commit eea7969

Please sign in to comment.