/DnsTube/Utility.cs

https://github.com/drittich/DnsTube · C# · 85 lines · 75 code · 10 blank · 0 comment · 11 complexity · 73e98d5aaf61896c1824099195662f18 MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using System.Net.Http;
  4. using System.Text.RegularExpressions;
  5. using System.Text.Json;
  6. namespace DnsTube
  7. {
  8. public class Utility
  9. {
  10. public static string GetPublicIpAddress(IpSupport protocol, HttpClient Client, out string errorMesssage)
  11. {
  12. string publicIpAddress = null;
  13. var maxAttempts = 3;
  14. var attempts = 0;
  15. errorMesssage = null;
  16. var url = protocol == IpSupport.IPv4 ? "http://ipv4bot.whatismyipaddress.com" : "http://ipv6bot.whatismyipaddress.com";
  17. while (publicIpAddress == null && attempts < maxAttempts)
  18. {
  19. try
  20. {
  21. attempts++;
  22. var response = Client.GetStringAsync(url).Result;
  23. var candidatePublicIpAddress = response.Replace("\n", "");
  24. if (!IsValidIpAddress(protocol, candidatePublicIpAddress))
  25. throw new Exception($"Malformed response, expected IP address: {response}");
  26. publicIpAddress = candidatePublicIpAddress;
  27. }
  28. catch (Exception e)
  29. {
  30. if (attempts >= maxAttempts)
  31. errorMesssage = e.Message;
  32. }
  33. }
  34. return publicIpAddress;
  35. }
  36. public static GithubRelease GetLatestRelease()
  37. {
  38. var url = "https://api.github.com/repos/drittich/DnsTube/releases/latest";
  39. GithubRelease release = null;
  40. using (var client = new HttpClient())
  41. try
  42. {
  43. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  44. client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
  45. var response = client.GetStringAsync(url).Result;
  46. release = JsonSerializer.Deserialize<GithubRelease>(response);
  47. }
  48. catch { }
  49. return release;
  50. }
  51. public static string GetDateString()
  52. {
  53. return DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt");
  54. }
  55. public static bool IsValidIpAddress(IpSupport protocol, string ipString)
  56. {
  57. if (String.IsNullOrWhiteSpace(ipString))
  58. return false;
  59. if (protocol == IpSupport.IPv4)
  60. {
  61. string[] splitValues = ipString.Split('.');
  62. if (splitValues.Length != 4)
  63. return false;
  64. byte tempForParsing;
  65. return splitValues.All(r => byte.TryParse(r, out tempForParsing));
  66. }
  67. else
  68. {
  69. var regex = new Regex(@"(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)");
  70. var match = regex.Match(ipString);
  71. return match.Success;
  72. }
  73. }
  74. }
  75. }