-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
402 lines (328 loc) · 12.1 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
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fstream>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <vector>
std::mutex mtx;
struct udp_reply {
const char * reply;
int len;
};
struct stats {
int seeds;
int downloaded;
int leechs;
int servers;
};
char * hexstr2byte(const char * hexstr){
int len = strlen(hexstr);
if (len%2 == 0){
char * buff = (char *) malloc(len / 2);
int buff_len = 0;
const char * pos = hexstr;
for (int x = 0; x < len; x++){
unsigned char *byte = (unsigned char *) malloc(1);
sscanf(pos, "%2hhx", byte);
memcpy(buff + buff_len, byte, 1);
buff_len += 1;
pos += 2;
}
return buff;
} else {
return nullptr;
}
}
udp_reply get_connection_id(const char * ip, int port){
udp_reply connect_reply;
connect_reply.reply = "Error";
connect_reply.len = -1;
int socket_info = socket(AF_INET, SOCK_DGRAM, 0);
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
if (socket_info == -1){
std::cout << "Couldn't create a socket.\n";
return connect_reply;
}
sockaddr_in client;
client.sin_addr.s_addr = inet_addr(ip);
client.sin_family = AF_INET;
client.sin_port = htons(port);
if (connect(socket_info, (sockaddr *) &client, sizeof(client)) < 0){
std::cout << "Error connecting.\n";
return connect_reply;
}
const char protocol[] = "\x00\x00\x04\x17\x27\x10\x19\x80"; //Constant for BitTorrent protocol
int32_t action = htonl(0); //Connection request is zero.
int32_t txn_id = htonl(std::rand() % 10000 + 1);
char * buffer = (char *) malloc(16);
int curr_size = 0;
memcpy(buffer, protocol, 8);
curr_size += 8;
memcpy(buffer + curr_size, (char *) &action, sizeof(action));
curr_size += sizeof(action);
memcpy(buffer + curr_size, (char *) &txn_id, sizeof(txn_id));
curr_size += sizeof(txn_id);
if (send(socket_info, buffer, curr_size, 0) < 0){
std::cout << "Error sending message.\n";
return connect_reply;
}
char * response = (char *) malloc(1024);
setsockopt(socket_info, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
int len = recv(socket_info, response, 1024, 0);
if (len < 0){
return connect_reply;
} else {
connect_reply.reply = response;
connect_reply.len = len;
return connect_reply;
}
}
udp_reply scrape_tracker(const char * ip, int port, const char * connection_id, const char * infohash){
udp_reply scrape_reply;
scrape_reply.reply = "Error";
scrape_reply.len = -1;
int socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_info == -1){
std::cout << "Couldn't create a socket.\n";
return scrape_reply;
}
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
sockaddr_in client;
client.sin_addr.s_addr = inet_addr(ip);
client.sin_family = AF_INET;
client.sin_port = htons(port);
if (connect(socket_info, (sockaddr *) &client, sizeof(client)) < 0){
std::cout << "Error connecting.\n";
return scrape_reply;
} else {
char * buffer = (char *) malloc(36); //36 bytes to send scrape request.
int32_t action = htobe32(2); //Scrape action
int32_t txn_id = htonl(std::rand() % 10000 + 1); //Random transaction ID
int buff_size = 0;
memcpy(buffer, connection_id, 8);
buff_size += 8;
memcpy(buffer + buff_size, (char *) &action, sizeof(action));
buff_size += sizeof(action);
memcpy(buffer + buff_size, (char *) &txn_id, sizeof(txn_id));
buff_size += sizeof(txn_id);
memcpy(buffer + buff_size, hexstr2byte(infohash), 20);
buff_size += 20;
if (send(socket_info, buffer, buff_size, 0) < 0){
std::cout << "Error sending message.\n";
return scrape_reply;
}
char * response = (char *) malloc(1024);
setsockopt(socket_info, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
int len = recv(socket_info, response, 1024, 0);
if (len < 0){
return scrape_reply;
} else {
scrape_reply.reply = response;
scrape_reply.len = len;
return scrape_reply;
}
}
}
void hex2ip(const char * hex){
uint8_t ip[4];
memcpy(ip, hex, 4); //Copy the 4 bytes to a 4 x 1byte int array.
mtx.lock();
for (int x = 0; x < 3; x++){
std::cout << +ip[x] << ".";
}
std::cout << +ip[3] << '\n';
mtx.unlock();
}
void announce_tracker(const char * ip, int port, const char * connection_id, const char * infohash){
udp_reply announce_reply;
announce_reply.reply = "Error";
announce_reply.len = -1;
int socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_info == -1){
std::cout << "Couldn't create a socket.\n";
return;
}
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500000;
sockaddr_in client;
client.sin_addr.s_addr = inet_addr(ip);
client.sin_family = AF_INET;
client.sin_port = htons(port);
if (connect(socket_info, (sockaddr *) &client, sizeof(client)) < 0){
std::cout << "Error connecting.\n";
return;
}
char * buffer = (char *) malloc(98); //Buffer which will hold the hex fo packet;
int bufflen = 0;
int32_t action = htonl(1); //Annouce is 1
int32_t txn_id = htonl(std::rand() % 10000 + 1);
const char downloaded[] = "\x00\x00\x00\x00\x00\x00\x00\x00"; //8 byte downloaded value
const char left[] = "\x00\x00\x00\x00\x00\x00\x00\x00"; //8 byte left value
const char uploaded[] = "\x00\x00\x00\x00\x00\x00\x00\x00"; //8 byte uploaded value
int32_t event_id = htonl(2); //We tell tracker we started.
uint32_t client_ip = htonl(0); //Tell tracker to use packet IP, since normally not honored anyway.
uint32_t client_key = htonl(std::rand() % 10000 + 1);
int32_t num_want = htonl(-1); //Default peers. Can try diff values
uint16_t client_port = htons(5000); //Set some port, doesnt really matter.
// uint16_t extensions
//Peerid will be randomised during memcpy.
memcpy(buffer + bufflen, connection_id, 8); //8 byte connection id
bufflen += 8;
memcpy(buffer + bufflen, (char *) &action, 4); //4 byte action
bufflen += 4;
memcpy(buffer + bufflen, (char *) &txn_id, 4); //4 byte txn_id
bufflen += 4;
memcpy(buffer + bufflen, hexstr2byte(infohash), 20); //Copy the 20 bytes of infohash.
bufflen += 20;
memcpy(buffer + bufflen, (void *)memcpy, 20); //20 bytes for peerid
bufflen += 20;
memcpy(buffer + bufflen, downloaded, 8);
bufflen += 8;
memcpy(buffer + bufflen, left, 8);
bufflen += 8;
memcpy(buffer + bufflen, uploaded, 8);
bufflen += 8;
memcpy(buffer + bufflen, (char *) &event_id, 4); //4 bytes for the event
bufflen += 4;
memcpy(buffer + bufflen, (char *) &client_ip, 4); //4 bytes for ip.
bufflen += 4;
memcpy(buffer + bufflen, (char *) &client_key, 4); //4 bytes for random ip
bufflen += 4;
memcpy(buffer + bufflen, (char *) &num_want, 4);
bufflen += 4;
memcpy(buffer + bufflen, (char *) &port, 2);
bufflen += 2;
if (send(socket_info, buffer, bufflen, 0) < 0){
std::cout << "Error sending the announce.";
return;
}
char * response = (char *) malloc(1024);
setsockopt(socket_info, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
int len = recv(socket_info, response, 1024, 0);
if (len < 0){
return;
}
announce_reply.reply = response;
announce_reply.len = len;
int peers = (len - 20) / 6;
for (int i = 0; i < peers; i++){
if (20 + (i * 6) < 1020){
hex2ip(response + 20 + (i * 6));
}
}
}
void scrape(const char * infohash, std::string ip, int tracker_port, stats &t_stat){
const char * tracker_ip = ip.c_str();
udp_reply connect_reply;
connect_reply = get_connection_id(tracker_ip, tracker_port);
char * connection_id = (char *) malloc(8);
if (connect_reply.len == 16){
memcpy(connection_id, connect_reply.reply + 8, 8);
udp_reply scrape_reply = scrape_tracker(tracker_ip, tracker_port, connection_id, infohash);
if (scrape_reply.len == 20){
int seeds, done, leech;
memcpy((char *) &seeds, (char *) scrape_reply.reply+8, 4);
t_stat.seeds += be32toh(seeds);
memcpy((char *) &done, scrape_reply.reply+12, 4);
t_stat.downloaded += be32toh(done);
memcpy((char *) &leech, scrape_reply.reply+16, 4);
t_stat.leechs += be32toh(leech);
t_stat.servers++;
}
}
}
void announce(const char * infohash, std::string ip, int tracker_port){
const char * tracker_ip = ip.c_str();
udp_reply connect_reply = get_connection_id(tracker_ip, tracker_port);
char * connection_id = (char *) malloc(8);
if (connect_reply.len != 16){
return;
}
memcpy(connection_id, connect_reply.reply + 8, 8);
announce_tracker(tracker_ip, tracker_port, connection_id, infohash);
}
int main(int argc, char * argv[]){
std::srand(std::time(nullptr));
int opt;
int scrape_flag = 0, announce_flag = 0, nt_var = 1 , force_flag = 0;
std::string tracker_filename = "trackers.txt";
if (argc < 2){
std::cerr << "Usage: ./func [40 character hash to scrape]\n";
return -1;
}
if (strlen(argv[1]) != 40){
std::cerr << "Hash is not 40 characters wrong. Exiting...\n";
return -1;
}
char infohash[40];
strcpy(infohash, argv[1]);
while ((opt = getopt(argc, argv, "asft:r:")) != -1){
switch (opt){
case 'a':
announce_flag = 1;
break;
case 's':
scrape_flag = 1;
break;
case 't':
tracker_filename = optarg;
break;
case 'f':
force_flag = 1;
break;
case 'r':
nt_var = std::stoi(optarg);
break;
}
}
if (announce_flag == 1 && scrape_flag == 1){
std::cout << "You may only use -a (announce) OR -s (scrape) at a time.\n";
} else if (announce_flag == 0 && scrape_flag == 0){
scrape_flag = 1;
}
std::ifstream trackers(tracker_filename);
std::string tracker;
stats t_stat = {0, 0, 0, 0};
const int num_threads = 19;
const int multi_req = nt_var;
std::vector< std::vector< std::thread > > t;
int f = 0;
while (trackers >> tracker){
int pos = tracker.find(':');
std::string ip = tracker.substr(0, pos);
int port = stoi(tracker.substr(pos+1, tracker.length() - (pos + 1)));
std::vector< std::thread > curr_tracker;
for (int gg = 0; gg < multi_req; gg++){
if (scrape_flag == 1){
curr_tracker.push_back(std::thread(scrape, infohash, ip, port, std::ref(t_stat)));
} else if (announce_flag == 1){
curr_tracker.push_back(std::thread(announce, infohash, ip, port));
}
}
t.push_back(std::move(curr_tracker));
if (scrape_flag == 1 && force_flag != 1 ){
break;
}
}
for (int x = 0; x < t.size(); x++){
for (int gg = 0; gg < multi_req; gg++){
t[x][gg].join();
}
}
if (scrape_flag == 1){
std::cout << "Seeds: " << t_stat.seeds << "\nDownloads: " << t_stat.downloaded << "\nLeechs: " << t_stat.leechs << "\nFrom " << t_stat.servers <<" servers.\n";
}
return 0;
}