This commit is contained in:
陈国伟 2021-09-30 08:35:35 +08:00
parent 582d9504ef
commit 001c6dddf7
3 changed files with 18 additions and 4 deletions

View File

@ -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>

View File

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

View File

@ -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}");
}
}
}