forked from chdproxy/ptproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anncache.php
executable file
·199 lines (181 loc) · 5.14 KB
/
anncache.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
<?php
class BencodeException extends Exception {
}
class Bencode {
/**
* The version of the library.
*
* @var string
*/
const VERSION = "1.0";
/**
* Bencodes the given data structure.
*
* @param mixed
* @return string
* @throws BencodeException
*/
public static function encode($value) {
if (is_null($value)) {
return "0:";
}
if (is_string($value)) {
return strlen($value) . ":" . $value;
}
if (is_numeric($value)) {
return "i" . sprintf('%0.0f', $value) . "e";
}
if (is_array($value)) {
if (self::isAssoc($value)) {
ksort($value, SORT_STRING);
$buffer = "d";
foreach ($value as $key => $v) {
$buffer .= self::encode(strval($key));
$buffer .= self::encode($v);
}
$buffer .= "e";
}
else {
ksort($value, SORT_NUMERIC);
$buffer = "l";
foreach ($value as $v) {
$buffer .= self::encode($v);
}
$buffer .= "e";
}
return $buffer;
}
throw new BencodeException("Unable to encode data type: " . gettype($value));
}
/**
* Tells whether an array is associative or not. In order to be non-associative,
* each of the array's key numbers must correspond exactly to it's position
* in the array.
*
* @param array
* @return bool
*/
public static function isAssoc($array) {
return count($array) !== array_reduce(array_keys($array), array("Bencode", "isAssocCallback"), 0);
}
/**
* Decodes the given string. The second parameter is only used in recursion.
*
* @param string
* @param int
* @return mixed
* @throws BencodeException
*/
public static function decode($tokens, &$i = 0) {
if (is_string($tokens)) {
$tokens = str_split($tokens);
}
switch ($tokens[$i]) {
case "d":
$dict = array();
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return $dict;
}
else {
$key = self::decode($tokens, $i);
if (isset($tokens[++$i])) {
$dict[$key] = self::decode($tokens, $i);
}
else {
throw new BencodeException("Dictionary key ($key) without a value at index $i");
}
}
}
throw new BencodeException("Unterminated dictionary at index $i");
case "l":
$list = array();
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return $list;
}
else {
$list[] = self::decode($tokens, $i);
}
}
throw new BencodeException("Unterminated list at index $i");
case "i":
$buffer = '';
$T = FALSE;
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return round(floatval($buffer)); //FIXME
}
elseif (ctype_digit($tokens[$i])) {
$buffer .= $tokens[$i];
}
elseif ($tokens[$i] == "-" && !$T) { //FIXME
$buffer .= $tokens[$i];
$T = TRUE;
}
else {
throw new BencodeException("Unexpected token while parsing integer at index $i: {$tokens[$i]}");
}
}
throw new BencodeException("Unterminated integer at index $i");
case ctype_digit($tokens[$i]):
$length = $tokens[$i];
while (isset($tokens[++$i])) {
if ($tokens[$i] == ":") {
break;
}
elseif (ctype_digit($tokens[$i])) {
$length .= $tokens[$i];
}
else {
throw new BencodeException("Unexpected token while parsing string length at index $i: {$tokens[$i]}");
}
}
$end = $i + round(floatval($length)); //FIXME
$buffer = '';
while (isset($tokens[++$i])) {
if ($i <= $end) {
$buffer .= $tokens[$i];
if ($i == $end) {
return $buffer;
}
}
}
throw new BencodeException("Unterminated string at index $i");
case '0':
while (isset($tokens[++$i])) {
if ($tokens[$i] == ":") {
return NULL;
}
else {
throw new BencodeException("Unexpected token while parsing string length at index $i: {$tokens[$i]}");
}
}
}
throw new BencodeException("Unexpected token at index $i: {$tokens[$i]}");
}
/**
* A callback function used by {@link isAssoc()}.
*
* @return int
*/
protected static function isAssocCallback($a, $b) {
return $a === $b ? $a + 1 : 0;
}
}
header("Content-Type: text/plain; charset=utf-8");
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header("Pragma: no-cache");
if (isset($_GET['info_hash']) && strlen($_GET['info_hash']) == 20) {
$info_hash = strtolower(bin2hex($_GET['info_hash']));
$current_dir = getcwd();
if (file_exists($current_dir . '/' . $info_hash)) {
echo file_get_contents($current_dir . '/' . $info_hash, FILE_BINARY);
exit();
}
}
echo Bencode::encode(array(
'failure reason' => 'no cached peer',
'interval' => 60,
'min interval' => 10
));