From 110e28bd889559e830e131233f61ec8f345fa32c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Mon, 19 Jul 2021 15:22:30 +0800
Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91DnsClient=E7=9A=84=E5=88=9B?=
=?UTF-8?q?=E5=BB=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Core/DnsConfig.cs | 5 +++++
FastGithub.ReverseProxy/DomainResolver.cs | 23 +++++++++++------------
2 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/FastGithub.Core/DnsConfig.cs b/FastGithub.Core/DnsConfig.cs
index a302617..1ed4329 100644
--- a/FastGithub.Core/DnsConfig.cs
+++ b/FastGithub.Core/DnsConfig.cs
@@ -40,6 +40,11 @@ namespace FastGithub
return new IPEndPoint(address, this.Port);
}
+ public override string ToString()
+ {
+ return $"{this.IPAddress}:{this.Port}";
+ }
+
///
/// 是否为本机ip
///
diff --git a/FastGithub.ReverseProxy/DomainResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs
index 047d5ed..514eb2b 100644
--- a/FastGithub.ReverseProxy/DomainResolver.cs
+++ b/FastGithub.ReverseProxy/DomainResolver.cs
@@ -1,6 +1,7 @@
using DNS.Client;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
@@ -14,8 +15,9 @@ namespace FastGithub.ReverseProxy
///
sealed class DomainResolver
{
+ private DnsClient dnsClient;
private readonly IMemoryCache memoryCache;
- private readonly FastGithubConfig fastGithubConfig;
+ private readonly IOptionsMonitor options;
private readonly ILogger logger;
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d);
@@ -26,12 +28,15 @@ namespace FastGithub.ReverseProxy
///
public DomainResolver(
IMemoryCache memoryCache,
- FastGithubConfig fastGithubConfig,
+ IOptionsMonitor options,
ILogger logger)
{
this.memoryCache = memoryCache;
- this.fastGithubConfig = fastGithubConfig;
+ this.options = options;
this.logger = logger;
+
+ this.dnsClient = new DnsClient(options.CurrentValue.FastDns.ToIPEndPoint());
+ options.OnChange(opt => this.dnsClient = new DnsClient(opt.FastDns.ToIPEndPoint()));
}
///
@@ -63,32 +68,26 @@ namespace FastGithub.ReverseProxy
{
try
{
- var dns = this.fastGithubConfig.PureDns;
- var dnsClient = new DnsClient(dns);
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
var address = addresses?.FirstOrDefault();
if (address == null)
{
- throw new FastGithubException($"dns({dns}):解析不到{domain}的ip");
+ throw new Exception($"解析不到{domain}的ip");
}
// 受干扰的dns,常常返回127.0.0.1来阻断请求
// 如果解析到的ip为本机ip,会产生反向代理请求死循环
if (address.Equals(IPAddress.Loopback))
{
- throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}");
+ throw new Exception($"dns被污染,解析{domain}为{address}");
}
this.logger.LogInformation($"[{domain}->{address}]");
return address;
}
- catch (FastGithubException)
- {
- throw;
- }
catch (Exception ex)
{
- var dns = this.fastGithubConfig.PureDns;
+ var dns = this.options.CurrentValue.PureDns;
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex);
}
}