📜 ⬆️ ⬇️

Remote torrent client (transmission-daemon + firefox plugin + php)

It has long been matured to put a torrent client on a linux home server, and not as easy 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, found an interesting thing. A plugin for firefox that allows you to add a torrent file to the download list transmission in 2 clicks with the mouse. It was decided to try.


')
Installing the transmission-daemon on the server did not cause any difficulties, since it is in the packages.
Similarly, with the plugin for firelight.

Having used this case, a huge minus was immediately discovered: when I tried to download from torrents.ru, I got out an error.

It was decided not to despair, but to try to finish it so that torrents.ru could work.

Plug-in for firelight - xpi file. This is just a renamed zip archive. After unpacking it and sticking to the inside, I found out that he just sends a link to the torrent file transmission, and that one already downloads it. That is, you need to make some kind of proxy that would download the file itself and give it to the torrent client.

On php + wget, this code was quickly tweaked:
<?php
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);
}

?>


The algorithm is as follows:
1. If the link is transmitted in the url parameter to torrents.ru then
1.1 Attempting to download a file using saved cookies. If it failed
1.1.1 Login to torrents.ru using LOGIN and PASSWORD and save the transmitted cookies
1.1.2 Download files with new cookies. If this did not work out, then exit with shouts of “ERROR!”
1.2 Transferring file headers.
1.3 Transfer the file itself.
2. If the link is not on torrents.ru then
2.1 Transfer headers.
2.2 Download and file transfer.

Of course, it was possible not to make a fuss with wget, but to use curl in php, but I am not familiar with curl, but I’ve been working with wget for a long time.

It remains only to force the transmission to download torrent files through this proxy.
The solution was very simple. In the firewall plugin in the content / overlay.js file, you need to replace the addTorrent function with this:
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);
},


We assemble the plugin back, 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.php file and check.

Now the plugin works as it should. But there was one more unpleasant moment. If you enable the filtering feature to show the Torrent it! Menu, then it stops appearing on the torrents.ru website. Having stuck a little in the code, you can solve this problem as well. Just replace the contextMenuVisible function with this:
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;
}
},


In the new implementation, mime-type checking is disabled, and checking on the link text has been added.

Now everything works as it should.

That's all.

For lovers of native clients there is a transmisson-remote-gui project, whose interface is very similar to the utorrent interface.

Source: https://habr.com/ru/post/79190/


All Articles