aboutsummaryrefslogtreecommitdiffstats
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
parent52919e3976ab2082c011af42ac83871fc009ae01 (diff)
feat: detect VPN status
-rw-r--r--Common/net.py54
-rw-r--r--ticker.py10
-rw-r--r--vpn_list_example2
3 files changed, 66 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
diff --git a/ticker.py b/ticker.py
index a75e4ef..f830a70 100644
--- a/ticker.py
+++ b/ticker.py
@@ -4,6 +4,7 @@ Read, Parse, Post Stats to InfluxDB.
import Common.sensors as sensors
import Common.influx as influx
+import Common.net as net
# #############################################################################
@@ -12,6 +13,11 @@ import Common.influx as influx
readings, epochs = sensors.get_netatmo_readings()
# #############################################################################
+# Check VPN Status
+# #############################################################################
+is_vpn_connected = net.is_vpn_connected()
+
+# #############################################################################
# Build Data Post
# NB: For InfluxDB >=0.9.3, integer data points require a trailing i.
# For example, ncpus_allocated,parititon=cpu value=5i
@@ -28,6 +34,10 @@ for module_name, module_values in zip(readings.keys(), readings.values()):
epochs[module_name] )
lines.append(line)
+# VPN Status
+line = "is_vpn_connected value=%s" % is_vpn_connected
+lines.append(line)
+
# Join
data = "\n".join(lines)
diff --git a/vpn_list_example b/vpn_list_example
new file mode 100644
index 0000000..1499e9e
--- /dev/null
+++ b/vpn_list_example
@@ -0,0 +1,2 @@
+1.1.1.1
+1.1.1.2