2
0

HostsConflictValidator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Threading.Tasks;
  9. namespace FastGithub.Dns
  10. {
  11. /// <summary>
  12. /// host文件冲突验证器
  13. /// </summary>
  14. sealed class HostsConflictValidator : IConflictValidator
  15. {
  16. private readonly FastGithubConfig fastGithubConfig;
  17. private readonly ILogger<HostsConflictValidator> logger;
  18. /// <summary>
  19. /// host文件冲突验证器
  20. /// </summary>
  21. /// <param name="fastGithubConfig"></param>
  22. /// <param name="logger"></param>
  23. public HostsConflictValidator(
  24. FastGithubConfig fastGithubConfig,
  25. ILogger<HostsConflictValidator> logger)
  26. {
  27. this.fastGithubConfig = fastGithubConfig;
  28. this.logger = logger;
  29. }
  30. /// <summary>
  31. /// 验证冲突
  32. /// </summary>
  33. /// <returns></returns>
  34. public async Task ValidateAsync()
  35. {
  36. var hostsPath = @"/etc/hosts";
  37. if (OperatingSystem.IsWindows())
  38. {
  39. hostsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), $"drivers/{hostsPath}");
  40. }
  41. if (File.Exists(hostsPath) == false)
  42. {
  43. return;
  44. }
  45. var localAddresses = LocalMachine.GetAllIPv4Addresses().ToArray();
  46. var lines = await File.ReadAllLinesAsync(hostsPath);
  47. foreach (var line in lines)
  48. {
  49. if (HostsRecord.TryParse(line, out var record) == false)
  50. {
  51. continue;
  52. }
  53. if (localAddresses.Contains(record.Address) == true)
  54. {
  55. continue;
  56. }
  57. if (this.fastGithubConfig.IsMatch(record.Domain))
  58. {
  59. this.logger.LogError($"由于你的hosts文件设置了{record},{nameof(FastGithub)}无法加速此域名");
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// hosts文件记录
  65. /// </summary>
  66. private class HostsRecord
  67. {
  68. /// <summary>
  69. /// 获取域名
  70. /// </summary>
  71. public string Domain { get; }
  72. /// <summary>
  73. /// 获取地址
  74. /// </summary>
  75. public IPAddress Address { get; }
  76. private HostsRecord(string domain, IPAddress address)
  77. {
  78. this.Domain = domain;
  79. this.Address = address;
  80. }
  81. public override string ToString()
  82. {
  83. return $"[{this.Domain}->{this.Address}]";
  84. }
  85. public static bool TryParse(string record, [MaybeNullWhen(false)] out HostsRecord value)
  86. {
  87. value = null;
  88. if (record.TrimStart().StartsWith("#"))
  89. {
  90. return false;
  91. }
  92. var items = record.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  93. if (items.Length < 2)
  94. {
  95. return false;
  96. }
  97. if (IPAddress.TryParse(items[0], out var address) == false)
  98. {
  99. return false;
  100. }
  101. value = new HostsRecord(items[1], address);
  102. return true;
  103. }
  104. }
  105. }
  106. }