系统dns替换回退dns

This commit is contained in:
陈国伟 2021-09-15 09:58:29 +08:00
parent a74711363b
commit 40f50d9c42
11 changed files with 67 additions and 205 deletions

View File

@ -223,7 +223,7 @@ cert_refresh_delay = 240
##
## If more than one resolver is specified, they will be tried in sequence.
fallback_resolvers = ['9.9.9.9:53', '8.8.8.8:53']
fallback_resolvers = []
## Always use the fallback resolver before the system DNS settings.

View File

@ -1,42 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
namespace FastGithub.Configuration
{
/// <summary>
/// dns配置
/// </summary>
public record DnsConfig
{
/// <summary>
/// IP地址
/// </summary>
[AllowNull]
public string IPAddress { get; init; }
/// <summary>
/// 端口
/// </summary>
public int Port { get; init; } = 53;
/// <summary>
/// 转换为IPEndPoint
/// </summary>
/// <returns></returns>
/// <exception cref="FastGithubException"></exception>
public IPEndPoint ToIPEndPoint()
{
if (System.Net.IPAddress.TryParse(this.IPAddress, out var address) == false)
{
throw new FastGithubException($"无效的ip{this.IPAddress}");
}
if (this.Port == 53 && LocalMachine.ContainsIPAddress(address))
{
throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
}
return new IPEndPoint(address, this.Port);
}
}
}

View File

@ -5,7 +5,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
namespace FastGithub.Configuration
{
@ -18,11 +17,6 @@ namespace FastGithub.Configuration
private SortedDictionary<DomainPattern, DomainConfig> domainConfigs;
private ConcurrentDictionary<string, DomainConfig?> domainConfigCache;
/// <summary>
/// 回退的dns
/// </summary>
public IPEndPoint[] FallbackDns { get; set; }
/// <summary>
/// FastGithub配置
/// </summary>
@ -34,8 +28,7 @@ namespace FastGithub.Configuration
{
this.logger = logger;
var opt = options.CurrentValue;
this.FallbackDns = opt.FallbackDns.Select(item => item.ToIPEndPoint()).ToArray();
this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
@ -49,8 +42,7 @@ namespace FastGithub.Configuration
private void Update(FastGithubOptions options)
{
try
{
this.FallbackDns = options.FallbackDns.Select(item => item.ToIPEndPoint()).ToArray();
{
this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
namespace FastGithub.Configuration
{
@ -13,11 +12,6 @@ namespace FastGithub.Configuration
/// </summary>
public int HttpProxyPort { get; set; }
/// <summary>
/// 回退的dns
/// </summary>
public DnsConfig[] FallbackDns { get; set; } = Array.Empty<DnsConfig>();
/// <summary>
/// 代理的域名配置
/// </summary>

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
@ -17,50 +16,6 @@ namespace FastGithub.Configuration
/// </summary>
public static string Name => Environment.MachineName;
/// <summary>
/// 获取本机设备所有IP
/// </summary>
/// <returns></returns>
public static IEnumerable<IPAddress> GetAllIPAddresses()
{
yield return IPAddress.Loopback;
yield return IPAddress.IPv6Loopback;
foreach (var @interface in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var addressInfo in @interface.GetIPProperties().UnicastAddresses)
{
yield return addressInfo.Address;
}
}
}
/// <summary>
/// 获取本机设备所有IPv4
/// </summary>
/// <returns></returns>
public static IEnumerable<IPAddress> GetAllIPv4Addresses()
{
foreach (var address in GetAllIPAddresses())
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
yield return address;
}
}
}
/// <summary>
/// 返回本机设备是否包含指定IP
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static bool ContainsIPAddress(IPAddress address)
{
return GetAllIPAddresses().Contains(address);
}
/// <summary>
/// 获取可用的随机Tcp端口
/// </summary>

View File

@ -3,7 +3,6 @@ using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
@ -47,7 +46,6 @@ namespace FastGithub.Dns
return;
}
var localAddresses = LocalMachine.GetAllIPv4Addresses().ToArray();
var lines = await File.ReadAllLinesAsync(hostsPath);
foreach (var line in lines)
{
@ -55,7 +53,7 @@ namespace FastGithub.Dns
{
continue;
}
if (localAddresses.Contains(record.Address) == true)
if (IPAddress.Loopback.Equals(record.Address) == true)
{
continue;
}

View File

@ -2,6 +2,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
@ -27,22 +29,7 @@ namespace FastGithub.Dns
/// 验证冲突
/// </summary>
/// <returns></returns>
public Task ValidateAsync()
{
try
{
this.ValidateSystemProxy();
}
catch (Exception)
{
}
return Task.CompletedTask;
}
/// <summary>
/// 验证代理
/// </summary>
private void ValidateSystemProxy()
public async Task ValidateAsync()
{
var systemProxy = HttpClient.DefaultProxy;
if (systemProxy == null)
@ -51,18 +38,50 @@ namespace FastGithub.Dns
}
var httpProxyPort = this.options.Value.HttpProxyPort;
var loopbackProxyUri = new Uri($"http://127.0.0.1:{httpProxyPort}");
var localhostProxyUri = new Uri($"http://localhost:{httpProxyPort}");
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 && proxyServer != loopbackProxyUri && proxyServer != localhostProxyUri)
if (proxyServer == null)
{
continue;
}
if (await IsFastGithubProxyAsync(proxyServer, httpProxyPort) == false)
{
this.logger.LogError($"由于系统配置了{proxyServer}代理{domain}{nameof(FastGithub)}无法加速相关域名");
}
}
}
/// <summary>
/// 是否为fastgithub代理
/// </summary>
/// <param name="proxyServer"></param>
/// <param name="httpProxyPort"></param>
/// <returns></returns>
private static async Task<bool> IsFastGithubProxyAsync(Uri proxyServer, int httpProxyPort)
{
if (proxyServer.Port != httpProxyPort)
{
return false;
}
if (IPAddress.TryParse(proxyServer.Host, out var address))
{
return address.Equals(IPAddress.Loopback);
}
try
{
var addresses = await System.Net.Dns.GetHostAddressesAsync(proxyServer.Host);
return addresses.Contains(IPAddress.Loopback);
}
catch (Exception)
{
return false;
}
}
}
}

