DnscryptProxyService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using FastGithub.Configuration;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace FastGithub.DomainResolve
  8. {
  9. /// <summary>
  10. /// DnscryptProxy服务
  11. /// </summary>
  12. sealed class DnscryptProxyService
  13. {
  14. private const string name = "dnscrypt-proxy";
  15. private readonly FastGithubConfig fastGithubConfig;
  16. /// <summary>
  17. /// 获取相关进程
  18. /// </summary>
  19. public Process? Process { get; private set; }
  20. /// <summary>
  21. /// 获取服务控制状态
  22. /// </summary>
  23. public ControllState ControllState { get; private set; } = ControllState.None;
  24. /// <summary>
  25. /// DnscryptProxy服务
  26. /// </summary>
  27. /// <param name="fastGithubConfig"></param>
  28. public DnscryptProxyService(FastGithubConfig fastGithubConfig)
  29. {
  30. this.fastGithubConfig = fastGithubConfig;
  31. }
  32. /// <summary>
  33. /// 启动dnscrypt-proxy
  34. /// </summary>
  35. /// <param name="cancellationToken"></param>
  36. /// <returns></returns>
  37. public async Task StartAsync(CancellationToken cancellationToken)
  38. {
  39. this.ControllState = ControllState.Started;
  40. var tomlPath = $"{name}.toml";
  41. await TomlUtil.SetListensAsync(tomlPath, this.fastGithubConfig.PureDns, cancellationToken);
  42. foreach (var process in Process.GetProcessesByName(name))
  43. {
  44. process.Kill();
  45. }
  46. if (OperatingSystem.IsWindows())
  47. {
  48. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  49. StartDnscryptProxy("-service install")?.WaitForExit();
  50. StartDnscryptProxy("-service start")?.WaitForExit();
  51. this.Process = Process.GetProcessesByName(name).FirstOrDefault(item => item.SessionId == 0);
  52. }
  53. else
  54. {
  55. this.Process = StartDnscryptProxy(string.Empty);
  56. }
  57. }
  58. /// <summary>
  59. /// 停止dnscrypt-proxy
  60. /// </summary>
  61. public void Stop()
  62. {
  63. this.ControllState = ControllState.Stopped;
  64. if (OperatingSystem.IsWindows())
  65. {
  66. StartDnscryptProxy("-service stop")?.WaitForExit();
  67. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  68. }
  69. if (this.Process != null && this.Process.HasExited == false)
  70. {
  71. this.Process.Kill();
  72. }
  73. }
  74. /// <summary>
  75. /// 启动DnscryptProxy进程
  76. /// </summary>
  77. /// <param name="arguments"></param>
  78. private static Process? StartDnscryptProxy(string arguments)
  79. {
  80. return Process.Start(new ProcessStartInfo
  81. {
  82. FileName = OperatingSystem.IsWindows() ? $"{name}.exe" : name,
  83. Arguments = arguments,
  84. UseShellExecute = true,
  85. CreateNoWindow = true,
  86. WindowStyle = ProcessWindowStyle.Hidden
  87. });
  88. }
  89. /// <summary>
  90. /// 转换为字符串
  91. /// </summary>
  92. /// <returns></returns>
  93. public override string ToString()
  94. {
  95. return name;
  96. }
  97. }
  98. }