HttpClient支持传入SNI
This commit is contained in:
parent
d50c35466b
commit
a7304ea5e0
@ -10,18 +10,18 @@ namespace FastGithub.ReverseProxy
|
||||
/// </summary>
|
||||
class HttpClient : HttpMessageInvoker
|
||||
{
|
||||
private readonly bool tlsSni;
|
||||
private readonly string tlsSniValue;
|
||||
|
||||
/// <summary>
|
||||
/// YARP的HttpClient
|
||||
/// </summary>
|
||||
/// <param name="handler"></param>
|
||||
/// <param name="tlsSniValue"></param>
|
||||
/// <param name="disposeHandler"></param>
|
||||
/// <param name="tlsSni"></param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -33,7 +33,7 @@ namespace FastGithub.ReverseProxy
|
||||
public override Task<HttpResponseMessage> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,14 +26,14 @@ namespace FastGithub.ReverseProxy
|
||||
{
|
||||
this.domainResolver = domainResolver;
|
||||
this.logger = logger;
|
||||
this.InnerHandler = CreateNoneSniHttpHandler();
|
||||
this.InnerHandler = CreateSocketsHttpHandler();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建无Sni发送的httpHandler
|
||||
/// 创建转发代理的httpHandler
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
||||
@ -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
|
||||
/// <param name="host"></param>
|
||||
/// <param name="destination"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -6,29 +6,24 @@
|
||||
sealed class SniContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取请求是否为https
|
||||
/// 获取是否为https请求
|
||||
/// </summary>
|
||||
public bool IsHttps { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取是否发送Sni
|
||||
/// 获取Sni值
|
||||
/// </summary>
|
||||
public bool TlsSni { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sni值
|
||||
/// </summary>
|
||||
public string TlsSniValue { get; set; } = string.Empty;
|
||||
public string TlsSniValue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sni上下文
|
||||
/// </summary>
|
||||
/// <param name="isHttps"></param>
|
||||
/// <param name="tlsSni"></param>
|
||||
public SniContext(bool isHttps, bool tlsSni)
|
||||
/// <param name="tlsSniValue"></param>
|
||||
public SniContext(bool isHttps, string tlsSniValue)
|
||||
{
|
||||
this.IsHttps = isHttps;
|
||||
this.TlsSni = tlsSni;
|
||||
this.TlsSniValue = tlsSniValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user