DnscryptProxy.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using static PInvoke.AdvApi32;
  11. namespace FastGithub.DomainResolve
  12. {
  13. /// <summary>
  14. /// DnscryptProxy服务
  15. /// </summary>
  16. sealed class DnscryptProxy
  17. {
  18. private readonly ILogger<DnscryptProxy> logger;
  19. private readonly string processName;
  20. private readonly string serviceName;
  21. private readonly string exeFilePath;
  22. private readonly string tomlFilePath;
  23. /// <summary>
  24. /// 相关进程
  25. /// </summary>
  26. private Process? process;
  27. /// <summary>
  28. /// 获取监听的节点
  29. /// </summary>
  30. public IPEndPoint? LocalEndPoint { get; private set; }
  31. /// <summary>
  32. /// DnscryptProxy服务
  33. /// </summary>
  34. /// <param name="logger"></param>
  35. public DnscryptProxy(ILogger<DnscryptProxy> logger)
  36. {
  37. const string PATH = "dnscrypt-proxy";
  38. const string NAME = "dnscrypt-proxy";
  39. this.logger = logger;
  40. this.processName = NAME;
  41. this.serviceName = $"{nameof(FastGithub)}.{NAME}";
  42. this.exeFilePath = Path.Combine(PATH, OperatingSystem.IsWindows() ? $"{NAME}.exe" : NAME);
  43. this.tomlFilePath = Path.Combine(PATH, $"{NAME}.toml");
  44. }
  45. /// <summary>
  46. /// 启动dnscrypt-proxy
  47. /// </summary>
  48. /// <param name="cancellationToken"></param>
  49. /// <returns></returns>
  50. public async Task StartAsync(CancellationToken cancellationToken)
  51. {
  52. try
  53. {
  54. await this.StartCoreAsync(cancellationToken);
  55. }
  56. catch (Exception ex)
  57. {
  58. this.logger.LogWarning($"{this.processName}启动失败:{ex.Message}");
  59. }
  60. }
  61. /// <summary>
  62. /// 启动dnscrypt-proxy
  63. /// </summary>
  64. /// <param name="cancellationToken"></param>
  65. /// <returns></returns>
  66. private async Task StartCoreAsync(CancellationToken cancellationToken)
  67. {
  68. var port = GlobalListener.GetAvailablePort(5533);
  69. var localEndPoint = new IPEndPoint(IPAddress.Loopback, port);
  70. await TomlUtil.SetListensAsync(this.tomlFilePath, localEndPoint, cancellationToken);
  71. await TomlUtil.SetLogLevelAsync(this.tomlFilePath, 6, cancellationToken);
  72. await TomlUtil.SetLBStrategyAsync(this.tomlFilePath, "ph", cancellationToken);
  73. await TomlUtil.SetMinMaxTTLAsync(this.tomlFilePath, TimeSpan.FromMinutes(1d), TimeSpan.FromMinutes(2d), cancellationToken);
  74. if (OperatingSystem.IsWindows() && Environment.UserInteractive == false)
  75. {
  76. ServiceInstallUtil.StopAndDeleteService(this.serviceName);
  77. ServiceInstallUtil.InstallAndStartService(this.serviceName, this.exeFilePath, ServiceStartType.SERVICE_DEMAND_START);
  78. this.process = Process.GetProcessesByName(this.processName).FirstOrDefault(item => item.SessionId == 0);
  79. }
  80. else
  81. {
  82. this.process = StartDnscryptProxy();
  83. }
  84. if (this.process != null)
  85. {
  86. this.LocalEndPoint = localEndPoint;
  87. this.process.EnableRaisingEvents = true;
  88. this.process.Exited += (s, e) => this.LocalEndPoint = null;
  89. }
  90. }
  91. /// <summary>
  92. /// 停止服务
  93. /// </summary>
  94. public void Stop()
  95. {
  96. try
  97. {
  98. if (OperatingSystem.IsWindows() && Environment.UserInteractive == false)
  99. {
  100. ServiceInstallUtil.StopAndDeleteService(this.serviceName);
  101. }
  102. if (this.process != null && this.process.HasExited == false)
  103. {
  104. this.process.Kill();
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. this.logger.LogWarning($"{this.processName}停止失败:{ex.Message }");
  110. }
  111. finally
  112. {
  113. this.LocalEndPoint = null;
  114. }
  115. }
  116. /// <summary>
  117. /// 启动DnscryptProxy进程
  118. /// </summary>
  119. /// <returns></returns>
  120. private Process? StartDnscryptProxy()
  121. {
  122. return Process.Start(new ProcessStartInfo
  123. {
  124. FileName = this.exeFilePath,
  125. WorkingDirectory = Path.GetDirectoryName(this.exeFilePath),
  126. UseShellExecute = false,
  127. CreateNoWindow = true,
  128. WindowStyle = ProcessWindowStyle.Hidden
  129. });
  130. }
  131. /// <summary>
  132. /// 转换为字符串
  133. /// </summary>
  134. /// <returns></returns>
  135. public override string ToString()
  136. {
  137. return this.processName;
  138. }
  139. }
  140. }