forked from leeter/WinMTR-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinMTRDialog-registry.cpp
More file actions
367 lines (346 loc) · 15.5 KB
/
Copy pathWinMTRDialog-registry.cpp
File metadata and controls
367 lines (346 loc) · 15.5 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
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
module;
#pragma warning(disable : 4005)
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <WinSock2.h>
#include <afx.h>
#include <afxext.h>
#include <afxdisp.h>
#include <afxcmn.h>
#include <atlbase.h>
#include "resource.h"
#include "WinMTRBranding.h"
module WinMTR.Dialog:registry;
import :ClassDef;
import <algorithm>;
import <cstddef>;
import <format>;
import <iterator>;
import <string>;
import <vector>;
import WinMTR.Options;
import WinMTRUtils;
namespace {
constexpr wchar_t rootKeyName[] = LR"(Software\WinMTR)";
constexpr wchar_t configKeyName[] = LR"(Software\WinMTR\Config)";
constexpr wchar_t historyKeyName[] = LR"(Software\WinMTR\LRU)";
constexpr wchar_t historyCountName[] = L"NrLRU";
constexpr DWORD currentSettingsSchemaVersion = 2;
[[nodiscard]] CString localized(UINT id)
{
CString value;
value.LoadStringW(id);
return value;
}
[[nodiscard]] DWORD queryDword(CRegKey& key, const wchar_t* name, DWORD fallback) noexcept
{
DWORD value = fallback;
if (key.QueryDWORDValue(name, value) != ERROR_SUCCESS) {
value = fallback;
key.SetDWORDValue(name, value);
}
return value;
}
void persistHistory(const std::vector<std::wstring>& hosts)
{
CRegKey key;
if (key.Create(HKEY_CURRENT_USER, historyKeyName) != ERROR_SUCCESS) return;
DWORD oldCount = 0;
key.QueryDWORDValue(historyCountName, oldCount);
oldCount = std::min<DWORD>(oldCount, 4096);
for (DWORD index = 1; index <= std::max<DWORD>(oldCount, static_cast<DWORD>(hosts.size())); ++index) {
key.DeleteValue(std::format(L"Host{}", index).c_str());
}
for (size_t index = 0; index < hosts.size(); ++index) {
key.SetStringValue(std::format(L"Host{}", index + 1).c_str(), hosts[index].c_str());
}
key.SetDWORDValue(historyCountName, static_cast<DWORD>(hosts.size()));
}
[[nodiscard]] std::vector<std::wstring> loadPersistedHistory()
{
CRegKey key;
if (key.Open(HKEY_CURRENT_USER, historyKeyName, KEY_READ) != ERROR_SUCCESS) return {};
DWORD storedCount = 0;
if (key.QueryDWORDValue(historyCountName, storedCount) != ERROR_SUCCESS) return {};
storedCount = std::min<DWORD>(storedCount, 4096);
std::vector<std::wstring> hosts;
for (DWORD index = 1; index <= storedCount; ++index) {
wchar_t value[NI_MAXHOST]{};
ULONG characters = static_cast<ULONG>(std::size(value));
if (key.QueryStringValue(std::format(L"Host{}", index).c_str(), value, &characters) != ERROR_SUCCESS
|| value[0] == L'\0') continue;
if (const auto duplicate = std::find_if(hosts.begin(), hosts.end(), [&](const std::wstring& existing) {
return _wcsicmp(existing.c_str(), value) == 0;
}); duplicate != hosts.end()) {
hosts.erase(duplicate);
}
hosts.emplace_back(value);
}
return hosts;
}
void keepNewest(std::vector<std::wstring>& hosts, size_t limit)
{
if (hosts.size() > limit) {
hosts.erase(hosts.begin(), hosts.end() - static_cast<std::ptrdiff_t>(limit));
}
}
} // namespace
BOOL WinMTRDialog::InitRegistry() noexcept
{
CRegKey root;
if (root.Create(HKEY_CURRENT_USER, rootKeyName) != ERROR_SUCCESS) return FALSE;
root.SetStringValue(L"Version", WinMTRBranding::display_version.data());
root.SetStringValue(L"License", L"GNU General Public License v2");
root.SetStringValue(L"HomePage", WinMTRBranding::company_url.data());
CRegKey config;
if (config.Create(HKEY_CURRENT_USER, configKeyName) != ERROR_SUCCESS) return FALSE;
DWORD settingsSchemaVersion = 0;
if (config.QueryDWORDValue(L"SettingsSchemaVersion", settingsSchemaVersion) != ERROR_SUCCESS
|| settingsSchemaVersion < currentSettingsSchemaVersion) {
// Version 2 adds bounded grace and global packet-rate settings. queryDword
// writes their defaults for older installations during this initialization.
config.SetDWORDValue(L"SettingsSchemaVersion", currentSettingsSchemaVersion);
}
if (!hasPacketSizeFromCommandLine) packetSize = std::clamp<unsigned>(queryDword(config, L"PingSize",
WinMTRUtils::DEFAULT_PING_SIZE), WinMTRUtils::MIN_PING_SIZE, WinMTRUtils::MAX_PING_SIZE);
persistentHistoryLimit = std::clamp<unsigned>(queryDword(config, L"MaxLRU",
WinMTRUtils::DEFAULT_MAX_LRU), WinMTRUtils::MIN_MAX_LRU, WinMTRUtils::MAX_MAX_LRU);
if (!hasHistoryLimitFromCommandLine) historyLimit = persistentHistoryLimit;
if (!hasResolveNamesFromCommandLine) resolveNames = queryDword(config, L"UseDNS",
WinMTRUtils::DEFAULT_USE_DNS) != 0;
if (!hasIntervalFromCommandLine) interval = std::clamp<double>(queryDword(config, L"IntervalMs",
static_cast<DWORD>(WinMTRUtils::DEFAULT_INTERVAL * 1000.0)) / 1000.0,
WinMTRUtils::MIN_INTERVAL, WinMTRUtils::MAX_INTERVAL);
if (!hasMaxHopsFromCommandLine) maxHops = std::clamp<unsigned>(queryDword(config, L"MaxHops",
WinMTRUtils::DEFAULT_MAX_HOPS), WinMTRUtils::MIN_MAX_HOPS, WinMTRUtils::MAX_MAX_HOPS);
if (!hasTimeoutFromCommandLine) timeoutMs = std::clamp<unsigned>(queryDword(config, L"TimeoutMs",
WinMTRUtils::DEFAULT_TIMEOUT_MS), WinMTRUtils::MIN_TIMEOUT_MS, WinMTRUtils::MAX_TIMEOUT_MS);
if (!hasCyclesFromCommandLine) cycles = std::clamp<unsigned>(queryDword(config, L"Cycles",
WinMTRUtils::DEFAULT_CYCLES), WinMTRUtils::MIN_CYCLES, WinMTRUtils::MAX_CYCLES);
if (!hasGraceFromCommandLine) graceMs = std::clamp<unsigned>(queryDword(config, L"GraceMs",
WinMTRUtils::DEFAULT_GRACE_MS), WinMTRUtils::MIN_GRACE_MS, WinMTRUtils::MAX_GRACE_MS);
if (!hasMaxGlobalPpsFromCommandLine) maxGlobalPps = std::clamp<unsigned>(queryDword(config,
L"MaxGlobalPps", WinMTRUtils::DEFAULT_MAX_GLOBAL_PPS),
WinMTRUtils::MIN_MAX_GLOBAL_PPS, WinMTRUtils::MAX_MAX_GLOBAL_PPS);
if (!hasTosFromCommandLine) tos = std::clamp<unsigned>(queryDword(config, L"TOS",
WinMTRUtils::DEFAULT_TOS), WinMTRUtils::MIN_TOS, WinMTRUtils::MAX_TOS);
if (!hasPayloadPatternFromCommandLine) payloadPattern = std::clamp(static_cast<int>(queryDword(config,
L"PayloadPattern", static_cast<DWORD>(WinMTRUtils::DEFAULT_PAYLOAD_PATTERN))),
WinMTRUtils::MIN_PAYLOAD_PATTERN, WinMTRUtils::MAX_PAYLOAD_PATTERN);
if (!hasStartTtlFromCommandLine) startTtl = std::clamp<unsigned>(queryDword(config, L"StartTTL",
WinMTRUtils::DEFAULT_START_TTL), WinMTRUtils::MIN_START_TTL, maxHops.load());
if (!hasMinimumTtlFromCommandLine) minimumTtl = std::clamp<unsigned>(queryDword(config, L"MinimumTTL",
WinMTRUtils::DEFAULT_MINIMUM_TTL), WinMTRUtils::MIN_MINIMUM_TTL, maxHops.load());
if (!hasUnknownHostLimitFromCommandLine) unknownHostLimit = std::clamp<unsigned>(queryDword(config,
L"UnknownHostLimit", WinMTRUtils::DEFAULT_UNKNOWN_HOST_LIMIT),
WinMTRUtils::MIN_UNKNOWN_HOST_LIMIT, WinMTRUtils::MAX_UNKNOWN_HOST_LIMIT);
if (!hasEcmpDisplayLimitFromCommandLine) ecmpDisplayLimit = std::clamp<unsigned>(queryDword(config,
L"EcmpDisplayLimit", WinMTRUtils::DEFAULT_ECMP_DISPLAY_LIMIT),
WinMTRUtils::MIN_ECMP_DISPLAY_LIMIT, WinMTRUtils::MAX_ECMP_RESPONDERS);
if (!hasReplyCacheFromCommandLine) replyCacheSeconds = std::clamp<unsigned>(queryDword(config,
L"ReplyCacheSeconds", WinMTRUtils::DEFAULT_REPLY_CACHE_SECONDS),
WinMTRUtils::MIN_REPLY_CACHE_SECONDS, WinMTRUtils::MAX_REPLY_CACHE_SECONDS);
if (!hasLookupAsnIspFromCommandLine) lookupAsnIsp = queryDword(config, L"LookupAsnIsp",
WinMTRUtils::DEFAULT_LOOKUP_ASN_ISP) != 0;
if (!hasDontFragmentFromCommandLine) dontFragment = queryDword(config, L"DontFragment",
WinMTRUtils::DEFAULT_DONT_FRAGMENT) != 0;
if (!hasAddressFamiliesFromCommandLine) {
useIPv4 = queryDword(config, L"UseIPv4", WinMTRUtils::DEFAULT_USE_IPV4) != 0;
useIPv6 = queryDword(config, L"UseIPv6", WinMTRUtils::DEFAULT_USE_IPV6) != 0;
}
if (!hasQueryPublicInfoFromCommandLine) queryPublicInfo = queryDword(config, L"QueryPublicInfo",
WinMTRUtils::DEFAULT_QUERY_PUBLIC_NETWORK_INFO) != 0;
if (!hasPublicInfoRefreshFromCommandLine) {
publicInfoRefreshMode = std::clamp<unsigned>(queryDword(config, L"PublicInfoRefreshMode",
WinMTRUtils::DEFAULT_PUBLIC_INFO_REFRESH_MODE),
WinMTRUtils::PUBLIC_INFO_REFRESH_ON_NETWORK_CHANGE,
WinMTRUtils::PUBLIC_INFO_REFRESH_FIXED_INTERVAL);
publicInfoRefreshMinutes = std::clamp<unsigned>(queryDword(config, L"PublicInfoRefreshMinutes",
WinMTRUtils::DEFAULT_PUBLIC_INFO_REFRESH_MINUTES),
WinMTRUtils::MIN_PUBLIC_INFO_REFRESH_MINUTES,
WinMTRUtils::MAX_PUBLIC_INFO_REFRESH_MINUTES);
}
auto persistedHosts = loadPersistedHistory();
if (!hasHistoryLimitFromCommandLine) {
keepNewest(persistedHosts, persistentHistoryLimit);
persistHistory(persistedHosts);
}
sessionHistory = persistedHosts;
keepNewest(sessionHistory, std::max(historyLimit, persistentHistoryLimit));
auto visibleHosts = sessionHistory;
keepNewest(visibleHosts, historyLimit);
for (const auto& host : visibleHosts) comboHost.AddString(host.c_str());
historyCount = static_cast<int>(visibleHosts.size());
comboHost.AddString(localized(IDS_STRING_CLEAR_HISTORY));
return TRUE;
}
void WinMTRDialog::SaveSettings() noexcept
{
CRegKey config;
if (config.Create(HKEY_CURRENT_USER, configKeyName) != ERROR_SUCCESS) return;
config.SetDWORDValue(L"SettingsSchemaVersion", currentSettingsSchemaVersion);
config.SetDWORDValue(L"PingSize", packetSize.load());
config.SetDWORDValue(L"MaxLRU", historyLimit);
config.SetDWORDValue(L"UseDNS", resolveNames.load() ? 1 : 0);
config.SetDWORDValue(L"IntervalMs", static_cast<DWORD>(interval.load() * 1000.0 + 0.5));
config.SetDWORDValue(L"MaxHops", maxHops.load());
config.SetDWORDValue(L"TimeoutMs", timeoutMs.load());
config.SetDWORDValue(L"Cycles", cycles.load());
config.SetDWORDValue(L"GraceMs", graceMs.load());
config.SetDWORDValue(L"MaxGlobalPps", maxGlobalPps.load());
config.SetDWORDValue(L"TOS", tos.load());
config.SetDWORDValue(L"PayloadPattern", static_cast<DWORD>(payloadPattern.load()));
config.SetDWORDValue(L"StartTTL", startTtl.load());
config.SetDWORDValue(L"MinimumTTL", minimumTtl.load());
config.SetDWORDValue(L"UnknownHostLimit", unknownHostLimit.load());
config.SetDWORDValue(L"EcmpDisplayLimit", ecmpDisplayLimit.load());
config.SetDWORDValue(L"ReplyCacheSeconds", replyCacheSeconds.load());
config.SetDWORDValue(L"LookupAsnIsp", lookupAsnIsp.load() ? 1 : 0);
config.SetDWORDValue(L"DontFragment", dontFragment.load() ? 1 : 0);
config.SetDWORDValue(L"UseIPv4", useIPv4.load() ? 1 : 0);
config.SetDWORDValue(L"UseIPv6", useIPv6.load() ? 1 : 0);
config.SetDWORDValue(L"QueryPublicInfo", queryPublicInfo.load() ? 1 : 0);
config.SetDWORDValue(L"PublicInfoRefreshMode", publicInfoRefreshMode.load());
config.SetDWORDValue(L"PublicInfoRefreshMinutes", publicInfoRefreshMinutes.load());
}
void WinMTRDialog::ClearHistory()
{
persistHistory({});
sessionHistory.clear();
historyCount = 0;
comboHost.ResetContent();
comboHost.AddString(localized(IDS_STRING_CLEAR_HISTORY));
comboHost.SetWindowTextW(L"");
}
void WinMTRDialog::AddHostToHistory(const std::wstring& host)
{
sessionHistory.erase(std::remove_if(sessionHistory.begin(), sessionHistory.end(), [&](const std::wstring& value) {
return _wcsicmp(value.c_str(), host.c_str()) == 0;
}), sessionHistory.end());
sessionHistory.emplace_back(host);
keepNewest(sessionHistory, std::max(historyLimit, persistentHistoryLimit));
auto storedHosts = sessionHistory;
keepNewest(storedHosts, persistentHistoryLimit);
persistHistory(storedHosts);
auto visibleHosts = sessionHistory;
keepNewest(visibleHosts, historyLimit);
comboHost.ResetContent();
for (const auto& value : visibleHosts) comboHost.AddString(value.c_str());
comboHost.AddString(localized(IDS_STRING_CLEAR_HISTORY));
comboHost.SetWindowTextW(host.c_str());
historyCount = static_cast<int>(visibleHosts.size());
}
void WinMTRDialog::OnRestart() noexcept
{
if (state == STATES::STOPPING || state == STATES::EXIT) return;
if (state == STATES::TRACING) {
Transit(STATES::STOPPING);
return;
}
if (comboHost.GetCount() > 0 && comboHost.GetCurSel() == comboHost.GetCount() - 1) {
ClearHistory();
return;
}
if (!InitMTRNet()) return;
CString host;
comboHost.GetWindowTextW(host);
host.Trim();
currentTarget = host.GetString();
AddHostToHistory(currentTarget);
wmtrnet->ResetHops();
listMtr.DeleteAllItems();
displayRows.clear();
lastAutoRowCount = 0;
listMtr.ShowWindow(SW_HIDE);
listIsVisible = false;
firstDataResize = true;
const CString resolvingFormat = localized(IDS_STATUS_RESOLVING_HOST);
CString resolving;
resolving.Format(resolvingFormat.GetString(), currentTarget.c_str());
setStatus(resolving.GetString());
Transit(STATES::TRACING);
}
void WinMTRDialog::OnOptions()
{
if (state != STATES::IDLE) return;
WinMTROptions options(this);
options.SetInterval(interval.load());
options.SetPingSize(packetSize.load());
options.SetMaxHops(maxHops.load());
options.SetTimeoutMs(timeoutMs.load());
options.SetCycles(cycles.load());
options.SetGraceMs(graceMs.load());
options.SetMaxGlobalPps(maxGlobalPps.load());
options.SetTos(tos.load());
options.SetPattern(payloadPattern.load());
options.SetMaxLRU(historyLimit);
options.SetStartTtl(startTtl.load());
options.SetMinimumTtl(minimumTtl.load());
options.SetUnknownLimit(unknownHostLimit.load());
options.SetEcmpDisplayLimit(ecmpDisplayLimit.load());
options.SetReplyCacheSeconds(replyCacheSeconds.load());
options.SetUseDNS(resolveNames.load());
options.SetLookupAsnIsp(lookupAsnIsp.load());
options.SetDontFragment(dontFragment.load());
options.SetUseIPv4(useIPv4.load());
options.SetUseIPv6(useIPv6.load());
options.SetQueryPublicInfoOnStartup(queryPublicInfo.load());
options.SetPublicInfoRefreshMode(publicInfoRefreshMode.load());
options.SetPublicInfoRefreshMinutes(publicInfoRefreshMinutes.load());
if (options.DoModal() != IDOK) return;
const bool previouslyQuerying = queryPublicInfo.load();
const unsigned previousRefreshMode = publicInfoRefreshMode.load();
const unsigned previousRefreshMinutes = publicInfoRefreshMinutes.load();
interval = options.GetInterval();
packetSize = options.GetPingSize();
maxHops = options.GetMaxHops();
timeoutMs = options.GetTimeoutMs();
cycles = options.GetCycles();
graceMs = options.GetGraceMs();
maxGlobalPps = options.GetMaxGlobalPps();
tos = options.GetTos();
payloadPattern = options.GetPattern();
historyLimit = options.GetMaxLRU();
persistentHistoryLimit = historyLimit;
hasHistoryLimitFromCommandLine = false;
startTtl = options.GetStartTtl();
minimumTtl = options.GetMinimumTtl();
unknownHostLimit = options.GetUnknownLimit();
ecmpDisplayLimit = options.GetEcmpDisplayLimit();
replyCacheSeconds = options.GetReplyCacheSeconds();
resolveNames = options.GetUseDNS();
lookupAsnIsp = options.GetLookupAsnIsp();
dontFragment = options.GetDontFragment();
useIPv4 = options.GetUseIPv4();
useIPv6 = options.GetUseIPv6();
queryPublicInfo = options.GetQueryPublicInfoOnStartup();
publicInfoRefreshMode = options.GetPublicInfoRefreshMode();
publicInfoRefreshMinutes = options.GetPublicInfoRefreshMinutes();
SaveSettings();
auto hosts = sessionHistory;
keepNewest(hosts, historyLimit);
sessionHistory = hosts;
comboHost.ResetContent();
for (const auto& value : hosts) comboHost.AddString(value.c_str());
comboHost.AddString(localized(IDS_STRING_CLEAR_HISTORY));
historyCount = static_cast<int>(hosts.size());
persistHistory(hosts);
const bool refreshPolicyChanged = previousRefreshMode != publicInfoRefreshMode.load()
|| previousRefreshMinutes != publicInfoRefreshMinutes.load();
if ((!previouslyQuerying || refreshPolicyChanged) && queryPublicInfo.load()) startNetworkInfoQuery();
else if (previouslyQuerying && !queryPublicInfo.load()) {
networkInfoRestartPending = false;
stopNetworkInfoQuery();
{
std::scoped_lock lock(networkInfoMutex);
networkInfo = {};
}
buttonNetworkDetails.EnableWindow(FALSE);
SetDlgItemTextW(IDC_STATIC_PUBLIC_IP, localized(IDS_PUBLIC_INFO_QUERY_FAILED));
for (const int id : { IDC_STATIC_PUBLIC_HOSTNAME, IDC_STATIC_PUBLIC_COUNTRY, IDC_STATIC_PUBLIC_CITY,
IDC_STATIC_PUBLIC_ASN, IDC_STATIC_PUBLIC_ISP }) {
SetDlgItemTextW(id, localized(IDS_VALUE_UNAVAILABLE));
}
}
}