Easing VPN switching
Posted on April 16, 2020 • 2 min read • 324 wordsFollowing up to my entry about circumventing geoblocking, I’ve written a small python program solving my Netflix blocking problem.
I realized that Netflix is not working as they rigorously ban the use of VPNs or proxies. Hence, it does work when using my home server as gateway/router but not when I additionaly enable the VPN.
First, I tried to enable or disable the VPN when I see the corresponding DNS queries for either amazon or netflix services. However, it turned out that my TV communicates with each other service regardless of what I am currently using. Therefore it is not possible to identify the desired state.
To ease the trouble at least a bit, I wrote a small webserver which allows me to make the switch with the click on a bookmark.
The following code snippet shows a default setup for a Flask server in python. It listens on the defined IP address and port.
Flask Server
##!/usr/bin/env python3
from flask import Flask
import os
app = Flask(__name__)
if __name__ == '__main__':
app.run(host="192.168.42.19", port=5000)
To handle the VPN switching I just remove or add the ip rules to forward the traffic either to my wireguard table or using the default one. A very hacky check routine identifies the current routing state.
Handling the VPN
def enableVPNRoute():
print("enable VPN")
os.system("ip rule add iif enp3s0 lookup 51820")
os.system("ip rule flush")
pass
def disableVPNRoute():
print("disable VPN")
os.system("ip rule del iif enp3s0 lookup 51820")
pass
def isVPNOn():
ir = os.popen("ip rule").read()
return "iif" in ir
Finally, Flask introduces the app.route
directives which I now use to enable the VPN if the webserver is called as http://192.168.42.19:5000/amazon
or disabled as http://192.168.42.19:5000/netflix
.
Establishing the Routes
@app.route('/amazon')
def amazon():
if not isVPNOn():
enableVPNRoute()
return "Turned VPN on. Enjoy Amazon :-)"
@app.route('/netflix')
def netflix():
if isVPNOn():
disableVPNRoute()
return "Turned VPN off. Enjoy Netflix :-)"
Enjoy your next movies and shows 😉