Thursday, December 6, 2007

Start torrent downloads from an iPhone

I have a server that I use for, among other things, downloading files with bittorrent. I usually start these torrents by downloading them on my primary computer and then dropping them onto an alias to my server's torrents folder, which my bittorrent client (Transmission) monitors so that it can start the torrents automatically. I occasionally wanted to use my iPod Touch to start these torrents, but Mobile Safari refuses to download torrent files.

My solution was to use javascript to alter the torrent link to send its url to my server for download rather than trying to open it. A php script on the server could then download that url to the torrents folder. With both scripts in place and the permissions of the torrents folder modified to allow apache to write files, all I have to do is run the javascript as a bookmarklet before clicking the torrent link, and click the link to download the torrent file. The modified link will pass this url to the script to download it on the server.

Bookmarklet: torrent

PHP:
<?php
if (!($_SERVER['PHP_AUTH_USER'] == "username" && $_SERVER['PHP_AUTH_PW'] == "password")) {
header('WWW-Authenticate: Basic realm="torrent"');
header('HTTP/1.0 401 Unauthorized');
} else {
passthru('curl -H "Referer: '.$_SERVER['$HTTP_REFERER'].'" -H "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10" -o /path/to/monitored/torrents/folder/remote.torrent "'.$_SERVER['QUERY_STRING'].'"');
}
?><script type="text/javascript">history.back()</script>


Modifications required:
  • script url in bookmarklet
  • username in php script
  • password in php script
  • torrents folder path in php script

9 comments:

Dakota said...

allright, so I've been fiddling with this for a few hours and I think I've got something wrong. The problem probably is that I don't have much experience with PHP and almost none with Java script. I think it is with the PHP though.

passthru('curl -H "Referer: '.$_SERVER['$HTTP_REFERER'].'" -H "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10" -o /Users/Dakota/Sites/t/remote.torrent "'.

Is this accurate? I'm a little confused about the folder path.

whosawhatsis? said...

Running shell code from php can be finicky. Make sure that the folder exists and that your www user has write access. A permissions error will cause it to silently fail.

Dakota said...

I opened the info pane, but the www user doesn't show up. Actually, only me, staff, and everyone shows up. What's this? I've seen computers with a long list. How do I allow access to the www user?

Dakota said...

and when I try to add another user, nothing but my user and admin shows up in the "User's and Groups" section.

whosawhatsis? said...

The www user won't show up in the UI, it's not intended for use by users. The only way to do it in the finder is to give "everyone" write access. Technically, this is a potential security risk, but it's extremely unlikely that anyone will find it, especially if you disable the guest account in your Accounts prefpane.

Dakota said...

Now I get an error message that says: Warning: cannot modify header information - headers already sent by (output started at /Users/(username)/Sites/torrent-script.php:6) in /Users/(username)/Sites/torrent-script.php on line 8.
And it is repeated with line 9 also. I don't mean to bother you with all my troubles, but possibly a bug? I've checked the code and reread your page multiple times, but nothing seems to work.

whosawhatsis? said...

That usually means that you've got characters before you open your php code ("<?php" must be the very first thing in the file). This is often just a space or tab, so insert you cursor right in front of the angle bracket and hit delete, and see if anything happens. Unfortunately, I've seen this error occur with no editable characters there, as an artifact of the text encoding. I believe I fixed this by using the free TextWrangler to change the encoding to "UTF-8, no BOM".

In any case, the script only sends headers to request a password. You can bypass the password request by adding it to the request url in the bookmarklet in this form: http://username:password@torrent.server.address/t.php.

khlrqa said...

this is brilliant! thanks a lot for this solution. works perfect.

chodester said...

Hey I saw this post and thought u could help. I have an iPhone 3gs and have been trying to do this since I got it. If u can help that'd be awesome. Thank u