diff --git a/FastGithub.ReverseProxy/HttpClient.cs b/FastGithub.ReverseProxy/HttpClient.cs index 0e86e5c..fedb9ae 100644 --- a/FastGithub.ReverseProxy/HttpClient.cs +++ b/FastGithub.ReverseProxy/HttpClient.cs @@ -10,18 +10,18 @@ namespace FastGithub.ReverseProxy /// class HttpClient : HttpMessageInvoker { - private readonly bool tlsSni; + private readonly string tlsSniValue; /// /// YARP的HttpClient /// /// + /// /// - /// - public HttpClient(HttpMessageHandler handler, bool disposeHandler, bool tlsSni) : + public HttpClient(HttpMessageHandler handler, string tlsSniValue, bool disposeHandler = false) : base(handler, disposeHandler) { - this.tlsSni = tlsSni; + this.tlsSniValue = tlsSniValue; } /// @@ -33,7 +33,7 @@ namespace FastGithub.ReverseProxy public override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var isHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps; - request.SetSniContext(new SniContext(isHttps, this.tlsSni)); + request.SetSniContext(new SniContext(isHttps, this.tlsSniValue)); return base.SendAsync(request, cancellationToken); } } diff --git a/FastGithub.ReverseProxy/HttpClientHanlder.cs b/FastGithub.ReverseProxy/HttpClientHanlder.cs index c806768..1be2925 100644 --- a/FastGithub.ReverseProxy/HttpClientHanlder.cs +++ b/FastGithub.ReverseProxy/HttpClientHanlder.cs @@ -26,14 +26,14 @@ namespace FastGithub.ReverseProxy { this.domainResolver = domainResolver; this.logger = logger; - this.InnerHandler = CreateNoneSniHttpHandler(); + this.InnerHandler = CreateSocketsHttpHandler(); } /// - /// 创建无Sni发送的httpHandler + /// 创建转发代理的httpHandler /// /// - private static HttpMessageHandler CreateNoneSniHttpHandler() + private static SocketsHttpHandler CreateSocketsHttpHandler() { return new SocketsHttpHandler { @@ -83,11 +83,9 @@ namespace FastGithub.ReverseProxy request.RequestUri = builder.Uri; request.Headers.Host = uri.Host; - // 计算Sni var context = request.GetSniContext(); - if (context.IsHttps && context.TlsSni) + if (context.IsHttps && context.TlsSniValue.Length > 0) { - context.TlsSniValue = uri.Host; this.logger.LogInformation($"[{address}--Sni->{uri.Host}]"); } else diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index f3f27cb..c92edb5 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs @@ -55,9 +55,11 @@ namespace FastGithub.ReverseProxy { var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination); var requestConfig = new ForwarderRequestConfig { Timeout = domainConfig.Timeout }; - var httpClient = new HttpClient(this.httpClientHanlder, false, domainConfig.TlsSni); - var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig); + var tlsSniValue = domainConfig.TlsSni ? destinationPrefix.Host : string.Empty; + using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniValue); + + var error = await httpForwarder.SendAsync(context, destinationPrefix.ToString(), httpClient, requestConfig); await ResponseErrorAsync(context, error); } } @@ -68,18 +70,16 @@ namespace FastGithub.ReverseProxy /// /// /// - private string GetDestinationPrefix(string host, Uri? destination) + private Uri GetDestinationPrefix(string host, Uri? destination) { - var defaultValue = $"https://{host}/"; + var defaultValue = new Uri($"https://{host}/"); if (destination == null) { return defaultValue; } - var baseUri = new Uri(defaultValue); - var result = new Uri(baseUri, destination).ToString(); + var result = new Uri(defaultValue, destination); this.logger.LogInformation($"[{defaultValue}->{result}]"); - return result; } diff --git a/FastGithub.ReverseProxy/SniContext.cs b/FastGithub.ReverseProxy/SniContext.cs index 8646ab7..5c33d02 100644 --- a/FastGithub.ReverseProxy/SniContext.cs +++ b/FastGithub.ReverseProxy/SniContext.cs @@ -6,29 +6,24 @@ sealed class SniContext { /// - /// 获取请求是否为https + /// 获取是否为https请求 /// public bool IsHttps { get; } /// - /// 获取是否发送Sni + /// 获取Sni值 /// - public bool TlsSni { get; } - - /// - /// Sni值 - /// - public string TlsSniValue { get; set; } = string.Empty; + public string TlsSniValue { get; } /// /// Sni上下文 /// /// - /// - public SniContext(bool isHttps, bool tlsSni) + /// + public SniContext(bool isHttps, string tlsSniValue) { this.IsHttps = isHttps; - this.TlsSni = tlsSni; + this.TlsSniValue = tlsSniValue; } } }