From a497058d4213b6217e63e5528fb7b1149c9edb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 19 Sep 2021 17:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=90=AF=E5=8A=A8dnscryptpro?= =?UTF-8?q?xy=EF=BC=8C=E4=BB=A5=E7=BB=8F=E8=BF=87fg=E6=9D=A5=E4=B8=8B?= =?UTF-8?q?=E8=BD=BDpublic-resolvers=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/DnsInterceptor.cs | 33 ++++++++++++++++--- .../DnscryptProxyHostedService.cs | 12 +++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/FastGithub.Dns/DnsInterceptor.cs b/FastGithub.Dns/DnsInterceptor.cs index fbfa18a..7244333 100644 --- a/FastGithub.Dns/DnsInterceptor.cs +++ b/FastGithub.Dns/DnsInterceptor.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Buffers.Binary; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net; using System.Runtime.InteropServices; @@ -101,20 +102,21 @@ namespace FastGithub.Dns var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength); var requestPayload = new Span(packet.PacketPayload, (int)packet.PacketPayloadLength).ToArray(); - var request = Request.FromArray(requestPayload); - if (request.OperationCode != OperationCode.Query) + if (TryParseRequest(requestPayload, out var request) == false || + request.OperationCode != OperationCode.Query || + request.Questions.Count == 0) { return; } - var question = request.Questions.FirstOrDefault(); - if (question == null || question.Type != RecordType.A) + var question = request.Questions.First(); + if (question.Type != RecordType.A) { return; } var domain = question.Name; - if (this.fastGithubConfig.IsMatch(domain.ToString()) == false) + if (this.fastGithubConfig.IsMatch(question.Name.ToString()) == false) { return; } @@ -164,5 +166,26 @@ namespace FastGithub.Dns WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All); this.logger.LogInformation($"已拦截dns查询{domain}并伪造解析结果为{IPAddress.Loopback}"); } + + + /// + /// 尝试解析请求 + /// + /// + /// + /// + static bool TryParseRequest(byte[] payload, [MaybeNullWhen(false)] out Request request) + { + try + { + request = Request.FromArray(payload); + return true; + } + catch (Exception) + { + request = null; + return false; + } + } } } diff --git a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs index bdbba35..219c068 100644 --- a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs +++ b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs @@ -9,7 +9,7 @@ namespace FastGithub.DomainResolve /// /// DnscryptProxy后台服务 /// - sealed class DnscryptProxyHostedService : IHostedService + sealed class DnscryptProxyHostedService : BackgroundService { private readonly ILogger logger; private readonly DnscryptProxy dnscryptProxy; @@ -30,13 +30,13 @@ namespace FastGithub.DomainResolve /// /// 启动dnscrypt-proxy /// - /// + /// /// - public async Task StartAsync(CancellationToken cancellationToken) + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { - await this.dnscryptProxy.StartAsync(cancellationToken); + await this.dnscryptProxy.StartAsync(stoppingToken); } catch (Exception ex) { @@ -49,7 +49,7 @@ namespace FastGithub.DomainResolve /// /// /// - public Task StopAsync(CancellationToken cancellationToken) + public override Task StopAsync(CancellationToken cancellationToken) { try { @@ -59,7 +59,7 @@ namespace FastGithub.DomainResolve { this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}"); } - return Task.CompletedTask; + return base.StopAsync(cancellationToken); } } }