异常输出
This commit is contained in:
parent
c96dab0df8
commit
c3fefdf4d3
@ -77,7 +77,7 @@ namespace FastGithub
|
||||
store.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception )
|
||||
catch (Exception)
|
||||
{
|
||||
logger.LogError($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”");
|
||||
}
|
||||
|
||||
@ -30,13 +30,25 @@ namespace FastGithub
|
||||
if (options.CurrentValue.IsMatch(host) == false)
|
||||
{
|
||||
await context.Response.WriteAsJsonAsync(new { message = $"不支持以{host}访问" });
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
||||
var port = context.Request.Host.Port ?? 443;
|
||||
var destinationPrefix = $"https://{host}:{port}/";
|
||||
var httpClient = new HttpMessageInvoker(httpClientHanlder, disposeHandler: false);
|
||||
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
|
||||
|
||||
if (error != ForwarderError.None)
|
||||
{
|
||||
var port = context.Request.Host.Port ?? 443;
|
||||
var destinationPrefix = $"https://{host}:{port}/";
|
||||
var httpClient = new HttpMessageInvoker(httpClientHanlder, disposeHandler: false);
|
||||
await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
|
||||
var errorFeature = context.GetForwarderErrorFeature();
|
||||
if (errorFeature != null)
|
||||
{
|
||||
await context.Response.WriteAsJsonAsync(new
|
||||
{
|
||||
error = error.ToString(),
|
||||
message = errorFeature.Exception?.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
20
FastGithub.ReverseProxy/ReverseProxyException.cs
Normal file
20
FastGithub.ReverseProxy/ReverseProxyException.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace FastGithub.ReverseProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// 反向代理异常
|
||||
/// </summary>
|
||||
sealed class ReverseProxyException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// 反向代理异常
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="inner"></param>
|
||||
public ReverseProxyException(string message, Exception? inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,6 @@ using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -44,23 +43,36 @@ namespace FastGithub.ReverseProxy
|
||||
{
|
||||
// 缓存以避免做不必要的并发查询
|
||||
var key = $"domain:{domain}";
|
||||
var address = await this.memoryCache.GetOrCreateAsync(key, async e =>
|
||||
var address = await this.memoryCache.GetOrCreateAsync(key, e =>
|
||||
{
|
||||
e.SetAbsoluteExpiration(this.cacheTimeSpan);
|
||||
var dnsClient = new DnsClient(this.options.CurrentValue.TrustedDns.ToIPEndPoint());
|
||||
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
|
||||
return addresses?.FirstOrDefault();
|
||||
return this.LookupAsync(domain, cancellationToken);
|
||||
});
|
||||
|
||||
if (address == null)
|
||||
{
|
||||
var message = $"无法解析{domain}的ip";
|
||||
this.logger.LogWarning(message);
|
||||
throw new HttpRequestException(message);
|
||||
}
|
||||
|
||||
this.logger.LogInformation($"[{address}->{domain}]");
|
||||
return address;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找ip
|
||||
/// </summary>
|
||||
/// <param name="domain"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<IPAddress> LookupAsync(string domain, CancellationToken cancellationToken)
|
||||
{
|
||||
var endpoint = this.options.CurrentValue.TrustedDns.ToIPEndPoint();
|
||||
try
|
||||
{
|
||||
var dnsClient = new DnsClient(endpoint);
|
||||
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
|
||||
var address = addresses?.FirstOrDefault();
|
||||
return address ?? throw new Exception($"解析不到{domain}的ip");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new ReverseProxyException($"dns({endpoint}):{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user