DnsOverUdpHostedService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Hosting;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Net;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace FastGithub.Dns
  11. {
  12. /// <summary>
  13. /// dns后台服务
  14. /// </summary>
  15. sealed class DnsOverUdpHostedService : BackgroundService
  16. {
  17. private readonly DnsOverUdpServer dnsOverUdpServer;
  18. private readonly IEnumerable<IConflictValidator> conflictValidators;
  19. private readonly IOptionsMonitor<FastGithubOptions> options;
  20. private readonly ILogger<DnsOverUdpHostedService> logger;
  21. /// <summary>
  22. /// dns后台服务
  23. /// </summary>
  24. /// <param name="dnsOverUdpServer"></param>
  25. /// <param name="conflictValidators"></param>
  26. /// <param name="options"></param>
  27. /// <param name="logger"></param>
  28. public DnsOverUdpHostedService(
  29. DnsOverUdpServer dnsOverUdpServer,
  30. IEnumerable<IConflictValidator> conflictValidators,
  31. IOptionsMonitor<FastGithubOptions> options,
  32. ILogger<DnsOverUdpHostedService> logger)
  33. {
  34. this.dnsOverUdpServer = dnsOverUdpServer;
  35. this.conflictValidators = conflictValidators;
  36. this.options = options;
  37. this.logger = logger;
  38. options.OnChange(opt => SystemDnsUtil.FlushResolverCache());
  39. }
  40. /// <summary>
  41. /// 启动dns
  42. /// </summary>
  43. /// <param name="cancellationToken"></param>
  44. /// <returns></returns>
  45. public override async Task StartAsync(CancellationToken cancellationToken)
  46. {
  47. var dnsPort = this.options.CurrentValue.Listen.DnsPort;
  48. this.dnsOverUdpServer.Bind(IPAddress.Any, dnsPort);
  49. this.logger.LogInformation($"已监听udp端口{dnsPort},DNS服务启动完成");
  50. const int STANDARD_DNS_PORT = 53;
  51. if (dnsPort == STANDARD_DNS_PORT)
  52. {
  53. try
  54. {
  55. SystemDnsUtil.SetAsPrimitiveDns();
  56. SystemDnsUtil.FlushResolverCache();
  57. }
  58. catch (Exception ex)
  59. {
  60. this.logger.LogWarning(ex.Message);
  61. }
  62. }
  63. else
  64. {
  65. this.logger.LogWarning($"由于使用了非标准DNS端口{dnsPort},你需要将{nameof(FastGithub)}设置为标准DNS的上游");
  66. }
  67. foreach (var item in this.conflictValidators)
  68. {
  69. await item.ValidateAsync();
  70. }
  71. await base.StartAsync(cancellationToken);
  72. }
  73. /// <summary>
  74. /// dns后台
  75. /// </summary>
  76. /// <param name="stoppingToken"></param>
  77. /// <returns></returns>
  78. protected override Task ExecuteAsync(CancellationToken stoppingToken)
  79. {
  80. return this.dnsOverUdpServer.HandleAsync(stoppingToken);
  81. }
  82. /// <summary>
  83. /// 停止dns服务
  84. /// </summary>
  85. /// <param name="cancellationToken"></param>
  86. /// <returns></returns>
  87. public override Task StopAsync(CancellationToken cancellationToken)
  88. {
  89. this.dnsOverUdpServer.Dispose();
  90. this.logger.LogInformation("DNS服务已停止");
  91. try
  92. {
  93. SystemDnsUtil.RemoveFromPrimitiveDns();
  94. }
  95. catch (Exception ex)
  96. {
  97. this.logger.LogWarning(ex.Message);
  98. }
  99. finally
  100. {
  101. SystemDnsUtil.FlushResolverCache();
  102. }
  103. return base.StopAsync(cancellationToken);
  104. }
  105. }
  106. }