-
Notifications
You must be signed in to change notification settings - Fork 0
/
TotpConnection.py
93 lines (76 loc) · 2.98 KB
/
TotpConnection.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import pyotp
import subprocess
import time
from config import Config
def connect(str_qr_code, static_password, auth_file, username, ovpn_file, utun):
otp = pyotp.TOTP(str_qr_code).now()
password = static_password + str(otp)
with open(auth_file, 'w') as auth_f:
auth_f.write(f'{username}\n')
auth_f.write(password)
if vpn_not_connected(utun):
subprocess.Popen(['sudo', 'openvpn', '--config', ovpn_file])
print(f'sudo openvpn --config {ovpn_file}')
wait_vpn_connection(utun)
def vpn_not_connected(utun):
try:
subprocess.check_call(['ifconfig', utun])
print(f'{utun} is already connected')
return False
except subprocess.CalledProcessError:
return True
def wait_vpn_connection(utun):
connected = False
begin = time.time()
while not connected and (time.time() - begin) < 60:
try:
subprocess.check_call(['ifconfig', utun])
connected = True
except subprocess.CalledProcessError:
time.sleep(1)
if not connected:
raise Exception(f'Connection timeout {utun}')
print(f'{utun} connected successfully')
time.sleep(2)
return True
def kill_zombie_process(ovpn_filename):
try:
pids = subprocess.check_output(['pgrep', 'openvpn'])
pids_list = [pid for pid in pids.decode().split('\n') if pid != '']
for pid in pids_list:
ps_info = subprocess.check_output(['ps', '-p', pid])
ps_info_list = ps_info.decode().split('\n')
for process_info in ps_info_list:
if ovpn_filename in process_info:
subprocess.check_call(['sudo', 'kill', pid])
print(f'{pid} has been successfully removed')
except subprocess.CalledProcessError:
print("can't kill zombie process")
return False
return True
def disconnect_zombie_vpn(utun1, utun2, internal_ovpn_file):
try:
subprocess.check_call(['ifconfig', utun1])
except subprocess.CalledProcessError:
try:
subprocess.check_call(['ifconfig', utun2])
kill_zombie_process(internal_ovpn_file)
except subprocess.CalledProcessError:
return False
return True
if __name__ == '__main__':
disconnect_zombie_vpn(Config.ExternalConnection.UTUN,
Config.InternalConnection.UTUN,
Config.InternalConnection.OVPN_FILE)
connect(Config.ExternalConnection.STR_QR_CODE,
Config.ExternalConnection.PASSWORD,
Config.ExternalConnection.AUTH_FILE,
Config.ExternalConnection.USERNAME,
Config.ExternalConnection.OVPN_FILE,
Config.ExternalConnection.UTUN)
connect(Config.InternalConnection.STR_QR_CODE,
Config.InternalConnection.PASSWORD,
Config.InternalConnection.AUTH_FILE,
Config.InternalConnection.USERNAME,
Config.InternalConnection.OVPN_FILE,
Config.InternalConnection.UTUN)