DnscryptProxy.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using FastGithub.Configuration;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace FastGithub.DomainResolve
  10. {
  11. /// <summary>
  12. /// DnscryptProxy服务
  13. /// </summary>
  14. sealed class DnscryptProxy
  15. {
  16. private const string PATH = "dnscryptproxy";
  17. private const string NAME = "dnscrypt-proxy";
  18. /// <summary>
  19. /// 相关进程
  20. /// </summary>
  21. private Process? process;
  22. /// <summary>
  23. /// 获取监听的节点
  24. /// </summary>
  25. public IPEndPoint? LocalEndPoint { get; private set; }
  26. /// <summary>
  27. /// 启动dnscrypt-proxy
  28. /// </summary>
  29. /// <param name="cancellationToken"></param>
  30. /// <returns></returns>
  31. public async Task StartAsync(CancellationToken cancellationToken)
  32. {
  33. var tomlPath = Path.Combine(PATH, $"{NAME}.toml");
  34. var port = LocalMachine.GetAvailablePort(IPAddress.Loopback.AddressFamily, min: 5533);
  35. var localEndPoint = new IPEndPoint(IPAddress.Loopback, port);
  36. await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken);
  37. await TomlUtil.SetEdnsClientSubnetAsync(tomlPath, cancellationToken);
  38. foreach (var process in Process.GetProcessesByName(NAME))
  39. {
  40. process.Kill();
  41. process.WaitForExit();
  42. }
  43. if (OperatingSystem.IsWindows())
  44. {
  45. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  46. StartDnscryptProxy("-service install")?.WaitForExit();
  47. StartDnscryptProxy("-service start")?.WaitForExit();
  48. this.process = Process.GetProcessesByName(NAME).FirstOrDefault(item => item.SessionId == 0);
  49. }
  50. else
  51. {
  52. this.process = StartDnscryptProxy(string.Empty);
  53. }
  54. if (this.process != null)
  55. {
  56. this.LocalEndPoint = localEndPoint;
  57. this.process.EnableRaisingEvents = true;
  58. this.process.Exited += Process_Exited;
  59. }
  60. }
  61. /// <summary>
  62. /// 进程退出时
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void Process_Exited(object? sender, EventArgs e)
  67. {
  68. this.LocalEndPoint = null;
  69. }
  70. /// <summary>
  71. /// 停止dnscrypt-proxy
  72. /// </summary>
  73. public void Stop()
  74. {
  75. if (OperatingSystem.IsWindows())
  76. {
  77. StartDnscryptProxy("-service stop")?.WaitForExit();
  78. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  79. }
  80. if (this.process != null && this.process.HasExited == false)
  81. {
  82. this.process.Kill();
  83. }
  84. this.LocalEndPoint = null;
  85. }
  86. /// <summary>
  87. /// 启动DnscryptProxy进程
  88. /// </summary>
  89. /// <param name="arguments"></param>
  90. private static Process? StartDnscryptProxy(string arguments)
  91. {
  92. var fileName = OperatingSystem.IsWindows() ? $"{NAME}.exe" : NAME;
  93. return Process.Start(new ProcessStartInfo
  94. {
  95. FileName = Path.Combine(PATH, fileName),
  96. Arguments = arguments,
  97. WorkingDirectory = PATH,
  98. UseShellExecute = false,
  99. CreateNoWindow = true,
  100. WindowStyle = ProcessWindowStyle.Hidden
  101. });
  102. }
  103. /// <summary>
  104. /// 转换为字符串
  105. /// </summary>
  106. /// <returns></returns>
  107. public override string ToString()
  108. {
  109. return NAME;
  110. }
  111. }
  112. }