-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (141 loc) · 5.61 KB
/
Copy pathProgram.cs
File metadata and controls
156 lines (141 loc) · 5.61 KB
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
using Eto.Forms;
using System;
using System.Globalization;
using System.Diagnostics;
using System.Runtime.InteropServices;
using OpenTrace.UI;
using OpenTrace.Infrastructure;
namespace OpenTrace
{
class App
{
public static Application app;
/// <summary>
/// 命令行参数,用于快速启动 traceroute
/// </summary>
public static string[] CommandLineArgs { get; set; }
/// <summary>
/// 命令行中的额外参数(非目标地址),直接传递给 nexttrace
/// </summary>
public static string[] CommandLineExtraArgs { get; set; }
/// <summary>
/// 命令行中指定的目标地址
/// </summary>
public static string CommandLineTarget { get; set; }
}
internal class Program
{
[STAThread]
static void Main(string[] args)
{
UserSettings.LoadSettings();
if (!string.IsNullOrWhiteSpace(UserSettings.language))
{
CultureInfo.CurrentUICulture = new CultureInfo(UserSettings.language);
}
#if NET8_0_OR_GREATER
// 为 macOS 载入正确的 locale
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var asp = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/usr/bin/osascript",
ArgumentList = { "-e", "user locale of (get system info)" },
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = false,
CreateNoWindow = true
}
};
asp.Start();
try
{
var line = asp.StandardOutput.ReadLine()?.Replace("_","-");
var curCulture = new CultureInfo(line?.Trim()??"");
CultureInfo.CurrentUICulture = curCulture;
}
catch (Exception e) {}
}
#endif
// 本地化设置
if (CultureInfo.CurrentUICulture.Name == "zh-CN" && TimeZoneInfo.Local.Id == "China Standard Time")
{
if (UserSettings.mapProvider == "" && UserSettings.mapProvider != null) UserSettings.mapProvider = "baidu";
}
else
{
if (UserSettings.mapProvider == "" && UserSettings.mapProvider != null) UserSettings.mapProvider = "google";
}
// 保存命令行参数以便在 GUI 启动后使用
App.CommandLineArgs = args;
// 解析命令行参数:识别目标地址和额外参数
if (args.Length > 0)
{
ParseCommandLineArgs(args);
}
App.app = new Application(Eto.Platform.Detect);
App.app.Run(new MainForm());
}
/// <summary>
/// 解析命令行参数,分离目标地址和额外参数
/// </summary>
private static void ParseCommandLineArgs(string[] args)
{
var extraArgs = new System.Collections.Generic.List<string>();
string target = null;
// nexttrace 的选项参数列表(带值的参数)
var argsWithValue = new System.Collections.Generic.HashSet<string>
{
"-p", "--port", "-q", "--queries", "-m", "--max-hops",
"-d", "--data-provider", "-f", "--first", "-s", "--source",
"-D", "--dev", "-z", "--send-time", "-i", "--ttl-time",
"-g", "--language", "--file", "--from", "--pow-provider",
"--parallel-requests", "--max-attempts", "--icmp-mode",
"--source-port", "--listen", "--timeout", "--psize"
};
// 无值的开关参数
var switchArgs = new System.Collections.Generic.HashSet<string>
{
"-h", "--help", "--init", "-4", "--ipv4", "-6", "--ipv6",
"-T", "--tcp", "-U", "--udp", "-F", "--fast-trace",
"-n", "--no-rdns", "-a", "--always-rdns", "-P", "--route-path",
"-r", "--report", "--dn42", "-o", "--output", "-t", "--table",
"--raw", "-j", "--json", "-c", "--classic", "-M", "--map",
"-e", "--disable-mpls", "-V", "--version", "-C", "--no-color",
"--deploy"
};
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith("-"))
{
// 这是一个选项参数
extraArgs.Add(arg);
// 检查是否是带值的参数
if (argsWithValue.Contains(arg) && i + 1 < args.Length)
{
i++;
extraArgs.Add(args[i]);
}
}
else
{
// 这是一个位置参数(目标地址)
if (target == null)
{
target = arg;
}
else
{
// 额外的位置参数也添加到额外参数中
extraArgs.Add(arg);
}
}
}
App.CommandLineTarget = target;
App.CommandLineExtraArgs = extraArgs.ToArray();
}
}
}