1
1
using LinuxProxyChanger . Models ;
2
+ using Mono . Unix . Native ;
2
3
using Newtonsoft . Json ;
3
4
using System ;
4
5
using System . Diagnostics ;
5
6
using System . IO ;
7
+ using System . Linq ;
6
8
using System . Net . NetworkInformation ;
7
9
using System . Reflection ;
10
+ using System . Runtime . InteropServices ;
8
11
using System . Text ;
9
12
using System . Text . RegularExpressions ;
10
13
11
14
namespace LinuxProxyChanger
12
15
{
13
16
class Program
14
17
{
18
+ private static bool IsRoot => RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? Syscall . getuid ( ) . Equals ( 0 ) : false ;
19
+
15
20
/// <summary>
16
21
/// Settings json file
17
22
/// </summary>
@@ -57,6 +62,11 @@ static void Main(string[] args)
57
62
}
58
63
}
59
64
65
+ if ( settings . CallOnNetworkchange )
66
+ {
67
+ NetworkChange . NetworkAddressChanged += new NetworkAddressChangedEventHandler ( AddressChangedCallback ) ;
68
+ }
69
+
60
70
ConsoleKeyInfo cki ;
61
71
do
62
72
{
@@ -78,6 +88,8 @@ static void Main(string[] args)
78
88
break ;
79
89
}
80
90
} while ( cki . Key != ConsoleKey . Escape ) ;
91
+
92
+ Environment . Exit ( 0 ) ;
81
93
}
82
94
83
95
/// <summary>
@@ -109,6 +121,11 @@ static void Clear()
109
121
WriteColor ( $ "[// Title:] { Assembly . GetEntryAssembly ( ) . GetName ( ) . Name } ", ConsoleColor . DarkGreen ) ;
110
122
WriteColor ( $ "[// Version:] { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } ", ConsoleColor . DarkGreen ) ;
111
123
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 ) ;
112
129
WriteColor ( @"[//--Settings-----------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
113
130
WriteColor ( $ "[// Call on Networkchange:] { settings . CallOnNetworkchange } ", ConsoleColor . DarkGreen ) ;
114
131
WriteColor ( $ "[// Set proxy on Autostart:] { settings . SetProxyOnStartUp } ", ConsoleColor . DarkGreen ) ;
@@ -128,7 +145,37 @@ static void Clear()
128
145
WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
129
146
if ( ! Debugger . IsAttached )
130
147
{
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 ) ;
132
179
}
133
180
else
134
181
{
@@ -144,23 +191,34 @@ static void Clear()
144
191
/// <param name="e"></param>
145
192
static void AddressChangedCallback ( object sender , EventArgs e )
146
193
{
194
+ status = IPStatus . Unknown ;
147
195
NetworkInterface [ ] adapters = NetworkInterface . GetAllNetworkInterfaces ( ) ;
148
- foreach ( NetworkInterface n in adapters )
196
+
197
+ if ( adapters != null ) // No networkadapters found
149
198
{
150
- if ( n . OperationalStatus == OperationalStatus . Up && n . Id . Equals ( settings . NetworkChangeAdapter ) )
199
+ var networkChangeAdapterList = settings . NetworkChangeAdapters . Split ( "," ) ;
200
+
201
+ foreach ( NetworkInterface n in adapters )
151
202
{
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 )
158
204
{
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
+ }
160
218
}
219
+ //Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus);
220
+ //Console.WriteLine("Description is {0} [{1}]", n.Description, n.Id);
161
221
}
162
- //Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus);
163
- //Console.WriteLine("Description is {0} [{1}]", n.Description, n.Id);
164
222
}
165
223
}
166
224
@@ -170,15 +228,22 @@ static void AddressChangedCallback(object sender, EventArgs e)
170
228
/// <returns>Return status of the request</returns>
171
229
static IPStatus PingTest ( )
172
230
{
173
- Ping sender = new Ping ( ) ;
174
- PingOptions options = new PingOptions ( ) ;
231
+ try
232
+ {
233
+ Ping sender = new Ping ( ) ;
234
+ PingOptions options = new PingOptions ( ) ;
175
235
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 ) ;
179
239
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
+ }
182
247
}
183
248
184
249
/// <summary>
@@ -224,7 +289,7 @@ static void ConfirmProxy(bool isEnable)
224
289
}
225
290
else
226
291
{
227
- WriteColor ( $ "[// Linux Bash:] Command exit with code { proc . ExitCode } ", ConsoleColor . DarkRed ) ;
292
+ WriteColor ( $ "[// Linux Bash:] Command exit with code { proc . ExitCode } ", ConsoleColor . DarkYellow ) ;
228
293
}
229
294
}
230
295
catch ( Exception e )
@@ -241,25 +306,25 @@ static void EnableProxy()
241
306
{
242
307
foreach ( var file in settings . Files )
243
308
{
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 ) ;
246
311
if ( ! RemoveProxyFromFile ( file ) )
247
312
{
248
- WriteColor ( $ "[// EnableProxy :] Check file { file . Path } failed", ConsoleColor . DarkRed ) ;
313
+ WriteColor ( $ "[// # Error :] Check file { file . Path } failed", ConsoleColor . DarkRed ) ;
249
314
return ;
250
315
}
251
316
252
- WriteColor ( $ "[// EnableProxy: ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
317
+ WriteColor ( $ "[// # ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
253
318
if ( ! File . Exists ( file . Path ) )
254
319
{
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 ) ;
256
321
return ;
257
322
}
258
323
259
324
File . AppendAllLines ( file . Path , new [ ] { settings . UniquePrefixLine } ) ;
260
325
File . AppendAllLines ( file . Path , file . Proxy ) ;
261
326
File . AppendAllLines ( file . Path , new [ ] { settings . UniqueSuffixLine } ) ;
262
- WriteColor ( @$ "[//-- Done.]", ConsoleColor . DarkGreen ) ;
327
+ WriteColor ( @$ "[// ### Done.]", ConsoleColor . DarkGreen ) ;
263
328
}
264
329
ConfirmProxy ( true ) ;
265
330
}
@@ -271,9 +336,9 @@ static void DisableProxy()
271
336
{
272
337
foreach ( var file in settings . Files )
273
338
{
274
- WriteColor ( @$ "[//-- Disable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
339
+ WriteColor ( @$ "[// ### Disable Proxy for { file . Path } ]", ConsoleColor . DarkGreen ) ;
275
340
RemoveProxyFromFile ( file ) ;
276
- WriteColor ( @$ "[//-- Done.]", ConsoleColor . DarkGreen ) ;
341
+ WriteColor ( @$ "[// ### Done.]", ConsoleColor . DarkGreen ) ;
277
342
}
278
343
ConfirmProxy ( false ) ;
279
344
}
@@ -285,10 +350,10 @@ static void DisableProxy()
285
350
/// <returns>Success of the action</returns>
286
351
static bool RemoveProxyFromFile ( FileSettings file )
287
352
{
288
- WriteColor ( $ "[// DisableProxy: ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
353
+ WriteColor ( $ "[// # ] Setup file { file . Path } ", ConsoleColor . DarkGreen ) ;
289
354
if ( ! File . Exists ( file . Path ) )
290
355
{
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 ) ;
292
357
return false ;
293
358
}
294
359
@@ -317,17 +382,17 @@ static bool RemoveProxyFromFile(FileSettings file)
317
382
}
318
383
}
319
384
320
- WriteColor ( $ "[// DisableProxy: ] Override file { file . Path } ", ConsoleColor . DarkGreen ) ;
385
+ WriteColor ( $ "[// # ] Override file { file . Path } ", ConsoleColor . DarkGreen ) ;
321
386
var attributes = File . GetAttributes ( file . Path ) ;
322
387
File . Delete ( file . Path ) ;
323
388
if ( File . Exists ( file . Path ) )
324
389
{
325
- WriteColor ( $ "[// Error:] File { file . Path } could not be deleted", ConsoleColor . DarkRed ) ;
390
+ WriteColor ( $ "[// # Error:] File { file . Path } could not be deleted", ConsoleColor . DarkRed ) ;
326
391
return false ;
327
392
}
328
393
File . Move ( tmpFile , file . Path ) ;
329
394
330
- WriteColor ( $ "[// DisableProxy: ] Set file permissions", ConsoleColor . DarkGreen ) ;
395
+ WriteColor ( $ "[// # ] Set file permissions", ConsoleColor . DarkGreen ) ;
331
396
File . SetAttributes ( file . Path , attributes ) ;
332
397
333
398
return true ;
0 commit comments