initial commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Quasar.Common.DNS
|
||||
{
|
||||
public class Host
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the hostname of the Host.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Can be an IPv4, IPv6 address or hostname.
|
||||
/// </remarks>
|
||||
public string Hostname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the IP address of host.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Can be an IPv4 or IPv6 address.
|
||||
/// </remarks>
|
||||
public IPAddress IpAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the port of the Host.
|
||||
/// </summary>
|
||||
public ushort Port { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Hostname + ":" + Port;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Quasar.Common.DNS
|
||||
{
|
||||
public class HostsConverter
|
||||
{
|
||||
public List<Host> RawHostsToList(string rawHosts)
|
||||
{
|
||||
List<Host> hostsList = new List<Host>();
|
||||
|
||||
if (string.IsNullOrEmpty(rawHosts)) return hostsList;
|
||||
|
||||
var hosts = rawHosts.Split(';');
|
||||
|
||||
foreach (var host in hosts)
|
||||
{
|
||||
if ((string.IsNullOrEmpty(host) || !host.Contains(':'))) continue; // invalid host, ignore
|
||||
|
||||
ushort port;
|
||||
if (!ushort.TryParse(host.Substring(host.LastIndexOf(':') + 1), out port)) continue; // invalid, ignore host
|
||||
|
||||
hostsList.Add(new Host { Hostname = host.Substring(0, host.LastIndexOf(':')), Port = port });
|
||||
}
|
||||
|
||||
return hostsList;
|
||||
}
|
||||
|
||||
public string ListToRawHosts(IList<Host> hosts)
|
||||
{
|
||||
StringBuilder rawHosts = new StringBuilder();
|
||||
|
||||
foreach (var host in hosts)
|
||||
rawHosts.Append(host + ";");
|
||||
|
||||
return rawHosts.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Quasar.Common.DNS
|
||||
{
|
||||
public class HostsManager
|
||||
{
|
||||
public bool IsEmpty => _hosts.Count == 0;
|
||||
|
||||
private readonly Queue<Host> _hosts = new Queue<Host>();
|
||||
|
||||
public HostsManager(List<Host> hosts)
|
||||
{
|
||||
foreach(var host in hosts)
|
||||
_hosts.Enqueue(host);
|
||||
}
|
||||
|
||||
public Host GetNextHost()
|
||||
{
|
||||
var temp = _hosts.Dequeue();
|
||||
_hosts.Enqueue(temp); // add to the end of the queue
|
||||
|
||||
temp.IpAddress = ResolveHostname(temp);
|
||||
return temp;
|
||||
}
|
||||
|
||||
private static IPAddress ResolveHostname(Host host)
|
||||
{
|
||||
if (string.IsNullOrEmpty(host.Hostname)) return null;
|
||||
|
||||
IPAddress ip;
|
||||
if (IPAddress.TryParse(host.Hostname, out ip))
|
||||
{
|
||||
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
if (!Socket.OSSupportsIPv6) return null;
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
var ipAddresses = Dns.GetHostEntry(host.Hostname).AddressList;
|
||||
foreach (IPAddress ipAddress in ipAddresses)
|
||||
{
|
||||
switch (ipAddress.AddressFamily)
|
||||
{
|
||||
case AddressFamily.InterNetwork:
|
||||
return ipAddress;
|
||||
case AddressFamily.InterNetworkV6:
|
||||
/* Only use resolved IPv6 if no IPv4 address available,
|
||||
* otherwise it could be possible that the router the client
|
||||
* is using to connect to the internet doesn't support IPv6.
|
||||
*/
|
||||
if (ipAddresses.Length == 1)
|
||||
return ipAddress;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user