123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace FastGithub.DnscryptProxy
- {
- /// <summary>
- /// DnscryptProxy后台服务
- /// </summary>
- sealed class DnscryptProxyHostedService : IHostedService
- {
- private readonly DnscryptProxyService dnscryptProxyService;
- private readonly ILogger<DnscryptProxyHostedService> logger;
- private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d);
- /// <summary>
- /// DnscryptProxy后台服务
- /// </summary>
- /// <param name="dnscryptProxyService"></param>
- /// <param name="logger"></param>
- public DnscryptProxyHostedService(
- DnscryptProxyService dnscryptProxyService,
- ILogger<DnscryptProxyHostedService> logger)
- {
- this.dnscryptProxyService = dnscryptProxyService;
- this.logger = logger;
- }
- /// <summary>
- /// 启动dnscrypt-proxy
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task StartAsync(CancellationToken cancellationToken)
- {
- try
- {
- await this.dnscryptProxyService.StartAsync(cancellationToken);
- this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
- // 监听意外退出
- var process = this.dnscryptProxyService.Process;
- if (process == null)
- {
- this.OnProcessExit(null, new EventArgs());
- }
- else
- {
- process.EnableRaisingEvents = true;
- process.Exited += this.OnProcessExit;
- await this.WaitForDnsOKAsync(cancellationToken);
- }
- }
- catch (Exception ex)
- {
- this.logger.LogWarning($"{this.dnscryptProxyService}启动失败:{ex.Message}");
- }
- }
- /// <summary>
- /// 等待dns服务初始化
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- private async Task WaitForDnsOKAsync(CancellationToken cancellationToken)
- {
- this.logger.LogInformation($"{this.dnscryptProxyService}正在初始化");
- using var timeoutTokenSource = new CancellationTokenSource(this.dnsOKTimeout);
- try
- {
- using var linkeTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
- await this.dnscryptProxyService.WaitForDnsOKAsync(linkeTokenSource.Token);
- this.logger.LogInformation($"{this.dnscryptProxyService}初始化完成");
- }
- catch (Exception)
- {
- if (timeoutTokenSource.IsCancellationRequested)
- {
- this.logger.LogWarning($"{this.dnscryptProxyService}在{this.dnsOKTimeout.TotalSeconds}秒内未能初始化完成");
- }
- }
- }
- /// <summary>
- /// 进程退出时
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnProcessExit(object? sender, EventArgs e)
- {
- if (this.dnscryptProxyService.ControllState != ControllState.Stopped)
- {
- this.logger.LogCritical($"{this.dnscryptProxyService}已意外停止,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
- }
- }
- /// <summary>
- /// 停止dnscrypt-proxy
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public Task StopAsync(CancellationToken cancellationToken)
- {
- try
- {
- this.dnscryptProxyService.Stop();
- this.logger.LogInformation($"{this.dnscryptProxyService}已停止");
- }
- catch (Exception ex)
- {
- this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}");
- }
- return Task.CompletedTask;
- }
- }
- }
|