aboutsummaryrefslogtreecommitdiffstats
path: root/Common/net.py
diff options
context:
space:
mode:
authorVolker Hoffmann <volker@cheleb.net>2016-06-08 09:22:18 +0200
committerVolker Hoffmann <volker@cheleb.net>2016-06-08 09:22:41 +0200
commitee17d85d319a69fc87871ad01e1278542e5f1151 (patch)
treedfc2cd80e46352f12779907a73a00069bcfd090c /Common/net.py
parent52919e3976ab2082c011af42ac83871fc009ae01 (diff)
feat: detect VPN status
Diffstat (limited to 'Common/net.py')
-rw-r--r--Common/net.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/Common/net.py b/Common/net.py
new file mode 100644
index 0000000..126bb5c
--- /dev/null
+++ b/Common/net.py
@@ -0,0 +1,54 @@
+"""
+Networking Stuff.
+"""
+
+import requests
+import socket
+
+
+def is_vpn_connected():
+ """
+ Determine whether our VPN is connected. Checks whether our IP address
+ comes from a pool of public addresses associated with our VPN provider.
+
+ @return: is_vpn_connected - Connection Status [Bool]
+ """
+
+ # List of public IPs associated with VPN nodes.
+ vpn_list = load_vpn_list()
+
+ # Resolve using socket to avoid getting an IPv6 address
+ r = requests.get("http://%s" % socket.gethostbyname('icanhazip.com'))
+ ip = r.text.split()[0]
+
+ if ip in vpn_list:
+ return True
+ else:
+ return False
+
+
+def load_vpn_list(fname='vpn_list'):
+ """
+ Load list of external IP addresses associated with a VPN provider.
+
+ Format is one IP per line, i.e.
+ 1.1.1.1
+ 1.1.1.2
+ ...
+
+ See also vpn_list_example.
+
+ @param: fname - File name for list of external IPs [String]
+ @returns: vpn_list - List of external IPs [List of String]
+ """
+
+ # Loading gives lines = [ '1.1.1.1\n', '1.1.1.2\n' ].
+ # After stripping, gives vpn_list = [ '1.1.1.1', '1.1.1.1' ].
+ with open(fname, 'r') as f:
+ lines = f.readlines()
+ vpn_list = []
+ for line in lines:
+ vpn_list.append(line.strip())
+
+ # Return
+ return vpn_list