-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathremote_web.cpp
More file actions
406 lines (374 loc) · 12.9 KB
/
Copy pathremote_web.cpp
File metadata and controls
406 lines (374 loc) · 12.9 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
#include "remote_web.hpp"
#include "../utils/url_encoding.hpp"
#include <algorithm>
#include <array>
#include <cctype>
#include <cstring>
#include <unordered_set>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h>
#else
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
namespace acecode::web {
namespace {
std::string trim_ascii(std::string_view value) {
while (!value.empty() &&
std::isspace(static_cast<unsigned char>(value.front()))) {
value.remove_prefix(1);
}
while (!value.empty() &&
std::isspace(static_cast<unsigned char>(value.back()))) {
value.remove_suffix(1);
}
return std::string(value);
}
std::string ascii_lower_copy(std::string value) {
std::transform(
value.begin(),
value.end(),
value.begin(),
[](unsigned char ch) { return static_cast<char>(std::tolower(ch)); });
return value;
}
std::string strip_ipv6_brackets(std::string value) {
if (value.size() >= 2 && value.front() == '[' && value.back() == ']') {
return value.substr(1, value.size() - 2);
}
return value;
}
bool parse_ipv4(std::string_view host, std::array<unsigned char, 4>& bytes) {
in_addr address{};
const std::string text(host);
#ifdef _WIN32
if (InetPtonA(AF_INET, text.c_str(), &address) != 1) return false;
#else
if (inet_pton(AF_INET, text.c_str(), &address) != 1) return false;
#endif
std::memcpy(bytes.data(), &address, bytes.size());
return true;
}
bool parse_ipv6(std::string_view host, std::array<unsigned char, 16>& bytes) {
in6_addr address{};
const std::string text(host);
#ifdef _WIN32
if (InetPtonA(AF_INET6, text.c_str(), &address) != 1) return false;
#else
if (inet_pton(AF_INET6, text.c_str(), &address) != 1) return false;
#endif
std::memcpy(bytes.data(), &address, bytes.size());
return true;
}
bool viable_ipv4(const std::array<unsigned char, 4>& bytes) {
if (bytes[0] == 0 || bytes[0] == 127) return false;
if (bytes[0] == 169 && bytes[1] == 254) return false;
if (bytes[0] >= 224) return false;
return true;
}
bool viable_ipv6(const std::array<unsigned char, 16>& bytes) {
const bool unspecified = std::all_of(
bytes.begin(),
bytes.end(),
[](unsigned char byte) { return byte == 0; });
if (unspecified) return false;
bool loopback = true;
for (std::size_t i = 0; i < bytes.size() - 1; ++i) {
loopback = loopback && bytes[i] == 0;
}
if (loopback && bytes.back() == 1) return false;
// fe80::/10 is link-local and cannot be copied without an interface scope.
if (bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80) return false;
if (bytes[0] == 0xff) return false; // multicast
// Reject IPv4-mapped versions of destinations filtered above.
bool ipv4_mapped = true;
for (std::size_t i = 0; i < 10; ++i) {
ipv4_mapped = ipv4_mapped && bytes[i] == 0;
}
ipv4_mapped =
ipv4_mapped && bytes[10] == 0xff && bytes[11] == 0xff;
if (ipv4_mapped) {
std::array<unsigned char, 4> mapped{};
std::copy(bytes.begin() + 12, bytes.end(), mapped.begin());
return viable_ipv4(mapped);
}
return true;
}
bool viable_ip_host(std::string_view raw_host) {
const std::string host = strip_ipv6_brackets(trim_ascii(raw_host));
std::array<unsigned char, 4> ipv4{};
if (parse_ipv4(host, ipv4)) return viable_ipv4(ipv4);
std::array<unsigned char, 16> ipv6{};
if (parse_ipv6(host, ipv6)) return viable_ipv6(ipv6);
return false;
}
bool safe_hostname(std::string_view host) {
if (host.empty() || host.size() > 253) return false;
if (host.front() == '.' || host.back() == '.') return false;
bool label_has_character = false;
bool previous_hyphen = false;
std::size_t label_length = 0;
for (unsigned char ch : host) {
if (ch == '.') {
if (!label_has_character || previous_hyphen) return false;
label_has_character = false;
previous_hyphen = false;
label_length = 0;
continue;
}
if (!(std::isalnum(ch) || ch == '-')) return false;
++label_length;
if (label_length > 63) return false;
if (!label_has_character && ch == '-') return false;
label_has_character = true;
previous_hyphen = ch == '-';
}
return label_has_character && !previous_hyphen;
}
bool viable_destination_host(std::string_view raw_host) {
const std::string host = strip_ipv6_brackets(trim_ascii(raw_host));
std::array<unsigned char, 4> ipv4{};
if (parse_ipv4(host, ipv4)) return viable_ipv4(ipv4);
std::array<unsigned char, 16> ipv6{};
if (parse_ipv6(host, ipv6)) return viable_ipv6(ipv6);
const std::string lower = ascii_lower_copy(host);
return safe_hostname(lower) &&
lower != "localhost" &&
lower != "localhost.localdomain";
}
bool host_matches_listener_bind(
std::string_view raw_host,
std::string_view raw_bind,
bool allow_hostname) {
const std::string host = strip_ipv6_brackets(trim_ascii(raw_host));
const std::string bind = strip_ipv6_brackets(trim_ascii(raw_bind));
if (bind.empty()) return true;
std::array<unsigned char, 4> bind_ipv4{};
if (parse_ipv4(bind, bind_ipv4)) {
std::array<unsigned char, 4> host_ipv4{};
if (parse_ipv4(host, host_ipv4)) {
const bool wildcard = std::all_of(
bind_ipv4.begin(),
bind_ipv4.end(),
[](unsigned char byte) { return byte == 0; });
return wildcard || host_ipv4 == bind_ipv4;
}
std::array<unsigned char, 16> host_ipv6{};
if (parse_ipv6(host, host_ipv6)) return false;
return allow_hostname;
}
std::array<unsigned char, 16> bind_ipv6{};
if (parse_ipv6(bind, bind_ipv6)) {
std::array<unsigned char, 16> host_ipv6{};
if (parse_ipv6(host, host_ipv6)) {
const bool wildcard = std::all_of(
bind_ipv6.begin(),
bind_ipv6.end(),
[](unsigned char byte) { return byte == 0; });
return wildcard || host_ipv6 == bind_ipv6;
}
std::array<unsigned char, 4> host_ipv4{};
if (parse_ipv4(host, host_ipv4)) return false;
return allow_hostname;
}
return allow_hostname;
}
std::optional<std::string> numeric_host(const sockaddr* address) {
if (!address) return std::nullopt;
char buffer[INET6_ADDRSTRLEN] = {};
if (address->sa_family == AF_INET) {
const auto* ipv4 = reinterpret_cast<const sockaddr_in*>(address);
#ifdef _WIN32
if (!InetNtopA(AF_INET, &ipv4->sin_addr, buffer, sizeof(buffer))) {
#else
if (!inet_ntop(AF_INET, &ipv4->sin_addr, buffer, sizeof(buffer))) {
#endif
return std::nullopt;
}
} else if (address->sa_family == AF_INET6) {
const auto* ipv6 = reinterpret_cast<const sockaddr_in6*>(address);
#ifdef _WIN32
if (!InetNtopA(AF_INET6, &ipv6->sin6_addr, buffer, sizeof(buffer))) {
#else
if (!inet_ntop(AF_INET6, &ipv6->sin6_addr, buffer, sizeof(buffer))) {
#endif
return std::nullopt;
}
} else {
return std::nullopt;
}
return std::string(buffer);
}
} // namespace
std::optional<std::string> remote_web_host_from_header(
std::string_view host_header) {
std::string host = trim_ascii(host_header);
if (host.empty()) return std::nullopt;
if (host.front() == '[') {
const std::size_t close = host.find(']');
if (close == std::string::npos) return std::nullopt;
const std::string suffix = host.substr(close + 1);
if (!suffix.empty()) {
if (suffix.front() != ':' || suffix.size() == 1) {
return std::nullopt;
}
if (!std::all_of(
suffix.begin() + 1,
suffix.end(),
[](unsigned char ch) { return std::isdigit(ch) != 0; })) {
return std::nullopt;
}
}
host = host.substr(1, close - 1);
} else {
const std::size_t first_colon = host.find(':');
const std::size_t last_colon = host.rfind(':');
if (first_colon != std::string::npos && first_colon == last_colon) {
const std::string port = host.substr(first_colon + 1);
if (port.empty() ||
!std::all_of(
port.begin(),
port.end(),
[](unsigned char ch) { return std::isdigit(ch) != 0; })) {
return std::nullopt;
}
host.resize(first_colon);
}
}
host = trim_ascii(host);
if (!viable_destination_host(host)) return std::nullopt;
return ascii_lower_copy(host);
}
std::vector<std::string> rank_remote_web_hosts(
const std::vector<std::string>& discovered_hosts,
const std::optional<std::string>& preferred_host,
std::string_view listener_bind,
const std::optional<std::string>& computer_name) {
std::vector<std::string> result;
std::unordered_set<std::string> seen;
const auto append = [&](const std::string& raw, bool allow_hostname) {
std::string host = strip_ipv6_brackets(trim_ascii(raw));
const bool viable =
viable_ip_host(host) ||
(allow_hostname && viable_destination_host(host));
if (!viable ||
!host_matches_listener_bind(
host,
listener_bind,
allow_hostname)) {
return;
}
const std::string key = ascii_lower_copy(host);
if (seen.insert(key).second) result.push_back(std::move(host));
};
if (computer_name) append(*computer_name, true);
if (preferred_host) append(*preferred_host, true);
for (const auto& host : discovered_hosts) append(host, false);
return result;
}
std::vector<std::string> discover_remote_web_hosts() {
std::vector<std::string> discovered;
#ifdef _WIN32
ULONG buffer_size = 16 * 1024;
std::vector<unsigned char> buffer(buffer_size);
auto* addresses =
reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buffer.data());
constexpr ULONG flags =
GAA_FLAG_SKIP_ANYCAST |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER;
ULONG status = GetAdaptersAddresses(
AF_UNSPEC,
flags,
nullptr,
addresses,
&buffer_size);
if (status == ERROR_BUFFER_OVERFLOW) {
buffer.resize(buffer_size);
addresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buffer.data());
status = GetAdaptersAddresses(
AF_UNSPEC,
flags,
nullptr,
addresses,
&buffer_size);
}
if (status == NO_ERROR) {
for (auto* adapter = addresses;
adapter;
adapter = adapter->Next) {
if (adapter->OperStatus != IfOperStatusUp) continue;
for (auto* unicast = adapter->FirstUnicastAddress;
unicast;
unicast = unicast->Next) {
const auto host = numeric_host(unicast->Address.lpSockaddr);
if (host) discovered.push_back(*host);
}
}
}
#else
ifaddrs* interfaces = nullptr;
if (getifaddrs(&interfaces) == 0) {
for (const ifaddrs* current = interfaces;
current;
current = current->ifa_next) {
if (!current->ifa_addr ||
!(current->ifa_flags & IFF_UP)) {
continue;
}
const auto host = numeric_host(current->ifa_addr);
if (host) discovered.push_back(*host);
}
freeifaddrs(interfaces);
}
#endif
return rank_remote_web_hosts(discovered);
}
std::optional<std::string> discover_remote_web_computer_name() {
std::array<char, 256> buffer{};
#ifdef _WIN32
DWORD size = static_cast<DWORD>(buffer.size());
if (!GetComputerNameExA(
ComputerNameDnsHostname,
buffer.data(),
&size)) {
return std::nullopt;
}
std::string name(buffer.data(), size);
#else
if (gethostname(buffer.data(), buffer.size() - 1) != 0) {
return std::nullopt;
}
buffer.back() = '\0';
std::string name(buffer.data());
#endif
name = trim_ascii(name);
if (!viable_destination_host(name)) return std::nullopt;
return name;
}
std::string build_remote_web_url(
std::string_view raw_host,
int port,
std::string_view token) {
const std::string host = strip_ipv6_brackets(trim_ascii(raw_host));
const bool ipv6 = host.find(':') != std::string::npos;
std::string url = "http://";
if (ipv6) url += '[';
url += host;
if (ipv6) url += ']';
url += ':' + std::to_string(port);
url += "/?token=" +
acecode::utils::percent_encode_query_component(token);
return url;
}
} // namespace acecode::web