Easing VPN switching

Posted on April 16, 2020 • 2 min read • 343 words

Easing VPN switching

Following 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.

Flask Server

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

1##!/usr/bin/env python3
2from flask import Flask
3import os
4
5app = Flask(__name__)
6
7if __name__ == '__main__':
8    app.run(host="192.168.42.19", port=5000)

Handling the VPN

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

 1def enableVPNRoute():
 2    print("enable VPN")
 3    os.system("ip rule add iif enp3s0 lookup 51820")
 4    os.system("ip rule flush")
 5    pass
 6
 7
 8def disableVPNRoute():
 9    print("disable VPN")
10    os.system("ip rule del iif enp3s0 lookup 51820")
11    pass
12
13
14def isVPNOn():
15    ir = os.popen("ip rule").read()
16    return "iif" in ir

Establishing the Routes

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

 1@app.route('/amazon')
 2def amazon():
 3    if not isVPNOn():
 4        enableVPNRoute()
 5    return "Turned VPN on. Enjoy Amazon :-)"
 6
 7
 8@app.route('/netflix')
 9def netflix():
10    if isVPNOn():
11        disableVPNRoute()
12    return "Turned VPN off. Enjoy Netflix :-)"
13Enjoy your next movies and shows 😉
Nextlevel v/Peter Schneider

I work on everything cyber security and development, CVR: 42051993, mail: info@nextlevel-blog.de, phone: 60 59 76 35