This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathannounce.php
553 lines (456 loc) · 14.9 KB
/
announce.php
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
<?php
include 'Tracker/Config.php';
use Tracker\Config;
require_once('include/bittorrent_announce.php');
require_once('include/benc.php');
$sqlLink = get_mysql_link();
$row = null;
$res = null;
$errHandle = null;
$delay = 4; // avoid too slow
/**
* old torrent's promotion state
*
* 0: none
* 1: normal
* 2: free
* 3: 2x
* 4: 2x free
* 5: half
* 6: 2x half
* 7: 30%
*
* It Wlll Be Deprecated In Next Update !
*/
$SP_MAP = [
'0' => 1,
'1' => 1,
'2' => 0.1,
'3' => 1,
'4' => 0.1,
'5' => 0.5,
'6' => 0.5,
'7' => 0.3,
];
function Notice($err) {
// TODO:
benc_resp_raw('d' .benc_str('failure reason') .benc_str($err) .'e');
die();
}
function getParam($name) {
return $_GET[$name] ?? null;
};
function hashPad($hash) {
return str_pad($hash, 20);
}
function esc($str) {
global $sqlLink;
return $sqlLink->real_escape_string($str);
}
$info = [
'event' => getParam('event') ?: '',
'port' => intval(getParam('port')),
'left' => intval(getParam('left')),
'uploaded' => intval(getParam('uploaded')),
'downloaded' => intval(getParam('downloaded')),
'agent' => $_SERVER['HTTP_USER_AGENT'],
'peerid' => getParam('peer_id'),
'compact' => intval(getParam('compact')),
'downloadkey' => getParam('downloadkey'),
'passkey' => getParam('passkey'),
'infohash' => getParam('info_hash'),
'noPeerId' => intval(getParam('no_peer_id')),
'ip' => getip(),
'ipv6' => getParam('ipv6') ?: '',
'ts' => time(),
];
$info['hash'] = preg_replace_callback('/./s', function ($matches) {
return sprintf('%02x', ord($matches[0]));
}, hashPad($info['infohash']));
$peerid = esc($info['peerid']);
$infohash = esc($info['infohash']);
$numwant = max(intval(getParam('numwant')), intval(getParam('num_want')), 0);
$info['numwant'] = min($numwant ? $numwant : 16, 16);
// seeder
$seeder = ($info['left'] == 0);
// check fields
foreach (['passkey', 'infohash', 'peerid'] as $k) {
if ($info[$k] === null) {
Notice("Missing key: $k");
}
}
if (strlen($info['infohash']) != 20) Notice('Invalid "info_hash"');
if (strlen($info['peerid']) != 20) Notice('Invalid "peer_id"');
if (strlen($info['passkey']) != 32) Notice('Invalid "passkey"');
if ($info['port'] <= 0 || $info['port'] > 0xffff) Notice('Invalid "port"');
// validate passkey
// get user info
if (!$row = $Cache->get_value('tracker_user_' .$info['passkey'] .'_content')) {
$res = $sqlLink->query("SELECT id, downloadpos, enabled, uploaded, downloaded, class, parked, clientselect, showclienterror FROM users WHERE passkey='". esc($info['passkey'])."' LIMIT 0, 1")
or Notice('Error: 0x0001');
$row = $res->fetch_assoc();
if (!$row) {
// TODO: record invalid passkey
Notice('Invalid passkey! Please re-download .torrent file.');
}
$Cache->cache_value('tracker_user' .$info['passkey'] .'_content', $row, 1850);
}
$user = $row;
// validate user
if ($user['parked'] == 'yes')
Notice('Account is parked!');
if ($user['enabled'] == 'no')
Notice('Account is disabled!');
if ($user['downloadpos'] == 'no')
Notice('Download priviledge is disabled!');
// validate HP
if (!$row = $Cache->get_value('tracker_userbonus_' .$info['passkey'] .'_content')) {
$res = $sqlLink->query("SELECT bonus FROM tracker_bonus WHERE id = '$user[id]' LIMIT 0,1")
or Notice('Error: 0x0003');
$row = $res->fetch_assoc();
if (!$row) {
Notice('Please enable tracker first');
}
$Cache->cache_value('tracker_userbonus_' .$info['passkey'] .'_content', $row, 1950);
}
if (!$seeder && $row['bonus'] < 0) {
$errHandle = function () { Notice('You run out of HP.(HP耗尽)'); };
if ($info['uploaded'] == 0 && $info['downloaded'] == 0) {
$errHandle();
}
}
// validate client
// TODO: new method
$clicheck_res = check_client($info['peerid'], $info['agent']);
$client_familyid = check_client_family($info['peerid'], $info['agent'], $clicheck_res);
if ($clicheck_res || !$client_familyid) {
if ($user['showclienterror'] == 'no') {
// TODO: record invalid client version
$sqlLink->query("UPDATE users SET showclienterror = 'yes' WHERE id = '$user[id]'")
or Notice('Error: 0x1001');
}
Notice($clicheck_res);
} elseif ($user['showclienterror'] == 'yes') {
$userUpdateSet[] = "showclienterror = 'no'";
}
// validate torrent
// get torrent info
// TODO
if (!$row = $Cache->get_value('tracker_hash_' .$info['hash'] .'_content')) {
$res = $sqlLink->query("SELECT id, owner, sp_state, seeders, leechers, UNIX_TIMESTAMP(added) AS added, banned, timestampdiff(DAY, last_action, NOW()) as diff_action_day FROM torrents WHERE info_hash = '$infohash' LIMIT 0,1")
or Notice('Error: 0x0004');
$row = $res->fetch_assoc();
if (!$row) {
// TODO: record invalid hashinfo
Notice('Torrent not exists.(种子未出现)');
}
$Cache->cache_value('tracker_hash_' .$info['hash'] .'_content', $row, 1870);
}
$torrent = $row;
// validate torrent priviledge
if ($torrent['banned'] == 'yes' && $user['class'] < $seebanned_class && $torrent['owner'] != $user['id'])
Notice('Torrent banned!(种子被禁止)');
// get global promotion state
if (!$globalPromotionState = $Cache->get_value('tracker_global_promotion_state')) {
$res = $sqlLink->query("SELECT * FROM torrents_state LIMIT 1")
or Notice('Error: 0x0004');
$row = $res->fetch_row();
$globalPromotionState = $row ? $row[0] : 1;
$Cache->cache_value('tracker_global_promotion_state', $globalPromotionState, 1805);
}
// validate torrent-user info
// check torrent download key
$downloadkey = esc($info['downloadkey']);
if (!$row = $Cache->get_value("tracker_snatch_$torrent[id]_$user[id]_content")) {
$res = $sqlLink->query("SELECT id FROM tracker_snatch WHERE torrent = '$torrent[id]' AND userid = '$user[id]' AND downloadkey = '$downloadkey'")
or Notice('Error: 0x001a');
$row = $res->fetch_assoc();
if (!$row) {
// TODO: record invaild key
Notice('Invalid key! Please re-download .torrent file.(请重新下载 .torrent 文件)');
}
$Cache->cache_value("tracker_snatch_$torrent[id]_$user[id]_content", $row, 2017);
}
$snatch = $row;
// evalute interval time
$annIntervals = Tracker\Config::$annIntervals;
$annThreshold = Tracker\Config::$annIntervalsThreshold;
$annInterval = $annIntervals[0];
if ((TIMENOW - $torrent['added']) >= $annThreshold[1]) {
$realAnnInterval = $annIntervals[2];
} else if ((TIMENOW - $torrent['added']) < $annThreshold[0]) {
$realAnnInterval = $annIntervals[0];
} else {
$realAnnInterval = $annIntervals[1];
}
// get peer info
$peerFields = join(',', [
'seeder',
'peer_id',
'ip',
'ipv6',
'port',
'uploaded',
'downloaded',
'UNIX_TIMESTAMP(last_action) AS last_action',
'UNIX_TIMESTAMP(prev_action) AS prev_action',
]);
if ($info['left'] == 0) {
$numPeerMax = $torrent['leechers'];
} else {
$numPeerMax = $torrent['leechers'] + $torrent['seeders'];
}
$self = null;
$res = $sqlLink->query("SELECT $peerFields FROM tracker_peers WHERE torrent = '$torrent[id]' AND userid = '$user[id]' AND peer_id = '$peerid' LIMIT 0,1")
or Notice('Error: 0x0005');
$self = $res ? $res->fetch_assoc() : null;
// validate interval time
if ($self && $self['prev_action'] > (TIMENOW - $annInterval + $delay)) {
$errHandle = $errHandle ?: function () use ($annInterval) { Notice("There is a minimum announce time of $annInterval seconds.(服务器已收到请求,请等待)"); };
}
// validate leech and seed limit
$res = $sqlLink->query("SELECT COUNT(*) FROM tracker_peers WHERE torrent = '$torrent[id]' AND userid = '$user[id]' AND peer_id != '$peerid'")
or Notice('Error: 0x0006');
$num = $res ? $res->fetch_row()[0] : 0;
if ($num > 0 && !$seeder)
Notice('Waiting for cleaning redundant peers.(请手动清除冗余做种)');
if ($num > 5 && $seeder)
Notice('Please seed the same torrent from less than 4 locations.(请手动清除冗余做种)');
// basic info
$body = [
benc_str('interval') .'i' .$realAnnInterval .'e',
benc_str('min interval') .'i' .$annInterval .'e',
benc_str('complete') .'i' .$torrent['seeders'] .'e',
benc_str('incomplete') .'i' .$torrent['leechers'] .'e',
];
// get peer list
if (!$errHandle) {
// check compact flag
if (!ip2long($info['ip'])) $info['compact'] = 0; // disable for IPv6
$res = $sqlLink->query("SELECT $peerFields FROM tracker_peers WHERE torrent = '$torrent[id]' "
.($seeder ? "AND seeder = 0 " : '')
."ORDER BY RAND() LIMIT 0, $info[numwant]")
or Notice('Error: 0x0008');
$peerList = [];
while ($res && $row = $res->fetch_assoc()) {
$row['peer_id'] = hashPad($row['peer_id']);
if ($row['peer_id'] === $info['peerid']) {
continue;
}
// TODO: network address transform
// TODO: ip or ipv6 from 'ip' field
if ($info['compact'] == 1) {
$l = ip2long($row['ip']);
// skip IPv6 address
if ($l)
$peerList[] = pack('Nn', sprintf('%d', $l), $row['port']);
} elseif ($info['noPeerId'] == 1) {
if ($row['ip'])
$peerList[] = 'd'
. implode('', array_map('benc_str', ['ip', $row['ip'], 'port']))
. 'i' .$row['port'] . 'e'
. 'e';
if ($row['ipv6'] && $row['ip'] != $row['ipv6'])
$peerList[] = 'd'
. implode('', array_map('benc_str', ['ip', $row['ipv6'], 'port']))
. 'i' . $row['port'] . 'e'
. 'e';
} else {
if ($row['ip'])
$peerList[] = 'd'
. join('', array_map('benc_str', ['ip', $row['ip'], 'peer id', $row['peer_id'], 'port']))
. 'i' . $row['port'] . 'e'
. 'e';
if ($row['ipv6'])
$peerList[] = 'd'
. join('', array_map('benc_str', ['ip', $row['ipv6'], 'peer id', $row['peer_id'], 'port']))
. 'i' . $row['port'] . 'e'
. 'e';
}
}
$peerInfo = benc_str('peers');
if ($info['compact'] == 1)
$peerInfo .= benc_str(join('', $peerList));
else
$peerInfo .= 'l' .join('', $peerList) .'e';
// TODO: check user level
$body[] = $peerInfo;
} else {
$body[] = '5:peers0:';
}
// update peer
$dt = esc(date('Y-m-d H:i:s', $info['ts']));
if ($self) {
// TODO: ip info
$updates = [
"togo = '$info[left]'",
"uploaded = '$info[uploaded]'",
"downloaded = '$info[downloaded]'",
"ip = '" .esc($info['ip']) ."'",
"ipv6 = '" .esc($info['ipv6']) ."'",
"last_action = '$dt'",
"seeder = '$seeder'",
];
if ($info['event'] == 'started')
$updates[] = "startdat = '$dt'";
if (!$errHandle)
$updates[] = "prev_action = '$dt'";
$where = "torrent = '$torrent[id]' AND userid = '$user[id]' AND peer_id = '$peerid'";
if ($info['event'] == 'completed') { // complete: update seeder status
$sqlLink->query("UPDATE tracker_peers SET " .join(',', $updates) ." WHERE $where")
or Notice('Error: 0x1002');
} elseif ($info['event'] == 'stopped') { // stop: delete peer
// TODO: check if time is 0
$sqlLink->query("DELETE FROM tracker_peers WHERE $where")
or Notice('Error: 0x2001');
} else { // start or continue: update peer status
$sqlLink->query("UPDATE tracker_peers SET " .join(',', $updates) ." WHERE $where")
or Notice('Error: 0x1003');
}
if ($sqlLink->affected_rows == 0) {
// no peer exist
Notice('Error: 0x1003');
}
} else {
// TODO: if not find 'started' event, log it
$fields = [
'torrent',
'userid',
'peer_id',
'agent',
'port',
'ip',
'ipv6',
'uploaded',
'downloaded',
'uploadoffset',
'downloadoffset',
'togo',
'seeder',
'last_action',
'prev_action',
];
$values = [
$torrent['id'],
$user['id'],
$peerid,
esc($info['agent']),
$info['port'],
esc($info['ip']), // TODO: validate ip
esc($info['ipv6']),
$info['uploaded'],
$info['downloaded'],
$info['uploaded'],
$info['downloaded'],
$info['left'],
$seeder,
$dt,
$dt,
];
$sqlLink->query("INSERT INTO tracker_peers (" .join(',', $fields) .") VALUES ('" .join("','", $values) ."')")
or Notice('Error: 0x000a');
}
// calculate upload and download traffic and time <-- report
// NOTE: if traffic lt 0, guess peer should be reset
// TODO: if traffic lt 0, check reset or log anormaly
if (!$self || $info['event'] == 'started') { // ignore old peer
$upTraffic = $info['uploaded'];
$downTraffic = $info['downloaded'];
$timeTraffic = 0;
} else {
$upTraffic = $info['uploaded'] - $self['uploaded'];
$downTraffic = $info['downloaded'] - $self['downloaded'];
$timeTraffic = max(TIMENOW - $self['last_action'], 0);
}
// record traffic
if ($downTraffic || $upTraffic) {
$fields = [
'torrent',
'userid',
'peer_id',
'port',
'during',
'up',
'dl',
'rest_up',
'rest_dl',
'seeder',
];
$values = [
$torrent['id'],
$user['id'],
$peerid,
$info['port'],
$timeTraffic,
$upTraffic,
$downTraffic,
$upTraffic,
$downTraffic,
$seeder,
];
$sqlLink->query("INSERT INTO tracker_traffic (" .join(',', $fields) .") VALUES ('" .join("','", $values) ."')")
or Notice('Error: 0x000b');
}
// record time
$fields = [
'torrent',
'userid',
'during',
'seeder',
];
$values = [
$torrent['id'],
$user['id'],
$timeTraffic,
$seeder,
];
$sqlLink->query("INSERT INTO tracker_traffic_null (" .join(',', $fields) .") VALUES ('" .join("','", $values) ."')")
or Notice('Error: 0x000c');
// update snatch
$updates = [];
// check if timeTrafffic == 0
if ($timeTraffic > 0) {
$updates[] = "uploaded = uploaded + " . max(0, $upTraffic); // ensure traffic gt 0
$updates[] = "downloaded = downloaded + " . max(0, $downTraffic);
$updates[] = ($seeder && !$downTraffic) ? "seedtime = seedtime + $timeTraffic" : "leechtime = leechtime + $timeTraffic";
}
if ($info['event'] == 'completed') {
$updates[] = "finish_times = finish_times + 1";
$updates[] = "finishdat = '$dt'";
}
if (count($updates) > 0) {
$sqlLink->query("UPDATE tracker_snatch SET " .join(',', $updates) ." WHERE id=$snatch[id]")
or Notice('Error: 0x1009');
}
// update torrent
if ($seeder) {
$updates = [
"visible = 'yes'",
"last_action = '$dt'",
];
if ($info['event'] == 'completed') {
$updates[] = 'times_completed = times_completed + 1';
}
$sqlLink->query("UPDATE torrents SET " .join(',', $updates) ." WHERE id = '$torrent[id]'")
or Notice('Error: 0x1005');
// generator tag
// TODO: use torrent 'tag' field
if ($torrent['diff_action_day'] > 100)
$sqlLink->query("INSERT INTO sitelog (added, txt, security_level) VALUES('$dt', 'Torrent $torrent[id] added BUMP tag. The seeder (User $user[id]) exists after $torrent[diff_action_day] days.', 'mod')")
or Notice('Error: 0x000d');
}
// update user
// NOTE: not update user seed/leech time and query sum from `snatch` table.
if ($client_familyid != 0 && $client_familyid != $user['clientselect'])
$userUpdateSet[] = "clientselect = '$client_familyid'";
if ($seeder)
$userUpdateSet[] = "last_access = '$dt'";
if (isset($userUpdateSet) && count($userUpdateSet) > 0) {
$sqlLink->query("UPDATE users SET " .join(',', $userUpdateSet) ." WHERE id = '$user[id]'")
or Notice('Error: 0x1006');
}
// output info
if ($errHandle) $errHandle();
benc_resp_raw('d' .join('', $body) .'e');
exit();
/* END */