11using LinuxProxyChanger . Models ;
2+ using Mono . Unix . Native ;
23using Newtonsoft . Json ;
34using System ;
45using System . Diagnostics ;
56using System . IO ;
7+ using System . Linq ;
68using System . Net . NetworkInformation ;
79using System . Reflection ;
10+ using System . Runtime . InteropServices ;
811using System . Text ;
912using System . Text . RegularExpressions ;
1013
1114namespace LinuxProxyChanger
1215{
1316 class Program
1417 {
18+ private static bool IsRoot => RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? Syscall . getuid ( ) . Equals ( 0 ) : false ;
19+
1520 /// <summary>
1621 /// Settings json file
1722 /// </summary>
@@ -57,6 +62,11 @@ static void Main(string[] args)
5762 }
5863 }
5964
65+ if ( settings . CallOnNetworkchange )
66+ {
67+ NetworkChange . NetworkAddressChanged += new NetworkAddressChangedEventHandler ( AddressChangedCallback ) ;
68+ }
69+
6070 ConsoleKeyInfo cki ;
6171 do
6272 {
@@ -78,6 +88,8 @@ static void Main(string[] args)
7888 break ;
7989 }
8090 } while ( cki . Key != ConsoleKey . Escape ) ;
91+
92+ Environment . Exit ( 0 ) ;
8193 }
8294
8395 /// <summary>
@@ -109,6 +121,11 @@ static void Clear()
109121 WriteColor ( $ "[// Title:] { Assembly . GetEntryAssembly ( ) . GetName ( ) . Name } ", ConsoleColor . DarkGreen ) ;
110122 WriteColor ( $ "[// Version:] { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } ", ConsoleColor . DarkGreen ) ;
111123 WriteColor ( $ "[// Autor:] { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyCopyrightAttribute > ( ) . Copyright } ", ConsoleColor . DarkGreen ) ;
124+ WriteColor ( @"[//--Exit Codes---------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
125+ WriteColor ( $ "[// 0:] Application successful exited", ConsoleColor . DarkGreen ) ;
126+ WriteColor ( $ "[// 1:] Supported OS is not given", ConsoleColor . DarkGreen ) ;
127+ WriteColor ( $ "[// 2:] User has no root permissions", ConsoleColor . DarkGreen ) ;
128+ WriteColor ( $ "[// 3:] Networksadapters are not set", ConsoleColor . DarkGreen ) ;
112129 WriteColor ( @"[//--Settings-----------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
113130 WriteColor ( $ "[// Call on Networkchange:] { settings . CallOnNetworkchange } ", ConsoleColor . DarkGreen ) ;
114131 WriteColor ( $ "[// Set proxy on Autostart:] { settings . SetProxyOnStartUp } ", ConsoleColor . DarkGreen ) ;
@@ -128,7 +145,37 @@ static void Clear()
128145 WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
129146 if ( ! Debugger . IsAttached )
130147 {
131- return ;
148+ Environment . Exit ( 1 ) ;
149+ }
150+ else
151+ {
152+ Console . WriteLine ( Environment . NewLine ) ;
153+ }
154+ }
155+
156+ if ( ! IsRoot )
157+ {
158+ WriteColor ( @"[//--No root permissions------------------------------------------]" , ConsoleColor . DarkRed ) ;
159+ WriteColor ( $ "[//:] Please start this tool as root", ConsoleColor . DarkRed ) ;
160+ WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
161+ if ( ! Debugger . IsAttached )
162+ {
163+ Environment . Exit ( 2 ) ;
164+ }
165+ else
166+ {
167+ Console . WriteLine ( Environment . NewLine ) ;
168+ }
169+ }
170+
171+ if ( string . IsNullOrEmpty ( settings . NetworkChangeAdapters ) )
172+ {
173+ WriteColor ( @"[//--No Networkadapters-------------------------------------------]" , ConsoleColor . DarkRed ) ;
174+ WriteColor ( $ "[//:] Please insert Networkadapters (\" NetworkChangeAdapters\" ) in the settings.json", ConsoleColor . DarkRed ) ;
175+ WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
176+ if ( ! Debugger . IsAttached )
177+ {
178+ Environment . Exit ( 3 ) ;
132179 }
133180 else
134181 {
@@ -144,23 +191,34 @@ static void Clear()
144191 /// <param name="e"></param>
145192 static void AddressChangedCallback ( object sender , EventArgs e )
146193 {
194+ status = IPStatus . Unknown ;
147195 NetworkInterface [ ] adapters = NetworkInterface . GetAllNetworkInterfaces ( ) ;
148- foreach ( NetworkInterface n in adapters )
196+
197+ if ( adapters != null ) // No networkadapters found
149198 {
150- if ( n . OperationalStatus == OperationalStatus . Up && n . Id . Equals ( settings . NetworkChangeAdapter ) )
199+ var networkChangeAdapterList = settings . NetworkChangeAdapters . Split ( "," ) ;
200+
201+ foreach ( NetworkInterface n in adapters )
151202 {
152- status = PingTest ( ) ;
153- if ( status == IPStatus . Success )
154- {
155- EnableProxy ( ) ;
156- }
157- else
203+ if ( n . OperationalStatus == OperationalStatus . Up && networkChangeAdapterList . Contains ( n . Id ) && status != IPStatus . Success )
158204 {
159- DisableProxy ( ) ;
205+ status = PingTest ( ) ;
206+
207+ // Update staus text in console
208+ Clear ( ) ;
209+
210+ if ( status == IPStatus . Success )
211+ {
212+ EnableProxy ( ) ;
213+ }
214+ else
215+ {
216+ DisableProxy ( ) ;
217+ }
160218 }
219+ //Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus);
220+ //Console.WriteLine("Description is {0} [{1}]", n.Description, n.Id);
161221 }
162- //Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus);
163- //Console.WriteLine("Description is {0} [{1}]", n.Description, n.Id);
164222 }
165223 }
166224
@@ -170,15 +228,22 @@ static void AddressChangedCallback(object sender, EventArgs e)
170228 /// <returns>Return status of the request</returns>
171229 static IPStatus PingTest ( )
172230 {
173- Ping sender = new Ping ( ) ;
174- PingOptions options = new PingOptions ( ) ;
231+ try
232+ {
233+ Ping sender = new Ping ( ) ;
234+ PingOptions options = new PingOptions ( ) ;
175235
176- options . DontFragment = true ;
177- string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
178- byte [ ] buffer = Encoding . ASCII . GetBytes ( data ) ;
236+ options . DontFragment = true ;
237+ string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
238+ byte [ ] buffer = Encoding . ASCII . GetBytes ( data ) ;
179239
180- PingReply reply = sender . Send ( settings . ProxyIp , settings . Timeout , buffer , options ) ;
181- return reply . Status ;
240+ PingReply reply = sender . Send ( settings . ProxyIp , settings . Timeout , buffer , options ) ;
241+ return reply . Status ;
242+ }
243+ catch
244+ {
245+ return IPStatus . DestinationHostUnreachable ;
246+ }
182247 }
183248
184249 /// <summary>
@@ -224,7 +289,7 @@ static void ConfirmProxy(bool isEnable)
224289 }
225290 else
226291 {
227- WriteColor ( $ "[// Linux Bash:] Command exit with code { proc . ExitCode } ", ConsoleColor . DarkRed ) ;
292+ WriteColor ( $ "[// Linux Bash:] Command exit with code { proc . ExitCode } ", ConsoleColor . DarkYellow ) ;
228293 }
229294 }
230295 catch ( Exception e )
@@ -241,25 +306,25 @@ static void EnableProxy()
241306 {
242307 foreach ( var file in settings . Files )
243308 {
244- WriteColor ( @$ "[//-- Enable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
245- WriteColor ( $ "[// EnableProxy: ] Check file { file . Path } for old entrys", ConsoleColor . DarkGreen ) ;
309+ WriteColor ( @$ "[// ### Enable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
310+ WriteColor ( $ "[// # ] Check file { file . Path } for old entrys", ConsoleColor . DarkGreen ) ;
246311 if ( ! RemoveProxyFromFile ( file ) )
247312 {
248- WriteColor ( $ "[// EnableProxy :] Check file { file . Path } failed", ConsoleColor . DarkRed ) ;
313+ WriteColor ( $ "[// # Error :] Check file { file . Path } failed", ConsoleColor . DarkRed ) ;
249314 return ;
250315 }
251316
252- WriteColor ( $ "[// EnableProxy: ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
317+ WriteColor ( $ "[// # ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
253318 if ( ! File . Exists ( file . Path ) )
254319 {
255- WriteColor ( $ "[// Error:] File path { file . Path } could not be found", ConsoleColor . DarkRed ) ;
320+ WriteColor ( $ "[// # Error:] File path { file . Path } could not be found", ConsoleColor . DarkRed ) ;
256321 return ;
257322 }
258323
259324 File . AppendAllLines ( file . Path , new [ ] { settings . UniquePrefixLine } ) ;
260325 File . AppendAllLines ( file . Path , file . Proxy ) ;
261326 File . AppendAllLines ( file . Path , new [ ] { settings . UniqueSuffixLine } ) ;
262- WriteColor ( @$ "[//-- Done.]", ConsoleColor . DarkGreen ) ;
327+ WriteColor ( @$ "[// ### Done.]", ConsoleColor . DarkGreen ) ;
263328 }
264329 ConfirmProxy ( true ) ;
265330 }
@@ -271,9 +336,9 @@ static void DisableProxy()
271336 {
272337 foreach ( var file in settings . Files )
273338 {
274- WriteColor ( @$ "[//-- Disable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
339+ WriteColor ( @$ "[// ### Disable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
275340 RemoveProxyFromFile ( file ) ;
276- WriteColor ( @$ "[//-- Done.]", ConsoleColor . DarkGreen ) ;
341+ WriteColor ( @$ "[// ### Done.]", ConsoleColor . DarkGreen ) ;
277342 }
278343 ConfirmProxy ( false ) ;
279344 }
@@ -285,10 +350,10 @@ static void DisableProxy()
285350 /// <returns>Success of the action</returns>
286351 static bool RemoveProxyFromFile ( FileSettings file )
287352 {
288- WriteColor ( $ "[// DisableProxy: ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
353+ WriteColor ( $ "[// # ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
289354 if ( ! File . Exists ( file . Path ) )
290355 {
291- WriteColor ( $ "[// Error:] File path { file . Path } could not be found", ConsoleColor . DarkRed ) ;
356+ WriteColor ( $ "[// # Error:] File path { file . Path } could not be found", ConsoleColor . DarkRed ) ;
292357 return false ;
293358 }
294359
@@ -317,17 +382,17 @@ static bool RemoveProxyFromFile(FileSettings file)
317382 }
318383 }
319384
320- WriteColor ( $ "[// DisableProxy: ] Override file { file . Path } ", ConsoleColor . DarkGreen ) ;
385+ WriteColor ( $ "[// # ] Override file { file . Path } ", ConsoleColor . DarkGreen ) ;
321386 var attributes = File . GetAttributes ( file . Path ) ;
322387 File . Delete ( file . Path ) ;
323388 if ( File . Exists ( file . Path ) )
324389 {
325- WriteColor ( $ "[// Error:] File { file . Path } could not be deleted", ConsoleColor . DarkRed ) ;
390+ WriteColor ( $ "[// # Error:] File { file . Path } could not be deleted", ConsoleColor . DarkRed ) ;
326391 return false ;
327392 }
328393 File . Move ( tmpFile , file . Path ) ;
329394
330- WriteColor ( $ "[// DisableProxy: ] Set file permissions", ConsoleColor . DarkGreen ) ;
395+ WriteColor ( $ "[// # ] Set file permissions", ConsoleColor . DarkGreen ) ;
331396 File . SetAttributes ( file . Path , attributes ) ;
332397
333398 return true ;
0 commit comments