Files
The-Forging/Scripts/Misc/ServerList.cs
T

99 lines
2.0 KiB
C#
Raw Normal View History

2026-06-19 00:00:16 -04:00
#region References
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
2026-07-07 13:40:15 -04:00
using Server.Network;
2026-06-19 00:00:16 -04:00
#endregion
namespace Server.Misc
{
public class ServerList
2026-07-07 13:40:15 -04:00
{
public static string ServerName = Config.Get("Server.Name", "My Shard");
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
public static IPAddress Address => Config.Get("Server.Address", IPAddress.Loopback);
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
public static void Initialize()
{
2026-06-19 00:00:16 -04:00
EventSink.ServerList += EventSink_ServerList;
}
private static void EventSink_ServerList(ServerListEventArgs e)
{
try
{
var ns = e.State;
var s = ns.Socket;
var ipep = (IPEndPoint)s.LocalEndPoint;
var localAddress = ipep.Address;
var localPort = ipep.Port;
2026-07-07 13:40:15 -04:00
if (!IPAddress.IsLoopback(localAddress) && IsPrivateNetwork(localAddress))
{
ipep = (IPEndPoint)s.RemoteEndPoint;
2026-06-19 00:00:16 -04:00
2026-07-07 13:40:15 -04:00
if (!IsPrivateNetwork(ipep.Address))
{
localAddress = Address;
}
}
2026-06-19 00:00:16 -04:00
e.AddServer(ServerName, new IPEndPoint(localAddress, localPort));
}
catch
{
e.Rejected = true;
}
}
private static bool IsPrivateNetwork(IPAddress ip)
{
// 10.0.0.0/8
// 172.16.0.0/12
// 192.168.0.0/16
// 169.254.0.0/16
// 100.64.0.0/10 RFC 6598
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
return false;
}
if (Utility.IPMatch("192.168.*", ip))
{
return true;
}
if (Utility.IPMatch("10.*", ip))
{
return true;
}
if (Utility.IPMatch("172.16-31.*", ip))
{
return true;
}
if (Utility.IPMatch("169.254.*", ip))
{
return true;
}
if (Utility.IPMatch("100.64-127.*", ip))
{
return true;
}
return false;
}
}
}