Move EnsureIPv4Url to base Heartbeat class and make external IP lookup method public per request

This commit is contained in:
UnknownShadow200 2024-03-29 08:13:25 +11:00
parent d885b41161
commit eea79691ea
4 changed files with 36 additions and 29 deletions

View File

@ -38,8 +38,7 @@ namespace MCGalaxy.Network
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);
}
@ -50,27 +49,6 @@ namespace MCGalaxy.Network
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;
OnSendingHeartbeatEvent.Call(this, ref name);

View File

@ -19,6 +19,7 @@ using System;
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 @@ namespace MCGalaxy.Network
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";
}
}
}

View File

@ -213,5 +213,15 @@ namespace MCGalaxy.Network {
return null;
}
}
public static string LookupExternalIP() {
HttpWebRequest req = CreateRequest("http://classicube.net/api/myip/");
using (WebResponse response = req.GetResponse())
{
return GetResponseText(response);
}
}
}
}

View File

@ -126,12 +126,7 @@ namespace MCGalaxy.Authentication
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);
}