dns日志

This commit is contained in:
陈国伟 2021-09-17 13:21:20 +08:00
parent 82e51ab358
commit 2b716eaef7
2 changed files with 38 additions and 25 deletions

View File

@ -22,7 +22,7 @@ namespace FastGithub.Dns
private const string DNS_FILTER = "udp.DstPort == 53";
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DnsInterceptor> logger;
private readonly TimeSpan ttl = TimeSpan.FromMinutes(1d);
private readonly TimeSpan ttl = TimeSpan.FromMinutes(2d);
/// <summary>
/// 刷新DNS缓存
@ -118,14 +118,14 @@ namespace FastGithub.Dns
}
// 反转ip
var sourceAddress = ipPacket.SourceAddress;
ipPacket.SourceAddress = ipPacket.DestinationAddress;
ipPacket.DestinationAddress = sourceAddress;
var destAddress = ipPacket.DestinationAddress;
ipPacket.DestinationAddress = ipPacket.SourceAddress;
ipPacket.SourceAddress = destAddress;
// 反转端口
var sourcePort = udpPacket.SourcePort;
udpPacket.SourcePort = udpPacket.DestinationPort;
udpPacket.DestinationPort = sourcePort;
var destPort = udpPacket.DestinationPort;
udpPacket.DestinationPort = udpPacket.SourcePort;
udpPacket.SourcePort = destPort;
// 设置dns响应
var response = Response.FromRequest(request);
@ -146,6 +146,8 @@ namespace FastGithub.Dns
{
winDivertAddress.Direction = WinDivertDirection.Inbound;
}
this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
}
}
}

View File

@ -49,26 +49,11 @@ namespace FastGithub.HttpServer
{
if (context.Request.Method == HttpMethods.Get && context.Request.Path == "/proxy.pac")
{
var buidler = new StringBuilder();
buidler.AppendLine("function FindProxyForURL(url, host){");
buidler.AppendLine($" var proxy = 'PROXY {context.Request.Host}';");
foreach (var domain in this.fastGithubConfig.GetDomainPatterns())
{
buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return proxy;");
}
buidler.AppendLine(" return 'DIRECT';");
buidler.AppendLine("}");
var pacString = buidler.ToString();
context.Response.ContentType = "application/x-ns-proxy-autoconfig";
var pacString = this.GetProxyPacString(context);
await context.Response.WriteAsync(pacString);
}
else if (context.Request.Method != HttpMethods.Connect)
{
var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}";
await this.httpForwarder.SendAsync(context, destinationPrefix, this.httpClient);
}
else
else if (context.Request.Method == HttpMethods.Connect)
{
var endpoint = await this.GetTargetEndPointAsync(context.Request);
using var targetSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
@ -87,6 +72,31 @@ namespace FastGithub.HttpServer
await Task.WhenAny(task1, task2);
}
}
else
{
var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}";
await this.httpForwarder.SendAsync(context, destinationPrefix, this.httpClient);
}
}
/// <summary>
/// 获取proxypac脚本
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private string GetProxyPacString(HttpContext context)
{
var buidler = new StringBuilder();
buidler.AppendLine("function FindProxyForURL(url, host){");
buidler.AppendLine($" var proxy = 'PROXY {context.Request.Host}';");
foreach (var domain in this.fastGithubConfig.GetDomainPatterns())
{
buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return proxy;");
}
buidler.AppendLine(" return 'DIRECT';");
buidler.AppendLine("}");
return buidler.ToString();
}
/// <summary>
@ -105,12 +115,13 @@ namespace FastGithub.HttpServer
return new IPEndPoint(address, targetPort);
}
// 不关心的域名直接使用系统dns
if (this.fastGithubConfig.IsMatch(targetHost) == false)
{
return new DnsEndPoint(targetHost, targetPort);
}
// https,走反向代理中间人
// 目标端口为443https代理中间人
if (targetPort == HTTPS_PORT)
{
return new IPEndPoint(IPAddress.Loopback, HttpsReverseProxyPort.Value);