-
Notifications
You must be signed in to change notification settings - Fork 97
/
NextTraceWrapper.cs
419 lines (401 loc) · 18 KB
/
NextTraceWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
using Resources = OpenTrace.Properties.Resources;
using OpenTrace.Properties;
using System.Collections.Generic;
using OpenTrace;
using System.Runtime.InteropServices;
using System.Linq;
namespace NextTrace
{
class ExceptionalOutputEventArgs : EventArgs
{
public bool IsErrorOutput { get; set; }
public string Output { get; set; }
public ExceptionalOutputEventArgs(bool isErrorOutput, string output)
{
IsErrorOutput = isErrorOutput;
Output = output;
}
}
class AppQuitEventArgs : EventArgs
{
public int ExitCode { get; set; }
public AppQuitEventArgs(int exitCode)
{
ExitCode = exitCode;
}
}
enum AppStatus
{
Init,
Start,
Quit
}
internal class NextTraceWrapper
{
private Process _process;
public AppStatus Status { get; set; } = AppStatus.Init;
public event EventHandler AppStart;
public event EventHandler<AppQuitEventArgs> AppQuit;
public event EventHandler<ExceptionalOutputEventArgs> ExceptionalOutput;
private string nexttracePath;
private bool builtinNT = false;
private int errorOutputCount = 0;
public ObservableCollection<TracerouteResult> Output { get; } = new ObservableCollection<TracerouteResult>();
public NextTraceWrapper()
{
string curDir = AppDomain.CurrentDomain.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// 检查 Windows 平台可执行文件
List<string> winBinaryList = new List<string> { "nexttrace.exe", "nexttrace_windows_amd64.exe", "nexttrace_windows_arm64.exe", "nexttrace_windows_armv7.exe", "nexttrace_windows_386.exe" };
foreach (string winBinaryName in winBinaryList)
{
if (File.Exists(Path.Combine(curDir, winBinaryName)))
{
// 先检查根目录
nexttracePath = Path.Combine(curDir, winBinaryName);
break;
}
// 再检查PATH变量
string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
{
if (File.Exists(Path.Combine(pathDir, winBinaryName)))
{
nexttracePath = Path.Combine(pathDir, winBinaryName);
break;
}
}
if (nexttracePath != null) break;
}
}
else
{
// 检查其他平台可执行文件
List<string> otherBinaryList = new List<string> { "nexttrace", "nexttrace_android_arm64", "nexttrace_darwin_amd64", "nexttrace_darwin_arm64", "nexttrace_dragonfly_amd64", "nexttrace_freebsd_386", "nexttrace_freebsd_amd64", "nexttrace_freebsd_arm64", "nexttrace_freebsd_armv7", "nexttrace_linux_386", "nexttrace_linux_amd64", "nexttrace_linux_arm64", "nexttrace_linux_armv5", "nexttrace_linux_armv6", "nexttrace_linux_armv7", "nexttrace_linux_mips", "nexttrace_linux_mips64", "nexttrace_linux_mips64le", "nexttrace_linux_mipsle", "nexttrace_linux_ppc64", "nexttrace_linux_ppc64le", "nexttrace_linux_riscv64", "nexttrace_linux_s390x", "nexttrace_openbsd_386", "nexttrace_openbsd_amd64", "nexttrace_openbsd_arm64", "nexttrace_openbsd_armv7" };
foreach (string otherBinaryName in otherBinaryList)
{
if (File.Exists(Path.Combine(curDir, "OpenTrace.app/Contents/MacOS", otherBinaryName)))
{
nexttracePath = Path.Combine(curDir, "OpenTrace.app/Contents/MacOS", otherBinaryName);
builtinNT = true;
break;
}
if (File.Exists(Path.Combine(curDir, otherBinaryName)))
{
nexttracePath = Path.Combine(curDir, otherBinaryName);
builtinNT = true;
break;
}
string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
{
if (File.Exists(Path.Combine(pathDir, otherBinaryName)))
{
nexttracePath = Path.Combine(pathDir, otherBinaryName);
break;
}
}
if (nexttracePath != null) break;
}
}
// 检查是否手动指定了可执行文件
if (UserSettings.executablePath != "")
{
if (File.Exists(UserSettings.executablePath))
{
nexttracePath = UserSettings.executablePath;
}
else
{
throw new IOException(UserSettings.executablePath);
}
}
// 未能找到可执行文件
if (nexttracePath == null)
{
throw new FileNotFoundException("nexttrace.exe not found in any location");
}
}
public void Run(string host, bool MTRMode, params string[] extraArgs)
{
Task.Run(() =>
{
Console.WriteLine($"Using NextTrace: {nexttracePath}");
string arguments;
if (MTRMode)
{
arguments = ArgumentBuilder(host, extraArgs.Concat(new string[] { "--queries 1" }).ToArray(), new string[] { "queries" });
}
else
{
arguments = ArgumentBuilder(host, extraArgs);
}
#if NET8_0_OR_GREATER
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
Array.Find(extraArgs, e => e == "-T" || e == "-U") != null &&
Environment.UserName != "root")
{
FileSystemInfo fa = new FileInfo(nexttracePath);
if ((fa.UnixFileMode & UnixFileMode.SetUser) == 0)
{
if (!builtinNT)
App.app.Invoke(() => {
Eto.Forms.MessageBox.Show(Resources.MISSING_COMP_PRIV_MACOS);
});
else
{
var elvp = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/usr/bin/osascript",
ArgumentList = {
"-e",
$"do shell script \"chown root:admin '{nexttracePath}' && chmod +sx '{nexttracePath}'\" with administrator privileges with prompt \"{Resources.MISSING_PRIV_MACOS}\"",
},
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = true
}
};
elvp.Start();
elvp.WaitForExit();
}
Status = AppStatus.Quit;
AppQuit?.Invoke(this, new AppQuitEventArgs(0));
return;
}
}
#endif
_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = nexttracePath,
Arguments = arguments,
UseShellExecute = false,
StandardOutputEncoding = Encoding.GetEncoding(65001),
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
if (UserSettings.IPInsightToken != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_IPINSIGHT_TOKEN", UserSettings.IPInsightToken);
if (UserSettings.IPInfoToken != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_IPINFO_TOKEN", UserSettings.IPInfoToken);
if (UserSettings.ChunZhenEndpoint != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_CHUNZHENURL", UserSettings.ChunZhenEndpoint);
if (UserSettings.LeoMoeAPI_HOSTPORT != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_HOSTPORT", UserSettings.LeoMoeAPI_HOSTPORT);
if (UserSettings.NextTraceProxy != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_PROXY", UserSettings.NextTraceProxy);
if (UserSettings.POWProvider != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_POWPROVIDER", UserSettings.POWProvider);
if (UserSettings.IPAPI_Base != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_IPAPI_BASE", UserSettings.IPAPI_Base);
if (MTRMode) // 添加环境变量让NextTrace进入持续追踪模式
_process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_UNINTERRUPTED", "1");
Regex match1stLine = new Regex(@"^\d{1,2}\|");
_process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
// Debug.Print(e.Data);
// 去除输出中的控制字符
Regex formatCleanup = new Regex(@"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]");
string line = formatCleanup.Replace(e.Data, "");
Match match1 = match1stLine.Match(line);
if (match1.Success)
{
Output.Add(ProcessLine(line));
}
else
{
if (line.StartsWith("NextTrace ")) return;
if (line.StartsWith("traceroute to ")) return;
if (line.StartsWith("IP Geo Data Provider")) return;
if (line.StartsWith("[NextTrace API]")) return;
ExceptionalOutput?.Invoke(this, new ExceptionalOutputEventArgs(false, line));
if (errorOutputCount < 100)
{
errorOutputCount++;
}
else
{
Kill(); // 错误输出过多,强制结束
}
}
}
};
_process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
//Debug.Print(e.Data);
ExceptionalOutput?.Invoke(this, new ExceptionalOutputEventArgs(true, e.Data));
if (errorOutputCount < 100)
{
errorOutputCount++;
}
else
{
Kill(); // 错误输出过多,强制结束
}
}
};
_process.Exited += (sender, e) =>
{
Debug.Print("Exited");
Status = AppStatus.Quit;
AppQuit?.Invoke(this, new AppQuitEventArgs(_process.ExitCode));
};
_process.EnableRaisingEvents = true;
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
Status = AppStatus.Start;
AppStart?.Invoke(this, null);
});
}
private TracerouteResult ProcessLine(string line)
{
string No = "";
string IP = "*";
string Time = "";
string Geolocation = "";
string AS = "";
string Hostname = "";
string Organization = "";
string Latitude = "";
string Longitude = "";
string[] LineData = line.Split('|');
if (LineData.Length > 7)
{
No = LineData[0];
if (LineData[1] == "*")
{
Time = "*";
}
else
{
IP = LineData[1];
Time = LineData[3];
Geolocation = LineData[5] + " " + LineData[6] + " " + LineData[7] + " " + LineData[8];
AS = LineData[4];
Hostname = LineData[2];
Organization = LineData[9];
Latitude = LineData[10];
Longitude = LineData[11];
}
}
// 匹配特定网络地址
if (new Regex(@"^((127\.)|(192\.168\.)|(10\.)|(172\.1[6-9]\.)|(172\.2[0-9]\.)|(172\.3[0-1]\.)|(::1$)|([fF][cCdD]))").IsMatch(IP))
{
Geolocation = Resources.PRIVATE_ADDR;
}
if (new Regex(@"^((100\.6[4-9]\.)|(100\.[7-9][0-9]\.)|(100\.1[0-1][0-9]\.)|(100\.12[0-7]\.))").IsMatch(IP))
{
Geolocation = Resources.SHARED_ADDR;
}
if (new Regex(@"^169\.254\.").IsMatch(IP))
{
Geolocation = Resources.LINKLOCAL_ADDR;
}
if (new Regex(@"^127\.").IsMatch(IP))
{
Geolocation = Resources.LOOPBACK_ADDR;
}
// 打码 IP 地址
// maskedHopsMode 设置包含 ip_half, ip_full, ip_geo, all 四种打码模式
// maskedHops 指示打码的跳数
if (UserSettings.maskedHops > 0 && int.Parse(No) <= UserSettings.maskedHops)
{
if (UserSettings.maskedHopsMode == "ip_half")
{
if (IP.Contains(":"))
{
// IPv6 全部打码
IP = "****";
}
else if (IP.Contains("."))
{
// IPv4 打码后 2 节
IP = string.Join(".", IP.Split('.').Take(2).Concat(new string[] { "xx", "xx" }));
}
// 删除主机名
Hostname = "";
}
else if (UserSettings.maskedHopsMode == "ip_full")
{
IP = "****";
// 删除主机名
Hostname = "";
}
else if (UserSettings.maskedHopsMode == "ip_geo")
{
IP = "****";
Geolocation = "****";
// 删除主机名
Hostname = "";
}
else if (UserSettings.maskedHopsMode == "all")
{
IP = "****";
Geolocation = "****";
AS = "****";
Hostname = "";
Organization = "****";
Latitude = "****";
Longitude = "****";
}
}
return new TracerouteResult(No, IP, Time, Geolocation, AS, Hostname, Organization, Latitude, Longitude);
}
private string ArgumentBuilder(string host, string[] extraArgs, string[] ignoreUserArgs = null)
{
List<string> finalArgs = new List<string>();
finalArgs.Add(host);
finalArgs.Add("--raw");
finalArgs.Add("--map");
var checkArgsFromConfList = new List<string> { "queries", "port", "parallel_requests", "max_hops", "first", "send_time", "ttl_time", "source", "dev" };
UserSettings userSettings = new UserSettings();
foreach (var setting in userSettings.GetType().GetProperties())
{
if (checkArgsFromConfList.Contains(setting.Name) && (ignoreUserArgs == null || !ignoreUserArgs.Contains(setting.Name)))
{
if ((string)setting.GetValue(userSettings, null) != "")
finalArgs.Add("--" + setting.Name.Replace('_', '-') + " " + (string)setting.GetValue(userSettings, null));
}
}
if (UserSettings.rdns_mode == "disable") finalArgs.Add("-n");
if (UserSettings.rdns_mode == "always") finalArgs.Add("-a");
finalArgs.Add(System.Globalization.CultureInfo.CurrentUICulture.Name.StartsWith("zh") ? "--language cn" : "--language en");
finalArgs.Add(UserSettings.arguments);
finalArgs.AddRange(extraArgs);
Debug.Print(String.Join(" ", finalArgs));
return String.Join(" ", finalArgs);
}
public void Kill()
{
try
{
if (_process != null && !_process.HasExited)
_process.Kill();
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
// 验证IP有效性,返回处理后的IP(如把IPv6转为缩写形式等)IP无效则返回null。
private string ValidateIP(string IP)
{
return null;
}
}
}