-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpia_action.py
More file actions
60 lines (51 loc) · 2.02 KB
/
pia_action.py
File metadata and controls
60 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
##TODO add checking to make sure it can connect to PIA
import os
import requests
import subprocess
import argparse
import logging
LOG = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
class PiaAction:
def __init__(self):
self.configpath = '/etc/openvpn/client/'
def select_connection(self):
files = os.listdir(self.configpath)
conn_dict = {}
i = 0
for file in files:
if file.endswith(".ovpn"):
conn_dict[i]=file
i+=1
print(conn_dict)
connection = conn_dict[int(input('Select Connection: '))]
PiaAction.connect_pia(connection,self.configpath)
def connect_pia(self,connection):
LOG.info('Attempting to connect to PIA...')
configpath = self.configpath
vpn = subprocess.Popen("/usr/bin/sudo /usr/sbin/openvpn --cd {confdir} --config {confdir}{connection}".format(confdir=configpath, connection=connection
), shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
pid = vpn.pid
@staticmethod
def disconnect_pia():
LOG.info('Disconnecting PIA VPN')
command = subprocess.run(["/usr/bin/sudo", "/usr/bin/pkill", "openvpn"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if command.returncode != 0:
LOG.error('Process not killed')
LOG.error(command.stderr)
else:
LOG.info("PIA Killed!: {}".format(command.sterr))
@staticmethod
def check_pia():
LOG.info('Checking status of PIA VPN')
adapters = []
for line in open('/proc/net/dev'):
if ':' in line:
adapters.append(line.split(':')[0].strip(' ').lower())
exists = [i for i in adapters if "tun" in i]
if exists:
return True
LOG.info('PIA is running!')
else:
LOG.warning('PIA is NOT running')
return False