-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathannounce.php
More file actions
272 lines (227 loc) · 7.32 KB
/
announce.php
File metadata and controls
272 lines (227 loc) · 7.32 KB
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
<?php
/*
* Bitstorm - A small and fast Bittorrent tracker
* Written by Peter Caprioli, 2008
*/
/*************************
** Configuration start **
*************************/
//Enable debugging?
//This allows anyone to see the entire peer database by appending ?debug to the announce URL
define('__DEBUGGING_ON', false);
//How often should clients pull from the server? (Seconds)
define('__INTERVAL', 300);
//What's the minimum interval a client may pull the server? (Seconds)
//Some bittorrent clients does not obey this
define('__INTERVAL_MIN', 60);
//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', '/dev/shm/Bittorrent.Peers');
//In case someone tries to access the tracker using a browser,
//redirect to this URL or file
define('__REDIR_BROWSER', '/');
/***********************
** Configuration end **
***********************/
//Send response as text
header('Content-type: text/plain');
//Require TLS for all connections
//if (!isset($_SERVER['HTTPS'])) {
// die(track("This tracker requires HTTPS"));
//}
//Bencode data, 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';
}
$p = ''; //Peer directory
$c = $i = 0; //Complete and Incomplete clients
foreach($list as $d) { //Runs for each client
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']) && __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';
return $r;
}
//Find out if we are seeding or not. Assume not if unknown.
function is_seed() {
if (!isset($_GET['left'])) {
return false;
}
if ($_GET['left'] == 0) {
return true;
}
return false;
}
/*
* Database functions, store and load array from disk
* Very primitive, but does support file locking so we should never get any
* collisions
*/
//Save database to file
function db_save($data) {
$b = serialize($data);
$h = @fopen(__LOCATION_PEERS, 'w');
if (!$h) { return false; }
if (!@flock($h, LOCK_EX)) { return false; }
@fwrite($h, $b);
@fclose($h);
return true;
}
//Load database from file
function db_open() {
$p = '';
$m = '';
$h = @fopen(__LOCATION_PEERS, 'r');
if (!$h) { return false; }
if (!@flock($h, LOCK_SH)) { return false; }
while (!@feof($h)) {
$p .= @fread($h, 512);
}
@fclose($h);
return unserialize($p);
}
//Check if DB file exists, otherwise create it
function db_exists($create_empty=false) {
if (file_exists(__LOCATION_PEERS)) {
return true;
}
if ($create_empty) {
if (!db_save(array())) {
return false;
}
return true;
}
return false;
}
//Default announce time
$interval = __INTERVAL;
//Minimal announce time (does not apply to short announces)
$interval_min = __INTERVAL_MIN;
//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_ON) {
echo 'Connected peers:'.count($d)."\n\n";
print_r($d);
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 ($d[$sum][6] !== $_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] = array($_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_ON) {
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']) && $_GET['event'] === 'stopped') {
unset($d[$sum]);
db_save($d);
die(track(array())); //The docs 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 ($info[4] !== $_GET['info_hash']) {
unset($d[$id]);
}
}
//Remove self from list, no point in having ourselfes in the client dictionary
unset($d[$sum]);
//Balance out the interval
$interval += rand(0, 10);
//Bencode the dictionary and send it back
die(track($d, $interval, $interval_min));
?>