DnscryptProxyService.cs 3.2 KB

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