后台启动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 Microsoft.Extensions.Options;
|
||||||
using System;
|
using System;
|
||||||
using System.Buffers.Binary;
|
using System.Buffers.Binary;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
@ -101,20 +102,21 @@ namespace FastGithub.Dns
|
|||||||
var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
|
var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
|
||||||
var requestPayload = new Span<byte>(packet.PacketPayload, (int)packet.PacketPayloadLength).ToArray();
|
var requestPayload = new Span<byte>(packet.PacketPayload, (int)packet.PacketPayloadLength).ToArray();
|
||||||
|
|
||||||
var request = Request.FromArray(requestPayload);
|
if (TryParseRequest(requestPayload, out var request) == false ||
|
||||||
if (request.OperationCode != OperationCode.Query)
|
request.OperationCode != OperationCode.Query ||
|
||||||
|
request.Questions.Count == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var question = request.Questions.FirstOrDefault();
|
var question = request.Questions.First();
|
||||||
if (question == null || question.Type != RecordType.A)
|
if (question.Type != RecordType.A)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var domain = question.Name;
|
var domain = question.Name;
|
||||||
if (this.fastGithubConfig.IsMatch(domain.ToString()) == false)
|
if (this.fastGithubConfig.IsMatch(question.Name.ToString()) == false)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -164,5 +166,26 @@ namespace FastGithub.Dns
|
|||||||
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
|
||||||
this.logger.LogInformation($"已拦截dns查询{domain}并伪造解析结果为{IPAddress.Loopback}");
|
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>
|
/// <summary>
|
||||||
/// DnscryptProxy后台服务
|
/// DnscryptProxy后台服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyHostedService : IHostedService
|
sealed class DnscryptProxyHostedService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||||
private readonly DnscryptProxy dnscryptProxy;
|
private readonly DnscryptProxy dnscryptProxy;
|
||||||
@ -30,13 +30,13 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动dnscrypt-proxy
|
/// 启动dnscrypt-proxy
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="stoppingToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await this.dnscryptProxy.StartAsync(cancellationToken);
|
await this.dnscryptProxy.StartAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -49,7 +49,7 @@ namespace FastGithub.DomainResolve
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public override Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -59,7 +59,7 @@ namespace FastGithub.DomainResolve
|
|||||||
{
|
{
|
||||||
this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}");
|
this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
return base.StopAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user