Meum/Meum.Core/Functions.cs
2025-05-09 16:32:35 +05:00

48 lines
1.4 KiB
C#

using System.Net;
using System.Net.Quic;
using DTLib.Logging;
using Unofficial.MsQuic;
namespace Meum.Core;
public static class Functions
{
public static void InitMsQuic(ILogger? logger)
{
if (logger != null)
{
HarmonyMsQuicLoadFix.Apply(msg => logger.LogInfo(nameof(HarmonyMsQuicLoadFix), msg));
using var netEventListener = new NetEventListener(logger);
}
else HarmonyMsQuicLoadFix.Apply();
if (!QuicConnection.IsSupported)
throw new Exception("Quic is not supported, check for presence of libmsquic and openssl");
}
public static bool IsValidDomainName(string name)
{
return Uri.CheckHostName(name) != UriHostNameType.Unknown;
}
public static DnsEndPoint ParseDnsEndPoint(string address_str)
{
string host;
int port;
int colon_index = address_str.IndexOf(':');
if (colon_index == -1)
{
host = address_str;
port = Constants.ServerPortDefault;
}
else
{
host = address_str.Substring(0, colon_index);
port = Convert.ToInt32(address_str.Substring(colon_index + 1));
}
if(!IsValidDomainName(host))
throw new ArgumentException($"Invalid domain name '{host}'");
return new DnsEndPoint(host, port);
}
}