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 6.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using Shadowsocks.Model;
  10. using Shadowsocks.Properties;
  11. using Shadowsocks.Util;
  12. namespace Shadowsocks.Controller
  13. {
  14. class PolipoRunner
  15. {
  16. private Process _process;
  17. private int _runningPort;
  18. static PolipoRunner()
  19. {
  20. try
  21. {
  22. FileManager.UncompressFile(Utils.GetTempPath("ss_privoxy.exe"), Resources.privoxy_exe);
  23. FileManager.UncompressFile(Utils.GetTempPath("mgwz.dll"), Resources.mgwz_dll);
  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_privoxy");
  43. foreach (Process p in existingPolipo)
  44. {
  45. try
  46. {
  47. p.Kill();
  48. p.WaitForExit();
  49. }
  50. catch (Exception e)
  51. {
  52. Logging.LogUsefulException(e);
  53. }
  54. }
  55. string polipoConfig = Resources.privoxy_conf;
  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(Utils.GetTempPath("privoxy.conf"), Encoding.UTF8.GetBytes(polipoConfig));
  61. _process = new Process();
  62. // Configure the process using the StartInfo properties.
  63. _process.StartInfo.FileName = "ss_privoxy.exe";
  64. _process.StartInfo.Arguments = "privoxy.conf";
  65. _process.StartInfo.WorkingDirectory = Utils.GetTempPath();
  66. _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  67. _process.StartInfo.UseShellExecute = true;
  68. _process.StartInfo.CreateNoWindow = true;
  69. _process.Start();
  70. }
  71. RefreshTrayArea();
  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. Logging.LogUsefulException(e);
  85. }
  86. _process = null;
  87. }
  88. RefreshTrayArea();
  89. }
  90. private int GetFreePort()
  91. {
  92. int defaultPort = 8123;
  93. try
  94. {
  95. IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
  96. IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();
  97. List<int> usedPorts = new List<int>();
  98. foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners())
  99. {
  100. usedPorts.Add(endPoint.Port);
  101. }
  102. for (int port = defaultPort; port <= 65535; port++)
  103. {
  104. if (!usedPorts.Contains(port))
  105. {
  106. return port;
  107. }
  108. }
  109. }
  110. catch (Exception e)
  111. {
  112. // in case access denied
  113. Logging.LogUsefulException(e);
  114. return defaultPort;
  115. }
  116. throw new Exception("No free port found.");
  117. }
  118. [StructLayout(LayoutKind.Sequential)]
  119. public struct RECT
  120. {
  121. public int left;
  122. public int top;
  123. public int right;
  124. public int bottom;
  125. }
  126. [DllImport("user32.dll")]
  127. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  128. [DllImport("user32.dll")]
  129. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  130. [DllImport("user32.dll")]
  131. public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
  132. [DllImport("user32.dll")]
  133. public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
  134. public void RefreshTrayArea()
  135. {
  136. IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
  137. IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
  138. IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
  139. IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
  140. if (notificationAreaHandle == IntPtr.Zero)
  141. {
  142. notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area");
  143. IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
  144. IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area");
  145. RefreshTrayArea(overflowNotificationAreaHandle);
  146. }
  147. RefreshTrayArea(notificationAreaHandle);
  148. }
  149. private static void RefreshTrayArea(IntPtr windowHandle)
  150. {
  151. const uint wmMousemove = 0x0200;
  152. RECT rect;
  153. GetClientRect(windowHandle, out rect);
  154. for (var x = 0; x < rect.right; x += 5)
  155. for (var y = 0; y < rect.bottom; y += 5)
  156. SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x);
  157. }
  158. }
  159. }