There is work to be done! There's a war to be won!

Thursday, 29 August 2013

RaspberryPi + VPN + UPNP + BBC-iPlayer.

When he first told me about the RasPi, I was preoccupied with other things (probably work) and couldn't appreciate it; I spent time doing some reading up on the portability, the connectivity it offered and the amazingly low price. It didn't take long to realise how brilliant it was. I decided to build a VPN/UPNP server with an open source version of BBC's Iplayer. There are numerous sites detailing the config and build, but some are a little old and contain some oddities; I've listed what I did to get it working.

1) Buy Pi and pimp it up. You'll want a cover for it as its sold as a basic mobo without any protective casing.

2)  Buy supported USB 2.0 hard-drive; I'd read up about USB 3.0 working but the Pi only has a 2.0 USB port. Arguably it should be backwards compatible, but with my experience I couldn't get a 3.0 USB device to work correctly at all.

3) I bought a 4GB mini SD flash card with it that contained an image of the OS. It works perfectly well and guides you through the installation. Connect it to an analogue TV/display, if not use HDMI. I had fond memories of connecting my spectrum 48k when I was connecting it up, and used digital connection over HDMI into a TV.

4) Connect USB keyboard/mouse and connect to network; can purchase a wifi-dongle or stick in wired connection. Finally, power the beast on you're on your way to configuring. The configuration steps are really simple; supply values like DNS and enable things like ssh. After initial config, remove HDMI cable and CLI the next steps:

If you're not too concerned about security of using PPTP (depends what content you're storing on Pi), install it and config it without too much hassle. Else, consider OpenVPN.

Install and configure PPTP:

a) Get root on pi:
sudo bash (you're running in root shell, don't be silly now)

b) Update PI and get pptdp:
atp-get update
apt-get install pptpd

c) vi /etc/pptpd.conf:
pptpd reads its options from this file as the name suggests. You could just as easily pop everything onto the command line if you like and run it as a demon with all specified, but I like to use the conf file. At a bare minimum, add the following:

localip <individual ip> - <range_ip> : the local IP or range of IP's to be used on the tunneled PPP link
perhaps its a good idea to specify something locally and ensure its not in use ;)
remoteip <individual_ip> - <range_ip>: remote IP or range of IPs to be used. Add spares in the range in order for the client to make connections simultaneously. The debug and options parameters are added already, which in the Pi point to /etc/ppp/pptpd-options file:

d) vi /etc/ppp/pptpd-options and include: (look away if you're security conscious)
name pptpd: you'll use 'pptpd' later in the /etc/ppp/chap-secrets, so ensure its the same if you fiddle.
pap refuse 
chap refuse
mschap refuse
mtu  1490
mru 1490

e) Configure your gateway/router to forward packets to the Pi:
Therefore incoming connections (port 1723) -> Pi address.

f) vi /etc/ppp/chap-secrets (look away again as its a clear-text password in here, dodgy? Yes.)
edit fields with: client<tab> server<tab> secret<tab> IPaddress

g) service pptpd restart

h) vi /etc/sysctl.conf 
Using IPV4?

uncomment net.ipv4.ip_forward=1
Using IPV6?
uncomment net.ipv6.conf.all.forwarding=1

 i) Configure UPNP server: I use Mediatomb, see this link: (step 6).

j) Would be useful to use a static IP address and consider dynamic DNS of some kind. There are loads of options out there. Do it.

k) Configure VPN PPTP connection with username/password (specified in secrets file) ad naturally the host you're connecting to. (which is why you performed dynamic DNS step above).

l) apt-get install get_iplayer ffmpeg flvstreamer rtmpdump libwww-perl

ffmpeg: convert downloaded flash (.flv) to .mp4 (which is why you built UPNP to view through later)
flvstreamer: <optional> stream content if you like.
rtmpdump: recording the stream itself
libwww-perl: perl via www

m) chmod 777 get_iplayer

Right, so you're all done and ready to give things a try and connect. I tested from a mobile, connected over 3G and then ssh'ed into the host without too much fuss. Have a play with the syntax from CLI:

./get_iplayer (list all programs!)
 ./get_iplayer -g <id> --output "/your/large/mounted_usb_drive/"

You can also configure the Iplayer config file so the output/streamer/etc is read at each incarnation of ./get_iplayer:

Eg: ./get_iplayer --prefs-add --output="/your/large/mounted_usb_drive/" will populate  ~/.get_iplayer/options file.

There are literally thousands of commands to check out, the most useful is adding the programs to pvr and using cron to schedule:

./get_iplayer -h  <help>
./get_iplayer --pvr-add ='ProgramName' --type=tv 'ProgramName' <add program name to pvr>
./get_iplayer --pvr-list <list what you've added to pvr>

This is a great bible for syntax:

Additionally, use browser (cgi) via port 1935:

perl ~/get_iplayer/get_iplayer.cgi --port=1935 --getiplayer=/path_to_get_iplayer

700Mhz ARM CPU
3 x USB 2.0
Composite/HDMI Video Output
10/100 Ethernet
3.5mm Audio
SD Card for Onboard Storage
OS: Debian, Fedora, NetBSD, FreeBSD, 'Raspbian OS', Slackware Linux


Next project: Brewing beer :)

No comments:

Post a Comment