forked from wbenny/mini-tor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
333 lines (282 loc) · 7.36 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
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
#include <mini/logger.h>
#include <mini/console.h>
#include <mini/crypto/random.h>
#include <mini/io/stream_reader.h>
#include <mini/io/file.h>
#include <mini/tor/circuit.h>
#include <mini/tor/consensus.h>
#include <mini/tor/tor_socket.h>
#include <mini/tor/tor_stream.h>
#include <mini/net/http.h>
#define MINI_TOR_USE_CONSENSUS_CACHE 1
class tor_client
{
public:
tor_client(
void
)
{
_consensus.set_allowed_dir_ports({ 80, 443 });
}
~tor_client(
void
)
{
delete _circuit;
}
void
extend_to_random(
mini::tor::onion_router::status_flags flags,
mini::collections::list<uint16_t> or_ports = {}
)
{
auto routers = _consensus.get_onion_routers_by_criteria({
{}, or_ports, _forbidden_onion_routers, flags
});
auto random_router = routers[mini::crypto::random_device.get_random(routers.get_size())];
if (random_router)
{
_forbidden_onion_routers.add(random_router);
extend_to(random_router);
}
}
void
extend_to(
mini::tor::onion_router* onion_router
)
{
if (_circuit == nullptr)
{
mini_info(
"Connecting to node #%u: '%s' (%s:%u)",
get_hop_count() + 1,
onion_router->get_name().get_buffer(),
onion_router->get_ip_address().to_string().get_buffer(),
onion_router->get_or_port());
_socket.connect(onion_router);
if (_socket.is_connected())
{
_circuit = _socket.create_circuit();
if (get_hop_count() == 1)
{
mini_info("Connected...");
}
else
{
mini_error("Error while creating circuit!");
}
}
else
{
mini_error("Error while connecting!");
}
}
else
{
mini_info(
"Extending to node #%u: '%s' (%s:%u)",
get_hop_count() + 1,
onion_router->get_name().get_buffer(),
onion_router->get_ip_address().to_string().get_buffer(),
onion_router->get_or_port());
auto previous_hop_count = get_hop_count();
_circuit->extend(onion_router);
if (get_hop_count() == (previous_hop_count + 1))
{
mini_info("Extended...");
}
else
{
delete _circuit;
_circuit = nullptr;
mini_warning("Error when extending!");
}
}
}
void
extend_to(
const mini::string_ref onion_router_name
)
{
mini::tor::onion_router* router = _consensus.get_onion_router_by_name(onion_router_name);
if (router)
{
extend_to(router);
}
}
mini::string
http_get(
const mini::string_ref url
)
{
mini::tor::tor_stream* stream = nullptr;
mini::string result;
mini::string url_string = url;
mini_info("Accessing '%s'", url_string.get_buffer());
if (url_string.starts_with("http://"))
{
url_string = url_string.substring(7);
}
if (url_string.contains("/") == false)
{
url_string += "/";
}
mini::string_collection url_parts = url_string.split("/", 1);
mini::string host = url_parts[0];
mini::string path = url_parts[1];
uint16_t port = 80;
if (host.ends_with(".onion"))
{
mini::string onion = host.substring(0, host.get_size() - 6);
mini_info("Creating onion stream...");
stream = _circuit->create_onion_stream(onion, port);
}
else
{
mini_info("Creating stream...");
stream = _circuit->create_stream(host, port);
}
if (stream)
{
mini_info("Created...");
}
else
{
mini_error("Error while creating the onion stream");
return mini::string();
}
mini_info("Sending request...");
result = mini::net::http::client::get(host, port, path, *stream);
mini_info("Response received...");
delete stream;
return result;
}
mini::size_type
get_hop_count(
void
)
{
return _circuit
? _circuit->get_circuit_node_list().get_size()
: 0;
}
private:
mini::tor::consensus _consensus
#if defined (MINI_TOR_USE_CONSENSUS_CACHE)
= mini::tor::consensus("cached-consensus")
#endif
;
mini::tor::tor_socket _socket;
mini::tor::circuit* _circuit = nullptr;
mini::collections::list<mini::tor::onion_router*> _forbidden_onion_routers;
};
int __cdecl
main(
int argc,
char* argv[]
)
{
if (argc < 2)
{
mini::console::write("No parameter provided!\n");
mini::console::write("Usage:\n");
mini::console::write(" mini-tor [-v] [-vv] [-vvv] [url]\n");
mini::console::write("Example:\n");
mini::console::write(" mini-tor \"http://duskgytldkxiuqc6.onion/fedpapers/federndx.htm\"\n");
return -1;
}
mini::log.set_level(mini::logger::level::off);
//
// parse arguments.
//
int arg_index = 1;
if (mini::string_ref(argv[arg_index]).equals("-v"))
{
mini::log.set_level(mini::logger::level::warning);
arg_index += 1;
if (arg_index == argc)
{
return -1;
}
}
if (mini::string_ref(argv[arg_index]).equals("-vv"))
{
mini::log.set_level(mini::logger::level::info);
arg_index += 1;
if (arg_index == argc)
{
return -1;
}
}
if (mini::string_ref(argv[arg_index]).equals("-vvv"))
{
mini::log.set_level(mini::logger::level::debug);
arg_index += 1;
if (arg_index == argc)
{
return -1;
}
}
#if defined(_DEBUG)
mini::log.set_level(mini::logger::level::info);
#endif
//
// fetch the page.
//
static constexpr mini::size_type hops = 2;
static_assert(hops >= 2, "There must be at least 2 hops in the circuit");
static_assert(hops <= 9, "There must be at most 9 hops in the circuit");
mini_info("Fetching consensus...");
tor_client tor;
mini_info("Consensus fetched...");
connect_again:
while (tor.get_hop_count() < hops)
{
//
// first hop.
//
if (tor.get_hop_count() == 0)
{
tor.extend_to_random(
mini::tor::onion_router::status_flag::fast |
mini::tor::onion_router::status_flag::running |
mini::tor::onion_router::status_flag::valid,
{ 80, 443 });
}
//
// last hop (exit node).
//
else if (tor.get_hop_count() == (hops - 1))
{
tor.extend_to_random(
mini::tor::onion_router::status_flag::fast |
mini::tor::onion_router::status_flag::running |
mini::tor::onion_router::status_flag::valid |
mini::tor::onion_router::status_flag::exit);
}
//
// middle hops.
//
else
{
tor.extend_to_random(
mini::tor::onion_router::status_flag::fast |
mini::tor::onion_router::status_flag::running |
mini::tor::onion_router::status_flag::valid);
}
}
mini::string content = tor.http_get(0 ? "http://duskgytldkxiuqc6.onion/fedpapers/federndx.htm" : argv[arg_index]);
if (content.is_empty())
{
mini_info("Trying to build new circuit...");
goto connect_again;
}
//mini::io::file::write_from_string("out.txt", content);
mini::console::write("%s", content.get_buffer());
mini_info("");
mini_info("-----------------------------");
mini_info("content size: %u bytes", content.get_size());
mini_info("-----------------------------");
return 0;
}
// */