后台启动dnscryptproxy,以经过fg来下载public-resolvers文件
This commit is contained in:
parent
351be8c207
commit
a497058d42
@ -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<byte>(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}");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 尝试解析请求
|
||||
/// </summary>
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
static bool TryParseRequest(byte[] payload, [MaybeNullWhen(false)] out Request request)
|
||||
{
|
||||
try
|
||||
{
|
||||
request = Request.FromArray(payload);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
request = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ namespace FastGithub.DomainResolve
|
||||
/// <summary>
|
||||
/// DnscryptProxy后台服务
|
||||
/// </summary>
|
||||
sealed class DnscryptProxyHostedService : IHostedService
|
||||
sealed class DnscryptProxyHostedService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||
private readonly DnscryptProxy dnscryptProxy;
|
||||
@ -30,13 +30,13 @@ namespace FastGithub.DomainResolve
|
||||
/// <summary>
|
||||
/// 启动dnscrypt-proxy
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <param name="stoppingToken"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user