From 40f50d9c425f24b906c1206c3ddc09b75c6d7921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Wed, 15 Sep 2021 09:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9Fdns=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E5=9B=9E=E9=80=80dns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- @libs/dnscrypt-proxy.toml | 2 +- FastGithub.Configuration/DnsConfig.cs | 42 ------------- FastGithub.Configuration/FastGithubConfig.cs | 12 +--- FastGithub.Configuration/FastGithubOptions.cs | 8 +-- FastGithub.Configuration/LocalMachine.cs | 45 -------------- FastGithub.Dns/HostsConflictValidator.cs | 4 +- FastGithub.Dns/ProxyConflictValidtor.cs | 59 ++++++++++++------- FastGithub.DomainResolve/DomainResolver.cs | 51 +++++++--------- FastGithub.ReverseProxy/CertService.cs | 6 +- .../KestrelServerOptionsExtensions.cs | 33 ----------- FastGithub/appsettings.json | 10 ---- 11 files changed, 67 insertions(+), 205 deletions(-) delete mode 100644 FastGithub.Configuration/DnsConfig.cs diff --git a/@libs/dnscrypt-proxy.toml b/@libs/dnscrypt-proxy.toml index 2a24387..c74f14e 100644 --- a/@libs/dnscrypt-proxy.toml +++ b/@libs/dnscrypt-proxy.toml @@ -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. diff --git a/FastGithub.Configuration/DnsConfig.cs b/FastGithub.Configuration/DnsConfig.cs deleted file mode 100644 index a35ed0f..0000000 --- a/FastGithub.Configuration/DnsConfig.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Net; - -namespace FastGithub.Configuration -{ - /// - /// dns配置 - /// - public record DnsConfig - { - /// - /// IP地址 - /// - [AllowNull] - public string IPAddress { get; init; } - - /// - /// 端口 - /// - public int Port { get; init; } = 53; - - /// - /// 转换为IPEndPoint - /// - /// - /// - 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); - } - } -} diff --git a/FastGithub.Configuration/FastGithubConfig.cs b/FastGithub.Configuration/FastGithubConfig.cs index 0ed4576..7abcbe9 100644 --- a/FastGithub.Configuration/FastGithubConfig.cs +++ b/FastGithub.Configuration/FastGithubConfig.cs @@ -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 domainConfigs; private ConcurrentDictionary domainConfigCache; - /// - /// 回退的dns - /// - public IPEndPoint[] FallbackDns { get; set; } - /// /// FastGithub配置 /// @@ -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(); @@ -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(); } diff --git a/FastGithub.Configuration/FastGithubOptions.cs b/FastGithub.Configuration/FastGithubOptions.cs index 036f0ed..3a09396 100644 --- a/FastGithub.Configuration/FastGithubOptions.cs +++ b/FastGithub.Configuration/FastGithubOptions.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace FastGithub.Configuration { @@ -13,11 +12,6 @@ namespace FastGithub.Configuration /// public int HttpProxyPort { get; set; } - /// - /// 回退的dns - /// - public DnsConfig[] FallbackDns { get; set; } = Array.Empty(); - /// /// 代理的域名配置 /// diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs index a872f8e..f4520f7 100644 --- a/FastGithub.Configuration/LocalMachine.cs +++ b/FastGithub.Configuration/LocalMachine.cs @@ -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 /// public static string Name => Environment.MachineName; - /// - /// 获取本机设备所有IP - /// - /// - public static IEnumerable 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; - } - } - } - - /// - /// 获取本机设备所有IPv4 - /// - /// - public static IEnumerable GetAllIPv4Addresses() - { - foreach (var address in GetAllIPAddresses()) - { - if (address.AddressFamily == AddressFamily.InterNetwork) - { - yield return address; - } - } - } - - /// - /// 返回本机设备是否包含指定IP - /// - /// - /// - public static bool ContainsIPAddress(IPAddress address) - { - return GetAllIPAddresses().Contains(address); - } - - /// /// 获取可用的随机Tcp端口 /// diff --git a/FastGithub.Dns/HostsConflictValidator.cs b/FastGithub.Dns/HostsConflictValidator.cs index 466c24f..5de91e2 100644 --- a/FastGithub.Dns/HostsConflictValidator.cs +++ b/FastGithub.Dns/HostsConflictValidator.cs @@ -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; } diff --git a/FastGithub.Dns/ProxyConflictValidtor.cs b/FastGithub.Dns/ProxyConflictValidtor.cs index 2cab07a..4f7409b 100644 --- a/FastGithub.Dns/ProxyConflictValidtor.cs +++ b/FastGithub.Dns/ProxyConflictValidtor.cs @@ -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 /// 验证冲突 /// /// - public Task ValidateAsync() - { - try - { - this.ValidateSystemProxy(); - } - catch (Exception) - { - } - return Task.CompletedTask; - } - - /// - /// 验证代理 - /// - 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)}无法加速相关域名"); } } } + + /// + /// 是否为fastgithub代理 + /// + /// + /// + /// + private static async Task 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; + } + } } } diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index 1a3b249..63ce787 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -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 /// private async Task 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; } - - /// - /// 回退查找ip - /// - /// - /// - /// - /// - private async Task 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; - } - /// /// 查找最快的可用ip /// @@ -183,28 +163,39 @@ namespace FastGithub.DomainResolve /// /// /// - private async Task LookupAsync(IPEndPoint dns, DnsEndPoint domain, CancellationToken cancellationToken) + private async Task 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 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; } } diff --git a/FastGithub.ReverseProxy/CertService.cs b/FastGithub.ReverseProxy/CertService.cs index 4ae16e3..ca367d0 100644 --- a/FastGithub.ReverseProxy/CertService.cs +++ b/FastGithub.ReverseProxy/CertService.cs @@ -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(); } } } diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index 408ab46..94ab00e 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -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; - } } /// diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 7efe097..d09ff1c 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -2,16 +2,6 @@ // 新增的子配置文件appsettings.*.json,重启应用程序才生效 "FastGithub": { "HttpProxyPort": 2222, // http代理端口,非windows才使用 - "FallbackDns": [ // 用于解析不在DomainConfigs的域名 - { - "IPAddress": "114.114.114.114", - "Port": 53 - }, - { - "IPAddress": "8.8.8.8", - "Port": 53 - } - ], "DomainConfigs": { "*.fastgithub.com": { // 域名的*表示除.之外0到多个任意字符 "TlsSni": false, // 指示tls握手时是否发送SNI