-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker.cpp
1186 lines (1113 loc) · 35.9 KB
/
worker.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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <map>
#include <sstream>
#include <list>
#include <vector>
#include <set>
#include <algorithm>
#include <mutex>
#include <thread>
#include "ocelot.h"
#include "config.h"
#include "db.h"
#include "worker.h"
#include "misc_functions.h"
#include "site_comm.h"
#include "response.h"
#include "report.h"
#include "user.h"
#include "md5.cpp"
//---------- Worker - does stuff with input
worker::worker(config * conf_obj, torrent_list &torrents, user_list &users, std::vector<std::string> &_whitelist, mysql * db_obj, site_comm * sc) :
conf(conf_obj), db(db_obj), s_comm(sc), torrents_list(torrents), users_list(users), whitelist(_whitelist), status(OPEN), reaper_active(false)
{
load_config(conf);
}
void worker::load_config(config * conf) {
announce_interval = conf->get_uint("announce_interval");
del_reason_lifetime = conf->get_uint("del_reason_lifetime");
peers_timeout = conf->get_uint("peers_timeout");
numwant_limit = conf->get_uint("numwant_limit");
keepalive_enabled = conf->get_uint("keepalive_timeout") != 0;
site_password = conf->get_str("site_password");
report_password = conf->get_str("report_password");
}
void worker::reload_config(config * conf) {
load_config(conf);
}
void worker::reload_lists() {
status = PAUSED;
db->load_torrents(torrents_list);
db->load_users(users_list);
//db->load_whitelist(whitelist);
status = OPEN;
}
bool worker::shutdown() {
if (status == OPEN) {
status = CLOSING;
std::cout << "closing tracker... press Ctrl-C again to terminate" << std::endl;
return false;
} else if (status == CLOSING) {
std::cout << "shutting down uncleanly" << std::endl;
return true;
} else {
return false;
}
}
std::string worker::work(const std::string &input, std::string &ip, client_opts_t &client_opts) {
unsigned int input_length = input.length();
//---------- Parse request - ugly but fast. Using substr exploded.
if (input_length < 38) { // Way too short to be anything useful
return error("GET string too short", client_opts);
}
size_t e = input.find('?');
if (e == std::string::npos)e = input.size();
std::string passkey;
//TODO: refactor this legacy code
size_t a = 4;
if (a < e && input[a] == '/')
{
do {a++;} while (a < e && input[a] == '/');
if (a + 1 < e && input[a + 1] == '/')
a += 2;
if (a + 2 < e && input[a + 2] == '/')
a += 3;
if (a + 10 < e && input[a + 10] == '/')
{
passkey = input.substr(a, 10);
}
if (a + 32 < e && input[a + 32] == '/')
{
passkey = input.substr(a, 32);
}
}
size_t pos = 5;
pos = passkey.length() + 6;
// Get the action
enum action_t {
INVALID = 0, ANNOUNCE, SCRAPE, UPDATE, REPORT
};
action_t action = INVALID;
switch (input[pos]) {
case 'a':
stats.announcements++;
action = ANNOUNCE;
pos += 8;
break;
case 's':
stats.scrapes++;
action = SCRAPE;
pos += 6;
break;
case 'u':
action = UPDATE;
pos += 6;
break;
case 'r':
action = REPORT;
pos += 6;
break;
}
if (input[pos] != '?') {
// No parameters given. Probably means we're not talking to a torrent client
client_opts.html = true;
return response("Nothing to see here", client_opts);
}
// Parse URL params
std::list<std::string> infohashes; // For scrape only
params_type params;
std::string key;
std::string value;
bool parsing_key = true; // true = key, false = value
++pos; // Skip the '?'
for (; pos < input_length; ++pos) {
if (input[pos] == '=') {
parsing_key = false;
} else if (input[pos] == '&' || input[pos] == ' ') {
parsing_key = true;
if (action == SCRAPE && key == "info_hash") {
infohashes.push_back(value);
} else {
params[key] = value;
}
key.clear();
value.clear();
if (input[pos] == ' ') {
break;
}
} else {
if (parsing_key) {
key.push_back(input[pos]);
} else {
value.push_back(input[pos]);
}
}
}
++pos;
if (input.compare(pos, 5, "HTTP/") != 0) {
return error("Malformed HTTP request", client_opts);
}
std::string http_version;
pos += 5;
while (input[pos] != '\r' && input[pos] != '\n') {
http_version.push_back(input[pos]);
++pos;
}
++pos; // skip line break - should probably be += 2, but just in case a client doesn't send \r
// Parse headers
params_type headers;
parsing_key = true;
bool found_data = false;
for (; pos < input_length; ++pos) {
if (input[pos] == ':') {
parsing_key = false;
++pos; // skip space after :
} else if (input[pos] == '\n' || input[pos] == '\r') {
parsing_key = true;
if (found_data) {
found_data = false; // dodge for getting around \r\n or just \n
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
headers[key] = value;
key.clear();
value.clear();
}
} else {
found_data = true;
if (parsing_key) {
key.push_back(input[pos]);
} else {
value.push_back(input[pos]);
}
}
}
if (keepalive_enabled) {
auto hdr_http_close = headers.find("connection");
if (hdr_http_close == headers.end()) {
client_opts.http_close = (http_version == "1.0");
} else {
client_opts.http_close = (hdr_http_close->second != "Keep-Alive");
}
} else {
client_opts.http_close = true;
}
if (status != OPEN) {
return error("The tracker is temporarily unavailable.", client_opts);
}
if (action == INVALID) {
return error("Invalid action", client_opts);
}
if (action == UPDATE) {
if (passkey == site_password) {
return update(params, client_opts);
} else {
return error("Authentication failure", client_opts);
}
}
if (action == REPORT) {
if (passkey == report_password) {
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
return report(params, users_list, client_opts);
} else {
return error("Authentication failure", client_opts);
}
}
// Either a scrape or an announce
std::unique_lock<std::mutex> ul_lock(db->user_list_mutex);
auto user_it = users_list.find(passkey);
if (user_it == users_list.end()) {
return error("Passkey not found", client_opts);
}
user_ptr u = user_it->second;
ul_lock.unlock();
if (action == ANNOUNCE) {
// Let's translate the infohash into something nice
// info_hash is a url encoded (hex) base 20 number
std::string info_hash_decoded = hex_decode(params["info_hash"]);
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto tor = torrents_list.find(info_hash_decoded);
if (tor == torrents_list.end()) {
std::lock_guard<std::mutex> dr_lock(del_reasons_lock);
auto msg = del_reasons.find(info_hash_decoded);
if (msg != del_reasons.end()) {
if (msg->second.reason != -1) {
return error("Unregistered torrent: " + get_del_reason(msg->second.reason), client_opts);
} else {
return error("Unregistered torrent", client_opts);
}
} else {
return error("Unregistered torrent", client_opts);
}
}
return announce(input, tor->second, u, params, headers, ip, client_opts);
} else {
return scrape(infohashes, headers, client_opts);
}
}
std::string worker::announce(const std::string &input, torrent &tor, user_ptr &u, params_type ¶ms, params_type &headers, std::string &ip, client_opts_t &client_opts) {
cur_time = time(NULL);
if (params["compact"] != "1") {
return error("Your client does not support compact announces", client_opts);
}
int64_t left = std::max((int64_t)0, strtoint64(params["left"]));
int64_t uploaded = std::max((int64_t)0, strtoint64(params["uploaded"]));
int64_t downloaded = std::max((int64_t)0, strtoint64(params["downloaded"]));
int64_t corrupt = std::max((int64_t)0, strtoint64(params["corrupt"]));
int64_t bonus = 0; // bonus
int snatched = 0; // This is the value that gets sent to the database on a snatch
int active = 1; // This is the value that marks a peer as active/inactive in the database
bool inserted = false; // If we insert the peer as opposed to update
bool update_torrent = false; // Whether or not we should update the torrent in the DB
bool completed_torrent = false; // Whether or not the current announcement is a snatch
bool stopped_torrent = false; // Was the torrent just stopped?
//bool expire_token = false; // Whether or not to expire a token after torrent completion
bool peer_changed = false; // Whether or not the peer is new or has changed since the last announcement
bool invalid_ip = false;
bool inc_l = false, inc_s = false, dec_l = false, dec_s = false;
userid_t userid = u->get_id();
params_type::const_iterator peer_id_iterator = params.find("peer_id");
if (peer_id_iterator == params.end()) {
return error("No peer ID", client_opts);
}
const std::string peer_id = hex_decode(peer_id_iterator->second);
if (peer_id.length() != 20) {
return error("Invalid peer ID", client_opts);
}
std::unique_lock<std::mutex> wl_lock(db->whitelist_mutex);
if (whitelist.size() > 0) {
bool found = false; // Found client in whitelist?
for (unsigned int i = 0; i < whitelist.size(); i++) {
if (peer_id.compare(0, whitelist[i].length(), whitelist[i]) == 0) {
found = true;
break;
}
}
if (!found) {
return error("Your client is not on the whitelist", client_opts);
}
}
wl_lock.unlock();
std::stringstream peer_key_stream;
peer_key_stream << peer_id[12 + (tor.id & 7)] // "Randomize" the element order in the peer map by prefixing with a peer id byte
<< userid // Include user id in the key to lower chance of peer id collisions
<< peer_id;
const std::string peer_key(peer_key_stream.str());
if (params["event"] == "completed") {
// Don't update <snatched> here as we may decide to use other conditions later on
completed_torrent = (left == 0); // Sanity check just to be extra safe
} else if (params["event"] == "stopped") {
stopped_torrent = true;
peer_changed = true;
update_torrent = true;
active = 0;
}
peer * p;
peer_list::iterator peer_it;
// Insert/find the peer in the torrent list
if (left > 0) {
peer_it = tor.leechers.find(peer_key);
if (peer_it == tor.leechers.end()) {
// We could search the seed list as well, but the peer reaper will sort things out eventually
peer_it = add_peer(tor.leechers, peer_key);
inserted = true;
inc_l = true;
}
} else if (completed_torrent) {
peer_it = tor.leechers.find(peer_key);
if (peer_it == tor.leechers.end()) {
peer_it = tor.seeders.find(peer_key);
if (peer_it == tor.seeders.end()) {
peer_it = add_peer(tor.seeders, peer_key);
inserted = true;
inc_s = true;
} else {
completed_torrent = false;
}
} else if (tor.seeders.find(peer_key) != tor.seeders.end()) {
// If the peer exists in both peer lists, just decrement the seed count.
// Should be cheaper than searching the seed list in the left > 0 case
dec_s = true;
}
} else {
peer_it = tor.seeders.find(peer_key);
if (peer_it == tor.seeders.end()) {
peer_it = tor.leechers.find(peer_key);
if (peer_it == tor.leechers.end()) {
peer_it = add_peer(tor.seeders, peer_key);
inserted = true;
} else {
p = &peer_it->second;
std::pair<peer_list::iterator, bool> insert
= tor.seeders.insert(std::pair<std::string, peer>(peer_key, *p));
tor.leechers.erase(peer_it);
peer_it = insert.first;
peer_changed = true;
dec_l = true;
}
inc_s = true;
}
}
p = &peer_it->second;
int64_t upspeed = 0;
int64_t downspeed = 0;
if (inserted || params["event"] == "started") {
// New peer on this torrent (maybe)
update_torrent = true;
if (inserted) {
// If this was an existing peer, the user pointer will be corrected later
p->user = u;
}
p->first_announced = cur_time;
p->last_announced = 0;
p->uploaded = uploaded;
p->downloaded = downloaded;
p->corrupt = corrupt;
p->announces = 1;
peer_changed = true;
} else if (uploaded < p->uploaded || downloaded < p->downloaded) {
p->announces++;
p->uploaded = uploaded;
p->downloaded = downloaded;
peer_changed = true;
} else {
int64_t uploaded_change = 0;
int64_t downloaded_change = 0;
int64_t corrupt_change = 0;
p->announces++;
if (uploaded != p->uploaded) {
uploaded_change = uploaded - p->uploaded;
p->uploaded = uploaded;
}
if (downloaded != p->downloaded) {
downloaded_change = downloaded - p->downloaded;
p->downloaded = downloaded;
}
if (corrupt != p->corrupt) {
corrupt_change = corrupt - p->corrupt;
p->corrupt = corrupt;
tor.balance -= corrupt_change;
update_torrent = true;
}
peer_changed = peer_changed || uploaded_change || downloaded_change || corrupt_change;
if (uploaded_change || downloaded_change) {
tor.balance += uploaded_change;
tor.balance -= downloaded_change;
update_torrent = true;
if (cur_time > p->last_announced) {
upspeed = uploaded_change / (cur_time - p->last_announced);
downspeed = downloaded_change / (cur_time - p->last_announced);
}
//auto sit = tor.tokened_users.find(userid);
if (tor.free_torrent == NEUTRAL) {
downloaded_change = 0;
uploaded_change = 0;
} else if (tor.free_torrent == FREE /*|| sit != tor.tokened_users.end()*/) {
/*if (sit != tor.tokened_users.end()) {
expire_token = true;
std::stringstream record;
record << '(' << userid << ',' << tor.id << ',' << downloaded_change << ')';
std::string record_str = record.str();
db->record_token(record_str);
}*/
downloaded_change = 0;
}
if (uploaded_change || downloaded_change) {
int bonusrate=2;//TODO: insert read from config here
bonus = uploaded_change * bonusrate / 100;
std::stringstream record;
if(tor.poster_id==userid)
{record << '(' << userid << ',' << uploaded_change << ',' << downloaded_change << ',' << bonus << ',' << uploaded_change << ')';}
else{record << '(' << userid << ',' << uploaded_change << ',' << downloaded_change << ',' << bonus << ',' << 0 << ')';}
std::string record_str = record.str();
db->record_user(record_str);
}
}
}
p->left = left;
params_type::const_iterator param_ip = params.find("ip");
if (param_ip != params.end()) {
ip = param_ip->second;
} else if ((param_ip = params.find("ipv4")) != params.end()) {
ip = param_ip->second;
} else {
auto head_itr = headers.find("x-forwarded-for");
if (head_itr != headers.end()) {
size_t ip_end_pos = head_itr->second.find(',');
if (ip_end_pos != std::string::npos) {
ip = head_itr->second.substr(0, ip_end_pos);
} else {
ip = head_itr->second;
}
}
}
uint16_t port = strtoint32(params["port"]) & 0xFFFF;
// Generate compact ip/port string
if (inserted || port != p->port || ip != p->ip) {
p->port = port;
p->ip = ip;
p->ip_port = "";
char x = 0;
for (size_t pos = 0, end = ip.length(); pos < end; pos++) {
if (ip[pos] == '.') {
p->ip_port.push_back(x);
x = 0;
continue;
} else if (!isdigit(ip[pos])) {
invalid_ip = true;
break;
}
x = x * 10 + ip[pos] - '0';
}
if (!invalid_ip) {
p->ip_port.push_back(x);
p->ip_port.push_back(port >> 8);
p->ip_port.push_back(port & 0xFF);
}
if (p->ip_port.length() != 6) {
p->ip_port.clear();
invalid_ip = true;
}
p->invalid_ip = invalid_ip;
} else {
invalid_ip = p->invalid_ip;
}
// Update the peer
p->last_announced = cur_time;
p->visible = peer_is_visible(u, p);
// Add peer data to the database
std::stringstream record;
int tor_type=0;
bool seeder = (left == 0);
std::string peer_hash = md5(hex_decode(params["info_hash"])+peer_id+inttostr(port)+ip);
if (tor.free_torrent == NEUTRAL) {
tor_type = 2;
} else if (tor.free_torrent == FREE) {
tor_type = 1;
}
if (peer_changed) {
record << '(' << userid << ',' << tor.id << ',' << tor_type << ',' << uploaded << ',' << downloaded << ',' << upspeed << ',' << downspeed << ',' << left << ',' << seeder << ',' << port << ',';
std::string record_str = record.str();
db->record_peer(record_str, ip, peer_id, headers["user-agent"], peer_hash);
} else {
record << '(' << tor.id << ',' << tor_type << ',' << userid << ',' << port << ',' << seeder << ',';
std::string record_str = record.str();
db->record_peer(record_str, ip, peer_id, peer_hash);
}
// Select peers!
uint32_t numwant;
params_type::const_iterator param_numwant = params.find("numwant");
if (param_numwant == params.end()) {
numwant = numwant_limit;
} else {
numwant = std::min((int32_t)numwant_limit, strtoint32(param_numwant->second));
}
if (stopped_torrent) {
numwant = 0;
if (left > 0) {
dec_l = true;
} else {
dec_s = true;
}
} else if (completed_torrent) {
snatched = 1;
update_torrent = true;
tor.completed++;
/*
std::stringstream record;
std::string record_ip;
if (u->is_protected()) {
record_ip = "";
} else {
record_ip = ip;
}
record << '(' << userid << ',' << tor.id << ',' << cur_time;
std::string record_str = record.str();
db->record_snatch(record_str, record_ip);
*/
// User is a seeder now!
if (!inserted) {
std::pair<peer_list::iterator, bool> insert
= tor.seeders.insert(std::pair<std::string, peer>(peer_key, *p));
tor.leechers.erase(peer_it);
peer_it = insert.first;
p = &peer_it->second;
dec_l = inc_s = true;
}
/* if (expire_token) {
s_comm->expire_token(tor.id, userid);
tor.tokened_users.erase(userid);
}*/
} else if (!u->can_leech() && left > 0) {
numwant = 0;
}
std::string peers;
if (numwant > 0) {
peers.reserve(numwant*6);
unsigned int found_peers = 0;
if (left > 0) { // Show seeders to leechers first
if (tor.seeders.size() > 0) {
// We do this complicated stuff to cycle through the seeder list, so all seeders will get shown to leechers
// Find out where to begin in the seeder list
peer_list::const_iterator i;
if (tor.last_selected_seeder == "") {
i = tor.seeders.begin();
} else {
i = tor.seeders.find(tor.last_selected_seeder);
if (i == tor.seeders.end() || ++i == tor.seeders.end()) {
i = tor.seeders.begin();
}
}
// Find out where to end in the seeder list
peer_list::const_iterator end;
if (i == tor.seeders.begin()) {
end = tor.seeders.end();
} else {
end = i;
if (--end == tor.seeders.begin()) {
++end;
++i;
}
}
// Add seeders
while (i != end && found_peers < numwant) {
if (i == tor.seeders.end()) {
i = tor.seeders.begin();
}
// Don't show users themselves
if (i->second.user->is_deleted() || i->second.user->get_id() == userid || !i->second.visible) {
++i;
continue;
}
peers.append(i->second.ip_port);
found_peers++;
tor.last_selected_seeder = i->first;
++i;
}
}
if (found_peers < numwant && tor.leechers.size() > 1) {
for (peer_list::const_iterator i = tor.leechers.begin(); i != tor.leechers.end() && found_peers < numwant; ++i) {
// Don't show users themselves or leech disabled users
if (i->second.user->is_deleted() || i->second.ip_port == p->ip_port || i->second.user->get_id() == userid || !i->second.visible) {
continue;
}
found_peers++;
peers.append(i->second.ip_port);
}
}
} else if (tor.leechers.size() > 0) { // User is a seeder, and we have leechers!
for (peer_list::const_iterator i = tor.leechers.begin(); i != tor.leechers.end() && found_peers < numwant; ++i) {
// Don't show users themselves or leech disabled users
if (i->second.user->get_id() == userid || !i->second.visible) {
continue;
}
found_peers++;
peers.append(i->second.ip_port);
}
}
}
// Update the stats
stats.succ_announcements++;
if (dec_l || dec_s || inc_l || inc_s) {
if (inc_l) {
p->user->incr_leeching();
stats.leechers++;
}
if (inc_s) {
p->user->incr_seeding();
stats.seeders++;
}
if (dec_l) {
p->user->decr_leeching();
stats.leechers--;
}
if (dec_s) {
p->user->decr_seeding();
stats.seeders--;
}
}
// Correct the stats for the old user if the peer's user link has changed
if (p->user != u) {
if (!stopped_torrent) {
if (left > 0) {
u->incr_leeching();
p->user->decr_leeching();
} else {
u->incr_seeding();
p->user->decr_seeding();
}
}
p->user = u;
}
// Delete peers as late as possible to prevent access problems
if (stopped_torrent) {
if (left > 0) {
tor.leechers.erase(peer_it);
} else {
tor.seeders.erase(peer_it);
}
}
// Putting this after the peer deletion gives us accurate swarm sizes
if (update_torrent || tor.last_flushed + 3600 < cur_time) {
tor.last_flushed = cur_time;
std::stringstream record;
record << '(' << tor.id << ',' << tor.seeders.size() << ',' << tor.leechers.size() << ',' << snatched << ')';
std::string record_str = record.str();
db->record_torrent(record_str);
}
if (!u->can_leech() && left > 0) {
return error("Access denied, leeching forbidden", client_opts);
}
std::string output = "d8:completei";
output.reserve(350);
output += inttostr(tor.seeders.size());
output += "e10:downloadedi";
output += inttostr(tor.completed);
output += "e10:incompletei";
output += inttostr(tor.leechers.size());
output += "e8:intervali";
output += inttostr(announce_interval + std::min((size_t)600, tor.seeders.size())); // ensure a more even distribution of announces/second
output += "e12:min intervali";
output += inttostr(announce_interval);
output += "e5:peers";
if (peers.length() == 0) {
output += "0:";
} else {
output += inttostr(peers.length());
output += ":";
output += peers;
}
if (invalid_ip) {
output += warning("Illegal character found in IP address. IPv6 is not supported");
}
output += 'e';
/* gzip compression actually makes announce returns larger from our
* testing. Feel free to enable this here if you'd like but be aware of
* possibly inflated return size
*/
/*if (headers["accept-encoding"].find("gzip") != std::string::npos) {
client_opts.gzip = true;
}*/
return response(output, client_opts);
}
std::string worker::scrape(const std::list<std::string> &infohashes, params_type &headers, client_opts_t &client_opts) {
std::string output = "d5:filesd";
for (std::list<std::string>::const_iterator i = infohashes.begin(); i != infohashes.end(); ++i) {
std::string infohash = *i;
infohash = hex_decode(infohash);
torrent_list::iterator tor = torrents_list.find(infohash);
if (tor == torrents_list.end()) {
continue;
}
torrent *t = &(tor->second);
output += inttostr(infohash.length());
output += ':';
output += infohash;
output += "d8:completei";
output += inttostr(t->seeders.size());
output += "e10:incompletei";
output += inttostr(t->leechers.size());
output += "e10:downloadedi";
output += inttostr(t->completed);
output += "ee";
}
output += "ee";
if (headers["accept-encoding"].find("gzip") != std::string::npos) {
client_opts.gzip = true;
}
return response(output, client_opts);
}
//TODO: Restrict to local IPs
std::string worker::update(params_type ¶ms, client_opts_t &client_opts) {
if (params["action"] == "change_passkey") {
std::string oldpasskey = params["oldpasskey"];
std::string newpasskey = params["newpasskey"];
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
auto u = users_list.find(oldpasskey);
if (u == users_list.end()) {
std::cout << "No user with passkey " << oldpasskey << " exists when attempting to change passkey to " << newpasskey << std::endl;
} else {
users_list[newpasskey] = u->second;
users_list.erase(oldpasskey);
std::cout << "Changed passkey from " << oldpasskey << " to " << newpasskey << " for user " << u->second->get_id() << std::endl;
}
} else if (params["action"] == "add_torrent") {
torrent *t;
std::string info_hash = params["info_hash"];
info_hash = hex_decode(info_hash);
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto i = torrents_list.find(info_hash);
if (i == torrents_list.end()) {
t = &torrents_list[info_hash];
t->id = strtoint32(params["id"]);
t->balance = 0;
t->completed = 0;
t->last_selected_seeder = "";
} else {
t = &i->second;
}
if (params["freetorrent"] == "0") {
t->free_torrent = NORMAL;
} else if (params["freetorrent"] == "1") {
t->free_torrent = FREE;
} else {
t->free_torrent = NEUTRAL;
}
std::cout << "Added torrent " << t->id << ". FL: " << t->free_torrent << " " << params["freetorrent"] << std::endl;
} else if (params["action"] == "update_torrent") {
std::string info_hash = params["info_hash"];
info_hash = hex_decode(info_hash);
freetype fl;
if (params["freetorrent"] == "0") {
fl = NORMAL;
} else if (params["freetorrent"] == "1") {
fl = FREE;
} else {
fl = NEUTRAL;
}
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto torrent_it = torrents_list.find(info_hash);
if (torrent_it != torrents_list.end()) {
torrent_it->second.free_torrent = fl;
std::cout << "Updated torrent " << torrent_it->second.id << " to FL " << fl << std::endl;
} else {
std::cout << "Failed to find torrent " << info_hash << " to FL " << fl << std::endl;
}
} else if (params["action"] == "update_torrents") {
// Each decoded infohash is exactly 20 characters long.
std::string info_hashes = params["info_hashes"];
info_hashes = hex_decode(info_hashes);
freetype fl;
if (params["freetorrent"] == "0") {
fl = NORMAL;
} else if (params["freetorrent"] == "1") {
fl = FREE;
} else {
fl = NEUTRAL;
}
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
for (unsigned int pos = 0; pos < info_hashes.length(); pos += 20) {
std::string info_hash = info_hashes.substr(pos, 20);
auto torrent_it = torrents_list.find(info_hash);
if (torrent_it != torrents_list.end()) {
torrent_it->second.free_torrent = fl;
std::cout << "Updated torrent " << torrent_it->second.id << " to FL " << fl << std::endl;
} else {
std::cout << "Failed to find torrent " << info_hash << " to FL " << fl << std::endl;
}
}
}/* else if (params["action"] == "add_token") {
std::string info_hash = hex_decode(params["info_hash"]);
int userid = atoi(params["userid"].c_str());
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto torrent_it = torrents_list.find(info_hash);
if (torrent_it != torrents_list.end()) {
torrent_it->second.tokened_users.insert(userid);
} else {
std::cout << "Failed to find torrent to add a token for user " << userid << std::endl;
}
} else if (params["action"] == "remove_token") {
std::string info_hash = hex_decode(params["info_hash"]);
int userid = atoi(params["userid"].c_str());
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto torrent_it = torrents_list.find(info_hash);
if (torrent_it != torrents_list.end()) {
torrent_it->second.tokened_users.erase(userid);
} else {
std::cout << "Failed to find torrent " << info_hash << " to remove token for user " << userid << std::endl;
}
}*/ else if (params["action"] == "delete_torrent") {
std::string info_hash = params["info_hash"];
info_hash = hex_decode(info_hash);
int reason = -1;
auto reason_it = params.find("reason");
if (reason_it != params.end()) {
reason = atoi(params["reason"].c_str());
}
std::lock_guard<std::mutex> tl_lock(db->torrent_list_mutex);
auto torrent_it = torrents_list.find(info_hash);
if (torrent_it != torrents_list.end()) {
std::cout << "Deleting torrent " << torrent_it->second.id << " for the reason '" << get_del_reason(reason) << "'" << std::endl;
stats.leechers -= torrent_it->second.leechers.size();
stats.seeders -= torrent_it->second.seeders.size();
for (auto &p: torrent_it->second.leechers) {
p.second.user->decr_leeching();
}
for (auto &p: torrent_it->second.seeders) {
p.second.user->decr_seeding();
}
std::lock_guard<std::mutex> dr_lock(del_reasons_lock);
del_message msg;
msg.reason = reason;
msg.time = time(NULL);
del_reasons[info_hash] = msg;
torrents_list.erase(torrent_it);
} else {
std::cout << "Failed to find torrent " << bintohex(info_hash) << " to delete " << std::endl;
}
} else if (params["action"] == "add_user") {
std::string passkey = params["passkey"];
userid_t userid = strtoint32(params["id"]);
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
auto u = users_list.find(passkey);
if (u == users_list.end()) {
bool protect_ip = params["visible"] == "0";
user_ptr tmp_user = std::make_shared<user>(userid, true, protect_ip);
users_list.insert(std::pair<std::string, user_ptr>(passkey, tmp_user));
std::cout << "Added user " << passkey << " with id " << userid << std::endl;
} else {
std::cout << "Tried to add already known user " << passkey << " with id " << userid << std::endl;
u->second->set_deleted(false);
}
} else if (params["action"] == "remove_user") {
std::string passkey = params["passkey"];
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
auto u = users_list.find(passkey);
if (u != users_list.end()) {
std::cout << "Removed user " << passkey << " with id " << u->second->get_id() << std::endl;
u->second->set_deleted(true);
users_list.erase(u);
}
} else if (params["action"] == "remove_users") {
// Each passkey is exactly 32 characters long.
std::string passkeys = params["passkeys"];
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
for (unsigned int pos = 0; pos < passkeys.length(); pos += 32) {
std::string passkey = passkeys.substr(pos, 32);
auto u = users_list.find(passkey);
if (u != users_list.end()) {
std::cout << "Removed user " << passkey << std::endl;
u->second->set_deleted(true);
users_list.erase(passkey);
}
}
} else if (params["action"] == "update_user") {
std::string passkey = params["passkey"];
bool can_leech = true;
bool protect_ip = false;
if (params["can_leech"] == "0") {
can_leech = false;
}
if (params["visible"] == "0") {
protect_ip = true;
}
std::lock_guard<std::mutex> ul_lock(db->user_list_mutex);
user_list::iterator i = users_list.find(passkey);
if (i == users_list.end()) {
std::cout << "No user with passkey " << passkey << " found when attempting to change leeching status!" << std::endl;
} else {
i->second->set_protected(protect_ip);
i->second->set_leechstatus(can_leech);
std::cout << "Updated user " << passkey << std::endl;
}
} else if (params["action"] == "add_whitelist") {
std::string peer_id = params["peer_id"];
std::lock_guard<std::mutex> wl_lock(db->whitelist_mutex);
whitelist.push_back(peer_id);
std::cout << "Whitelisted " << peer_id << std::endl;
} else if (params["action"] == "remove_whitelist") {
std::string peer_id = params["peer_id"];
std::lock_guard<std::mutex> wl_lock(db->whitelist_mutex);
for (unsigned int i = 0; i < whitelist.size(); i++) {
if (whitelist[i].compare(peer_id) == 0) {
whitelist.erase(whitelist.begin() + i);
break;
}
}
std::cout << "De-whitelisted " << peer_id << std::endl;
} else if (params["action"] == "edit_whitelist") {
std::string new_peer_id = params["new_peer_id"];
std::string old_peer_id = params["old_peer_id"];
std::lock_guard<std::mutex> wl_lock(db->whitelist_mutex);
for (unsigned int i = 0; i < whitelist.size(); i++) {
if (whitelist[i].compare(old_peer_id) == 0) {
whitelist.erase(whitelist.begin() + i);
break;
}
}
whitelist.push_back(new_peer_id);
std::cout << "Edited whitelist item from " << old_peer_id << " to " << new_peer_id << std::endl;
} else if (params["action"] == "update_announce_interval") {
const std::string interval = params["new_announce_interval"];
conf->set("announce_interval", interval);
announce_interval = conf->get_uint("announce_interval");
std::cout << "Edited announce interval to " << announce_interval << std::endl;
} else if (params["action"] == "info_torrent") {