From 1a283aa4ea46829def456bf28fc89cf34d275474 Mon Sep 17 00:00:00 2001 From: Syrone Wong Date: Sat, 17 Dec 2016 09:06:53 +0800 Subject: [PATCH] Drop obsolete tray area refreshing code - naming private vars - object initializer Signed-off-by: Syrone Wong --- .../Controller/Service/PrivoxyRunner.cs | 90 +++++-------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs index 1dff5add..970473bf 100644 --- a/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs +++ b/shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs @@ -1,13 +1,9 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; -using System.Net.NetworkInformation; using System.Net.Sockets; -using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using Shadowsocks.Model; @@ -19,9 +15,9 @@ namespace Shadowsocks.Controller { class PrivoxyRunner { - private static int Uid; - private static string UniqueConfigFile; - private static Job PrivoxyJob; + private static int _uid; + private static string _uniqueConfigFile; + private static Job _privoxyJob; private Process _process; private int _runningPort; @@ -29,9 +25,9 @@ namespace Shadowsocks.Controller { try { - Uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance. - UniqueConfigFile = $"privoxy_{Uid}.conf"; - PrivoxyJob = new Job(); + _uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance. + _uniqueConfigFile = $"privoxy_{_uid}.conf"; + _privoxyJob = new Job(); FileManager.UncompressFile(Utils.GetTempPath("ss_privoxy.exe"), Resources.privoxy_exe); FileManager.UncompressFile(Utils.GetTempPath("mgwz.dll"), Resources.mgwz_dll); @@ -54,29 +50,33 @@ namespace Shadowsocks.Controller KillProcess(p); } string privoxyConfig = Resources.privoxy_conf; - _runningPort = this.GetFreePort(); + _runningPort = GetFreePort(); privoxyConfig = privoxyConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_PORT__", _runningPort.ToString()); privoxyConfig = privoxyConfig.Replace("__PRIVOXY_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); - FileManager.ByteArrayToFile(Utils.GetTempPath(UniqueConfigFile), Encoding.UTF8.GetBytes(privoxyConfig)); + FileManager.ByteArrayToFile(Utils.GetTempPath(_uniqueConfigFile), Encoding.UTF8.GetBytes(privoxyConfig)); - _process = new Process(); - // Configure the process using the StartInfo properties. - _process.StartInfo.FileName = "ss_privoxy.exe"; - _process.StartInfo.Arguments = UniqueConfigFile; - _process.StartInfo.WorkingDirectory = Utils.GetTempPath(); - _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - _process.StartInfo.UseShellExecute = true; - _process.StartInfo.CreateNoWindow = true; + _process = new Process + { + // Configure the process using the StartInfo properties. + StartInfo = + { + FileName = "ss_privoxy.exe", + Arguments = _uniqueConfigFile, + WorkingDirectory = Utils.GetTempPath(), + WindowStyle = ProcessWindowStyle.Hidden, + UseShellExecute = true, + CreateNoWindow = true + } + }; _process.Start(); /* * Add this process to job obj associated with this ss process, so that * when ss exit unexpectedly, this process will be forced killed by system. */ - PrivoxyJob.AddProcess(_process.Handle); + _privoxyJob.AddProcess(_process.Handle); } - RefreshTrayArea(); } public void Stop() @@ -87,7 +87,6 @@ namespace Shadowsocks.Controller _process.Dispose(); _process = null; } - RefreshTrayArea(); } private static void KillProcess(Process p) @@ -135,7 +134,7 @@ namespace Shadowsocks.Controller { var cmd = process.GetCommandLine(); - return cmd.Contains(UniqueConfigFile); + return cmd.Contains(_uniqueConfigFile); } } catch (Exception ex) @@ -169,48 +168,5 @@ namespace Shadowsocks.Controller return defaultPort; } } - - [StructLayout(LayoutKind.Sequential)] - public struct RECT - { - public int left; - public int top; - public int right; - public int bottom; - } - [DllImport("user32.dll")] - public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); - [DllImport("user32.dll")] - public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); - [DllImport("user32.dll")] - public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); - [DllImport("user32.dll")] - public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam); - - public void RefreshTrayArea() - { - IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null); - IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null); - IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null); - IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area"); - if (notificationAreaHandle == IntPtr.Zero) - { - notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area"); - IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null); - IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area"); - RefreshTrayArea(overflowNotificationAreaHandle); - } - RefreshTrayArea(notificationAreaHandle); - } - - private static void RefreshTrayArea(IntPtr windowHandle) - { - const uint wmMousemove = 0x0200; - RECT rect; - GetClientRect(windowHandle, out rect); - for (var x = 0; x < rect.right; x += 5) - for (var y = 0; y < rect.bottom; y += 5) - SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x); - } } }