PingerHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Optimizer
  10. {
  11. internal class PingerHelper
  12. {
  13. internal static string[] GoogleDNSv4 = { "8.8.8.8", "8.8.4.4" };
  14. internal static string[] GoogleDNSv6 = { "2001:4860:4860::8888", "2001:4860:4860::8844" };
  15. internal static string[] OpenDNSv4 = { "208.67.222.222", "208.67.220.220" };
  16. internal static string[] OpenDNSv6 = { "2620:0:ccc::2", "2620:0:ccd::2" };
  17. internal static string[] CloudflareDNSv4 = { "1.1.1.1", "1.0.0.1" };
  18. internal static string[] CloudflareDNSv6 = { "2606:4700:4700::1111", "2606:4700:4700::1001" };
  19. internal static string[] Quad9DNSv4 = { "9.9.9.9", "149.112.112.112" };
  20. internal static string[] Quad9DNSv6 = { "2620:fe::fe", string.Empty };
  21. internal static string[] CleanBrowsingDNSv4 = { "185.228.168.168", "185.228.168.169" };
  22. internal static string[] CleanBrowsingDNSv6 = { "2a0d:2a00:1::", "2a0d:2a00:2::" };
  23. internal static string[] CleanBrowsingAdultDNSv4 = { "185.228.168.10", "185.228.168.11" };
  24. internal static string[] CleanBrowsingAdultDNSv6 = { "2a0d:2a00:1::1", "2a0d:2a00:2::1" };
  25. internal static string[] AlternateDNSv4 = { "76.76.19.19", "76.223.122.150" };
  26. internal static string[] AlternateDNSv6 = { "2602:fcbc::ad", "2602:fcbc:2::ad" };
  27. internal static string[] AdguardDNSv4 = { "94.140.14.14", "94.140.15.15" };
  28. internal static string[] AdguardDNSv6 = { "2a10:50c0::ad1:ff", "2a10:50c0::ad2:ff" };
  29. internal static Ping pinger = new Ping();
  30. internal static bool ShowHiddenAdapters = false;
  31. internal static NetworkInterface[] NetworkAdapters = GetActiveNetworkAdapters();
  32. static IPAddress addressToPing;
  33. internal static NetworkInterface[] GetActiveNetworkAdapters()
  34. {
  35. if (ShowHiddenAdapters) NetworkAdapters = NetworkInterface.GetAllNetworkInterfaces();
  36. if (!ShowHiddenAdapters) NetworkAdapters = NetworkInterface.GetAllNetworkInterfaces().Where(
  37. a => a.OperationalStatus == OperationalStatus.Up &&
  38. (a.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || a.NetworkInterfaceType == NetworkInterfaceType.Ethernet) &&
  39. a.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily.ToString() == "InterNetwork")).ToArray();
  40. return NetworkAdapters;
  41. }
  42. internal static IEnumerable<string> GetDNSFromNetworkAdapter(NetworkInterface nic)
  43. {
  44. return nic.GetIPProperties().DnsAddresses.Select(z => z.ToString());
  45. }
  46. internal static void SetDNS(string nic, string[] dnsv4, string[] dnsv6)
  47. {
  48. string cmdv4Primary = $"netsh interface ipv4 set dnsservers {nic} static {dnsv4[0]} primary";
  49. string cmdv4Alternate = $"netsh interface ipv4 add dnsservers {nic} {dnsv4[1]} index=2";
  50. string cmdv6Primary = $"netsh interface ipv6 set dnsservers {nic} static {dnsv6[0]} primary";
  51. string cmdv6Alternate = $"netsh interface ipv6 add dnsservers {nic} {dnsv6[1]} index=2";
  52. Utilities.RunCommand(cmdv4Primary);
  53. Utilities.RunCommand(cmdv4Alternate);
  54. Utilities.RunCommand(cmdv6Primary);
  55. Utilities.RunCommand(cmdv6Alternate);
  56. }
  57. internal static void ResetDefaultDNS(string nic)
  58. {
  59. string cmdv4 = $"netsh interface ipv4 set dnsservers {nic} dhcp";
  60. string cmdv6 = $"netsh interface ipv6 set dnsservers {nic} dhcp";
  61. Utilities.RunCommand(cmdv4);
  62. Utilities.RunCommand(cmdv6);
  63. }
  64. internal static PingReply PingHost(string nameOrAddress)
  65. {
  66. PingReply reply;
  67. try
  68. {
  69. addressToPing = Dns.GetHostAddresses(nameOrAddress).First(address => address.AddressFamily == AddressFamily.InterNetwork);
  70. reply = pinger.Send(addressToPing);
  71. return reply;
  72. }
  73. catch
  74. {
  75. return null;
  76. }
  77. }
  78. internal static bool IsInternetAvailable()
  79. {
  80. const int timeout = 1000;
  81. const string host = "1.1.1.1";
  82. var ping = new Ping();
  83. var buffer = new byte[32];
  84. var pingOptions = new PingOptions();
  85. try
  86. {
  87. var reply = ping.Send(host, timeout, buffer, pingOptions);
  88. return (reply != null && reply.Status == IPStatus.Success);
  89. }
  90. catch (Exception)
  91. {
  92. return false;
  93. }
  94. }
  95. internal static void FlushDNSCache()
  96. {
  97. Utilities.RunCommand("ipconfig /flushdns");
  98. }
  99. }
  100. }