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.

PrivoxyRunner.cs 8.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.NetworkInformation;
  9. using System.Net.Sockets;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using Shadowsocks.Model;
  14. using Shadowsocks.Properties;
  15. using Shadowsocks.Util;
  16. using Shadowsocks.Util.ProcessManagement;
  17. namespace Shadowsocks.Controller
  18. {
  19. class PrivoxyRunner
  20. {
  21. private static int Uid;
  22. private static string UniqueConfigFile;
  23. private static Job PrivoxyJob;
  24. private Process _process;
  25. private int _runningPort;
  26. static PrivoxyRunner()
  27. {
  28. try
  29. {
  30. Uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance.
  31. UniqueConfigFile = $"privoxy_{Uid}.conf";
  32. PrivoxyJob = new Job();
  33. FileManager.UncompressFile(Utils.GetTempPath("ss_privoxy.exe"), Resources.privoxy_exe);
  34. FileManager.UncompressFile(Utils.GetTempPath("mgwz.dll"), Resources.mgwz_dll);
  35. }
  36. catch (IOException e)
  37. {
  38. Logging.LogUsefulException(e);
  39. }
  40. }
  41. public int RunningPort => _runningPort;
  42. public void Start(Configuration configuration)
  43. {
  44. if (_process == null)
  45. {
  46. Process[] existingPrivoxy = Process.GetProcessesByName("ss_privoxy");
  47. foreach (Process p in existingPrivoxy.Where(IsChildProcess))
  48. {
  49. KillProcess(p);
  50. }
  51. string privoxyConfig = Resources.privoxy_conf;
  52. _runningPort = this.GetFreePort();
  53. privoxyConfig = privoxyConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString());
  54. privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_PORT__", _runningPort.ToString());
  55. privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
  56. FileManager.ByteArrayToFile(Utils.GetTempPath(UniqueConfigFile), Encoding.UTF8.GetBytes(privoxyConfig));
  57. _process = new Process();
  58. // Configure the process using the StartInfo properties.
  59. _process.StartInfo.FileName = "ss_privoxy.exe";
  60. _process.StartInfo.Arguments = UniqueConfigFile;
  61. _process.StartInfo.WorkingDirectory = Utils.GetTempPath();
  62. _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  63. _process.StartInfo.UseShellExecute = true;
  64. _process.StartInfo.CreateNoWindow = true;
  65. _process.Start();
  66. /*
  67. * Add this process to job obj associated with this ss process, so that
  68. * when ss exit unexpectedly, this process will be forced killed by system.
  69. */
  70. PrivoxyJob.AddProcess(_process.Handle);
  71. }
  72. RefreshTrayArea();
  73. }
  74. public void Stop()
  75. {
  76. if (_process != null)
  77. {
  78. KillProcess(_process);
  79. _process.Dispose();
  80. _process = null;
  81. }
  82. RefreshTrayArea();
  83. }
  84. private static void KillProcess(Process p)
  85. {
  86. try
  87. {
  88. p.CloseMainWindow();
  89. p.WaitForExit(100);
  90. if (!p.HasExited)
  91. {
  92. p.Kill();
  93. p.WaitForExit();
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. Logging.LogUsefulException(e);
  99. }
  100. }
  101. /*
  102. * We won't like to kill other ss instances' ss_privoxy.exe.
  103. * This function will check whether the given process is created
  104. * by this process by checking the module path or command line.
  105. *
  106. * Since it's required to put ss in different dirs to run muti instances,
  107. * different instance will create their unique "privoxy_UID.conf" where
  108. * UID is hash of ss's location.
  109. */
  110. private static bool IsChildProcess(Process process)
  111. {
  112. try
  113. {
  114. if (Utils.IsPortableMode())
  115. {
  116. /*
  117. * Under PortableMode, we could identify it by the path of ss_privoxy.exe.
  118. */
  119. var path = process.MainModule.FileName;
  120. return Utils.GetTempPath("ss_privoxy.exe").Equals(path);
  121. }
  122. else
  123. {
  124. var cmd = process.GetCommandLine();
  125. return cmd.Contains(UniqueConfigFile);
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. /*
  131. * Sometimes Process.GetProcessesByName will return some processes that
  132. * are already dead, and that will cause exceptions here.
  133. * We could simply ignore those exceptions.
  134. */
  135. Logging.LogUsefulException(ex);
  136. return false;
  137. }
  138. }
  139. private int GetFreePort()
  140. {
  141. int defaultPort = 8123;
  142. try
  143. {
  144. // TCP stack please do me a favor
  145. TcpListener l = new TcpListener(IPAddress.Loopback, 0);
  146. l.Start();
  147. var port = ((IPEndPoint)l.LocalEndpoint).Port;
  148. l.Stop();
  149. return port;
  150. }
  151. catch (Exception e)
  152. {
  153. // in case access denied
  154. Logging.LogUsefulException(e);
  155. return defaultPort;
  156. }
  157. }
  158. [StructLayout(LayoutKind.Sequential)]
  159. public struct RECT
  160. {
  161. public int left;
  162. public int top;
  163. public int right;
  164. public int bottom;
  165. }
  166. [DllImport("user32.dll")]
  167. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  168. [DllImport("user32.dll")]
  169. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  170. [DllImport("user32.dll")]
  171. public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
  172. [DllImport("user32.dll")]
  173. public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
  174. public void RefreshTrayArea()
  175. {
  176. IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
  177. IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
  178. IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
  179. IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
  180. if (notificationAreaHandle == IntPtr.Zero)
  181. {
  182. notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area");
  183. IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
  184. IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area");
  185. RefreshTrayArea(overflowNotificationAreaHandle);
  186. }
  187. RefreshTrayArea(notificationAreaHandle);
  188. }
  189. private static void RefreshTrayArea(IntPtr windowHandle)
  190. {
  191. const uint wmMousemove = 0x0200;
  192. RECT rect;
  193. GetClientRect(windowHandle, out rect);
  194. for (var x = 0; x < rect.right; x += 5)
  195. for (var y = 0; y < rect.bottom; y += 5)
  196. SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x);
  197. }
  198. }
  199. }