mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Move EnsureIPv4Url to base Heartbeat class and make external IP lookup method public per request
This commit is contained in:
parent
d885b41161
commit
eea79691ea
@ -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);
|
||||
}
|
||||
@ -49,27 +48,6 @@ namespace MCGalaxy.Network
|
||||
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;
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user