diff --git a/Directory.Build.props b/Directory.Build.props index 9b161f6..7724f9e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.0.3 + 2.0.4 enable github加速神器 https://github.com/dotnetcore/FastGithub diff --git a/FastGithub.Http/HttpClientHandler.cs b/FastGithub.Http/HttpClientHandler.cs index 15b9c4a..f7d4b3f 100644 --- a/FastGithub.Http/HttpClientHandler.cs +++ b/FastGithub.Http/HttpClientHandler.cs @@ -203,6 +203,10 @@ namespace FastGithub.Http var parser = new Org.BouncyCastle.X509.X509CertificateParser(); var x509Cert = parser.ReadCertificate(cert.GetRawCertData()); var subjects = x509Cert.GetSubjectAlternativeNames(); + if (subjects == null) + { + yield break; + } foreach (var subject in subjects) { diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs index 3c6fbda..5bbb441 100644 --- a/FastGithub.HttpServer/HttpProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs @@ -22,6 +22,8 @@ namespace FastGithub.HttpServer { private const string LOOPBACK = "127.0.0.1"; private const string LOCALHOST = "localhost"; + private const int HTTP_PORT = 80; + private const int HTTPS_PORT = 443; private readonly FastGithubConfig fastGithubConfig; private readonly IDomainResolver domainResolver; @@ -114,11 +116,7 @@ namespace FastGithub.HttpServer /// private bool IsFastGithubServer(HostString host) { - if (host.Port == this.fastGithubConfig.HttpProxyPort) - { - return host.Host == LOOPBACK || host.Host == LOCALHOST; - } - return false; + return host.Port == this.fastGithubConfig.HttpProxyPort && (host.Host == LOOPBACK || host.Host == LOCALHOST); } /// @@ -146,9 +144,7 @@ namespace FastGithub.HttpServer /// /// private async Task GetTargetEndPointAsync(HostString host) - { - const int HTTP_PORT = 80; - const int HTTPS_PORT = 443; + { var targetHost = host.Host; var targetPort = host.Port ?? HTTPS_PORT; diff --git a/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs b/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs index 067b3de..a1d03f0 100644 --- a/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs @@ -3,6 +3,7 @@ using FastGithub.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Yarp.ReverseProxy.Forwarder; @@ -13,11 +14,19 @@ namespace FastGithub.HttpServer /// sealed class HttpReverseProxyMiddleware { + private const string LOOPBACK = "127.0.0.1"; + private const string LOCALHOST = "localhost"; + private const int HTTP_PORT = 80; + private const int HTTPS_PORT = 443; + + private static readonly DomainConfig sniDomainConfig = new() { TlsSni = true }; + private readonly IHttpForwarder httpForwarder; private readonly IHttpClientFactory httpClientFactory; private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; + public HttpReverseProxyMiddleware( IHttpForwarder httpForwarder, IHttpClientFactory httpClientFactory, @@ -39,7 +48,7 @@ namespace FastGithub.HttpServer public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var host = context.Request.Host; - if (this.fastGithubConfig.TryGetDomainConfig(host.Host, out var domainConfig) == false) + if (this.TryGetDomainConfig(host, out var domainConfig) == false) { await next(context); } @@ -62,6 +71,34 @@ namespace FastGithub.HttpServer } } + /// + /// 获取域名的DomainConfig + /// + /// + /// + /// + private bool TryGetDomainConfig(HostString host, [MaybeNullWhen(false)] out DomainConfig domainConfig) + { + if (this.fastGithubConfig.TryGetDomainConfig(host.Host, out domainConfig) == true) + { + return true; + } + + // http(s)://127.0.0.1 + // http(s)://localhost + if (host.Host == LOOPBACK || host.Host == LOCALHOST) + { + if (host.Port == null || host.Port == HTTPS_PORT || host.Port == HTTP_PORT) + { + return false; + } + } + + // 未配置的域名,但dns污染解析为127.0.0.1的域名 + domainConfig = sniDomainConfig; + return true; + } + /// /// 获取目标前缀 ///