Netflix – Using a VPN for just one application
(note: if you’re on Windows, you could take a look at this blogpost, or install Ubuntu first)
Some companies have not really yet grasped the concept of a global Internet, and like to restrict things to the public of just one country. In this case, the movie industry comes to mind, which allows Netflix to only air certain movies and tv shows in certain countries. Although I am located in The Netherlands, and Netflix recently opened their doors there as well, I’m still affected by geo-restrictions. In The Netherlands I’m served Dutch subs, and some tv shows are not available. Others, are available on DVD only in the US, whereas I can simply stream them in The Netherlands.
There’s a simple solution of course, a VPN service. With most VPN services, you simply download a small application, and click & run. The VPN application will direct all your traffic to a host in a location of your choosing, and for the outside world it will look as if you were located at that location.
However, even though that works, I don’t want to route all of my traffic through a US host (for you know, there’s the NSA, MPAA, they got it all). So I needed a way to route all of the Netflix traffic through the VPN, while leaving the rest of my traffic unaffected.
My first attempt did not succeed. Most routing is based on destination IP, but Netflix uses Amazon and other generic CDNs for content distribution. This brings along two problems; The ip-adresses used are constantly changed, so it’s near-impossible to track specific IP-addresses. Especially not, because Amazon hosts more than one website, which I’d like to access via my regular IP. The first attempt therefore used a hosts file that mapped the Amazon hostnames to alternative IP’s, and based on those IP’s I then did some destination based routing. The problem is that netflix uses many hostnames, therewith not really maintainable (I stopped collecting them after two-dozen). I didn’t try configuring an HTTP proxy in Firefox, because I figured it wouldn’t work in Silverlight. My first attempt can be found here.
[h2]Setting it up[/h2]
So given that using destination IP’s wasn’t really an option to make any distinction on, I opted to do it based on UID. IPtables has the ability to have a firewall rule match on the user that initiated the traffic, so running netflix as its own user enables one to use a whole set of firewall rules dedicated to just Netflix.
We begin by installing netflix-desktop and setting up the Netflix-user:
apt-add-repository ppa:ehoover/compholio
apt-get update
apt-get install netflix-desktop
useradd -m netflix
[h3]Allow a sudo’ed application to use X[/h3]
Netflix will not start though. It requires access to X, and we haven’t done anything with the network yet either. To allow Netflix to use X, it needs access to your ‘xauthority’ file. You can find the location of this file by running the following command in Bash.
export | grep xauthority -i
declare -x XAUTHORITY="/run/gdm/auth-for-dolf-7osFEz/database"
Then when you have this file, you can grant the Netflix user access:
chmod 640 /run/gdm/auth-for-dolf-7osFEz/database
chown dolf:netflix /run/gdm/auth-for-dolf-7osFEz/database
Now you should be able to run Netflix with the following command. That is, you get a nice window, telling you that you do not have access from your country/ip.
DISPLAY=":0" XAUTHORITY="/run/gdm/auth-for-dolf-7osFEz/database" sudo -u netflix -H netflix-desktop
[h3]Networking[/h3]
Next up is the networking. First you’ll need a VPN service. I went with Private Internet Access (PIA)[/url] because it works with Netflix, is cheap, and offers an OpenVPN service (some are stuck in the PPTP era). To ensure not all your traffic is automatically redirected to the VPN, and to not have to type your password everytime, you do have to modify the default config files PIA provides. E.g.:
client
dev tunUS # renamed the interface name for brevity.
proto udp
remote us-california.privateinternetaccess.com 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
tls-client
remote-cert-tls server
auth-user-pass
comp-lzo
verb 1
reneg-sec 0
ca /etc/openvpn/privateinternetaccess/ca.crt
# Add these lines:
auth-user-pass /etc/openvpn/privateinternetaccess/passwd
route-nopull
keepalive 1 30
Linux supports multiple routing tables. When you show the contents of the routing table (‘ip route show’), it shows the contents of the default routing table. Just compare the output of the following two commands:
> ip route show
default via 192.168.1.1 dev eth0 proto static
> ip route show table 254
default via 192.168.1.1 dev eth0 proto static
So after you’ve set up the VPN service, and you know there is more than one routing table you can use, all you need to do is configure a new routing table that sends all traffic through the VPN, and tell Linux to use that routing table for all traffic originating from the Netflix user:
ip route add default dev tunUS table 2
iptables -t mangle -A OUTPUT -m owner --gid-owner netflix -j MARK --set-mark 2
ip rule add fwmark 2 table 2
[h3]RP_Filter[/h3]
Linux has a feature called reverse-path filtering. This feature drops traffic from any ip it has no route for on that interface. Because it does not keep into account the use of multiple routing tables, we’ll need to disable it:
for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 0 > $f
done
[h3]Audio through PulseAudio[/h3]
If you start Netflix as netflix user, you should get a nice GUI, and no more errors about being in the wrong country. However, once you start to play a movie or an episode of a tv serie, you’ll have no sound yet. Assuming a default Ubuntu installation, Pulseaudio is used to manage all audio, and by default it doesn’t support multiple users. Now you can try to get that right the right way, or you can go the quick-and-dirty way (my favorite). The only disadvantage is that if Netflix is playing, no other applications can use the sound system.
sudo usermod -a -G audio netflix
sudo ln -s /home/dolf/.pulse-cookie /home/netflix/.pulse-cookie
sudo chown dolf:audio .pulse-cookie
sudo chmod 640 .pulse-cookie
Now you should be good to go, and all should be running.
I myself proxy all my traffic via my own VPS (located in the EU as well), and from there I route my traffic via the appropriate VPN service. But because the Netflix application runs locally, I cannot make a distinction based on GID on my VPS. In a next blog I’ll show how you can still get that done. EDIT: New blogpost: Netflix – Application based routing over another VPN
Edit September 24, 2013: I did some more digging. It turns out the rp_filtering will by defintion have to be disabled. Therefore, the section ‘RP_Filter’ has been added.
Edit September 26, 2013: Added the keep-alive statement to the OpenVPN config so there are less timeouts and your routes etc should be more stable due to less changing IP’s.