You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PolipoRunner.cs 4.4 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Shadowsocks.Model;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Text;
  9. using System.Net.NetworkInformation;
  10. using System.Net;
  11. namespace Shadowsocks.Controller
  12. {
  13. class PolipoRunner
  14. {
  15. private Process _process;
  16. private static string temppath;
  17. private int _runningPort;
  18. static PolipoRunner()
  19. {
  20. temppath = Path.GetTempPath();
  21. try
  22. {
  23. FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe);
  24. }
  25. catch (IOException e)
  26. {
  27. Logging.LogUsefulException(e);
  28. }
  29. }
  30. public int RunningPort
  31. {
  32. get
  33. {
  34. return _runningPort;
  35. }
  36. }
  37. public void Start(Configuration configuration)
  38. {
  39. Server server = configuration.GetCurrentServer();
  40. if (_process == null)
  41. {
  42. Process[] existingPolipo = Process.GetProcessesByName("ss_polipo");
  43. foreach (Process p in existingPolipo)
  44. {
  45. try
  46. {
  47. p.Kill();
  48. p.WaitForExit();
  49. }
  50. catch (Exception e)
  51. {
  52. Console.WriteLine(e.ToString());
  53. }
  54. }
  55. string polipoConfig = Resources.polipo_config;
  56. _runningPort = this.GetFreePort();
  57. polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
  58. polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString());
  59. polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
  60. FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
  61. _process = new Process();
  62. // Configure the process using the StartInfo properties.
  63. _process.StartInfo.FileName = temppath + "/ss_polipo.exe";
  64. _process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\"";
  65. _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  66. _process.StartInfo.UseShellExecute = true;
  67. _process.StartInfo.CreateNoWindow = true;
  68. //_process.StartInfo.RedirectStandardOutput = true;
  69. //_process.StartInfo.RedirectStandardError = true;
  70. _process.Start();
  71. }
  72. }
  73. public void Stop()
  74. {
  75. if (_process != null)
  76. {
  77. try
  78. {
  79. _process.Kill();
  80. _process.WaitForExit();
  81. }
  82. catch (Exception e)
  83. {
  84. Console.WriteLine(e.ToString());
  85. }
  86. _process = null;
  87. }
  88. }
  89. private int GetFreePort()
  90. {
  91. int defaultPort = 8123;
  92. try
  93. {
  94. IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
  95. IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();
  96. List<int> usedPorts = new List<int>();
  97. foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners())
  98. {
  99. usedPorts.Add(endPoint.Port);
  100. }
  101. for (int port = defaultPort; port <= 65535; port++)
  102. {
  103. if (!usedPorts.Contains(port))
  104. {
  105. return port;
  106. }
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. // in case access denied
  112. Logging.LogUsefulException(e);
  113. return defaultPort;
  114. }
  115. throw new Exception("No free port found.");
  116. }
  117. }
  118. }