11using System ;
2- using System . Collections . Generic ;
3- using System . ComponentModel ;
42using System . Diagnostics ;
53using System . IO ;
64using System . Linq ;
75using System . Net ;
8- using System . Net . NetworkInformation ;
96using System . Net . Sockets ;
10- using System . Runtime . InteropServices ;
117using System . Text ;
128using System . Windows . Forms ;
139using Shadowsocks . Model ;
@@ -19,19 +15,19 @@ namespace Shadowsocks.Controller
1915{
2016 class PrivoxyRunner
2117 {
22- private static int Uid ;
23- private static string UniqueConfigFile ;
24- private static Job PrivoxyJob ;
18+ private static int _uid ;
19+ private static string _uniqueConfigFile ;
20+ private static Job _privoxyJob ;
2521 private Process _process ;
2622 private int _runningPort ;
2723
2824 static PrivoxyRunner ( )
2925 {
3026 try
3127 {
32- Uid = Application . StartupPath . GetHashCode ( ) ; // Currently we use ss's StartupPath to identify different Privoxy instance.
33- UniqueConfigFile = $ "privoxy_{ Uid } .conf";
34- PrivoxyJob = new Job ( ) ;
28+ _uid = Application . StartupPath . GetHashCode ( ) ; // Currently we use ss's StartupPath to identify different Privoxy instance.
29+ _uniqueConfigFile = $ "privoxy_{ _uid } .conf";
30+ _privoxyJob = new Job ( ) ;
3531
3632 FileManager . UncompressFile ( Utils . GetTempPath ( "ss_privoxy.exe" ) , Resources . privoxy_exe ) ;
3733 FileManager . UncompressFile ( Utils . GetTempPath ( "mgwz.dll" ) , Resources . mgwz_dll ) ;
@@ -54,29 +50,33 @@ public void Start(Configuration configuration)
5450 KillProcess ( p ) ;
5551 }
5652 string privoxyConfig = Resources . privoxy_conf ;
57- _runningPort = this . GetFreePort ( ) ;
53+ _runningPort = GetFreePort ( ) ;
5854 privoxyConfig = privoxyConfig . Replace ( "__SOCKS_PORT__" , configuration . localPort . ToString ( ) ) ;
5955 privoxyConfig = privoxyConfig . Replace ( "__PRIVOXY_BIND_PORT__" , _runningPort . ToString ( ) ) ;
6056 privoxyConfig = privoxyConfig . Replace ( "__PRIVOXY_BIND_IP__" , configuration . shareOverLan ? "0.0.0.0" : "127.0.0.1" ) ;
61- FileManager . ByteArrayToFile ( Utils . GetTempPath ( UniqueConfigFile ) , Encoding . UTF8 . GetBytes ( privoxyConfig ) ) ;
57+ FileManager . ByteArrayToFile ( Utils . GetTempPath ( _uniqueConfigFile ) , Encoding . UTF8 . GetBytes ( privoxyConfig ) ) ;
6258
63- _process = new Process ( ) ;
64- // Configure the process using the StartInfo properties.
65- _process . StartInfo . FileName = "ss_privoxy.exe" ;
66- _process . StartInfo . Arguments = UniqueConfigFile ;
67- _process . StartInfo . WorkingDirectory = Utils . GetTempPath ( ) ;
68- _process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
69- _process . StartInfo . UseShellExecute = true ;
70- _process . StartInfo . CreateNoWindow = true ;
59+ _process = new Process
60+ {
61+ // Configure the process using the StartInfo properties.
62+ StartInfo =
63+ {
64+ FileName = "ss_privoxy.exe" ,
65+ Arguments = _uniqueConfigFile ,
66+ WorkingDirectory = Utils . GetTempPath ( ) ,
67+ WindowStyle = ProcessWindowStyle . Hidden ,
68+ UseShellExecute = true ,
69+ CreateNoWindow = true
70+ }
71+ } ;
7172 _process . Start ( ) ;
7273
7374 /*
7475 * Add this process to job obj associated with this ss process, so that
7576 * when ss exit unexpectedly, this process will be forced killed by system.
7677 */
77- PrivoxyJob . AddProcess ( _process . Handle ) ;
78+ _privoxyJob . AddProcess ( _process . Handle ) ;
7879 }
79- RefreshTrayArea ( ) ;
8080 }
8181
8282 public void Stop ( )
@@ -87,7 +87,6 @@ public void Stop()
8787 _process . Dispose ( ) ;
8888 _process = null ;
8989 }
90- RefreshTrayArea ( ) ;
9190 }
9291
9392 private static void KillProcess ( Process p )
@@ -135,7 +134,7 @@ private static bool IsChildProcess(Process process)
135134 {
136135 var cmd = process . GetCommandLine ( ) ;
137136
138- return cmd . Contains ( UniqueConfigFile ) ;
137+ return cmd . Contains ( _uniqueConfigFile ) ;
139138 }
140139 }
141140 catch ( Exception ex )
@@ -169,48 +168,5 @@ private int GetFreePort()
169168 return defaultPort ;
170169 }
171170 }
172-
173- [ StructLayout ( LayoutKind . Sequential ) ]
174- public struct RECT
175- {
176- public int left ;
177- public int top ;
178- public int right ;
179- public int bottom ;
180- }
181- [ DllImport ( "user32.dll" ) ]
182- public static extern IntPtr FindWindow ( string lpClassName , string lpWindowName ) ;
183- [ DllImport ( "user32.dll" ) ]
184- public static extern IntPtr FindWindowEx ( IntPtr hwndParent , IntPtr hwndChildAfter , string lpszClass , string lpszWindow ) ;
185- [ DllImport ( "user32.dll" ) ]
186- public static extern bool GetClientRect ( IntPtr hWnd , out RECT lpRect ) ;
187- [ DllImport ( "user32.dll" ) ]
188- public static extern IntPtr SendMessage ( IntPtr hWnd , uint msg , int wParam , int lParam ) ;
189-
190- public void RefreshTrayArea ( )
191- {
192- IntPtr systemTrayContainerHandle = FindWindow ( "Shell_TrayWnd" , null ) ;
193- IntPtr systemTrayHandle = FindWindowEx ( systemTrayContainerHandle , IntPtr . Zero , "TrayNotifyWnd" , null ) ;
194- IntPtr sysPagerHandle = FindWindowEx ( systemTrayHandle , IntPtr . Zero , "SysPager" , null ) ;
195- IntPtr notificationAreaHandle = FindWindowEx ( sysPagerHandle , IntPtr . Zero , "ToolbarWindow32" , "Notification Area" ) ;
196- if ( notificationAreaHandle == IntPtr . Zero )
197- {
198- notificationAreaHandle = FindWindowEx ( sysPagerHandle , IntPtr . Zero , "ToolbarWindow32" , "User Promoted Notification Area" ) ;
199- IntPtr notifyIconOverflowWindowHandle = FindWindow ( "NotifyIconOverflowWindow" , null ) ;
200- IntPtr overflowNotificationAreaHandle = FindWindowEx ( notifyIconOverflowWindowHandle , IntPtr . Zero , "ToolbarWindow32" , "Overflow Notification Area" ) ;
201- RefreshTrayArea ( overflowNotificationAreaHandle ) ;
202- }
203- RefreshTrayArea ( notificationAreaHandle ) ;
204- }
205-
206- private static void RefreshTrayArea ( IntPtr windowHandle )
207- {
208- const uint wmMousemove = 0x0200 ;
209- RECT rect ;
210- GetClientRect ( windowHandle , out rect ) ;
211- for ( var x = 0 ; x < rect . right ; x += 5 )
212- for ( var y = 0 ; y < rect . bottom ; y += 5 )
213- SendMessage ( windowHandle , wmMousemove , 0 , ( y << 16 ) + x ) ;
214- }
215171 }
216172}
0 commit comments