| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using STUN.Interfaces;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- namespace STUN.Client
- {
- public class DefaultDnsQuery : IDnsQuery
- {
- public async Task<IPAddress> QueryAsync(string host)
- {
- try
- {
- var ip = IsIPAddress(host);
- if (ip != null)
- {
- return ip;
- }
- var res = await Dns.GetHostAddressesAsync(host);
- return res.FirstOrDefault();
- }
- catch
- {
- return null;
- }
- }
- public IPAddress Query(string host)
- {
- try
- {
- var ip = IsIPAddress(host);
- if (ip != null)
- {
- return ip;
- }
- var res = Dns.GetHostAddresses(host);
- return res.FirstOrDefault();
- }
- catch
- {
- return null;
- }
- }
- private static IPAddress IsIPAddress(string host)
- {
- if (host != null && IPAddress.TryParse(host, out var ip))
- {
- return ip;
- }
- return null;
- }
- }
- }
|