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 ;
6
7
using System . Linq ;
7
8
using System . Net . NetworkInformation ;
8
9
using System . Reflection ;
10
+ using System . Runtime . InteropServices ;
9
11
using System . Text ;
10
12
using System . Text . RegularExpressions ;
11
13
12
14
namespace LinuxProxyChanger
13
15
{
14
16
class Program
15
17
{
18
+ private static bool IsRoot => RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? Syscall . getuid ( ) . Equals ( 0 ) : false ;
19
+
16
20
/// <summary>
17
21
/// Settings json file
18
22
/// </summary>
@@ -58,6 +62,11 @@ static void Main(string[] args)
58
62
}
59
63
}
60
64
65
+ if ( settings . CallOnNetworkchange )
66
+ {
67
+ NetworkChange . NetworkAddressChanged += new NetworkAddressChangedEventHandler ( AddressChangedCallback ) ;
68
+ }
69
+
61
70
ConsoleKeyInfo cki ;
62
71
do
63
72
{
@@ -79,6 +88,8 @@ static void Main(string[] args)
79
88
break ;
80
89
}
81
90
} while ( cki . Key != ConsoleKey . Escape ) ;
91
+
92
+ Environment . Exit ( 0 ) ;
82
93
}
83
94
84
95
/// <summary>
@@ -110,6 +121,11 @@ static void Clear()
110
121
WriteColor ( $ "[// Title:] { Assembly . GetEntryAssembly ( ) . GetName ( ) . Name } ", ConsoleColor . DarkGreen ) ;
111
122
WriteColor ( $ "[// Version:] { Assembly . GetEntryAssembly ( ) . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) . Version } ", ConsoleColor . DarkGreen ) ;
112
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 ) ;
113
129
WriteColor ( @"[//--Settings-----------------------------------------------------]" , ConsoleColor . DarkGreen ) ;
114
130
WriteColor ( $ "[// Call on Networkchange:] { settings . CallOnNetworkchange } ", ConsoleColor . DarkGreen ) ;
115
131
WriteColor ( $ "[// Set proxy on Autostart:] { settings . SetProxyOnStartUp } ", ConsoleColor . DarkGreen ) ;
@@ -129,7 +145,22 @@ static void Clear()
129
145
WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
130
146
if ( ! Debugger . IsAttached )
131
147
{
132
- 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 ) ;
133
164
}
134
165
else
135
166
{
@@ -144,7 +175,7 @@ static void Clear()
144
175
WriteColor ( @"[//---------------------------------------------------------------]" , ConsoleColor . DarkRed ) ;
145
176
if ( ! Debugger . IsAttached )
146
177
{
147
- return ;
178
+ Environment . Exit ( 3 ) ;
148
179
}
149
180
else
150
181
{
@@ -160,15 +191,16 @@ static void Clear()
160
191
/// <param name="e"></param>
161
192
static void AddressChangedCallback ( object sender , EventArgs e )
162
193
{
194
+ status = IPStatus . Unknown ;
163
195
NetworkInterface [ ] adapters = NetworkInterface . GetAllNetworkInterfaces ( ) ;
164
196
165
- if ( adapters = = null ) // No networkadapters found
197
+ if ( adapters ! = null ) // No networkadapters found
166
198
{
167
199
var networkChangeAdapterList = settings . NetworkChangeAdapters . Split ( "," ) ;
168
200
169
201
foreach ( NetworkInterface n in adapters )
170
202
{
171
- if ( n . OperationalStatus == OperationalStatus . Up && networkChangeAdapterList . Contains ( n . Id ) )
203
+ if ( n . OperationalStatus == OperationalStatus . Up && networkChangeAdapterList . Contains ( n . Id ) && status != IPStatus . Success )
172
204
{
173
205
status = PingTest ( ) ;
174
206
@@ -196,15 +228,22 @@ static void AddressChangedCallback(object sender, EventArgs e)
196
228
/// <returns>Return status of the request</returns>
197
229
static IPStatus PingTest ( )
198
230
{
199
- Ping sender = new Ping ( ) ;
200
- PingOptions options = new PingOptions ( ) ;
231
+ try
232
+ {
233
+ Ping sender = new Ping ( ) ;
234
+ PingOptions options = new PingOptions ( ) ;
201
235
202
- options . DontFragment = true ;
203
- string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
204
- byte [ ] buffer = Encoding . ASCII . GetBytes ( data ) ;
236
+ options . DontFragment = true ;
237
+ string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa" ;
238
+ byte [ ] buffer = Encoding . ASCII . GetBytes ( data ) ;
205
239
206
- PingReply reply = sender . Send ( settings . ProxyIp , settings . Timeout , buffer , options ) ;
207
- 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
+ }
208
247
}
209
248
210
249
/// <summary>
0 commit comments