-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
216 lines (196 loc) · 7.94 KB
/
index.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
<?php
include('config.php');
function get_torrent_status($infohash){
$bytes_done = get_rtorrent_stat('d.bytes_done', $infohash);
$tot_size = get_rtorrent_stat('d.size_bytes', $infohash);
$perc = 0;
if($tot_size != 0){
$perc = $bytes_done/$tot_size;
}
$ret = array(
'bytes_done' => $bytes_done,
'tot_size' => $tot_size,
'percent' => $perc,
'tot_size_Mb' => intval($tot_size/(1024*1024))
);
return $ret;
}
//connect to rtorrent and ask for some data using it's XMLRPC system
function get_rtorrent_dat($stat, $infohash = '', $arg2 = ''){
$infohash = htmlspecialchars($infohash, ENT_XML1, 'UTF-8');
$arg2 = htmlspecialchars($arg2, ENT_XML1, 'UTF-8');
$acutal_len = (222 + (87 - 40) - 56) + strlen($stat) + strlen($infohash) + strlen($arg2);
$dat = str_replace(['222'], [''.$acutal_len], base64_decode('NDIxOkNPTlRFTlRfTEVOR1RIADIyMgBSRVFVRVNUX01FVEhPRABQT1NUAFJFUVVFU1RfVVJJAC9SUEMyAFFVRVJZX1NUUklORwAAQ09OVEVOVF9UWVBF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ret = '';
while (!feof($sock)) {
$ret .= fgets($sock, 128);
}
fclose($sock);
$parts = explode("\r\n\r\n", $ret);
return $parts[1];
}
function get_rtorrent_stat($stat, $infohash){
$ret = get_rtorrent_dat($stat, $infohash);
$number = explode('i8>', $ret);
if(!isset($number[1])){
return false;
}
$response_nr = intval($number[1]);
return $response_nr;
}
function get_rtorrent_strs($stat, $arg1 = '', $index = NULL){
$hashesr = get_rtorrent_dat($stat, $arg1);
$hparts = explode('<string>', $hashesr);
array_shift($hparts);//shift away crap
$ret = array();
foreach($hparts AS $str){
$h2 = explode('</string>', $str);
$ret[] = $h2[0];
}
if(is_null($index)){
return $ret;
}
return $ret[$index];
}
function clean_magnet($magnet){
$parms = explode('&', $magnet);
$parms2 = explode('&', $magnet);
if(count($parms) == count($parms2)){
return htmlspecialchars_decode($magnet);
}
return $magnet;
}
function get_hash_from_magnet($magnet){
$query = parse_url($magnet, PHP_URL_QUERY);
$parms = explode('&', $query);
$parms2 = explode('&', $query);
if(count($parms) == count($parms2)){
$parms = explode('&', htmlspecialchars_decode($query));
}
$prms = array();
$infohash = 'notresolved';
foreach($parms AS $prm){
list($ky, $vl) = explode('=', $prm);
if($ky == 'xt'){
$infohash = strtoupper(substr($vl, 9));
if(strlen($infohash) == 32){
require_once("Base32.php");
$infohash = strtoupper(bin2hex(Base32\Base32::decode($infohash)));
}
}
}
return $infohash;
}
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
function get_live_stream_link($rtorrent_base_path){
global $public_link_downloaded_folder;
$filename = basename($rtorrent_base_path);
if(is_file($rtorrent_base_path)){
return('live_stream.php?f='.$public_link_downloaded_folder.rawurlencode($filename));
}else{
$files = array();
$sortedfiles = array();
$downloaded_folder_len = strlen($rtorrent_base_path);
getDirContents($rtorrent_base_path, $files);
foreach ($files as $file) {
$sortedfiles[substr($file, $downloaded_folder_len)] = filesize($file);
}
arsort($sortedfiles);
$sortedfiles = array_keys($sortedfiles);
return('live_stream.php?f='.$public_link_downloaded_folder.rawurlencode($filename.$sortedfiles[0]));
}
}
if(isset($_GET['add_torrent'])){
//Only allow magnet links to stop command injections in to rtorrent
$ret = array('body' => '');
if(substr($_GET['add_torrent'], 0, 8) == 'magnet:?'){
//we need to extract the infohash as that is what rtorrent uses to identify torrents
$ret['infohash'] = get_hash_from_magnet($_GET['add_torrent']);
//Clean the stuff we send to rtorrent. Sometimes rtorrent just locks up if you send it certain urls(probably due to some strange string enoding that rtorrent uses )
$_GET['add_torrent'] = 'magnet:?xt=urn:btih:'.$ret['infohash'].'&tr='.rawurlencode('udp://tracker.opentrackr.org:1337/announce');
$infohashes = get_rtorrent_strs('download_list');
$hashes = array();
$hash_meta_file = '/tmp/hash2file.json';
if(!file_exists($hash_meta_file)){
file_put_contents($hash_meta_file, json_encode($hashes));
}
$hashes = json_decode(file_get_contents($hash_meta_file), true);
$rtorrent_base_path = null;
$already_downloaded = false;
if(isset($hashes[$ret['infohash']])){
if(file_exists($hashes[$ret['infohash']]['base_path'])){
$already_downloaded = true;
$rtorrent_base_path = $hashes[$ret['infohash']]['base_path'];
$ret['name'] = $hashes[$ret['infohash']]['name'];
}
}
if(!$already_downloaded && !in_array($ret['infohash'], $infohashes)){
$ret['new_torrent'] = true;
$ret['body'] .= "<p>adding torrent ".$ret['infohash']." to rtorrent...</p>";
get_rtorrent_dat('load.start_verbose', '', $_GET['add_torrent']);
sleep(4);//Give rtorrent some time to find the torrent
}else{
$ret['new_torrent'] = false;
}
if(!$already_downloaded){
$ret['name'] = get_rtorrent_strs('d.name', $ret['infohash'], 0);
$stats = get_torrent_status($ret['infohash']);
if($stats['percent'] == 1){
$rtorrent_base_path = get_rtorrent_strs('d.base_path', $ret['infohash'], 0);
$hashes[$ret['infohash']] = array('name' => $ret['name'], 'base_path' => $rtorrent_base_path);
file_put_contents($hash_meta_file, json_encode($hashes));
$already_downloaded = true;
}
}
//torrent is done
if($already_downloaded){
$ret['done'] = true;
$ret['body'] .= "<p>torrent is done, start streaming now</p>";
$live_stream_link = get_live_stream_link($rtorrent_base_path);
$ret['body'] .= '<a href="'.$live_stream_link.'">'.$ret['name'].'</a>';
}else{
$ret['done'] = false;
$ret['body'] .= $ret['name']."<br>";
$ret['body'] .= '
<div style="width:200px;height:20px;border:1px solid white;">
<div style="width:'.( 200*$stats['percent']).'px;height:20px;background-color:green;">
</div>'.$stats['tot_size_Mb'].' Mb<br>';
}
echo(json_encode($ret));
}
exit;
}
if(isset($_GET['list_torrents'])){
$infohashes = get_rtorrent_strs('download_list');
foreach($infohashes AS $hash){
$names = get_rtorrent_strs('d.name', $hash);
$name = $names[0];
echo "<p><a href=\"#link=magnet%3A%3Fxt%3Durn%3Abtih%3A".$hash."\">".$hash." (".$name.")</a></p>";
}
exit;
}
//Load and send the HTML
$htm = file_get_contents("static/index.html");
$static_script_start = strpos($htm,'<script name="static_backend"');
$static_script_end = strpos($htm, '000;</script>', $static_script_start)+9;
$server_js = '<script name="server_backend">'.file_get_contents('server_client.js').'</script>';
$server_htm = substr($htm, 0, $static_script_start).$server_js.substr($htm, $static_script_end);
echo($server_htm);