@@ -24,15 +24,18 @@ internal class CheckIPWorking
24
24
private readonly int downloadTimeout ;
25
25
public string downloadException = "" ;
26
26
public string frontingException = "" ;
27
+ private bool isDiagnosing = false ;
28
+ public bool isV2rayExecutionSuccess = false ;
27
29
28
- public CheckIPWorking ( string ip , ScanSpeed targetSpeed , CustomConfigInfo scanConfig , int downloadTimeout )
30
+ public CheckIPWorking ( string ip , ScanSpeed targetSpeed , CustomConfigInfo scanConfig , int downloadTimeout , bool isDiagnosing = false )
29
31
{
30
32
this . ip = ip ;
31
33
this . port = getPortByIP ( ) ;
32
34
v2rayConfigPath = $ "v2ray-config/generated/config.{ ip } .json";
33
35
this . targetSpeed = targetSpeed ;
34
36
this . scanConfig = scanConfig ;
35
37
this . downloadTimeout = downloadTimeout ;
38
+ this . isDiagnosing = isDiagnosing ;
36
39
}
37
40
38
41
public CheckIPWorking ( )
@@ -41,27 +44,36 @@ public CheckIPWorking()
41
44
42
45
public bool check ( )
43
46
{
44
- bool success = false ;
45
- Tools . logStep ( "\n ------- Start IP Check ------- " ) ;
46
-
47
+ bool v2rayDLSuccess = false ;
48
+ Tools . logStep ( "\n ------------ Start IP Check ------------" , isDiagnosing ) ;
49
+ Tools . logStep ( "IP: " + this . ip , isDiagnosing ) ;
50
+
47
51
// first of all quick test on fronting domain through cloudflare
48
- if ( checkFronting ( ) )
52
+ bool frontingSuccess = checkFronting ( ) ;
53
+
54
+ if ( frontingSuccess || isDiagnosing ) // on diagnosing we will always test v2ray
49
55
{
50
- // don't speed test is selected by user
51
- if ( targetSpeed . isSpeedZero ( ) )
56
+ // don't speed test if that mode is selected by user
57
+ if ( targetSpeed . isSpeedZero ( ) && ! isDiagnosing )
52
58
{
53
- success = true ;
59
+ v2rayDLSuccess = true ;
54
60
}
55
61
else
56
62
{
57
63
// then test quality of connection by downloading small file through v2ray vpn
58
- success = checkV2ray ( ) ;
64
+ v2rayDLSuccess = checkV2ray ( ) ;
59
65
}
60
66
61
67
}
62
68
63
- Tools . logStep ( "\n ------- End IP Check -------\n " ) ;
64
- return success ;
69
+ Tools . logStep (
70
+ string . Format ( Environment . NewLine + "Fronting Result: {0}" , frontingSuccess ? "SUCCESS" : "FAILED" ) + Environment . NewLine +
71
+ string . Format ( "v2ray.exe Execution: {0}" , isV2rayExecutionSuccess ? "SUCCESS" : "FAILED" ) + Environment . NewLine +
72
+ string . Format ( "Download Result: {0}" , v2rayDLSuccess ? "SUCCESS" : "FAILED" ) , isDiagnosing
73
+ ) ;
74
+
75
+ Tools . logStep ( "\n ------------ End IP Check ------------\n " , isDiagnosing ) ;
76
+ return v2rayDLSuccess ;
65
77
66
78
}
67
79
@@ -102,28 +114,35 @@ public bool checkFronting(bool withCustumDNSResolver = true, int timeout = 1)
102
114
103
115
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
104
116
client . Timeout = TimeSpan . FromSeconds ( timeout ) ;
117
+ Tools . logStep ( Environment . NewLine + "----- Fronting Test -----" , isDiagnosing ) ;
105
118
106
- Tools . logStep ( $ "Start fronting check, timeout: { timeout } , Resolver IP: { ip } , withCustumDNSResolver: { withCustumDNSResolver . ToString ( ) } ") ;
119
+ Tools . logStep ( $ "Start fronting check, timeout: { timeout } , Resolver IP: { ip } , withCustumDNSResolver: { withCustumDNSResolver . ToString ( ) } ", isDiagnosing ) ;
107
120
Stopwatch sw = new Stopwatch ( ) ;
108
121
try
109
122
{
110
123
111
124
string frUrl = "https://" + ConfigManager . Instance . getAppConfig ( ) ? . frontDomain ;
112
- Tools . logStep ( $ "Starting fronting check with url: { frUrl } ") ;
125
+ Tools . logStep ( $ "Fronting check with url: { frUrl } ", isDiagnosing ) ;
113
126
sw . Start ( ) ;
114
127
var html = client . GetStringAsync ( frUrl ) . Result ;
115
- Tools . logStep ( $ "Fronting check done in { sw . ElapsedMilliseconds : n0} ms, content: '{ html . Substring ( 0 , 50 ) } '") ;
128
+ Tools . logStep ( $ "Fronting check done in { sw . ElapsedMilliseconds : n0} ms, content: '{ html . Substring ( 0 , 50 ) } '", isDiagnosing ) ;
116
129
frontingDuration = sw . ElapsedMilliseconds ;
117
130
return true ;
118
131
}
119
132
catch ( Exception ex )
120
133
{
121
134
string message = ex . Message ;
122
- Tools . logStep ( $ "Fronting check had exception: { message } ") ;
135
+ if ( isTimeoutException ( ex ) )
136
+ {
137
+ Tools . logStep ( "Fronting timed out." , isDiagnosing ) ;
138
+ }
139
+ else
140
+ {
141
+ Tools . logStep ( $ "Fronting check had exception: { message } ", isDiagnosing ) ;
123
142
124
- // monitor exceptions
125
- if ( ! message . Contains ( "A task was canceled." ) )
143
+ // monitor exceptions
126
144
frontingException = message ;
145
+ }
127
146
128
147
return false ;
129
148
}
@@ -147,46 +166,59 @@ private bool checkDownloadSpeed()
147
166
148
167
var client = new HttpClient ( handler ) ;
149
168
client . Timeout = TimeSpan . FromSeconds ( timeout ) ; // 2 seconds
150
- Tools . logStep ( $ "Start check dl speed, proxy port: { port } , timeout: { timeout } sec, target speed: { targetSpeed . getTargetSpeed ( ) : n0} b/s") ;
169
+ Tools . logStep ( Environment . NewLine + "----- Download Test -----" , isDiagnosing ) ;
170
+ Tools . logStep ( $ "Start check dl speed, proxy port: { port } , timeout: { timeout } sec, target speed: { targetSpeed . getTargetSpeed ( ) : n0} b/s", isDiagnosing ) ;
151
171
Stopwatch sw = new Stopwatch ( ) ;
152
172
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
153
173
154
174
try
155
175
{
156
176
sw . Start ( ) ;
157
177
string dlUrl = "https://" + ConfigManager . Instance . getAppConfig ( ) . scanDomain + targetSpeed . getTargetFileSize ( timeout ) ;
158
- Tools . logStep ( $ "Starting dl url: { dlUrl } ") ;
178
+ Tools . logStep ( $ "Starting dl url: { dlUrl } ", isDiagnosing ) ;
159
179
var data = client . GetStringAsync ( dlUrl ) . Result ;
160
- Tools . logStep ( $ "*** Download success in { sw . ElapsedMilliseconds : n0} ms, dl size: { data . Length : n0} bytes for IP { ip } ") ;
180
+ Tools . logStep ( $ "*** Download success in { sw . ElapsedMilliseconds : n0} ms, dl size: { data . Length : n0} bytes for IP { ip } ", isDiagnosing ) ;
161
181
162
182
return data . Length == targetSpeed . getTargetSpeed ( ) * timeout ;
163
183
}
164
184
catch ( Exception ex )
165
185
{
166
186
string message = ex . Message ;
167
- Tools . logStep ( $ "dl had exception: { message } ") ;
168
-
169
- // monitor exceptions
170
- if ( ! message . Contains ( "A task was canceled." ) )
171
- downloadException = message ;
172
-
173
- if ( ex . InnerException != null && ex . InnerException ? . Message != "" && ! ex . Message . Contains ( ex . InnerException ? . Message ) )
187
+ if ( isTimeoutException ( ex ) )
174
188
{
175
- Tools . logStep ( $ "Inner exception: { ex . InnerException ? . Message } " ) ;
189
+ Tools . logStep ( "Download timed out." , isDiagnosing ) ;
176
190
}
191
+ else
192
+ {
193
+ Tools . logStep ( $ "Download had exception: { message } ", isDiagnosing ) ;
194
+ // monitor exceptions
195
+ downloadException = message ;
196
+
197
+ if ( ex . InnerException != null && ex . InnerException ? . Message != "" && ! ex . Message . Contains ( ex . InnerException ? . Message ) )
198
+ {
199
+ Tools . logStep ( $ "Inner exception: { ex . InnerException ? . Message } ", isDiagnosing ) ;
200
+ }
201
+ }
202
+
177
203
return false ;
178
204
}
179
205
finally
180
206
{
181
207
downloadDuration = sw . ElapsedMilliseconds ;
182
208
if ( downloadDuration > ( timeout * 1000 ) + 500 )
183
209
{
184
- Tools . logStep ( $ "Download took too long! { downloadDuration : n0} ms for IP { ip } ") ;
210
+ Tools . logStep ( $ "Download took too long! { downloadDuration : n0} ms for IP { ip } ", isDiagnosing ) ;
185
211
}
186
212
handler . Dispose ( ) ;
187
213
client . Dispose ( ) ;
188
214
}
189
215
}
216
+ private bool isTimeoutException ( Exception ex )
217
+ {
218
+ string msg = ex . Message ;
219
+ return msg . Contains ( "The request was aborted" ) ||
220
+ msg . Contains ( "A task was canceled." ) ;
221
+ }
190
222
191
223
private bool createV2rayConfigFile ( )
192
224
{
@@ -222,7 +254,7 @@ private bool createV2rayConfigFile()
222
254
}
223
255
catch ( Exception ex )
224
256
{
225
- Tools . logStep ( $ "createV2rayConfigFile has exception: { ex . Message } ") ;
257
+ Tools . logStep ( $ "createV2rayConfigFile has exception: { ex . Message } ", isDiagnosing ) ;
226
258
return false ;
227
259
}
228
260
@@ -252,18 +284,19 @@ private bool runV2rayProcess()
252
284
//}
253
285
startInfo . UseShellExecute = false ;
254
286
startInfo . Arguments = $ "run -config=\" { v2rayConfigPath } \" ";
255
- Tools . logStep ( $ "Starting v2ray.exe with arg: { startInfo . Arguments } ") ;
287
+ Tools . logStep ( Environment . NewLine + "----- Running v2ray.exe -----" , isDiagnosing ) ;
288
+ Tools . logStep ( $ "Starting v2ray.exe with arg: { startInfo . Arguments } ", isDiagnosing ) ;
256
289
bool wasSuccess = false ;
257
290
try
258
291
{
259
292
process = Process . Start ( startInfo ) ;
260
293
Thread . Sleep ( 1500 ) ;
261
294
wasSuccess = process . Responding && ! process . HasExited ;
262
- Tools . logStep ( $ "v2ray.exe executed success: { wasSuccess } ") ;
295
+ Tools . logStep ( $ "v2ray.exe executed success: { wasSuccess } ", isDiagnosing ) ;
263
296
}
264
297
catch ( Exception ex )
265
298
{
266
- Tools . logStep ( $ "v2ray.exe execution had exception: { ex . Message } ") ;
299
+ Tools . logStep ( $ "v2ray.exe execution had exception: { ex . Message } ", isDiagnosing ) ;
267
300
}
268
301
269
302
// log error
@@ -272,11 +305,15 @@ private bool runV2rayProcess()
272
305
try
273
306
{
274
307
string err = process . StandardError . ReadToEnd ( ) ;
275
- Tools . logStep ( $ "v2ray.exe Error: { err } ") ;
308
+ string message = $ "v2ray.exe Error: { err } ";
309
+ Tools . logStep ( message , isDiagnosing ) ;
310
+ downloadException = message ;
276
311
}
277
312
catch ( Exception ) { }
278
313
}
279
314
315
+ isV2rayExecutionSuccess = wasSuccess ;
316
+
280
317
return wasSuccess ;
281
318
}
282
319
0 commit comments