FastGithubOptions.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. namespace FastGithub
  5. {
  6. public class FastGithubOptions
  7. {
  8. private DomainMatch[]? domainMatches;
  9. public DnsIPEndPoint TrustedDns { get; set; } = new DnsIPEndPoint { Address = "127.0.0.1", Port = 5533 };
  10. public DnsIPEndPoint UntrustedDns { get; set; } = new DnsIPEndPoint { Address = "114.1114.114.114", Port = 53 };
  11. public HashSet<string> DomainMatches { get; set; } = new();
  12. public bool IsMatch(string domain)
  13. {
  14. if (this.domainMatches == null)
  15. {
  16. this.domainMatches = this.DomainMatches.Select(item => new DomainMatch(item)).ToArray();
  17. }
  18. return this.domainMatches.Any(item => item.IsMatch(domain));
  19. }
  20. private class DomainMatch
  21. {
  22. private readonly Regex regex;
  23. private readonly string value;
  24. public DomainMatch(string value)
  25. {
  26. this.value = value;
  27. var pattern = Regex.Escape(value).Replace(@"\*", ".*");
  28. this.regex = new Regex($"^{pattern}$");
  29. }
  30. public bool IsMatch(string domain)
  31. {
  32. return this.regex.IsMatch(domain);
  33. }
  34. public override string ToString()
  35. {
  36. return this.value;
  37. }
  38. }
  39. }
  40. }