12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- namespace FastGithub.ReverseProxy
- {
- /// <summary>
- /// DnscryptProxy后台服务
- /// </summary>
- sealed class DnscryptProxyHostedService : IHostedService
- {
- private const string dnscryptFile = "dnscrypt-proxy";
- private readonly ILogger<DnscryptProxyHostedService> logger;
- private Process? dnscryptProcess;
- /// <summary>
- /// DnscryptProxy后台服务
- /// </summary>
- /// <param name="logger"></param>
- public DnscryptProxyHostedService(ILogger<DnscryptProxyHostedService> logger)
- {
- this.logger = logger;
- }
- /// <summary>
- /// 启动dnscrypt-proxy
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public Task StartAsync(CancellationToken cancellationToken)
- {
- try
- {
- var fileName = dnscryptFile;
- if (OperatingSystem.IsWindows())
- {
- fileName = $"{dnscryptFile}.exe";
- }
- if (File.Exists(fileName) == true)
- {
- this.dnscryptProcess = Process.Start(new ProcessStartInfo
- {
- FileName = fileName,
- UseShellExecute = true,
- CreateNoWindow = true,
- WindowStyle = ProcessWindowStyle.Hidden
- });
- };
- }
- catch (Exception ex)
- {
- this.logger.LogWarning(ex.Message);
- }
- return Task.CompletedTask;
- }
- /// <summary>
- /// 停止dnscrypt-proxy
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public Task StopAsync(CancellationToken cancellationToken)
- {
- if (this.dnscryptProcess != null)
- {
- this.dnscryptProcess.Kill();
- }
- return Task.CompletedTask;
- }
- }
- }
|