Remote torrent client (transmission-daemon + firefox plugin + php)
The idea has long been ripened to put a torrent client on the home linux server, but not as simple but as convenient as possible for remote use. A simple web interface was not enough for this, I wanted something more convenient.
After a long search on the Internet, I found an interesting thing. A plugin to firefox allows you to add a torrent file to the list of transmission downloads with 2 clicks of the mouse. It was decided to try.
Installing transmission-daemon on the server did not cause any difficulties, since it is in packages.
Similarly with the plug-in for firenis.
Using this case, a huge minus was immediately discovered: when I tried to download from torrents.ru, I got an error.
It was decided not to despair, but to try to finish it so that torrents.ru works.
Firefox plugin - xpi file. This is just a renamed zip archive. After unpacking it and delving into the insides, I found that he was just sending a link to the torrent file transmission, and he already downloaded it. That is, you need to make some proxy, which would download the file and give it to the torrent client.
On php + wget, I quickly got this code: The algorithm is as follows: 1. If the link passed in the url parameter to torrents.ru then 1.1 Attempt to download the file using saved cookies. If it doesn’t succeed, then 1.1.1. Log in to torrents.ru using the LOGIN and PASSWORD and save the transferred cookies. 1.1.2. Download the file with new cookies. If this did not work, then the exit with shouts “ERROR!” 1.2 Transferring file headers.
1.3 Transfer of the file itself.
2. If the link is not on torrents.ru then
2.1 Transfer of headers.
2.2 Download and file transfer.
Of course, it was possible not to fence the garden with wget, but to use curl included in the php, but I am new to curl, but I have been working with wget for a long time.
It remains only to make transmission download torrent files through this proxy.
The solution was very simple. In the plug-in for firefox in the content / overlay.js file, you need to replace the addTorrent function with this: Put the plug-in back in, import it into firefox, save the php script in the root directory of the web server (the web server should be in the same place as the torrent client) in the torrent file .php and check.
Now the plugin works as it should. But there was one more unpleasant moment. If you enable the function of filtering the display of the Torrent it! Menu, then on the torrents.ru website it stops appearing. After digging a bit in the code, you can solve this problem. It’s easy to replace the contextMenuVisible function with this: in the new implementation, mime-type checking is disabled and link text checking is added. Now everything works as it should. That's all. For lovers of native clients, there is the transmisson-remote-gui project , whose interface is very similar to the utorrent interface.
After a long search on the Internet, I found an interesting thing. A plugin to firefox allows you to add a torrent file to the list of transmission downloads with 2 clicks of the mouse. It was decided to try.
Installing transmission-daemon on the server did not cause any difficulties, since it is in packages.
Similarly with the plug-in for firenis.
Using this case, a huge minus was immediately discovered: when I tried to download from torrents.ru, I got an error.
It was decided not to despair, but to try to finish it so that torrents.ru works.
Firefox plugin - xpi file. This is just a renamed zip archive. After unpacking it and delving into the insides, I found that he was just sending a link to the torrent file transmission, and he already downloaded it. That is, you need to make some proxy, which would download the file and give it to the torrent client.
On php + wget, I quickly got this code: The algorithm is as follows: 1. If the link passed in the url parameter to torrents.ru then 1.1 Attempt to download the file using saved cookies. If it doesn’t succeed, then 1.1.1. Log in to torrents.ru using the LOGIN and PASSWORD and save the transferred cookies. 1.1.2. Download the file with new cookies. If this did not work, then the exit with shouts “ERROR!” 1.2 Transferring file headers.
if(!$_REQUEST['url']) exit(0);
$url = trim($_REQUEST['url']);
$filename = 'tor.torrent'; //default name
if(preg_match("/http:\/\/[\w\.]*torrents.ru/",$url,$mass)){
// torrents.ru!!!!
if(preg_match("/^http.*\?t=([0-9]*)$/",$url,$mass))
{
$filename = "[torrents.ru].t".$mass[1].".torrent";
}
$login = 'ЛОГИН';
$pass = 'ПАРОЛЬ';
$cmd_login = "wget -nv --post-data 'login_username=".$login."&login_password=".$pass."&ses_short=1&login=Вход' --save-cookies=tor-cookie login.torrents.ru/forum/login.php --referer=http://torrents.ru/forum/index.php -O ./logintest.html";
$cmd_download = "wget --keep-session-cookies --load-cookies=tor-cookie --save-cookies=tor-cookie ".$url." -O ".$filename;
$mime_good = "BitTorrent file";
$cmd_mime = "file -b ";
//Try download use old cookies
$ret = shell_exec($cmd_download);
//Check torrent file
if($mime_good != trim(shell_exec($cmd_mime.$filename))){
// download fail, must login
$ret = shell_exec($cmd_login);
sleep(1);
$ret = shell_exec($cmd_download);
if($mime_good != trim(shell_exec($cmd_mime.$filename))){
echo "ERROR!!!";
exit(0);
}
}
// torrent file loaded, now we can upload it
header( "Content-Type: application/x-bittorrent;" );
header( "Content-Disposition: inline; filename=\"".$filename."\"" );
readfile($filename);
unlink($filename);
}// if torrents.ru
else
{
// NO torrents.ru
$filename = basename($url);
header( "Content-Type: application/x-bittorrent;" );
header( "Content-Disposition: inline; filename=\"".$filename."\"" );
readfile($url);
}
?>
1.3 Transfer of the file itself.
2. If the link is not on torrents.ru then
2.1 Transfer of headers.
2.2 Download and file transfer.
Of course, it was possible not to fence the garden with wget, but to use curl included in the php, but I am new to curl, but I have been working with wget for a long time.
It remains only to make transmission download torrent files through this proxy.
The solution was very simple. In the plug-in for firefox in the content / overlay.js file, you need to replace the addTorrent function with this: Put the plug-in back in, import it into firefox, save the php script in the root directory of the web server (the web server should be in the same place as the torrent client) in the torrent file .php and check.
addTorrent: function(torrentURL, callback) {
autotrans.log(autotrans.strings.getString("extensions.autotrans.adding_url") + ": " + torrentURL);
var transURL = autotrans.prefService.getCharPref("extensions.autotrans.url");
torrentURL = torrentURL.replace(/&/g,"%26");
torrentURL = torrentURL.replace(/:\/\//g,"%3A%2F%2F");
torrentURL = torrentURL.replace(/\?/g,"%3F");
torrentURL = torrentURL.replace(/=/g,"%3D");
torrentURL = "http://localhost/torrent.php?url=".concat(torrentURL);
autotrans.ajaxToUrl(transURL, autotrans.jsonService.encode({ method: "torrent-add", arguments: { filename: torrentURL } }), callback);
},
Now the plugin works as it should. But there was one more unpleasant moment. If you enable the function of filtering the display of the Torrent it! Menu, then on the torrents.ru website it stops appearing. After digging a bit in the code, you can solve this problem. It’s easy to replace the contextMenuVisible function with this: in the new implementation, mime-type checking is disabled and link text checking is added. Now everything works as it should. That's all. For lovers of native clients, there is the transmisson-remote-gui project , whose interface is very similar to the utorrent interface.
contextMenuVisible: function() {
if (autotrans.prefService.getBoolPref("extensions.autotrans.extfilter")) {
// Only show context menu item if selected file has torrent extension or mime-type
var visible = false;
if (gContextMenu.onLink) {
/*try {
var request = new XMLHttpRequest();
request.open("HEAD", gContextMenu.linkURL, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var mime = request.getResponseHeader("Content-Type");
autotrans.showContextMenu(mime == "application/x-bittorrent");
}
}
request.send(null);
} catch (e) {
autotrans.log(autotrans.strings.getString("extensions.autotrans.error") + ": " + e);
}*/
visible = /\.torrent$/.test(gContextMenu.linkURL);
if(!visible ) visible = /\.torrent$/.test(gContextMenu.linkText());
}
return visible;
} else {
return true;
}
},