From 3cdefb8553369f3a016518cfb8554c21fe3b3136 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Fri, 17 Sep 2021 17:49:33 +0800
Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91CalcChecksums?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Dns/DnsInterceptor.cs | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/FastGithub.Dns/DnsInterceptor.cs b/FastGithub.Dns/DnsInterceptor.cs
index d7433cb..51cffda 100644
--- a/FastGithub.Dns/DnsInterceptor.cs
+++ b/FastGithub.Dns/DnsInterceptor.cs
@@ -73,26 +73,31 @@ namespace FastGithub.Dns
{
try
{
- this.ProcessDnsPacket(packetBuffer, ref winDivertAddress, ref packetLength);
+ if (this.ModifyDnsPacket(packetBuffer, ref winDivertAddress, ref packetLength))
+ {
+ WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
+ }
}
catch (Exception ex)
{
this.logger.LogWarning(ex.Message);
}
-
- WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
- WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
+ finally
+ {
+ WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
+ }
}
}
}
///
- /// 处理DNS数据包
+ /// 修改DNS数据包
///
///
///
///
- private void ProcessDnsPacket(byte[] packetBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
+ ///
+ private bool ModifyDnsPacket(byte[] packetBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
{
var packetData = packetBuffer.AsSpan(0, (int)packetLength).ToArray();
var packet = Packet.ParsePacket(LinkLayers.Raw, packetData);
@@ -102,19 +107,19 @@ namespace FastGithub.Dns
var request = Request.FromArray(udpPacket.PayloadData);
if (request.OperationCode != OperationCode.Query)
{
- return;
+ return false;
}
var question = request.Questions.FirstOrDefault();
if (question == null || question.Type != RecordType.A)
{
- return;
+ return false;
}
var domain = question.Name;
if (this.fastGithubConfig.IsMatch(domain.ToString()) == false)
{
- return;
+ return false;
}
// 反转ip
@@ -148,6 +153,7 @@ namespace FastGithub.Dns
}
this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}");
+ return true;
}
}
}