View File

@ -112,7 +112,7 @@ namespace FastGithub.DomainResolve
if (address == null)
{
expiration = this.fallbackExpiration;
address = await this.LookupByFallbackAsync(domain, cancellationToken);
address = await this.LookupAsync(null, domain, cancellationToken);
}
if (address == null)
@ -140,11 +140,12 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
private async Task<IPAddress?> LookupByDnscryptAsync(DnsEndPoint domain, CancellationToken cancellationToken, int maxTryCount = 2)
{
if (this.dnscryptProxy.LocalEndPoint != null)
var dns = this.dnscryptProxy.LocalEndPoint;
if (dns != null)
{
for (var i = 0; i < maxTryCount; i++)
{
var address = await this.LookupAsync(this.dnscryptProxy.LocalEndPoint, domain, cancellationToken);
var address = await this.LookupAsync(dns, domain, cancellationToken);
if (address != null)
{
return address;
@ -154,27 +155,6 @@ namespace FastGithub.DomainResolve
return default;
}
/// <summary>
/// 回退查找ip
/// </summary>
/// <param name="domain"></param>
/// <param name="cancellationToken"></param>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
private async Task<IPAddress?> LookupByFallbackAsync(DnsEndPoint domain, CancellationToken cancellationToken)
{
foreach (var dns in this.fastGithubConfig.FallbackDns)
{
var address = await this.LookupAsync(dns, domain, cancellationToken);
if (address != null)
{
return address;
}
}
return default;
}
/// <summary>
/// 查找最快的可用ip
/// </summary>
@ -183,28 +163,39 @@ namespace FastGithub.DomainResolve
/// <param name="cancellationToken"></param>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
private async Task<IPAddress?> LookupAsync(IPEndPoint dns, DnsEndPoint domain, CancellationToken cancellationToken)
private async Task<IPAddress?> LookupAsync(IPEndPoint? dns, DnsEndPoint domain, CancellationToken cancellationToken)
{
var dnsName = dns?.ToString() ?? "System";
try
{
var dnsClient = new DnsClient(dns);
var addresses = await dnsClient.Lookup(domain.Host, RecordType.A, cancellationToken);
IEnumerable<IPAddress> addresses;
if (dns != null)
{
var dnsClient = new DnsClient(dns);
addresses = await dnsClient.Lookup(domain.Host, RecordType.A, cancellationToken);
}
else
{
var addrs = await Dns.GetHostAddressesAsync(domain.Host);
addresses = addrs.Where(item => item.AddressFamily == AddressFamily.InterNetwork);
}
addresses = addresses.Where(address => this.disableIPAddressCache.TryGetValue(address, out _) == false).ToList();
var address = await this.FindFastValueAsync(addresses, domain.Port, cancellationToken);
if (address == null)
{
this.logger.LogWarning($"dns({dns})解析不到{domain.Host}可用的ip解析");
this.logger.LogWarning($"dns({dnsName})解析不到{domain.Host}可用的ip解析");
}
else
{
this.logger.LogInformation($"dns({dns}): {domain.Host}->{address}");
this.logger.LogInformation($"dns({dnsName}): {domain.Host}->{address}");
}
return address;
}
catch (Exception ex)
{
cancellationToken.ThrowIfCancellationRequested();
this.logger.LogWarning($"dns({dns})无法解析{domain.Host}{ex.Message}");
this.logger.LogWarning($"dns({dnsName})无法解析{domain.Host}{ex.Message}");
return default;
}
}

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace FastGithub.ReverseProxy
@ -182,10 +183,7 @@ namespace FastGithub.ReverseProxy
}
yield return LocalMachine.Name;
foreach (var address in LocalMachine.GetAllIPv4Addresses())
{
yield return address.ToString();
}
yield return IPAddress.Loopback.ToString();
}
}
}

