完善错误响应
This commit is contained in:
parent
519556203c
commit
33eb6ff6b0
@ -35,6 +35,7 @@ namespace FastGithub.ReverseProxy
|
||||
/// </summary>
|
||||
/// <param name="domain"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="FastGithubException"></exception>
|
||||
public async Task<IPAddress> ResolveAsync(string domain, CancellationToken cancellationToken)
|
||||
{
|
||||
// 缓存以避免做不必要的并发查询
|
||||
@ -53,6 +54,7 @@ namespace FastGithub.ReverseProxy
|
||||
/// <param name="domain"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="FastGithubException">
|
||||
private async Task<IPAddress> LookupAsync(string domain, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
@ -70,7 +72,7 @@ namespace FastGithub.ReverseProxy
|
||||
// 如果解析到的ip为本机ip,会产生反向代理请求死循环
|
||||
if (address.Equals(IPAddress.Loopback))
|
||||
{
|
||||
throw new FastGithubException($"dns({dns}):解析{domain}被干扰为{address}");
|
||||
throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}");
|
||||
}
|
||||
return address;
|
||||
}
|
||||
@ -81,7 +83,7 @@ namespace FastGithub.ReverseProxy
|
||||
catch (Exception ex)
|
||||
{
|
||||
var dns = this.fastGithubConfig.PureDns;
|
||||
throw new FastGithubException($"dns({dns}):{ex.Message}", ex);
|
||||
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,18 +13,18 @@ namespace FastGithub.ReverseProxy
|
||||
/// </summary>
|
||||
class NoSniHttpClientHanlder : DelegatingHandler
|
||||
{
|
||||
private readonly DomainResolver trustedDomainResolver;
|
||||
private readonly DomainResolver domainResolver;
|
||||
private readonly ILogger<NoSniHttpClientHanlder> logger;
|
||||
|
||||
/// <summary>
|
||||
/// 不发送NoSni的HttpClientHandler
|
||||
/// </summary>
|
||||
/// <param name="trustedDomainResolver"></param>
|
||||
/// <param name="domainResolver"></param>
|
||||
public NoSniHttpClientHanlder(
|
||||
DomainResolver trustedDomainResolver,
|
||||
DomainResolver domainResolver,
|
||||
ILogger<NoSniHttpClientHanlder> logger)
|
||||
{
|
||||
this.trustedDomainResolver = trustedDomainResolver;
|
||||
this.domainResolver = domainResolver;
|
||||
this.logger = logger;
|
||||
this.InnerHandler = CreateNoneSniHttpHandler();
|
||||
}
|
||||
@ -73,7 +73,7 @@ namespace FastGithub.ReverseProxy
|
||||
var uri = request.RequestUri;
|
||||
if (uri != null && uri.HostNameType == UriHostNameType.Dns)
|
||||
{
|
||||
var address = await this.trustedDomainResolver.ResolveAsync(uri.Host, cancellationToken);
|
||||
var address = await this.domainResolver.ResolveAsync(uri.Host, cancellationToken);
|
||||
this.logger.LogInformation($"[{address}--NoSni->{uri.Host}]");
|
||||
|
||||
var builder = new UriBuilder(uri)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
@ -15,17 +16,20 @@ namespace FastGithub.ReverseProxy
|
||||
private readonly SniHttpClientHanlder sniHttpClientHanlder;
|
||||
private readonly NoSniHttpClientHanlder noSniHttpClientHanlder;
|
||||
private readonly FastGithubConfig fastGithubConfig;
|
||||
private readonly ILogger<ReverseProxyMiddleware> logger;
|
||||
|
||||
public ReverseProxyMiddleware(
|
||||
IHttpForwarder httpForwarder,
|
||||
SniHttpClientHanlder sniHttpClientHanlder,
|
||||
NoSniHttpClientHanlder noSniHttpClientHanlder,
|
||||
FastGithubConfig fastGithubConfig)
|
||||
FastGithubConfig fastGithubConfig,
|
||||
ILogger<ReverseProxyMiddleware> logger)
|
||||
{
|
||||
this.httpForwarder = httpForwarder;
|
||||
this.sniHttpClientHanlder = sniHttpClientHanlder;
|
||||
this.noSniHttpClientHanlder = noSniHttpClientHanlder;
|
||||
this.fastGithubConfig = fastGithubConfig;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -63,7 +67,7 @@ namespace FastGithub.ReverseProxy
|
||||
/// <param name="host"></param>
|
||||
/// <param name="destination"></param>
|
||||
/// <returns></returns>
|
||||
private static string GetDestinationPrefix(string host, Uri? destination)
|
||||
private string GetDestinationPrefix(string host, Uri? destination)
|
||||
{
|
||||
var defaultValue = $"https://{host}/";
|
||||
if (destination == null)
|
||||
@ -72,7 +76,10 @@ namespace FastGithub.ReverseProxy
|
||||
}
|
||||
|
||||
var baseUri = new Uri(defaultValue);
|
||||
return new Uri(baseUri, destination).ToString();
|
||||
var result = new Uri(baseUri, destination).ToString();
|
||||
this.logger.LogInformation($"[{defaultValue}->{result}]");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -12,7 +12,7 @@ namespace FastGithub.ReverseProxy
|
||||
class SniHttpClientHanlder : DelegatingHandler
|
||||
{
|
||||
private readonly DomainResolver domainResolver;
|
||||
private readonly ILogger<NoSniHttpClientHanlder> logger;
|
||||
private readonly ILogger<SniHttpClientHanlder> logger;
|
||||
|
||||
/// <summary>
|
||||
/// 携带Sni的HttpClientHandler
|
||||
@ -20,7 +20,7 @@ namespace FastGithub.ReverseProxy
|
||||
/// <param name="domainResolver"></param>
|
||||
public SniHttpClientHanlder(
|
||||
DomainResolver domainResolver,
|
||||
ILogger<NoSniHttpClientHanlder> logger)
|
||||
ILogger<SniHttpClientHanlder> logger)
|
||||
{
|
||||
this.domainResolver = domainResolver;
|
||||
this.logger = logger;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user