From 001c6dddf7371af6f165f631b33ba2a028496a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 30 Sep 2021 08:35:35 +0800 Subject: [PATCH] fix bug --- Directory.Build.props | 2 +- FastGithub.Http/LifetimeHttpHandlerCleaner.cs | 1 + .../Dns/HostsConflictSolver.cs | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index a98fd3b..9b161f6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.0.3-Smooth_Switching_IPAddress + 2.0.3 enable github加速神器 https://github.com/dotnetcore/FastGithub diff --git a/FastGithub.Http/LifetimeHttpHandlerCleaner.cs b/FastGithub.Http/LifetimeHttpHandlerCleaner.cs index b77fe0d..94e72f2 100644 --- a/FastGithub.Http/LifetimeHttpHandlerCleaner.cs +++ b/FastGithub.Http/LifetimeHttpHandlerCleaner.cs @@ -48,6 +48,7 @@ namespace FastGithub.Http /// private async void StartCleanup() { + await Task.Yield(); while (this.Cleanup() == false) { await Task.Delay(this.CleanupInterval); diff --git a/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs b/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs index 48ae608..b8b5163 100644 --- a/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs +++ b/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 logger; /// /// host文件冲解决者 /// /// - public HostsConflictSolver(FastGithubConfig fastGithubConfig) + /// + public HostsConflictSolver( + FastGithubConfig fastGithubConfig, + ILogger logger) { this.fastGithubConfig = fastGithubConfig; + this.logger = logger; } /// @@ -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}"); + } } }