陈国伟 3 年之前
父节点
当前提交
001c6dddf7

+ 1 - 1
Directory.Build.props

@@ -1,6 +1,6 @@
 <Project>
 	<PropertyGroup>
-		<Version>2.0.3-Smooth_Switching_IPAddress</Version>
+		<Version>2.0.3</Version>
 		<Nullable>enable</Nullable>
 		<Description>github加速神器</Description>
 		<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>

+ 1 - 0
FastGithub.Http/LifetimeHttpHandlerCleaner.cs

@@ -48,6 +48,7 @@ namespace FastGithub.Http
         /// </summary>
         private async void StartCleanup()
         {
+            await Task.Yield();
             while (this.Cleanup() == false)
             {
                 await Task.Delay(this.CleanupInterval);

+ 16 - 3
FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs

@@ -1,4 +1,5 @@
 using FastGithub.Configuration;
+using Microsoft.Extensions.Logging;
 using System;
 using System.IO;
 using System.Runtime.Versioning;
@@ -15,14 +16,19 @@ namespace FastGithub.PacketIntercept.Dns
     sealed class HostsConflictSolver : IDnsConflictSolver
     {
         private readonly FastGithubConfig fastGithubConfig;
+        private readonly ILogger<HostsConflictSolver> logger;
 
         /// <summary>
         /// host文件冲解决者
         /// </summary>
         /// <param name="fastGithubConfig"></param>
-        public HostsConflictSolver(FastGithubConfig fastGithubConfig)
+        /// <param name="logger"></param>
+        public HostsConflictSolver(
+            FastGithubConfig fastGithubConfig,
+            ILogger<HostsConflictSolver> logger)
         {
             this.fastGithubConfig = fastGithubConfig;
+            this.logger = logger;
         }
 
         /// <summary>
@@ -56,8 +62,15 @@ namespace FastGithub.PacketIntercept.Dns
 
             if (hasConflicting == true)
             {
-                File.SetAttributes(hostsPath, FileAttributes.Normal);
-                await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken);
+                try
+                {
+                    File.Move(hostsPath, Path.ChangeExtension(hostsPath, ".bak"), overwrite: true);
+                    await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken);
+                }
+                catch (Exception ex)
+                {
+                    this.logger.LogWarning($"无法解决hosts文件冲突:{ex.Message}");
+                }
             }
         }