-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
181 lines (175 loc) · 6.35 KB
/
main.cpp
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
#include <iostream>
#include <utility>
#include "Options.h"
#include "Generator.h"
#include "Scraper.h"
#include "Query.h"
#include "Connection.h"
#include "Action.h"
#include "Writer.h"
#include "Controller.h"
using namespace std;
namespace {
template <typename Generator, typename Query, typename Connection, typename RequestWriter>
void run_scraper(Generator&& generator, Query&& query, Connection&& connection,
Controller& controller, RequestWriter& writer, boost::asio::io_service& ios,
const Options& options) {
Scraper<Generator, Query, Connection, RequestWriter> scraper{
std::forward<Query>(query), std::forward<Connection>(connection), std::forward<RequestWriter>(writer), controller,
ios,
options.is_verbose(), options.get_error_path()
};
scraper.run(std::forward<Generator>(generator));
}
Generator& make_generator(const Options& options) {
if (options.is_stdin()) {
static StdinGenerator stdin_generator{};
return stdin_generator;
} else {
static UriGenerator uri_generator{ options.get_pattern(), options.is_leading_zeros(), options.is_telescoping() };
return uri_generator;
}
}
GetQuery make_get(const Options& options) {
return GetQuery{
GetAction{ options.get_output_path(), options.get_screen(), options.is_verbose() }, options.is_print_found(), options.is_verbose()
};
}
HeadQuery make_head(const Options& options) {
return HeadQuery{
HeadAction{ options.get_output_path(), options.is_verbose() }, options.is_print_found(), options.is_verbose()
};
}
}
int main(int argc, const char** argv) {
try {
Options options{argc, argv};
if (options.is_help()) {
cout << options.get_help() << endl;
return EXIT_SUCCESS;
}
cout << options.get_pretty_print() << endl;
boost::asio::io_service ios;
RequestWriter writer{options.get_host(), options.is_verbose(), options.get_user_agent()};
FixedController fixed_controller{options.get_initial_coroutines(), options.get_sample_interval()};
AdaptiveController adaptive_controller{
options.get_initial_coroutines(), options.get_sample_size(),
options.get_sample_interval(), options.get_minimum_coroutines(), options.get_maximum_coroutines()
};
auto& controller = options.is_optimizer()
? static_cast<Controller&>(adaptive_controller)
: static_cast<Controller&>(fixed_controller);
if (!options.is_stdin()) {
UriGenerator uri_generator{ options.get_pattern(), options.is_leading_zeros(), options.is_telescoping() };
try {
const auto cardinality = uri_generator.get_range_size();
cout << "[ ] URL generation set cardinality is " << cardinality << endl;
}
catch (const overflow_error&) {
cout << "[!] URL generation set log cardinality is " << uri_generator.get_log_range_size() << endl;
}
}
auto& generator = make_generator(options);
if (options.is_test()) {
cout << "[ ] TEST: Writing URIs to console" << endl;
const auto prefix = options.is_tls() ? "https://" : "http://";
while(const auto& uri = generator.next()) {
cout << prefix << options.get_host() << *uri << endl;
}
return EXIT_SUCCESS;
}
if (options.is_tls()) {
if (options.is_proxy()) {
auto connection = ProxiedTlsConnection{options.get_proxy(), options.get_host(), options.is_verify(), options.is_sensitive_teardown(), ios};
if (options.is_contents()) {
run_scraper(generator,
make_get(options),
move(connection),
controller,
writer,
ios,
options);
}
else {
run_scraper(generator,
make_head(options),
move(connection),
controller,
writer,
ios,
options);
}
}
else {
auto connection = TlsConnection{options.get_host(), options.is_verify(), options.is_sensitive_teardown(), ios};
if (options.is_contents()) {
run_scraper(generator,
make_get(options),
move(connection),
controller,
writer,
ios,
options);
}
else {
run_scraper(generator,
make_head(options),
move(connection),
controller,
writer,
ios,
options);
}
}
}
else {
if (options.is_proxy()) {
auto connection = ProxiedConnection{options.get_proxy(), options.get_host(), options.is_sensitive_teardown(), ios};
if (options.is_contents()) {
run_scraper(generator,
make_get(options),
move(connection),
controller,
writer,
ios,
options);
}
else {
run_scraper(generator,
make_head(options),
move(connection),
controller,
writer,
ios,
options);
}
}
else {
auto connection = PlaintextConnection{options.get_host(), options.is_sensitive_teardown(), ios};
if (options.is_contents()) {
run_scraper(generator,
make_get(options),
move(connection),
controller,
writer,
ios,
options);
}
else {
run_scraper(generator,
make_head(options),
move(connection),
controller,
writer,
ios,
options);
}
}
}
}
catch (OptionsException& e) { cerr << "[-] " << e.what() << endl; }
catch (exception& e) {
cerr << "[-] Unknown error occurred: " << e.what() << endl;
}
return EXIT_SUCCESS;
}