-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcli_preprocessor.cpp
More file actions
718 lines (656 loc) · 25.9 KB
/
Copy pathcli_preprocessor.cpp
File metadata and controls
718 lines (656 loc) · 25.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
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
#include "cli_preprocessor.hpp"
#include "dns/dns_resolver.hpp"
#include "http/resolver.hpp"
#include "utils/exceptions.hpp"
#include "utils/random_utils.hpp"
#include "utils/string_utils.hpp"
#include <algorithm>
#include <boost/asio/io_context.hpp>
#include <boost/asio/thread_pool.hpp>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <set>
#include <spdlog/spdlog.h>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
// defined (and assigned to) in main.cpp
extern bool silent;
extern bool compare_cl;
namespace dooked {
namespace net = boost::asio;
using namespace fmt::v7::literals;
namespace {
std::string history_timestamp(std::time_t const timestamp) {
std::string output{};
if (timet_to_string(output, static_cast<std::size_t>(timestamp),
"%Y-%m-%d %H:%M:%S")) {
return output;
}
return {};
}
std::string normalize_history_key(std::string value) {
std::transform(value.begin(), value.end(), value.begin(),
[](unsigned char c) { return std::tolower(c); });
return value;
}
std::string history_key(std::string const &domain_name,
dns_record_type_e const record_type,
std::string const &rdata) {
return "{}\x1f{}\x1f{}"_format(normalize_history_key(domain_name),
static_cast<int>(record_type),
normalize_history_key(rdata));
}
std::string history_key(json_data_t const &record) {
return history_key(record.domain_name, record.type, record.rdata);
}
std::string history_key(std::string const &domain_name,
probe_result_t const &record) {
return history_key(domain_name, record.type, record.rdata);
}
std::optional<std::time_t> parse_timestamp(std::string const &input,
char const *format) {
std::tm parsed{};
parsed.tm_isdst = -1;
std::istringstream stream{input};
stream >> std::get_time(&parsed, format);
if (stream.fail()) {
return std::nullopt;
}
auto const timestamp = std::mktime(&parsed);
if (timestamp == static_cast<std::time_t>(-1)) {
return std::nullopt;
}
return timestamp;
}
std::optional<std::time_t> parse_history_timestamp(std::string const &input) {
if (input.empty()) {
return std::nullopt;
}
if (auto const parsed = parse_timestamp(input, "%Y-%m-%d %H:%M:%S")) {
return parsed;
}
return parse_timestamp(input, "%Y-%m-%d");
}
std::optional<std::time_t> parse_us_timestamp(std::string const &input) {
if (auto const parsed = parse_timestamp(input, "%m/%d/%Y %H:%M:%S")) {
return parsed;
}
if (auto const parsed = parse_timestamp(input, "%m/%d/%Y %H:%M")) {
return parsed;
}
return parse_timestamp(input, "%m/%d/%Y");
}
probe_result_t previous_to_probe_result(json_data_t const &previous,
std::string const &fallback_time) {
probe_result_t result{};
result.rdata = previous.rdata;
result.first_seen = previous.first_seen;
result.last_seen = previous.last_seen;
result.seen = previous.seen;
result.type = previous.type;
result.ttl = static_cast<std::uint32_t>(previous.ttl);
if (result.last_seen.empty()) {
result.last_seen = fallback_time;
}
if (result.first_seen.empty()) {
result.first_seen = result.last_seen;
}
if (result.seen <= 0) {
result.seen = 1;
}
return result;
}
bool should_report_last_seen(json_data_t const &record,
runtime_args_t const &rt_args) {
if (!rt_args.last_seen_before) {
return false;
}
auto const last_seen = parse_history_timestamp(record.last_seen);
return !last_seen || *last_seen <= *rt_args.last_seen_before;
}
void report_first_seen(runtime_args_t const &rt_args, std::string const &domain,
probe_result_t const &record) {
if (rt_args.report_first_seen) {
spdlog::info("[FIRST-SEEN][{}][{}] `{}`", domain,
dns_record_type_to_str(record.type), record.rdata);
}
}
void report_last_seen(runtime_args_t const &rt_args, json_data_t const &record) {
if (should_report_last_seen(record, rt_args)) {
auto const when = record.last_seen.empty() ? "unknown" : record.last_seen;
spdlog::info("[LAST-SEEN][{}][{}] `{}` last seen {}", record.domain_name,
dns_record_type_to_str(record.type), record.rdata, when);
}
}
void merge_history(std::vector<json_data_t> const *previous_result,
map_container_t<probe_result_t> ¤t_result,
runtime_args_t const &rt_args, std::time_t const now) {
auto const timestamp = history_timestamp(now);
auto ¤t_data_map = current_result.result();
std::unordered_map<std::string, json_data_t const *> previous_by_key{};
std::unordered_set<std::string> current_keys{};
if (previous_result) {
previous_by_key.reserve(previous_result->size());
for (auto const &record : *previous_result) {
previous_by_key.emplace(history_key(record), &record);
}
}
for (auto &[domain_name, domain_info] : current_data_map) {
for (auto &record : domain_info.dns_result_list_) {
auto const key = history_key(domain_name, record);
current_keys.insert(key);
auto const previous_iter = previous_by_key.find(key);
if (previous_iter != previous_by_key.end()) {
auto const &previous = *previous_iter->second;
record.first_seen = previous.first_seen.empty()
? (previous.last_seen.empty()
? timestamp
: previous.last_seen)
: previous.first_seen;
record.seen = previous.seen > 0 ? previous.seen + 1 : 2;
} else {
record.first_seen = timestamp;
record.seen = 1;
report_first_seen(rt_args, domain_name, record);
}
record.last_seen = timestamp;
}
}
if (!previous_result) {
return;
}
for (auto const &previous : *previous_result) {
auto const key = history_key(previous);
if (current_keys.find(key) != current_keys.end()) {
continue;
}
report_last_seen(rt_args, previous);
auto &domain_info = current_data_map[previous.domain_name];
if (domain_info.http_result_.http_status_ == 0) {
domain_info.http_result_.content_length_ = previous.content_length;
domain_info.http_result_.http_status_ = previous.http_code;
}
domain_info.dns_result_list_.push_back(
previous_to_probe_result(previous, timestamp));
current_keys.insert(key);
}
}
std::optional<std::time_t> resolve_last_seen_cutoff(cli_args_t const &cli_args) {
if (!cli_args.last_seen_date.empty()) {
return parse_us_timestamp(cli_args.last_seen_date);
}
if (cli_args.last_seen_days >= 0) {
return std::time(nullptr) -
static_cast<std::time_t>(cli_args.last_seen_days) * 24 * 60 * 60;
}
return std::nullopt;
}
void sort_dns_results(map_container_t<probe_result_t> &result_map) {
for (auto &result_pair : result_map.result()) {
std::sort(result_pair.second.dns_result_list_.begin(),
result_pair.second.dns_result_list_.end(),
[](auto const &a, auto const &b) {
return std::tie(a.type, a.rdata) < std::tie(b.type, b.rdata);
});
}
}
} // namespace
void compare_http_result(int const base_cl, json_data_t const &prev_http_result,
http_response_t const ¤t_result) {
auto const current_req_cl = current_result.content_length_;
auto const current_req_http_code = current_result.http_status_;
auto const previous_req_cl = prev_http_result.content_length;
auto const previous_req_http_code = prev_http_result.http_code;
if (compare_cl) {
// check if content-length's changed
bool const baseline_specified = base_cl != -1;
if ((baseline_specified && (current_req_cl > base_cl)) ||
((!baseline_specified) && current_req_cl != previous_req_cl)) {
spdlog::info("[CHANGED][CONTENT-LENGTH][{}] from `{}` to `{}`",
prev_http_result.domain_name, previous_req_cl,
current_req_cl);
}
}
// the HTTP code has changed
if (previous_req_http_code == 200 && current_req_http_code != 200) {
if (current_req_http_code ==
(int)response_type_e::cannot_resolve_name) { // special case
spdlog::info("[CHANGED][HTTP CODE][{}] used to resolve but now doesn't",
prev_http_result.domain_name);
} else {
spdlog::info("[CHANGED][HTTP CODE][{}] from `{}` to `{}`",
prev_http_result.domain_name,
code_string(previous_req_http_code),
code_string(current_req_http_code));
}
}
}
[[nodiscard]] std::vector<json_data_t>::const_iterator compare_dns_result(
std::vector<json_data_t>::const_iterator iter,
std::vector<json_data_t>::const_iterator end_iter,
http_dns_response_t<probe_result_t> const ¤t_domain_info,
int const base_content_length,
jd_domain_comparator_t const &domain_comparator) {
auto const last_elem_iter =
std::upper_bound(iter, end_iter, *iter, domain_comparator);
auto const previous_total_elem =
(std::size_t)std::distance(iter, last_elem_iter);
auto const ¤t_domain_info_list = current_domain_info.dns_result_list_;
auto const current_total_elem = current_domain_info_list.size();
// something is missing
if (current_total_elem < previous_total_elem) {
for (auto start_iter = iter; start_iter != last_elem_iter; ++start_iter) {
bool const found = std::binary_search(
current_domain_info_list.cbegin(), current_domain_info_list.cend(),
*start_iter, [](auto const &a, auto const &b) {
return a.type == b.type &&
case_insensitive_compare(a.rdata, b.rdata);
});
if (!found) {
spdlog::error("[MISSING][{}][{}] `{}`", iter->domain_name,
dns_record_type_to_str(start_iter->type),
start_iter->rdata);
}
}
// information may have been changed
} else if (current_total_elem == previous_total_elem) {
auto record_type = dns_record_type_e::DNS_REC_UNDEFINED;
for (auto start_iter = iter; start_iter != last_elem_iter; ++start_iter) {
// find records of the same type: A, AAAA, MX, NS...
auto const eq_range = std::equal_range(
current_domain_info_list.cbegin(), current_domain_info_list.cend(),
*start_iter,
[](auto const &a, auto const &b) { return a.type < b.type; });
// see if the data is the same.
auto const find_iter = std::find_if(
eq_range.first, eq_range.second,
[&val = *start_iter](auto const &record) {
return case_insensitive_compare(val.rdata, record.rdata);
});
// if we cannot find it
if (find_iter == eq_range.second) {
auto const distance = std::distance(eq_range.first, eq_range.second);
if (distance == 0) {
spdlog::error("[REMOVED][{}][{}] `{}`", iter->domain_name,
dns_record_type_to_str(start_iter->type),
start_iter->rdata);
} else if (distance == 1) {
spdlog::info("[CHANGED][{}][{}] from `{}` to `{}`", iter->domain_name,
dns_record_type_to_str(start_iter->type),
start_iter->rdata, eq_range.first->rdata);
} else {
if (record_type != iter->type) {
record_type = iter->type;
for (auto current_range = eq_range.first;
current_range != eq_range.second; ++current_range) {
spdlog::info("[NEW][{}][{}] `{}`", iter->domain_name,
dns_record_type_to_str(current_range->type),
current_range->rdata);
}
}
}
}
}
} else {
// new information has been added
for (auto const ¤t_elem : current_domain_info_list) {
bool const found = std::binary_search(
iter, last_elem_iter, current_elem, [](auto const &a, auto const &b) {
return a.type == b.type &&
case_insensitive_compare(a.rdata, b.rdata);
});
if (!found) {
spdlog::info("[NEW][{}][{}] `{}`", iter->domain_name,
dns_record_type_to_str(current_elem.type),
current_elem.rdata);
}
}
}
compare_http_result(base_content_length, *iter,
current_domain_info.http_result_);
return last_elem_iter;
}
void compare_results(std::vector<json_data_t> const &previous_result,
map_container_t<probe_result_t> const ¤t_result,
int const content_length) {
if (!silent) {
spdlog::info("Trying to compare old with new result");
}
// previous data is already sorted.
// current data is a pre-sorted data.
auto const ¤t_data_map = current_result.cresult();
auto const end_iter = previous_result.cend();
auto const domain_comparator = jd_domain_comparator_t{};
// %^ designate color start,
// %$ designates the end of color,
// %v is the message we want to log.
spdlog::set_pattern("[%^CHECK%$] %v");
for (auto iter = previous_result.cbegin(); iter < end_iter;) {
auto const current_find_iter = current_data_map.find(iter->domain_name);
if (current_find_iter == current_data_map.end()) {
spdlog::error("{} not found in new result", iter->domain_name);
// find the next domain name following this current domain
iter = std::upper_bound(iter, end_iter, *iter, domain_comparator);
continue;
}
auto const ¤t_domain_info = current_find_iter->second;
auto next_iter = compare_dns_result(iter, end_iter, current_domain_info,
content_length, domain_comparator);
iter = next_iter;
}
}
void dns_functor(net::io_context &io_context, net::ssl::context *ssl_context,
runtime_args_t &rt_args,
map_container_t<probe_result_t> &result_map,
std::size_t const socket_count, bool const deferring) {
std::vector<std::unique_ptr<custom_resolver_socket_t>> sockets{};
sockets.resize(socket_count);
for (std::size_t i = 0; i < socket_count; ++i) {
sockets[i] = std::make_unique<custom_resolver_socket_t>(
io_context, ssl_context, *rt_args.names, *rt_args.resolvers,
result_map);
sockets[i]->defer_http_request(deferring);
sockets[i]->start();
}
io_context.run();
}
void http_functor(net::io_context &io_context, net::ssl::context *ssl_context,
runtime_args_t &rt_args,
map_container_t<probe_result_t> &result_map,
std::size_t const socket_count) {
std::vector<std::unique_ptr<http_resolver_t>> sockets{};
sockets.resize(socket_count);
for (std::size_t i = 0; i < socket_count; ++i) {
sockets[i] = std::make_unique<http_resolver_t>(io_context, ssl_context,
*rt_args.names, result_map);
sockets[i]->start();
}
io_context.run();
}
bool process_text_file(std::string const &input_filename,
runtime_args_t &rt_args) {
auto domain_names = get_names<std::string>(input_filename);
if (domain_names && !domain_names->empty()) {
rt_args.names.emplace();
for (auto const &domain_name : *domain_names) {
rt_args.names->push_back({domain_name});
}
} else {
spdlog::error("There was an error trying to get input file");
return false;
}
return true;
}
bool process_json_file(std::string const &input_filename,
runtime_args_t &rt_args) {
rt_args.previous_data =
get_names<json_data_t>(input_filename, file_type_e::json_type);
auto &previous_records = rt_args.previous_data;
if (previous_records && !previous_records->empty()) {
std::set<std::string> unique_names{};
for (auto const &domain_name : *previous_records) {
unique_names.insert(domain_name.domain_name);
}
rt_args.names.emplace();
for (auto const &name : unique_names) {
rt_args.names->push_back(name);
}
} else {
spdlog::error("There was an error trying to get input file");
return false;
}
return true;
}
// most likely from stdin
bool determine_unknown_file(cli_args_t const &cli_args,
runtime_args_t &rt_args) {
bool const using_stdin = cli_args.input_filename.empty();
if (!using_stdin) {
return false;
}
auto const filename = std::filesystem::temp_directory_path() /
get_random_string(get_random_integer());
std::ofstream temp_file{filename};
if (!temp_file) {
spdlog::error("unable to open temporary file");
return false;
}
std::string line{};
while (std::getline(std::cin, line)) {
temp_file << line;
}
auto const file_type = get_file_type(filename);
std::error_code ec{};
// read the file and remove the temporary file thereafter
if (is_text_file(file_type)) {
return (process_text_file(filename.string(), rt_args) &&
std::filesystem::remove(filename, ec));
} else if (is_json_file(file_type)) {
return process_json_file(filename.string(), rt_args) &&
std::filesystem::remove(filename, ec);
}
return false;
}
bool read_input_file(cli_args_t const &cli_args, runtime_args_t &rt_args) {
bool const using_stdin = cli_args.input_filename.empty();
if (using_stdin) {
auto const file_type = static_cast<file_type_e>(cli_args.file_type);
if (file_type == file_type_e::txt_type) {
return process_text_file(cli_args.input_filename, rt_args);
} else if (file_type == file_type_e::json_type) {
return process_json_file(cli_args.input_filename, rt_args);
}
return determine_unknown_file(cli_args, rt_args);
}
auto const file_type = get_file_type(cli_args.input_filename);
if (is_text_file(file_type)) {
return process_text_file(cli_args.input_filename, rt_args);
} else if (is_json_file(file_type)) {
return process_json_file(cli_args.input_filename, rt_args);
}
return false;
}
void start_name_checking(runtime_args_t &&rt_args) {
std::size_t const user_specified_thread =
(rt_args.thread_count > 0) ? (std::size_t)rt_args.thread_count
: DOOKED_SUPPORTED_THREADS;
auto const thread_count =
(std::min)(rt_args.names->size(), user_specified_thread);
auto const max_open_sockets =
(std::min)(rt_args.names->size(), (thread_count * 2));
// minimum of 1 socket per thread
auto const sockets_per_thread =
(std::max)(1, (int)(max_open_sockets / thread_count));
if (!silent) {
spdlog::info("Native thread count: {}", thread_count);
spdlog::info("Sockets per thread: {}", sockets_per_thread);
spdlog::info("Total input: {}", rt_args.names->size());
}
bool const using_lock = (thread_count > 1);
map_container_t<probe_result_t> result_map(using_lock);
bool const deferring = rt_args.http_request_time_ == http_process_e::deferred;
// by default, we use tls v1.2, and only switch to 1.3 if 1.2 fails
net::ssl::context ssl_context(net::ssl::context::tlsv12_client);
ssl_context.set_default_verify_paths();
ssl_context.set_verify_mode(net::ssl::verify_none);
ssl_context.set_options(net::ssl::context::default_workarounds |
net::ssl::context::no_sslv2 |
net::ssl::context::no_sslv3);
decltype(rt_args.names) deferred_names_;
if (deferring) { // copy the names before dns probing
deferred_names_.emplace(rt_args.names->clone());
}
net::io_context io_context((int)thread_count);
std::optional<net::thread_pool> thread_pool(std::in_place, thread_count);
{ // perform DNS record probing.
for (std::size_t index = 0; index < thread_count; ++index) {
net::post(*thread_pool, [&] {
dns_functor(io_context, &ssl_context, rt_args, result_map,
sockets_per_thread, deferring);
});
}
thread_pool->join();
}
// if we deferred HTTP/S "probe", now is the time to get to it
if (deferring) {
io_context.reset();
thread_pool.emplace(thread_count);
rt_args.names.emplace(std::move(*deferred_names_));
for (std::size_t index = 0; index < thread_count; ++index) {
net::post(*thread_pool, [&] {
http_functor(io_context, &ssl_context, rt_args, result_map,
sockets_per_thread);
});
}
thread_pool->join();
}
auto const now = std::time(nullptr);
// compare old with new result -- only if we had previous record
if (rt_args.previous_data) {
auto &previous_data = *rt_args.previous_data;
// sort the (domain)names in (alphabetical, record type) tuple order
std::sort(previous_data.begin(), previous_data.end(),
[](json_data_t const &a, json_data_t const &b) {
return std::tie(a.domain_name, a.type) <
std::tie(b.domain_name, b.type);
});
sort_dns_results(result_map);
compare_results(previous_data, result_map, rt_args.content_length);
merge_history(&previous_data, result_map, rt_args, now);
} else {
merge_history(nullptr, result_map, rt_args, now);
}
sort_dns_results(result_map);
if (!silent) {
spdlog::info("Writing JSON output");
}
write_json_result(result_map, rt_args);
}
void run_program(cli_args_t const &cli_args) {
runtime_args_t rt_args{};
// settle resolvers.
std::vector<std::string> resolver_strings{};
if (cli_args.resolver_filename.empty()) {
if (cli_args.resolver.empty()) {
if (!silent) {
spdlog::info("No resolver specified, using default");
}
resolver_strings.push_back("8.8.8.8 53");
} else {
split_string(cli_args.resolver, resolver_strings, ',');
}
} else {
if (auto resolvers = get_names<std::string>(cli_args.resolver_filename);
resolvers && !resolvers->empty()) {
resolver_strings = std::move(*resolvers);
} else {
return spdlog::error("Unable to read file content");
}
}
rt_args.report_first_seen = cli_args.report_first_seen;
rt_args.last_seen_before = resolve_last_seen_cutoff(cli_args);
if (!cli_args.last_seen_date.empty() && !rt_args.last_seen_before) {
return spdlog::error("invalid --lsd date `{}`; expected MM/DD/YYYY",
cli_args.last_seen_date);
}
// read input file
if (!read_input_file(cli_args, rt_args)) {
return;
}
// try opening an output file
{
std::string filename{};
auto const out_file_path{get_filepath(cli_args.output_filename)};
bool const output_specified = !out_file_path.empty();
if (!output_specified || cli_args.include_date) {
std::string appended_time{};
bool const time_obtained = timet_to_string(
appended_time, std::time(nullptr), "%d_%m_%Y__%H_%M_%S");
if (output_specified && time_obtained) {
filename = "{}-{}.json"_format(out_file_path, appended_time);
} else if (!output_specified && !time_obtained) {
return spdlog::error("Unable to generate time for output name,"
"will use output filename only");
} else if (!time_obtained && output_specified) {
spdlog::warn("Unable to generate name for output file");
filename = "{}.json"_format(out_file_path);
} else if (!output_specified && time_obtained) {
filename = "dooked-{}.json"_format(appended_time);
}
} else {
filename = "{}.json"_format(out_file_path);
}
std::ofstream file{filename, std::ios::trunc};
if (!file) {
return spdlog::error("unable to open `{}` for out", filename);
}
if (!silent) {
spdlog::info("Output filename: {}", filename);
}
rt_args.output_file = std::make_unique<std::ofstream>(std::move(file));
rt_args.output_filename = std::move(filename);
}
// convert strings to UDP endpoints
#ifdef _DEBUG
spdlog::info("Converting UDP endpoints");
#endif // _DEBUG
std::vector<resolver_address_t> resolver_eps{};
resolver_eps.reserve(resolver_strings.size());
auto the_transformer = [](auto &&resolver) -> resolver_address_t {
std::vector<std::string> split{};
split_string(resolver, split, ' ');
unsigned int port = 53;
if (auto const split_size = split.size();
(split_size < 1 || split_size > 2)) {
throw general_exception_t{"invalid ip:port => " + resolver};
}
if (split.size() == 2) {
port = std::stoul(split[1]);
}
trim(split[0]);
boost::system::error_code ec{};
auto const ip_address = net::ip::make_address(split[0], ec);
if (ec) {
throw general_exception_t{ec.message()};
}
net::ip::udp::endpoint const ep{ip_address, (std::uint16_t)port};
return {ep};
};
try {
for (auto const &resolver_string : resolver_strings) {
resolver_eps.push_back(the_transformer(resolver_string));
}
} catch (std::exception const &e) {
return spdlog::error(e.what());
}
rt_args.resolvers.emplace(std::move(resolver_eps));
rt_args.http_request_time_ =
static_cast<http_process_e>(cli_args.post_http_request);
rt_args.thread_count = cli_args.thread_count;
rt_args.content_length = cli_args.content_length;
return start_name_checking(std::move(rt_args));
}
void report_error(std::string const &message) { spdlog::error(message); }
void report_error(char const *format, std::string const &message) {
spdlog::error(format, message);
}
void report_error(char const *format, int const value, bool const boolean_value,
std::string const &str) {
spdlog::error(format, value, boolean_value, str);
}
void print_banner() {
auto const header = R"sep("
·▄▄▄▄ ▄ •▄ ▄▄▄ .·▄▄▄▄
██▪ ██ ▪ ▪ █▌▄▌▪▀▄.▀·██▪ ██
▐█· ▐█▌ ▄█▀▄ ▄█▀▄ ▐▀▀▄·▐▀▀▪▄▐█· ▐█▌
██. ██ ▐█▌.▐▌▐█▌.▐▌▐█.█▌▐█▄▄▌██. ██
▀▀▀▀▀• ▀█▄▀▪ ▀█▄▀▪·▀ ▀ ▀▀▀ ▀▀▀▀▀•
DNS and Target History Local Storage
Made with ❥ by codingo (https://twitter.com/codingo_)
)sep";
fprintf(stdout, "%s", header);
}
} // namespace dooked