Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Sep 30, 2021
1 parent 582d950 commit 001c6dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions FastGithub.Http/LifetimeHttpHandlerCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void Add(LifetimeHttpHandler handler)
/// </summary>
private async void StartCleanup()
{
await Task.Yield();
while (this.Cleanup() == false)
{
await Task.Delay(this.CleanupInterval);
Expand Down
19 changes: 16 additions & 3 deletions FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FastGithub.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Runtime.Versioning;
Expand All @@ -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>
Expand Down Expand Up @@ -56,8 +62,15 @@ public async Task SolveAsync(CancellationToken cancellationToken)

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

Expand Down

0 comments on commit 001c6dd

Please sign in to comment.