PingerHelper.cs 4.8 KB

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