HostsConflictSolver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.Versioning;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace FastGithub.PacketIntercept.Dns
  10. {
  11. /// <summary>
  12. /// host文件冲解决者
  13. /// </summary>
  14. [SupportedOSPlatform("windows")]
  15. sealed class HostsConflictSolver : IDnsConflictSolver
  16. {
  17. private readonly FastGithubConfig fastGithubConfig;
  18. private readonly ILogger<HostsConflictSolver> logger;
  19. /// <summary>
  20. /// host文件冲解决者
  21. /// </summary>
  22. /// <param name="fastGithubConfig"></param>
  23. /// <param name="logger"></param>
  24. public HostsConflictSolver(
  25. FastGithubConfig fastGithubConfig,
  26. ILogger<HostsConflictSolver> logger)
  27. {
  28. this.fastGithubConfig = fastGithubConfig;
  29. this.logger = logger;
  30. }
  31. /// <summary>
  32. /// 解决冲突
  33. /// </summary>
  34. /// <param name="cancellationToken"></param>
  35. /// <returns></returns>
  36. public async Task SolveAsync(CancellationToken cancellationToken)
  37. {
  38. var hostsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
  39. if (File.Exists(hostsPath) == false)
  40. {
  41. return;
  42. }
  43. var hasConflicting = false;
  44. var hostsBuilder = new StringBuilder();
  45. var lines = await File.ReadAllLinesAsync(hostsPath, cancellationToken);
  46. foreach (var line in lines)
  47. {
  48. if (this.IsConflictingLine(line))
  49. {
  50. hasConflicting = true;
  51. hostsBuilder.AppendLine($"# {line}");
  52. }
  53. else
  54. {
  55. hostsBuilder.AppendLine(line);
  56. }
  57. }
  58. if (hasConflicting == true)
  59. {
  60. try
  61. {
  62. File.Move(hostsPath, Path.ChangeExtension(hostsPath, ".bak"), overwrite: true);
  63. await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken);
  64. }
  65. catch (Exception ex)
  66. {
  67. this.logger.LogWarning($"无法解决hosts文件冲突:{ex.Message}");
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 恢复冲突
  73. /// </summary>
  74. /// <param name="cancellationToken"></param>
  75. /// <returns></returns>
  76. public Task RestoreAsync(CancellationToken cancellationToken)
  77. {
  78. return Task.CompletedTask;
  79. }
  80. /// <summary>
  81. /// 是否为冲突的行
  82. /// </summary>
  83. /// <param name="line"></param>
  84. /// <returns></returns>
  85. private bool IsConflictingLine(string line)
  86. {
  87. if (line.TrimStart().StartsWith("#"))
  88. {
  89. return false;
  90. }
  91. var items = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  92. if (items.Length < 2)
  93. {
  94. return false;
  95. }
  96. var domain = items[1];
  97. return this.fastGithubConfig.IsMatch(domain);
  98. }
  99. }
  100. }