增加代理验证
This commit is contained in:
parent
57db095f66
commit
de38ad6e80
@ -3,6 +3,7 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -15,24 +16,24 @@ namespace FastGithub.Dns
|
||||
sealed class DnsHostedService : BackgroundService
|
||||
{
|
||||
private readonly DnsServer dnsServer;
|
||||
private readonly HostsFileValidator hostsValidator;
|
||||
private readonly IEnumerable<IDnsValidator> dnsValidators;
|
||||
private readonly ILogger<DnsHostedService> logger;
|
||||
|
||||
/// <summary>
|
||||
/// dns后台服务
|
||||
/// </summary>
|
||||
/// <param name="dnsServer"></param>
|
||||
/// <param name="hostsValidator"></param>
|
||||
/// <param name="dnsValidators"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="logger"></param>
|
||||
public DnsHostedService(
|
||||
DnsServer dnsServer,
|
||||
HostsFileValidator hostsValidator,
|
||||
IEnumerable<IDnsValidator> dnsValidators,
|
||||
IOptionsMonitor<FastGithubOptions> options,
|
||||
ILogger<DnsHostedService> logger)
|
||||
{
|
||||
this.dnsServer = dnsServer;
|
||||
this.hostsValidator = hostsValidator;
|
||||
this.dnsValidators = dnsValidators;
|
||||
this.logger = logger;
|
||||
|
||||
options.OnChange(opt =>
|
||||
@ -72,7 +73,11 @@ namespace FastGithub.Dns
|
||||
this.logger.LogWarning($"不支持自动设置DNS,请根据你的系统平台情况修改主DNS为{IPAddress.Loopback}");
|
||||
}
|
||||
|
||||
await this.hostsValidator.ValidateAsync();
|
||||
foreach (var item in this.dnsValidators)
|
||||
{
|
||||
await item.ValidateAsync();
|
||||
}
|
||||
|
||||
await base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
@ -13,19 +13,19 @@ namespace FastGithub.Dns
|
||||
/// <summary>
|
||||
/// host文件配置验证器
|
||||
/// </summary>
|
||||
sealed class HostsFileValidator
|
||||
sealed class HostsValidator : IDnsValidator
|
||||
{
|
||||
private readonly FastGithubConfig fastGithubConfig;
|
||||
private readonly ILogger<HostsFileValidator> logger;
|
||||
private readonly ILogger<HostsValidator> logger;
|
||||
|
||||
/// <summary>
|
||||
/// host文件配置验证器
|
||||
/// </summary>
|
||||
/// <param name="fastGithubConfig"></param>
|
||||
/// <param name="logger"></param>
|
||||
public HostsFileValidator(
|
||||
public HostsValidator(
|
||||
FastGithubConfig fastGithubConfig,
|
||||
ILogger<HostsFileValidator> logger)
|
||||
ILogger<HostsValidator> logger)
|
||||
{
|
||||
this.fastGithubConfig = fastGithubConfig;
|
||||
this.logger = logger;
|
||||
16
FastGithub.Dns/IDnsValidator.cs
Normal file
16
FastGithub.Dns/IDnsValidator.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Dns
|
||||
{
|
||||
/// <summary>
|
||||
/// Dns验证器
|
||||
/// </summary>
|
||||
interface IDnsValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ValidateAsync();
|
||||
}
|
||||
}
|
||||
64
FastGithub.Dns/ProxyValidtor.cs
Normal file
64
FastGithub.Dns/ProxyValidtor.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using FastGithub.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Dns
|
||||
{
|
||||
/// <summary>
|
||||
/// 代理验证
|
||||
/// </summary>
|
||||
sealed class ProxyValidtor : IDnsValidator
|
||||
{
|
||||
private readonly IOptions<FastGithubOptions> options;
|
||||
private readonly ILogger<ProxyValidtor> logger;
|
||||
|
||||
public ProxyValidtor(
|
||||
IOptions<FastGithubOptions> options,
|
||||
ILogger<ProxyValidtor> logger)
|
||||
{
|
||||
this.options = options;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证是否使用了代理
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task ValidateAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.ValidateSystemProxy();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证代理
|
||||
/// </summary>
|
||||
private void ValidateSystemProxy()
|
||||
{
|
||||
var systemProxy = HttpClient.DefaultProxy;
|
||||
if (systemProxy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var domain in this.options.Value.DomainConfigs.Keys)
|
||||
{
|
||||
var destination = new Uri($"https://{domain.Replace('*', 'a')}");
|
||||
var proxyServer = systemProxy.GetProxy(destination);
|
||||
if (proxyServer != null)
|
||||
{
|
||||
this.logger.LogError($"由于系统配置了{proxyServer}代理{domain},{nameof(FastGithub)}无法加速相关域名");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -18,7 +18,8 @@ namespace FastGithub
|
||||
{
|
||||
services.TryAddSingleton<RequestResolver>();
|
||||
services.TryAddSingleton<DnsServer>();
|
||||
services.TryAddSingleton<HostsFileValidator>();
|
||||
services.AddSingleton<IDnsValidator, HostsValidator>();
|
||||
services.AddSingleton<IDnsValidator, ProxyValidtor>();
|
||||
return services.AddHostedService<DnsHostedService>();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user