Wednesday, 3 March 2010

SABNzbd+ Binary Usenet on the iPhone - A comprehensive guide

I have been toying with this for a little while now so I thought I'd share some of the knowledge I've gained and try and guide you through what is going to be a mammoth tutorial. I'm not kidding, there is so much to do to get this to work properly that you might get tired of this half way through and think that it's just not worth the hassle. Basically if you're up for a challenge then this is for you.

So what is SABNzbd+ you ask? Well it's a server based binary Usenet downloader written in Python (If you don't know what Usenet is then you might as well leave now - or Google it first ;-)). As it is a server, it's web based and doesn't need a GUI. Being written in python, it's multi platform (We'll be using the mac source as python will compile it "On-the-fly"). For the purposes of this tutorial, I'm assuming that you'll have a working knowledge of the iPhone's file structure and how to copy files to it. So if you're still interested, read on.....

What You'll need

Prerequisites:

iPhone (Jailbroken with cydia installed)
Mobile Terminal, Sysinfo, python 2.5, open SSH (Also installs openSSL which you may need later). All these are installable from Cydia.
Safari Download Manager. This is highly recommended but it is a shareware program (Available in cydia, but i'm sure Xsellize or sinfuliphone is also somewhere you could look ;-))
iFile (Same source as the above. Also shareware)
PC running at least Windowsw XP. (I'm not a Mac user so I can't advise on it's use but I'm sure most of the steps are similar) with WinRar or 7Zip installed. Also needs WinSCP to transfer files.

Packages to download on PC:

SABNzbd+ from http://sourceforge.net/projects/sabnzbdplus/files/sabnzbdplus/sabnzbd-0.5.0/SABnzbd-0.5.0-osx-src.tar.gz/download (This is the beta version, but it's a bit more stable for the iPhone than the earlier versions).
Cheetah 2.4.2 from http://pypi.python.org/pypi/Cheetah/2.4.2

Method:

Using 7Zip or Winrar, open the archive containing the SABNzbd+ files (It's a gzipped tarball, so you'll need to unzip it twice).

To make things easier, I made a folder on my desktop called "sab" and copied the files and folders from the archive into it.

You now need to put Cheetah into the folder (This is the webserver). Unzip the Cheetah archive and drag the folder called "cheetah" into your "sab" folder (Rename "cheetah" to "Cheetah" so that it starts with a capital "C").

Using WinSCP, copy the sab folder to /var/mobile on your iPhone.

This'll pretty much work "out-of-the-box." To run it we'll need to do a bit of commandline stuff (This is where the nitty gritty bit starts). Open Mobile terminal and at the first prompt type:

su

Terminal will now prompt for a password, which is "alpine" by default (Don't leave password set to this if you have SSH installed as it leaves you open to attack). You'll not see the letters you are typing, this is a "Feature" not a fault. Terminal starts in the directory /var/mobile by default, which is why we've copied the "sab" folder there. We're now going to enter that directory. So at the root prompt, type:

cd sab

Which will put you at /var/mobile/sab. Now type:

python SABNzbd.py -d

Note that all commands are case sensitive. This last command will start the server in daemon mode, that is, it will run in the background. If we were to omit the -d, the server would run in the terminal window (I found that this consumed large amounts of memory and scrolls tons of useless text across the screen).

Now fire up Safari and type "localhost:8080/sabnzbd/" in the address bar. This should start a short wizard which will setup some basic settings and create an .ini file which we'll have to edit later. At this stage SSL will not work, so set up your newsgroup provider using port 119 to start with. If you wish to use SSL, I'll explain how to set it up later.

I'll not bother with other settings just yet. The .ini file is a quicker way to change settings anyway. The one major drawback with running this server on the iphone is that it won't restart itself (or rather that I more than likely lack the sufficient knowledge to get it to shutdown and restart using the web interface). The server just hangs and you are forced to kill the process manually. Hopefully someone will show me how to overcome this.

To restart the server when changing settings, you need to kill the process manually. To do this open terminal again. You need root access but you should be still logged in from starting your server. Otherwise just do the SU thing again. Then simply type:

killall python 2.5

This works the vast majority of times. You can check whether it's closed by using the SysInfo app and checking the running processes. Python normally appears somewhere near the bottom. Note that you cannot kill this process from SysInfo, as it was started by root. So make a note of the PID number. Then, in mobile terminal, you can issue the command:

kill -9 XXX (Where XXX is the PID number of the running python process).

This will definately kill the server. You can then restart using the command shown earlier.

Now you'll want to be able to download some nzb files to work with your new usenet client. I recommend Safari Download Manager as it allows you to add mime types. In your settings app, the Safari Download Manager is towards the bottom of the first page, just below the appstore settings. tap "File Types", then under "Custom" tap "Add File Type". In "Extensions" type "nzb" and in "Mimetypes" type "application/x-nzb". You'll now be able to download the majority of nzb files from the web.

So how do we import these files into SABNzbd+? Well, the button on the web page is greyed out, so I found a workaround. I simply tell it to look for nzb files in a certain folder - in my case, /var/mobile/Library/Downloads, which is where Download Manager puts them. To do this, fire up iFile and navigate to /var/mobile/sab/sabnzbd.ini. View the file with Text Viewer. There are lots of fields that you can edit here, but the one you need is "dirscan_dir =". Edit this line (Yes, ifile's Text viewer allows you to edit text too) and add "/var/mobile/Library/Downloads". Note the case sensitivity. Now when you restart the server, any nzbs downloaded now will be picked up by SABNzbd+. I'll not go into the usage of the server as that's a whole new tutorial that I'll have to add in future.

How to Enable SSL

This is where it gets complicated. If you have SSL access to your news server, you'll probably want to enable this and it's no mean feat. In addition to the above requirements you'll also need:

Pyopenssl from http://pypi.python.org/pypi/pyOpenSSL (Get the source) and
This library file http://www.megaupload.com/?d=QK8X5MGW Copy this to /lib/ on your phone

To compile the pyopenssl package, you'll need to install some extra stuff on your iPhone first download this file on your phone using Download Manger http://aaronash.com/iphone/fake-libgcc_1.0_iphoneos-arm.deb. Open the download manager screen and tap on the downloaded file. Tap "Open in iFile". When iFile opens tap "Install". This "Fake" library is needed to install iPhone 2.0 Toolchain which is done through Cydia.

Once this is done, copy the Pyopenssl source into a folder on your phone and navigate to it in mobile terminal (I put mine in a folder called "ssl" in /var/mobile. Issue the following commands (Root access required):

cd /var/mobile/ssl
python setup.py build

Everything should now be in place to compile the program and it should exit without error. At the next prompt type:

python setup.py install

when this completes, simply edit your ini file to show "ssl_enabled = 1" and change your server and port numbers as required by your server. Then restart server and you should be good to go.

Good Luck

Simon