优化dns服务
This commit is contained in:
parent
079cc9082d
commit
30382becd7
114
FastGithub.Dns/DnsHostedService.cs
Normal file
114
FastGithub.Dns/DnsHostedService.cs
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// dns后台服务
|
||||||
|
/// </summary>
|
||||||
|
sealed class DnsHostedService : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly DnsServer dnsServer;
|
||||||
|
private readonly HostsFileValidator hostsValidator;
|
||||||
|
private readonly ILogger<DnsHostedService> logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// dns后台服务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dnsServer"></param>
|
||||||
|
/// <param name="hostsValidator"></param>
|
||||||
|
/// <param name="options"></param>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
public DnsHostedService(
|
||||||
|
DnsServer dnsServer,
|
||||||
|
HostsFileValidator hostsValidator,
|
||||||
|
IOptionsMonitor<FastGithubOptions> options,
|
||||||
|
ILogger<DnsHostedService> logger)
|
||||||
|
{
|
||||||
|
this.dnsServer = dnsServer;
|
||||||
|
this.hostsValidator = hostsValidator;
|
||||||
|
this.logger = logger;
|
||||||
|
|
||||||
|
options.OnChange(opt =>
|
||||||
|
{
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
SystemDnsUtil.DnsFlushResolverCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动dns
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.dnsServer.Bind(IPAddress.Any, 53);
|
||||||
|
this.logger.LogInformation("DNS服务启动成功");
|
||||||
|
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SystemDnsUtil.DnsSetPrimitive(IPAddress.Loopback);
|
||||||
|
SystemDnsUtil.DnsFlushResolverCache();
|
||||||
|
this.logger.LogInformation($"设置为本机主DNS成功");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"设置为本机主DNS失败:{ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.logger.LogWarning("平台不支持自动设置DNS,请手动设置网卡的主DNS为127.0.0.1");
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.hostsValidator.ValidateAsync();
|
||||||
|
await base.StartAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// dns后台
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stoppingToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
return this.dnsServer.ListenAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止dns服务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.dnsServer.Dispose();
|
||||||
|
this.logger.LogInformation("DNS服务已停止");
|
||||||
|
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SystemDnsUtil.DnsFlushResolverCache();
|
||||||
|
SystemDnsUtil.DnsRemovePrimitive(IPAddress.Loopback);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"恢复DNS记录失败:{ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return base.StopAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
111
FastGithub.Dns/DnsServer.cs
Normal file
111
FastGithub.Dns/DnsServer.cs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
using DNS.Protocol;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// dns服务器
|
||||||
|
/// </summary>
|
||||||
|
sealed class DnsServer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly RequestResolver requestResolver;
|
||||||
|
private readonly ILogger<DnsServer> logger;
|
||||||
|
private readonly Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||||
|
private readonly byte[] buffer = new byte[ushort.MaxValue];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// dns服务器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="requestResolver"></param>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
public DnsServer(
|
||||||
|
RequestResolver requestResolver,
|
||||||
|
ILogger<DnsServer> logger)
|
||||||
|
{
|
||||||
|
this.requestResolver = requestResolver;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定地址和端口
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address"></param>
|
||||||
|
/// <param name="port"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public void Bind(IPAddress address, int port)
|
||||||
|
{
|
||||||
|
if (OperatingSystem.IsWindows() && UdpTable.TryGetOwnerProcessId(port, out var processId))
|
||||||
|
{
|
||||||
|
Process.GetProcessById(processId).Kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
const int SIO_UDP_CONNRESET = unchecked((int)0x9800000C);
|
||||||
|
this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||||||
|
this.socket.Bind(new IPEndPoint(address, port));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 监听dns请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task ListenAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
while (cancellationToken.IsCancellationRequested == false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await this.socket.ReceiveFromAsync(this.buffer, SocketFlags.None, remoteEndPoint);
|
||||||
|
var datas = new byte[result.ReceivedBytes];
|
||||||
|
this.buffer.AsSpan(0, datas.Length).CopyTo(datas);
|
||||||
|
this.HandleRequestAsync(datas, result.RemoteEndPoint, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.OperationAborted)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理dns请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datas"></param>
|
||||||
|
/// <param name="remoteEndPoint"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
private async void HandleRequestAsync(byte[] datas, EndPoint remoteEndPoint, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var request = Request.FromArray(datas);
|
||||||
|
var remoteEndPointRequest = new RemoteEndPointRequest(request, remoteEndPoint);
|
||||||
|
var response = await this.requestResolver.Resolve(remoteEndPointRequest, cancellationToken);
|
||||||
|
await this.socket.SendToAsync(response.ToArray(), SocketFlags.None, remoteEndPoint);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogTrace($"处理DNS异常:{ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 释放资源
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
this.socket.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,203 +0,0 @@
|
|||||||
using DNS.Protocol;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FastGithub.Dns
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// dns后台服务
|
|
||||||
/// </summary>
|
|
||||||
sealed class DnsServerHostedService : BackgroundService
|
|
||||||
{
|
|
||||||
private readonly RequestResolver requestResolver;
|
|
||||||
private readonly HostsFileValidator hostsValidator;
|
|
||||||
private readonly ILogger<DnsServerHostedService> logger;
|
|
||||||
|
|
||||||
private readonly Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
||||||
private readonly byte[] buffer = new byte[ushort.MaxValue];
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// dns后台服务
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="requestResolver"></param>
|
|
||||||
/// <param name="hostsValidator"></param>
|
|
||||||
/// <param name="options"></param>
|
|
||||||
/// <param name="logger"></param>
|
|
||||||
public DnsServerHostedService(
|
|
||||||
RequestResolver requestResolver,
|
|
||||||
HostsFileValidator hostsValidator,
|
|
||||||
IOptionsMonitor<FastGithubOptions> options,
|
|
||||||
ILogger<DnsServerHostedService> logger)
|
|
||||||
{
|
|
||||||
this.requestResolver = requestResolver;
|
|
||||||
this.hostsValidator = hostsValidator;
|
|
||||||
this.logger = logger;
|
|
||||||
|
|
||||||
options.OnChange(opt =>
|
|
||||||
{
|
|
||||||
if (OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
SystemDnsUtil.DnsFlushResolverCache();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 启动dns
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
await this.BindAsync(cancellationToken);
|
|
||||||
this.logger.LogInformation("DNS服务启动成功");
|
|
||||||
|
|
||||||
this.SetAsPrimitiveNameServer();
|
|
||||||
await this.hostsValidator.ValidateAsync();
|
|
||||||
|
|
||||||
await base.StartAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尝试多次绑定
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task BindAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
const int DNS_PORT = 53;
|
|
||||||
if (OperatingSystem.IsWindows() && UdpTable.TryGetOwnerProcessId(DNS_PORT, out var processId))
|
|
||||||
{
|
|
||||||
Process.GetProcessById(processId).Kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
var localEndPoint = new IPEndPoint(IPAddress.Any, DNS_PORT);
|
|
||||||
var delay = TimeSpan.FromMilliseconds(100d);
|
|
||||||
for (var i = 10; i >= 0; i--)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
this.socket.Bind(localEndPoint);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
if (i == 0)
|
|
||||||
{
|
|
||||||
throw new FastGithubException($"无法监听{localEndPoint},udp端口已被其它程序占用");
|
|
||||||
}
|
|
||||||
await Task.Delay(delay, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
const int SIO_UDP_CONNRESET = unchecked((int)0x9800000C);
|
|
||||||
this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设置自身为主dns
|
|
||||||
/// </summary>
|
|
||||||
private void SetAsPrimitiveNameServer()
|
|
||||||
{
|
|
||||||
if (OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
SystemDnsUtil.DnsSetPrimitive(IPAddress.Loopback);
|
|
||||||
SystemDnsUtil.DnsFlushResolverCache();
|
|
||||||
this.logger.LogInformation($"设置为本机主DNS成功");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
this.logger.LogWarning($"设置为本机主DNS失败:{ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.logger.LogWarning("平台不支持自动设置DNS,请手动设置网卡的主DNS为127.0.0.1");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// dns后台
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stoppingToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
|
|
||||||
{
|
|
||||||
var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
|
||||||
while (stoppingToken.IsCancellationRequested == false)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await this.socket.ReceiveFromAsync(this.buffer, SocketFlags.None, remoteEndPoint);
|
|
||||||
var datas = new byte[result.ReceivedBytes];
|
|
||||||
this.buffer.AsSpan(0, datas.Length).CopyTo(datas);
|
|
||||||
this.HandleRequestAsync(datas, result.RemoteEndPoint, stoppingToken);
|
|
||||||
}
|
|
||||||
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.OperationAborted)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 处理dns请求
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="datas"></param>
|
|
||||||
/// <param name="remoteEndPoint"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
private async void HandleRequestAsync(byte[] datas, EndPoint remoteEndPoint, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var request = Request.FromArray(datas);
|
|
||||||
var remoteEndPointRequest = new RemoteEndPointRequest(request, remoteEndPoint);
|
|
||||||
var response = await this.requestResolver.Resolve(remoteEndPointRequest, cancellationToken);
|
|
||||||
await this.socket.SendToAsync(response.ToArray(), SocketFlags.None, remoteEndPoint);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
this.logger.LogTrace($"处理DNS异常:{ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 停止dns服务
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override Task StopAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
this.socket.Dispose();
|
|
||||||
this.logger.LogInformation("DNS服务已停止");
|
|
||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
SystemDnsUtil.DnsFlushResolverCache();
|
|
||||||
SystemDnsUtil.DnsRemovePrimitive(IPAddress.Loopback);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
this.logger.LogWarning($"恢复DNS记录失败:{ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return base.StopAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -6,7 +6,7 @@ namespace FastGithub
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// dns服务注册扩展
|
/// dns服务注册扩展
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DnsServerServiceCollectionExtensions
|
public static class DnsServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注册dns服务
|
/// 注册dns服务
|
||||||
@ -17,8 +17,9 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
return services
|
return services
|
||||||
.AddSingleton<RequestResolver>()
|
.AddSingleton<RequestResolver>()
|
||||||
|
.AddSingleton<DnsServer>()
|
||||||
.AddSingleton<HostsFileValidator>()
|
.AddSingleton<HostsFileValidator>()
|
||||||
.AddHostedService<DnsServerHostedService>();
|
.AddHostedService<DnsHostedService>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user