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

Who's hacks?

I already have a blog, but I wanted a place to share some of my hacks and the little bits of code I'm always writing, which may or may not be useful to anyone else. Because this is so different from the subjects of my existing blog, I decided to start a separate one. I plan to use this space to post new hacks as I create them, as well as to post some of older ones when the mood strikes. I hope this will motivate me to clean up some of my code, but don't be surprised to see a lot of one-liners, few if any comments, and stuff that barely works. Everything it offered as-is, and I don't promise to respond to questions about implementation or modification, although I probably will in most cases.