forked from leeter/WinMTR-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinMTRNetworkData.cpp
More file actions
814 lines (760 loc) · 28.7 KB
/
Copy pathWinMTRNetworkData.cpp
File metadata and controls
814 lines (760 loc) · 28.7 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
#include <VersionHelpers.h>
#include <WinDNS.h>
#include <WinHTTP.h>
#include <Iphlpapi.h>
#include "WinMTRNetworkData.h"
#include "WinMTRAddressPolicy.h"
#include "WinMTRBranding.h"
#include "WinMTRProviderJson.h"
#include <algorithm>
#include <atomic>
#include <array>
#include <chrono>
#include <condition_variable>
#include <cctype>
#include <cstdint>
#include <cwctype>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <sstream>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <utility>
#pragma comment(lib, "winhttp.lib")
#pragma comment(lib, "dnsapi.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
namespace winmtr::network_data {
namespace {
struct InternetCloser final {
void operator()(void* value) const noexcept
{
if (value != nullptr) {
WinHttpCloseHandle(value);
}
}
};
using InternetHandle = std::unique_ptr<void, InternetCloser>;
struct CancelableInternetState final {
explicit CancelableInternetState(void* initial) noexcept : value(initial) {}
~CancelableInternetState() noexcept { close(); }
void close() noexcept
{
if (void* handle = value.exchange(nullptr, std::memory_order_acq_rel)) {
WinHttpCloseHandle(handle);
}
}
std::atomic<void*> value = nullptr;
};
class CancelableInternetHandle final {
public:
explicit CancelableInternetHandle(void* value)
: state_(std::make_shared<CancelableInternetState>(value)) {}
[[nodiscard]] void* get() const noexcept
{
return state_->value.load(std::memory_order_acquire);
}
[[nodiscard]] explicit operator bool() const noexcept { return get() != nullptr; }
[[nodiscard]] std::shared_ptr<CancelableInternetState> state() const noexcept
{
return state_;
}
private:
std::shared_ptr<CancelableInternetState> state_;
};
struct ProviderHealth final {
unsigned consecutiveFailures = 0;
std::uint64_t retryAfterTick = 0;
};
std::mutex providerHealthMutex;
std::unordered_map<std::wstring, ProviderHealth> providerHealth;
[[nodiscard]] bool providerAvailable(std::wstring_view provider)
{
const auto now = GetTickCount64();
std::scoped_lock lock(providerHealthMutex);
const auto found = providerHealth.find(std::wstring(provider));
return found == providerHealth.end() || found->second.retryAfterTick == 0
|| now >= found->second.retryAfterTick;
}
void recordProviderResult(std::wstring_view provider, bool success)
{
std::scoped_lock lock(providerHealthMutex);
auto& health = providerHealth[std::wstring(provider)];
if (success) {
health = {};
return;
}
health.consecutiveFailures = std::min(health.consecutiveFailures + 1u, 10u);
const auto exponent = std::min(health.consecutiveFailures - 1u, 8u);
const auto baseDelayMs = std::min<std::uint64_t>(300'000ull, 1'000ull << exponent);
const auto jitterMs = std::hash<std::wstring_view>{}(provider) % 251u;
health.retryAfterTick = GetTickCount64() + baseDelayMs + jitterMs;
}
class QueryBudget final {
public:
QueryBudget(std::stop_token parent, std::chrono::milliseconds duration)
: forwardParent_(parent, [this] { cancellation_.request_stop(); }),
timer_([this, duration](std::stop_token timerStop) {
std::mutex mutex;
std::condition_variable_any changed;
std::unique_lock lock(mutex);
(void)changed.wait_for(lock, timerStop, duration, [] { return false; });
if (!timerStop.stop_requested()) cancellation_.request_stop();
})
{
}
[[nodiscard]] std::stop_token token() const noexcept
{
return cancellation_.get_token();
}
private:
std::stop_source cancellation_;
std::stop_callback<std::function<void()>> forwardParent_;
std::jthread timer_;
};
struct DnsRecordCloser final {
void operator()(DNS_RECORDW* value) const noexcept
{
if (value != nullptr) {
DnsRecordListFree(value, DnsFreeRecordListDeep);
}
}
};
using DnsRecordList = std::unique_ptr<DNS_RECORDW, DnsRecordCloser>;
[[nodiscard]] std::wstring utf8ToWide(std::string_view value)
{
if (value.empty()) {
return {};
}
const int required = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(),
static_cast<int>(value.size()), nullptr, 0);
if (required <= 0) {
return {};
}
std::wstring result(static_cast<size_t>(required), L'\0');
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()),
result.data(), required);
return result;
}
[[nodiscard]] std::string wideToUtf8(std::wstring_view value)
{
if (value.empty()) {
return {};
}
const int required = WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()),
nullptr, 0, nullptr, nullptr);
if (required <= 0) {
return {};
}
std::string result(static_cast<size_t>(required), '\0');
WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), result.data(), required,
nullptr, nullptr);
return result;
}
[[nodiscard]] std::wstring trim(std::wstring value)
{
const auto notSpace = [](wchar_t ch) { return std::iswspace(ch) == 0; };
value.erase(value.begin(), std::find_if(value.begin(), value.end(), notSpace));
value.erase(std::find_if(value.rbegin(), value.rend(), notSpace).base(), value.end());
return value;
}
void appendFailureReason(IpConnectionInfo& info, std::wstring_view reason)
{
if (reason.empty()) return;
if (!info.failureReason.empty()) info.failureReason.append(L"; ");
info.failureReason.append(reason);
}
[[nodiscard]] std::wstring boundedProviderText(std::wstring value, std::size_t maximum,
IpConnectionInfo& info)
{
value = trim(std::move(value));
if (value.size() <= maximum) return value;
value.resize(maximum > 0 ? maximum - 1 : 0);
if (maximum > 0) value.push_back(L'\x2026');
appendFailureReason(info, L"Provider field was truncated to a safe display limit");
return value;
}
void assignProviderValue(IpConnectionInfo& info, std::wstring& target,
const std::optional<std::string>& value, std::size_t maximum)
{
if (value) {
target = boundedProviderText(utf8ToWide(*value), maximum, info);
}
}
void addUnique(std::vector<std::wstring>& values, std::wstring value)
{
if (!value.empty() && std::find(values.begin(), values.end(), value) == values.end()) {
values.emplace_back(std::move(value));
}
}
[[nodiscard]] std::wstring join(const std::vector<std::wstring>& values, std::wstring_view separator)
{
std::wstring result;
for (const auto& value : values) {
if (!result.empty()) result.append(separator);
result.append(value);
}
return result;
}
[[nodiscard]] std::optional<std::string> httpGet(std::wstring_view host, std::wstring_view path,
std::stop_token stopToken)
{
if (stopToken.stop_requested() || !providerAvailable(host)) return std::nullopt;
const auto failed = [host]() -> std::optional<std::string> {
recordProviderResult(host, false);
return std::nullopt;
};
InternetHandle session{ WinHttpOpen(WinMTRBranding::http_user_agent.data(), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0) };
if (!session) return failed();
WinHttpSetTimeouts(session.get(), 3000, 3000, 3000, 3500);
if (!IsWindows10OrGreater()) {
DWORD protocols = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
if (!WinHttpSetOption(session.get(), WINHTTP_OPTION_SECURE_PROTOCOLS,
&protocols, sizeof(protocols))) return failed();
}
InternetHandle connection{ WinHttpConnect(session.get(), std::wstring(host).c_str(),
INTERNET_DEFAULT_HTTPS_PORT, 0) };
if (!connection || stopToken.stop_requested()) return failed();
CancelableInternetHandle request{ WinHttpOpenRequest(connection.get(), L"GET", std::wstring(path).c_str(),
nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH) };
if (!request) return failed();
std::stop_callback cancelRequest(stopToken,
[state = request.state()] { state->close(); });
if (stopToken.stop_requested()) return std::nullopt;
const wchar_t headers[] = L"Accept: application/json, text/plain;q=0.9\r\nAccept-Encoding: identity\r\n";
if (!WinHttpSendRequest(request.get(), headers, static_cast<DWORD>(-1L), WINHTTP_NO_REQUEST_DATA, 0, 0, 0) ||
!WinHttpReceiveResponse(request.get(), nullptr)) {
return stopToken.stop_requested() ? std::nullopt : failed();
}
DWORD status = 0;
DWORD statusSize = sizeof(status);
if (!WinHttpQueryHeaders(request.get(), WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize, WINHTTP_NO_HEADER_INDEX) || status < 200 || status >= 300) {
return failed();
}
std::string body;
for (;;) {
if (stopToken.stop_requested()) return std::nullopt;
DWORD available = 0;
if (!WinHttpQueryDataAvailable(request.get(), &available)) {
return stopToken.stop_requested() ? std::nullopt : failed();
}
if (available == 0) break;
const size_t oldSize = body.size();
body.resize(oldSize + available);
DWORD read = 0;
if (!WinHttpReadData(request.get(), body.data() + oldSize, available, &read)) {
return stopToken.stop_requested() ? std::nullopt : failed();
}
body.resize(oldSize + read);
if (body.size() > 1024 * 1024) return failed();
}
recordProviderResult(host, true);
return body;
}
[[nodiscard]] bool parseAddress(const std::wstring& value, SOCKADDR_INET& address) noexcept
{
address = {};
if (InetPtonW(AF_INET, value.c_str(), &address.Ipv4.sin_addr) == 1) {
address.Ipv4.sin_family = AF_INET;
return true;
}
if (InetPtonW(AF_INET6, value.c_str(), &address.Ipv6.sin6_addr) == 1) {
address.Ipv6.sin6_family = AF_INET6;
return true;
}
return false;
}
[[nodiscard]] bool expectedFamily(const std::wstring& value, ADDRESS_FAMILY family) noexcept
{
SOCKADDR_INET address{};
return parseAddress(value, address) && address.si_family == family;
}
[[nodiscard]] std::wstring reverseName(const std::wstring& value)
{
SOCKADDR_INET address{};
if (!parseAddress(value, address)) return {};
wchar_t name[NI_MAXHOST]{};
if (GetNameInfoW(reinterpret_cast<const sockaddr*>(&address),
address.si_family == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6), name,
static_cast<DWORD>(std::size(name)), nullptr, 0, NI_NAMEREQD) == 0) {
return name;
}
return {};
}
thread_local std::wstring g_geoCode;
thread_local std::wstring g_geoName;
BOOL CALLBACK enumGeoCallback(GEOID geoId)
{
wchar_t code[16]{};
if (GetGeoInfoW(geoId, GEO_ISO2, code, static_cast<int>(std::size(code)), 0) <= 0 ||
_wcsicmp(code, g_geoCode.c_str()) != 0) {
return TRUE;
}
wchar_t friendly[128]{};
if (GetGeoInfoW(geoId, GEO_FRIENDLYNAME, friendly, static_cast<int>(std::size(friendly)), 0) > 0) {
g_geoName = friendly;
}
return FALSE;
}
[[nodiscard]] std::wstring localizedCountry(std::wstring code)
{
code = trim(std::move(code));
if (code.empty()) return {};
std::transform(code.begin(), code.end(), code.begin(), [](wchar_t ch) { return std::towupper(ch); });
// Windows 7's geographic database knows ISO-3166 names and localizes the
// friendly name. Temporarily select zh-TW for this worker so an English
// Windows installation still produces the product's requested locale.
const LANGID previousUiLanguage = GetThreadUILanguage();
const LCID previousLocale = GetThreadLocale();
const LANGID traditionalChinese = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
SetThreadUILanguage(traditionalChinese);
SetThreadLocale(MAKELCID(traditionalChinese, SORT_DEFAULT));
g_geoCode = code;
g_geoName.clear();
EnumSystemGeoID(GEOCLASS_NATION, 0, enumGeoCallback);
SetThreadLocale(previousLocale);
SetThreadUILanguage(previousUiLanguage);
if (!g_geoName.empty()) return g_geoName;
return code;
}
void parseOrganization(std::wstring organization, IpConnectionInfo& info)
{
organization = boundedProviderText(std::move(organization), 512, info);
if (organization.size() > 2 && (organization[0] == L'A' || organization[0] == L'a') &&
(organization[1] == L'S' || organization[1] == L's')) {
auto separator = organization.find_first_of(L" \t");
info.asn = boundedProviderText(
organization.substr(2, separator == std::wstring::npos ? std::wstring::npos : separator - 2),
32, info);
if (separator != std::wstring::npos) {
info.isp = boundedProviderText(organization.substr(separator + 1), 512, info);
}
}
else if (!organization.empty()) {
info.isp = std::move(organization);
}
}
[[nodiscard]] IpConnectionInfo parseIpInfo(const std::string& json)
{
IpConnectionInfo info;
const auto fields = winmtr::provider_json::parse_ipinfo(json);
assignProviderValue(info, info.address, fields.address, 64);
assignProviderValue(info, info.hostname, fields.hostname, 253);
assignProviderValue(info, info.city, fields.city, 128);
assignProviderValue(info, info.region, fields.region, 128);
assignProviderValue(info, info.countryCode, fields.country_code, 8);
if (fields.organization) parseOrganization(utf8ToWide(*fields.organization), info);
return info;
}
[[nodiscard]] IpConnectionInfo parseIpApi(const std::string& json)
{
IpConnectionInfo info;
const auto fields = winmtr::provider_json::parse_ipapi(json);
assignProviderValue(info, info.address, fields.address, 64);
assignProviderValue(info, info.city, fields.city, 128);
assignProviderValue(info, info.region, fields.region, 128);
assignProviderValue(info, info.countryCode, fields.country_code, 8);
assignProviderValue(info, info.country, fields.country, 128);
assignProviderValue(info, info.asn, fields.asn, 32);
assignProviderValue(info, info.isp, fields.organization, 512);
if (info.asn.size() > 2 && _wcsnicmp(info.asn.c_str(), L"AS", 2) == 0) info.asn.erase(0, 2);
return info;
}
[[nodiscard]] std::wstring percentEncode(std::wstring_view value)
{
static constexpr char digits[] = "0123456789ABCDEF";
const auto utf8 = wideToUtf8(value);
std::wstring out;
for (const unsigned char ch : utf8) {
if (std::isalnum(ch) != 0 || ch == '-' || ch == '_' || ch == '.' || ch == '~') {
out.push_back(static_cast<wchar_t>(ch));
}
else {
out.push_back(L'%');
out.push_back(static_cast<wchar_t>(digits[ch >> 4]));
out.push_back(static_cast<wchar_t>(digits[ch & 0x0f]));
}
}
return out;
}
[[nodiscard]] std::vector<std::wstring> dnsTxt(const std::wstring& name)
{
DNS_RECORDW* rawRecords = nullptr;
if (DnsQuery_W(name.c_str(), DNS_TYPE_TEXT, DNS_QUERY_STANDARD, nullptr, &rawRecords, nullptr) != ERROR_SUCCESS) {
return {};
}
DnsRecordList records{ rawRecords };
std::vector<std::wstring> values;
for (auto record = records.get(); record != nullptr; record = record->pNext) {
if (record->wType != DNS_TYPE_TEXT || record->Data.TXT.dwStringCount == 0) continue;
std::wstring value;
for (DWORD index = 0; index < record->Data.TXT.dwStringCount; ++index) {
if (index != 0) value.push_back(L' ');
value.append(record->Data.TXT.pStringArray[index]);
}
values.emplace_back(trim(std::move(value)));
}
return values;
}
[[nodiscard]] std::wstring cymruOriginName(const std::wstring& address)
{
SOCKADDR_INET parsed{};
if (!parseAddress(address, parsed)) return {};
std::wostringstream out;
if (parsed.si_family == AF_INET) {
const auto* bytes = reinterpret_cast<const unsigned char*>(&parsed.Ipv4.sin_addr);
out << static_cast<unsigned>(bytes[3]) << L'.' << static_cast<unsigned>(bytes[2]) << L'.'
<< static_cast<unsigned>(bytes[1]) << L'.' << static_cast<unsigned>(bytes[0])
<< WinMTRBranding::sources::team_cymru_ipv4_suffix;
}
else {
const auto* bytes = reinterpret_cast<const unsigned char*>(&parsed.Ipv6.sin6_addr);
static constexpr wchar_t digits[] = L"0123456789abcdef";
for (int index = 15; index >= 0; --index) {
out << digits[bytes[index] & 0x0f] << L'.' << digits[bytes[index] >> 4] << L'.';
}
out << WinMTRBranding::sources::team_cymru_ipv6_suffix;
}
return out.str();
}
[[nodiscard]] std::pair<std::wstring, std::wstring> queryCymru(const std::wstring& address)
{
const auto providerName = WinMTRBranding::sources::team_cymru_name;
if (!providerAvailable(providerName)) return {};
const auto origin = cymruOriginName(address);
if (origin.empty()) return {};
const auto responses = dnsTxt(origin);
if (responses.empty()) {
recordProviderResult(providerName, false);
return {};
}
auto first = responses.front();
auto delimiter = first.find(L'|');
IpConnectionInfo limits;
std::wstring asn = boundedProviderText(first.substr(0, delimiter), 32, limits);
if (asn.size() > 2 && _wcsnicmp(asn.c_str(), L"AS", 2) == 0) asn.erase(0, 2);
if (asn.empty()) {
recordProviderResult(providerName, false);
return {};
}
const auto names = dnsTxt(L"AS" + asn + std::wstring(WinMTRBranding::sources::team_cymru_asn_suffix));
std::wstring provider;
if (!names.empty()) {
auto line = names.front();
auto lastDelimiter = line.rfind(L'|');
if (lastDelimiter != std::wstring::npos) {
provider = boundedProviderText(line.substr(lastDelimiter + 1), 512, limits);
}
}
recordProviderResult(providerName, true);
return { asn, provider };
}
[[nodiscard]] bool hasMetadata(const IpConnectionInfo& info) noexcept
{
return !info.hostname.empty() || !info.city.empty() || !info.region.empty()
|| !info.country.empty() || !info.countryCode.empty() || !info.asn.empty()
|| !info.isp.empty();
}
void finalize(IpConnectionInfo& info, std::stop_token stopToken, bool resolveHostname)
{
if (!info.available()) return;
if (resolveHostname && info.hostname.empty() && !stopToken.stop_requested()) {
info.hostname = reverseName(info.address);
}
if (!info.countryCode.empty()) info.country = localizedCountry(info.countryCode);
}
[[nodiscard]] IpConnectionInfo queryCurrentFamily(ADDRESS_FAMILY family, std::stop_token stopToken)
{
IpConnectionInfo selected;
const auto primaryName = family == AF_INET
? WinMTRBranding::sources::ipinfo_ipv4_name
: WinMTRBranding::sources::ipinfo_ipv6_name;
if (!stopToken.stop_requested()) {
if (const auto body = httpGet(primaryName, L"/json", stopToken)) {
auto candidate = parseIpInfo(*body);
if (expectedFamily(candidate.address, family)) {
candidate.source = primaryName;
selected = std::move(candidate);
}
}
}
// A fallback is selected only when the primary source did not return a
// usable address. Never merge fields from two services into one result.
if (!selected.available() && !stopToken.stop_requested()) {
if (const auto body = httpGet(WinMTRBranding::sources::ipapi_name, L"/json/", stopToken)) {
auto candidate = parseIpApi(*body);
if (expectedFamily(candidate.address, family)) {
candidate.source = WinMTRBranding::sources::ipapi_name;
selected = std::move(candidate);
}
}
}
if (!selected.available() && !stopToken.stop_requested()) {
const auto fallbackName = family == AF_INET
? WinMTRBranding::sources::ipify_ipv4_name
: WinMTRBranding::sources::ipify_ipv6_name;
if (const auto body = httpGet(fallbackName, L"/", stopToken)) {
const auto address = trim(utf8ToWide(*body));
if (expectedFamily(address, family)) {
selected.address = address;
selected.source = fallbackName;
}
}
}
finalize(selected, stopToken, true);
if (selected.source.empty()) {
appendFailureReason(selected, L"No provider returned a usable address for the requested family");
}
else if (selected.source != primaryName) {
appendFailureReason(selected, std::wstring(primaryName)
+ L" was unavailable or returned an unusable response");
}
return selected;
}
[[nodiscard]] std::vector<std::wstring> localDnsServers()
{
ULONG size = 16 * 1024;
std::vector<unsigned char> buffer(size);
auto addresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buffer.data());
ULONG result = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_FRIENDLY_NAME, nullptr, addresses, &size);
if (result == ERROR_BUFFER_OVERFLOW) {
buffer.resize(size);
addresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buffer.data());
result = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_FRIENDLY_NAME, nullptr, addresses, &size);
}
std::vector<std::wstring> servers;
if (result != NO_ERROR) return servers;
for (auto adapter = addresses; adapter != nullptr; adapter = adapter->Next) {
if (adapter->OperStatus != IfOperStatusUp) continue;
for (auto dns = adapter->FirstDnsServerAddress; dns != nullptr; dns = dns->Next) {
wchar_t text[INET6_ADDRSTRLEN]{};
const auto socketAddress = dns->Address.lpSockaddr;
const void* rawAddress = nullptr;
if (socketAddress->sa_family == AF_INET) rawAddress = &reinterpret_cast<sockaddr_in*>(socketAddress)->sin_addr;
else if (socketAddress->sa_family == AF_INET6) rawAddress = &reinterpret_cast<sockaddr_in6*>(socketAddress)->sin6_addr;
if (rawAddress != nullptr && InetNtopW(socketAddress->sa_family, rawAddress, text,
static_cast<DWORD>(std::size(text))) != nullptr) {
addUnique(servers, text);
}
}
}
return servers;
}
void queryWhoAmI(DnsConnectionInfo& info, std::vector<std::wstring>& sources)
{
const auto records = dnsTxt(std::wstring(WinMTRBranding::sources::akahelp_host));
if (records.empty()) {
info.ecsSupport = EcsSupport::unknown;
return;
}
info.source = WinMTRBranding::sources::akahelp_name;
addUnique(sources, info.source);
info.ecsSupport = EcsSupport::unsupported;
for (const auto& record : records) {
auto separator = record.find_first_of(L" \t");
const auto key = separator == std::wstring::npos ? record : record.substr(0, separator);
const auto value = separator == std::wstring::npos ? std::wstring{} : trim(record.substr(separator + 1));
if (_wcsicmp(key.c_str(), L"ns") == 0 && info.publicAddress.empty()) info.publicAddress = value;
else if (_wcsicmp(key.c_str(), L"ecs") == 0 && !value.empty()) {
info.ecsSubnet = value;
info.ecsSupport = EcsSupport::supported;
}
}
}
[[nodiscard]] const std::wstring& shown(const std::wstring& value)
{
static const std::wstring unavailable = std::wstring(WinMTRBranding::network_strings::unavailable);
return value.empty() ? unavailable : value;
}
} // namespace
bool isPublicAddress(const std::wstring& address) noexcept
{
SOCKADDR_INET parsed{};
if (!parseAddress(address, parsed)) return false;
if (parsed.si_family == AF_INET) {
const auto hostOrder = ntohl(parsed.Ipv4.sin_addr.S_un.S_addr);
return address_policy::is_public_ipv4(hostOrder);
}
std::array<std::uint8_t, 16> bytes{};
std::copy_n(reinterpret_cast<const std::uint8_t*>(&parsed.Ipv6.sin6_addr),
bytes.size(), bytes.begin());
return address_policy::is_public_ipv6(bytes);
}
IpConnectionInfo queryAddress(const std::wstring& address, std::stop_token stopToken,
bool resolveHostname, bool allowSecondaryHttpFallback)
{
IpConnectionInfo result;
if (!isPublicAddress(address) || stopToken.stop_requested()) return result;
SOCKADDR_INET parsed{};
(void)parseAddress(address, parsed);
const auto primaryName = parsed.si_family == AF_INET6
? WinMTRBranding::sources::ipinfo_ipv6_name
: WinMTRBranding::sources::ipinfo_ipv4_name;
if (!stopToken.stop_requested()) {
if (const auto body = httpGet(primaryName,
L"/" + percentEncode(address) + L"/json", stopToken)) {
auto candidate = parseIpInfo(*body);
if (hasMetadata(candidate)) {
candidate.address = address;
candidate.source = primaryName;
result = std::move(candidate);
}
}
}
// Keep each record internally consistent: only select a fallback after the
// ipinfo response is wholly unavailable or unusable, and never merge fields.
if (!result.available() && !stopToken.stop_requested()) {
const auto [asn, provider] = queryCymru(address);
if (!asn.empty()) {
result.address = address;
result.asn = asn;
result.isp = provider;
result.source = WinMTRBranding::sources::team_cymru_name;
}
}
if (!result.available() && allowSecondaryHttpFallback && !stopToken.stop_requested()) {
if (const auto body = httpGet(WinMTRBranding::sources::ipapi_name,
L"/" + percentEncode(address) + L"/json/", stopToken)) {
auto candidate = parseIpApi(*body);
if (hasMetadata(candidate)) {
candidate.address = address;
candidate.source = WinMTRBranding::sources::ipapi_name;
result = std::move(candidate);
}
}
}
if (!result.available() && !stopToken.stop_requested()) {
result.failureReason = allowSecondaryHttpFallback
? L"ipinfo and all configured fallback providers returned no usable metadata"
: L"ipinfo and Team Cymru returned no usable metadata; secondary HTTPS fallback is disabled";
}
finalize(result, stopToken, resolveHostname);
return result;
}
CurrentNetworkInfo queryCurrent(std::stop_token stopToken)
{
CurrentNetworkInfo result;
QueryBudget budget(stopToken, std::chrono::seconds(8));
const auto queryToken = budget.token();
WSADATA wsaData{};
const bool wsaStarted = WSAStartup(MAKEWORD(2, 2), &wsaData) == 0;
struct WsaCleanup final {
bool active;
~WsaCleanup() { if (active) WSACleanup(); }
} cleanup{ wsaStarted };
std::jthread ipv4Query([&] {
try { result.ipv4 = queryCurrentFamily(AF_INET, queryToken); }
catch (...) { result.ipv4.failureReason = L"IPv4 provider query failed"; }
});
std::jthread ipv6Query([&] {
try { result.ipv6 = queryCurrentFamily(AF_INET6, queryToken); }
catch (...) { result.ipv6.failureReason = L"IPv6 provider query failed"; }
});
if (!queryToken.stop_requested()) {
queryWhoAmI(result.dns, result.successfulSources);
result.dns.localServers = localDnsServers();
if (isPublicAddress(result.dns.publicAddress)) {
auto dnsMetadata = queryAddress(result.dns.publicAddress, queryToken, true, true);
result.dns.hostname = dnsMetadata.hostname;
result.dns.city = dnsMetadata.city;
result.dns.region = dnsMetadata.region;
result.dns.country = dnsMetadata.country;
result.dns.countryCode = dnsMetadata.countryCode;
result.dns.asn = dnsMetadata.asn;
result.dns.provider = dnsMetadata.isp;
result.dns.metadataSource = dnsMetadata.source;
result.dns.failureReason = dnsMetadata.failureReason;
if (!dnsMetadata.source.empty()) addUnique(result.successfulSources, dnsMetadata.source);
}
}
if (ipv4Query.joinable()) ipv4Query.join();
if (ipv6Query.joinable()) ipv6Query.join();
if (!result.ipv4.source.empty()) addUnique(result.successfulSources, result.ipv4.source);
if (!result.ipv6.source.empty()) addUnique(result.successfulSources, result.ipv6.source);
result.updatedAtUnixMs = static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count());
result.timedOut = queryToken.stop_requested() && !stopToken.stop_requested();
if (!result.anyAvailable()) {
result.refreshError = result.timedOut
? L"The public network information query exceeded its 8-second budget"
: L"No configured provider returned usable network information";
}
result.complete = true;
return result;
}
std::wstring formatDetails(const CurrentNetworkInfo& info)
{
using namespace WinMTRBranding::network_strings;
std::wostringstream out;
const auto field = [&](std::wstring_view label, const std::wstring& value) {
out << label << L'\t' << shown(value) << L"\r\n";
};
const auto section = [&](std::wstring_view title, const IpConnectionInfo& value) {
out << title << L"\r\n";
field(address, value.address);
field(hostname, value.hostname);
field(city, value.city);
field(region, value.region);
field(country, value.country);
field(asn, value.asn);
field(provider, value.isp);
field(sources, value.source);
if (!value.failureReason.empty()) field(L"失敗原因", value.failureReason);
};
if (info.updatedAtUnixMs != 0) {
field(L"Last updated (Unix ms)", std::to_wstring(info.updatedAtUnixMs));
}
if (info.refreshing) field(L"狀態", L"更新中(顯示上次成功資料)");
else if (info.stale) field(L"狀態", L"更新失敗(顯示上次成功資料)");
if (!info.refreshError.empty()) field(L"更新錯誤", info.refreshError);
if (info.updatedAtUnixMs != 0 || info.refreshing || info.stale
|| !info.refreshError.empty()) out << L"\r\n";
section(ipv4_section, info.ipv4);
out << L"\r\n";
section(ipv6_section, info.ipv6);
out << L"\r\n" << recursive_dns_section << L"\r\n";
field(address, info.dns.publicAddress);
field(hostname, info.dns.hostname);
field(city, info.dns.city);
field(region, info.dns.region);
field(country, info.dns.country);
field(asn, info.dns.asn);
field(provider, info.dns.provider);
field(sources, info.dns.source);
if (!info.dns.metadataSource.empty()) field(L"Metadata source", info.dns.metadataSource);
if (!info.dns.failureReason.empty()) field(L"失敗原因", info.dns.failureReason);
std::wstring ecsValue;
switch (info.dns.ecsSupport) {
case EcsSupport::supported:
ecsValue = ecs_supported;
if (!info.dns.ecsSubnet.empty()) ecsValue.append(L"(").append(info.dns.ecsSubnet).append(L")");
break;
case EcsSupport::unsupported: ecsValue = ecs_unsupported; break;
default: ecsValue = ecs_unknown; break;
}
field(ecs, ecsValue);
out << L"\r\n" << local_dns << L"\r\n";
if (info.dns.localServers.empty()) out << L'\t' << unavailable << L"\r\n";
else {
for (const auto& server : info.dns.localServers) out << L'\t' << server << L"\r\n";
}
out << L"\r\n";
field(sources, join(info.successfulSources, L"、"));
return out.str();
}
} // namespace winmtr::network_data