DnscryptProxy.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 EndPoint { get; }
  26. /// <summary>
  27. /// DnscryptProxy服务
  28. /// </summary>
  29. public DnscryptProxy()
  30. {
  31. var port = LocalMachine.GetAvailablePort(IPAddress.Loopback.AddressFamily, min: 5353);
  32. this.EndPoint = new IPEndPoint(IPAddress.Loopback, port);
  33. }
  34. /// <summary>
  35. /// 启动dnscrypt-proxy
  36. /// </summary>
  37. /// <param name="cancellationToken"></param>
  38. /// <returns></returns>
  39. public async Task StartAsync(CancellationToken cancellationToken)
  40. {
  41. var tomlPath = Path.Combine(PATH, $"{NAME}.toml");
  42. await TomlUtil.SetListensAsync(tomlPath, this.EndPoint, cancellationToken);
  43. await TomlUtil.SetEdnsClientSubnetAsync(tomlPath, cancellationToken);
  44. foreach (var process in Process.GetProcessesByName(NAME))
  45. {
  46. process.Kill();
  47. process.WaitForExit();
  48. }
  49. if (OperatingSystem.IsWindows())
  50. {
  51. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  52. StartDnscryptProxy("-service install")?.WaitForExit();
  53. StartDnscryptProxy("-service start")?.WaitForExit();
  54. this.process = Process.GetProcessesByName(NAME).FirstOrDefault(item => item.SessionId == 0);
  55. }
  56. else
  57. {
  58. this.process = StartDnscryptProxy(string.Empty);
  59. }
  60. }
  61. /// <summary>
  62. /// 停止dnscrypt-proxy
  63. /// </summary>
  64. public void Stop()
  65. {
  66. if (OperatingSystem.IsWindows())
  67. {
  68. StartDnscryptProxy("-service stop")?.WaitForExit();
  69. StartDnscryptProxy("-service uninstall")?.WaitForExit();
  70. }
  71. if (this.process != null && this.process.HasExited == false)
  72. {
  73. this.process.Kill();
  74. }
  75. }
  76. /// <summary>
  77. /// 启动DnscryptProxy进程
  78. /// </summary>
  79. /// <param name="arguments"></param>
  80. private static Process? StartDnscryptProxy(string arguments)
  81. {
  82. var fileName = OperatingSystem.IsWindows() ? $"{NAME}.exe" : NAME;
  83. return Process.Start(new ProcessStartInfo
  84. {
  85. FileName = Path.Combine(PATH, fileName),
  86. Arguments = arguments,
  87. WorkingDirectory = PATH,
  88. UseShellExecute = false,
  89. CreateNoWindow = true,
  90. WindowStyle = ProcessWindowStyle.Hidden
  91. });
  92. }
  93. /// <summary>
  94. /// 转换为字符串
  95. /// </summary>
  96. /// <returns></returns>
  97. public override string ToString()
  98. {
  99. return NAME;
  100. }
  101. }
  102. }