View File

@ -7,9 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
namespace FastGithub
{
@ -44,37 +42,6 @@ namespace FastGithub
var logger = kestrel.GetLogger();
kestrel.Listen(IPAddress.Loopback, httpProxyPort);
logger.LogInformation($"已监听http://127.0.0.1:{httpProxyPort}http代理启动完成");
if (SystemHasSetHttpProxy() == false)
{
logger.LogWarning($"请设置系统或浏览器代理为http://127.0.0.1:{httpProxyPort}或自动代理为http://127.0.0.1:{httpProxyPort}/proxy.pac");
}
bool SystemHasSetHttpProxy()
{
var systemProxy = HttpClient.DefaultProxy;
if (systemProxy == null)
{
return false;
}
var domainPattern = options.DomainConfigs.Keys.FirstOrDefault();
if (domainPattern == null)
{
return true;
}
var destination = new Uri($"https://{domainPattern.Replace('*', 'a')}");
var proxyServer = systemProxy.GetProxy(destination);
if (proxyServer == null)
{
return false;
}
var loopbackProxyUri = new Uri($"http://127.0.0.1:{httpProxyPort}");
var localhostProxyUri = new Uri($"http://localhost:{httpProxyPort}");
return proxyServer == loopbackProxyUri || proxyServer == localhostProxyUri;
}
}
/// <summary>

View File

@ -2,16 +2,6 @@
// appsettings.*.json
"FastGithub": {
"HttpProxyPort": 2222, // httpwindows使
"FallbackDns": [ // DomainConfigs
{
"IPAddress": "114.114.114.114",
"Port": 53
},
{
"IPAddress": "8.8.8.8",
"Port": 53
}
],
"DomainConfigs": {
"*.fastgithub.com": { // *.0
"TlsSni": false, // tlsSNI