-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannounce.php
408 lines (329 loc) · 11.3 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
<?php
/*
* Bitstorm - A small and fast Bittorrent tracker
* Copyright 2008 Peter Caprioli & Simon Sessingø
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*************************
** Configuration start **
*************************/
require "errorLog.php";
/*
* Enable debugging?
* This allows anyone to see the entire peer database by appending ?debug to the announce URL.
* It will also create debugging file used to report php errors.
*/
define('__DEBUGGING_ENABLED', true);
/**
* Version
*/
define('__VERSION', 1.5);
/**
* How often should clients pull server for new clients? (Seconds)
*/
define('__INTERVAL', 30);
/**
* What's the minimum interval a client may pull the server? (Seconds)
* Some bittorrent clients does not obey this
*/
define('__INTERVAL_MIN', 300);
/**
* How long should we wait for a client to re-announce after the last announce expires? (Seconds)
*/
define('__CLIENT_TIMEOUT', 60);
/**
* Skip sending the peer id if client does not want it?
* Hint: Should be set to true
*/
define('__NO_PEER_ID', true);
/**
* Should seeders not see each others?
* Hint: Should be set to true
*/
define('__NO_SEED_P2P', true);
/**
* Where should we save the peer database
* On Linux, you should use /dev/shm as it is very fast.
* On Windows, you will need to change this value to some other valid path such as C:/Peers.txt
*/
define('__LOCATION_PEERS', 'peers.txt');
/**
* Should we enable short announces?
* This allows NATed clients to get updates much faster, but it also
* takes more load on the server. (This is just an experimental feature which may be turned off)
*/
define('__ENABLE_SHORT_ANNOUNCE', false);
/**
* In case someone tries to access the tracker using a browser, redirect to this URL or file
*/
define('__REDIR_BROWSER', 'http://10.0.10.145/announce');
define('__LOG_FILE', __DIR__ . '/error.log');
/***********************
** Configuration end **
***********************/
if(__DEBUGGING_ENABLED === true) {
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
if (file_exists(__LOG_FILE) === false) {
$handle = fopen(__LOG_FILE, 'w+b');
fclose($handle);
}
$date=date("H:i:s");
file_put_contents(__LOG_FILE, sprintf("$date --- Line: %s - Error: %s\n\n", $errline, $errstr),FILE_APPEND);
}, E_ALL);
}
//Send response as text
header('Content-type: Text/Plain');
header('X-Tracker-Version: Bitstorm ' . __VERSION); //Please give me some credit
function dbConnect(){
try{
$datab = new PDO('sqlite:/data/DB/database.sqlite');
$datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
echo "Impossible d'accéder à la base de données: ".$e->getMessage();
die();
}
return $datab;
}
$datab = dbConnect();
$req = $datab->prepare(
"UPDATE shareList
SET left = :left
WHERE idTorrent = (SELECT idTorrent FROM torrent WHERE hash = :hash) AND idPair != (SELECT idSource FROM torrent WHERE hash = :hash)"
);
$hash = bin2hex($_GET['info_hash']);
$left = 100-(($_GET['left']/($_GET['left'] + $_GET['downloaded'] + $_GET['uploaded']))*100);
$req->execute(
array(
'left' => $left,
'hash' => $hash
)
);
/**
* If you *really* dont want to, comment this line out.
* Bencoding function, returns a bencoded dictionary.
* You may go ahead and enter custom keys in the dictionary in this function if you'd like.
*/
function track($list, $interval = 60, $min_ival = 0)
{
if (is_string($list)) {
//Did we get a string? Return an error to the client
return 'd14:failure reason' . strlen($list) . ':' . $list . 'e' ;
}
if (!is_array($list)) {
unlink('peers.txt');
return 'd14:failure reason' . strlen($list) . ':' . $list . 'e' ;
}
$p = ''; //Peer directory
$c = $i = 0; //Complete and Incomplete clients
if (count($list)>0){
foreach ($list as $d) { //Runs for each client
file_put_contents ('list2.txt' , $d[7] ,FILE_APPEND);
if ($d[7]) { //Are we seeding?
$c++; //Seeding, add to complete list
if (__NO_SEED_P2P && is_seed()) { //Seeds should not see each others
continue;
}
} else {
$i++; //Not seeding, add to incomplete list
}
//Do some bencoding
$pid = '';
if (isset($_GET['no_peer_id']) === false && __NO_PEER_ID) { //Shall we include the peer id
$pid = '7:peer id' . strlen($d[1]) . ':' . $d[1];
}
$p .= 'd2:ip' . strlen($d[0]) . ':' . $d[0] . $pid . '4:porti' . $d[2] . 'ee';
}
}
//Add some other paramters in the dictionary and merge with peer list
$r = 'd8:intervali' . $interval . 'e12:min intervali' . $min_ival . 'e8:completei' . $c . 'e10:incompletei' . $i . 'e5:peersl' . $p . 'ee';
$date=date("H:i:s");
$res = "$date ---- = $r \n";
file_put_contents ('listR.txt' , $res."\n\n",FILE_APPEND);
return $r;
}
//Find out if we are seeding or not. Assume not if unknown.
function is_seed()
{
return (isset($_GET['left']) && (int)$_GET['left'] === 0);
}
/*
* Yeah, this is the database engine. It's pretty bad, uses files to store peers.
* Should be easy to rewrite to use SQL instead.
*
* Yes, sometimes collisions may occur and screw the DB over. It might or might not
* recover by itself.
*/
//Save database to file
function db_save($data)
{
$b = serialize($data);
$handle = fopen(__LOCATION_PEERS, 'wb');
if ($handle === false) {
return false;
}
if (flock($handle, LOCK_EX) === false) {
return false;
}
fwrite($handle, $b);
fclose($handle);
return true;
}
//Load database from file
function db_open()
{
$p = '';
$handle = fopen(__LOCATION_PEERS, 'rb');
if ($handle === false) {
return false;
}
if (flock($handle, LOCK_EX) === false) {
return false;
}
while (feof($handle) === false) {
$p .= fread($handle, 512);
}
fclose($handle);
return ((string)$p !== '') ? unserialize($p) : true;
}
//Check if DB file exists, otherwise create it
function db_exists($createEmpty = false)
{
if (file_exists(__LOCATION_PEERS) === true) {
return true;
}
if ($createEmpty === true) {
return db_save([]);
}
return false;
}
//Default announce time
$interval = __INTERVAL;
//Minimal announce time (does not apply to short announces)
$interval_min = __INTERVAL_MIN;
/*
* This is a pretty smart feature not present in other tracker software.
* If you expect to have many NATed clients, add short as a GET parameter,
* and clients will pull much more often.
*
* This can be done automatically, simply try to open a TCP connection to
* the client and assume it is NATed if not successful.
*/
if (isset($_GET['short']) && __ENABLE_SHORT_ANNOUNCE) {
$interval = 120;
$interval_min = 30;
}
//Did we get any parameters at all?
//Client is probably a web browser, do a redirect
if (empty($_GET)) {
header('Location: ' . __REDIR_BROWSER);
die();
}
//Create database if it does not exist
db_exists(true) or die(track('Unable to create database'));
$d = db_open();
//Do we want to debug? (Should not be used by default)
if (isset($_GET['debug']) && __DEBUGGING_ENABLED) {
echo 'Connected peers:' . count($d) . "\n\n";
die();
}
//Did we get a failure from the database?
if ($d === false) {
die(track('Database failure'));
}
//Do some input validation
function valdata($g, $must_be_20_chars = false)
{
if (!isset($_GET[$g])) {
die(track('Missing one or more arguments'));
}
if (!is_string($_GET[$g])) {
die(track('Invalid types on one or more arguments'));
}
if ($must_be_20_chars && strlen($_GET[$g]) != 20) {
die(track('Invalid length on ' . $g . ' argument'));
}
if (strlen($_GET[$g]) > 128) { //128 chars should really be enough
die(track('Argument ' . $g . ' is too large to handle'));
}
}
//Inputs that are needed, do not continue without these
valdata('peer_id', true);
valdata('port');
valdata('info_hash', true);
//Use the tracker key extension. Makes it much harder to steal a session.
if (!isset($_GET['key'])) {
$_GET['key'] = '';
}
valdata('key');
//Do we have a valid client port?
if (!ctype_digit($_GET['port']) || $_GET['port'] < 1 || $_GET['port'] > 65535) {
die(track('Invalid client port'));
}
//Array key, unique for each client and torrent
$sum = sha1($_GET['peer_id'] . $_GET['info_hash']);
//Make sure we've got a user agent to avoid errors
//Used for debugging
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
$_SERVER['HTTP_USER_AGENT'] = ''; //Must always be set
}
//When should we remove the client?
$expire = time() + $interval;
//Have this client registered itself before? Check that it uses the same key
if (isset($d[$sum])) {
if ((string)$d[$sum][6] !== (string)$_GET['key']) {
sleep(3); //Anti brute force
die(track('Access denied, authentication failed'));
}
}
//Add/update the client in our global list of clients, with some information
$d[$sum] = [$_SERVER['REMOTE_ADDR'], $_GET['peer_id'], $_GET['port'], $expire, $_GET['info_hash'], $_SERVER['HTTP_USER_AGENT'], $_GET['key'], is_seed()];
//No point in saving the user agent, unless we are debugging
if (!__DEBUGGING_ENABLED) {
unset($d[$sum][5]);
} elseif (!empty($_GET)) { //We are debugging, add GET parameters to database
$d[$sum]['get_parm'] = $_GET;
}
//Did the client stop the torrent?
//We dont care about other events
if (isset($_GET['event']) && (string)$_GET['event'] === 'stopped') {
unset($d[$sum]);
db_save($d);
die(track([])); //The RFC says its OK to return whatever we want when the client stops downloading,
//however, some clients will complain about the tracker not working, hence we return
//an empty bencoded peer list
}
//Check if any client timed out
foreach ($d as $k => $data) {
if (time() > $data[3] + __CLIENT_TIMEOUT) { //Give the client some extra time before timeout
unset($d[$k]); //Client has gone away, remove it
}
}
//Save the client list
db_save($d);
//Compare info_hash to the rest of our clients and remove anyone who does not have the correct torrent
foreach ($d as $id => $info) {
if ((string)$info[4] !== (string)$_GET['info_hash']) {
unset($d[$id]);
}
}
// Remove self from list, no point in having ourselfes in the client dictionary
unset($d[$sum]);
// Add a few more seconds on the timeout to balance the load
$interval += mt_rand(0, 10);
//file_put_contents ('listGet.txt' , implode('<br/>',$_GET));
// Bencode the dictionary and send it back
echo track($d, $interval, $interval_min);
exit(0);