-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairvpn_toggler.py
executable file
·335 lines (272 loc) · 11.5 KB
/
airvpn_toggler.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
import sys
import os
import subprocess
import random
import shelve
import signal
import time
# Commands
EXTERNAL_IP_CMD = ["wget", "http://ipinfo.io/ip", "-qO", "-"]
# STUNNEL_CMD = ["nohup", "stunnel", "{}", "&"]
# OPENVPN_CMD = ["nohup", "openvpn", "--config", "{}", "&"]
STUNNEL_CMD = 'nohup stunnel {stunnel_config}.ssl &'
OPENVPN_CMD = 'nohup openvpn --config {ovpn_config}.ovpn &'
SYSTRAY_CMD = ["nohup", "python", "setup_indicator.py"]
GEOLOCATE_CMD = 'echo "Looking for self ip geolocation..." && curl -s ipinfo.io/"$(wget http://ipinfo.io/ip -qO -)" | egrep -w "city|region|country"'
AIRVPN_CONFIGS_PATH = os.getcwd()
AIRVPN_DNS_STRING = """nameserver 10.4.0.1\nnameserver 10.5.0.1"""
OPEN_DNS_STRING = """nameserver 127.0.1.1\nnameserver 208.67.222.222\nnameserver 208.67.220.220"""
"Make sure that the user is a sudoer"
if not os.geteuid() == 0:
sys.exit("\nOnly root can run this script\n")
"""
This function turns on the vpn connection via SSL tunnel.
"""
def turn_on(show_systray):
initial_ip = subprocess.check_output(EXTERNAL_IP_CMD)
print("##########################################################\n")
print("# Initial external IP: {}".format(initial_ip))
print("# Initial geolocation:")
os.system(GEOLOCATE_CMD)
print("##########################################################\n")
print("")
# Letting the user choose country from available countries configs
countries_list = get_countries()
country_code = raw_input(
"Which country would you like to exit from?\n({})\n".format(", ".join(countries_list)))
# picking random server from our desired country
config_path = random.choice(get_config_path(country_code.upper()))
config_path = os.path.join(AIRVPN_CONFIGS_PATH, config_path)
# Setting up the appropriate commands with the desired server config
# STUNNEL_CMD[2] = config_path + ".ssl"
# OPENVPN_CMD[3] = config_path + ".ovpn"
stunnel_cmd = STUNNEL_CMD.format(
stunnel_config=config_path.replace('UDP', 'SSL'))
openvpn_cmd = OPENVPN_CMD.format(ovpn_config=config_path)
print("ok, attempting to exit via {}".format(config_path))
print('stunnel_cmd: {}'.format(stunnel_cmd))
print('openvpn_cmd: {}'.format(openvpn_cmd))
stunnel_pid = subprocess.Popen(stunnel_cmd,
stdout=open('/tmp/stunnel.log', 'w'),
stderr=open('/tmp/stunnel.log', 'a'),
# stderr=open('/tmp/stunnel_err.log', 'a'),
preexec_fn=os.setpgrp,
shell=True)
# Waiting for the stunnel process to complete the init
init_success = wait_for_process_init(
"/tmp/stunnel.log", "Configuration successful", "stunnel")
print('Running: {}'.format(openvpn_cmd))
openvpn_pid = subprocess.Popen(openvpn_cmd,
stdout=open('/tmp/openvpn.log', 'w'),
stderr=open('/tmp/openvpn_err.log', 'a'),
preexec_fn=os.setpgrp,
shell=True)
print("pids - stunnel: {}, openvpn: {}".format(stunnel_pid.pid, openvpn_pid.pid))
# Storing the pids of the background processes for shutdown later purpose
pids_shelve = shelve.open("pids.db")
pids_shelve["pids"] = {"stunnel_pid": stunnel_pid.pid,
"openvpn_pid": openvpn_pid.pid}
pids_shelve.close()
# Waiting for the openvpn process to complete the init
init_success = wait_for_process_init(
"/tmp/openvpn.log", "Initialization Sequence Completed", "openvpn")
# Validating the connection and setting system tray indicator
try:
final_ip = subprocess.check_output(EXTERNAL_IP_CMD)
if initial_ip == final_ip:
raise Exception("Same ip: {}".format(final_ip))
except Exception as e:
print("Error: {}".format(e))
print("failed to change IP using airvpn")
subprocess.Popen(['notify-send', 'Airvpn status', 'Airvpn setup failed',
'-t', '2000', '--icon=dialog-information'])
set_resolv_conf(False)
pids_shelve = shelve.open("pids.db")
pids_shelve["pids"] = []
pids_shelve.close()
os.kill(stunnel_pid.pid, signal.SIGTERM)
os.kill(openvpn_pid.pid, signal.SIGTERM)
exit()
# Sucess. Changing the DNS at /etc/resolve.conf to airvpn DNS to avoid DNS
# leaks
set_resolv_conf(True)
if show_systray:
# Calling the systray indicator setter with country code to appear on
# menu
SYSSTRAY_CMD_COUNTRY = SYSTRAY_CMD + \
list("{}".format(country_code.upper()))
systray_pid = subprocess.Popen(SYSSTRAY_CMD_COUNTRY,
stdout=open('/tmp/systray.log', 'w'),
stderr=open(
'/tmp/systray_err.log', 'a'),
preexec_fn=os.setpgrp)
# Storing the pid of the systray background processes for shutdown later
# purpose
pids_shelve = shelve.open("pids.db")
pids = pids_shelve["pids"]
if show_systray:
pids["systray_icon_pid"] = systray_pid.pid
pids_shelve["pids"] = pids
pids_shelve.close()
# Notifying the user on the successful setup
subprocess.Popen(['notify-send', 'Airvpn status', 'Airvpn setup sucess',
'-t', '5000', '--icon=dialog-information'])
print("##########################################################\n")
print("Sucess! Final external IP: {}.".format(final_ip.strip("\n")))
os.system(GEOLOCATE_CMD)
print("##########################################################\n")
print("")
"""
Turning off an active vpn+stunnel connection.
"""
def turn_off(show_systray):
# Fetching the pids from the shelve
pids_shelve = shelve.open("pids.db")
if pids_shelve.has_key("pids"):
pids = pids_shelve["pids"]
else:
pids = None
# Validating that the processes are running and if true, klling them and
# the systray icon
stunnel_status, stunnel_pid = is_process_running("stunnel")
openvpn_status, openvpn_pid = is_process_running("openvpn")
try:
assert len(
pids) > 0 or stunnel_status or openvpn_status, "couldn't find any trace of airvpn running"
except AssertionError as e:
exit(repr(e.args[0]))
if stunnel_status:
os.kill(int(stunnel_pid), signal.SIGTERM)
if openvpn_status:
os.kill(int(openvpn_pid), signal.SIGTERM)
if show_systray and pids.has_key("systray_icon_pid"):
os.kill(int(pids["systray_icon_pid"]), signal.SIGTERM)
# Emptying the shelve
pids_shelve["pids"] = []
pids_shelve.close()
# Changing the DNS at /etc/resolve.conf to opendns
set_resolv_conf(False)
# Notifying the user on the successful turning off
subprocess.Popen(['notify-send', 'Airvpn status', 'Airvpn is turned off',
'-t', '5000', '--icon=dialog-information'])
print("\nTurned off Airvpn. Validating...")
os.system(GEOLOCATE_CMD)
print("")
########################
### Helper functions ###
########################
"""
This (blocking) function accepts file path, pattern, and (optional) process name.
It then wait for the last line of the file to match the pattern.
"""
def wait_for_process_init(fpath, pattern, process_name=None):
last_line = tail(fpath, 1)
if len(last_line) == 0: # wait a second, file is empty
time.sleep(3)
last_line = tail(fpath, 1)
while not pattern in last_line[0]:
last_line = tail(fpath, 1)
if process_name:
msg = "Waiting for {} to finish init".format(process_name)
subprocess.Popen(
['notify-send', 'Airvpn status', '{}'.format(msg), '-t', '2000', '--icon=dialog-information'])
else:
subprocess.Popen(
['notify-send', 'Airvpn status', 'Waiting for background process to finish init', '-t', '2000', '--icon=dialog-information'])
time.sleep(2)
return True
"""
Checking if the proc_name is running
"""
def is_process_running(proc_name):
try:
_pid = subprocess.check_output(["pidof", proc_name])
except subprocess.CalledProcessError:
return False, None
if _pid and len(_pid) > 0:
return True, _pid.strip("\n")
"""
Returns a list of config files for given country_code
"""
def get_config_path(country_code):
res = []
for config_file in os.listdir(AIRVPN_CONFIGS_PATH):
# if "AirVPN_{}".format(country_code) in config_file and "SSL" in config_file:
if "AirVPN_{}".format(country_code) in config_file and "UDP" in config_file:
res.append(os.path.splitext(config_file)[0])
if len(res) == 0:
sys.exit("No config file for {} could be found. Exiting".format(country_code))
return res
"""
Extract country codes from Airvpn config file names
"""
def get_countries():
res = set()
for config_file in os.listdir(AIRVPN_CONFIGS_PATH):
if "AirVPN_" in config_file and "SSL" in config_file:
# print "config: {}".format(config_file)
country = config_file.split("AirVPN_")[1].split("-")[0]
res.add(country)
if len(res) == 0:
sys.exit("No config files could be found. Exiting")
return list(res)
"""
Changes the /etc/resolv.conf file to match opendns/airvpn dns.
@param state - True => Airvpn DNS. False => opendns
"""
def set_resolv_conf(state):
with open("/etc/resolv.conf", "w") as fout:
if state:
fout.write(AIRVPN_DNS_STRING)
else:
fout.write(OPEN_DNS_STRING)
"""
Returns the last lines of a given file.
Adapted from http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail#answer-136368
"""
def tail(f, lines=20):
total_lines_wanted = lines
BLOCK_SIZE = 1024
f = open(f, "r")
f.seek(0, 2)
block_end_byte = f.tell()
lines_to_go = total_lines_wanted
block_number = -1
blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting
# from the end of the file
while lines_to_go > 0 and block_end_byte > 0:
if (block_end_byte - BLOCK_SIZE > 0):
# read the last block we haven't yet read
f.seek(block_number * BLOCK_SIZE, 2)
blocks.append(f.read(BLOCK_SIZE))
else:
# file too small, start from begining
f.seek(0, 0)
# only read what was not read
blocks.append(f.read(block_end_byte))
lines_found = blocks[-1].count('\n')
lines_to_go -= lines_found
block_end_byte -= BLOCK_SIZE
block_number -= 1
all_read_text = ''.join(reversed(blocks))
f.close()
return all_read_text.splitlines()[-total_lines_wanted:]
if __name__ == '__main__':
if sys.version_info > (3, 0):
exit("\nCurrently, this program runs only under python2\n")
if "show" in sys.argv:
show_systray = True
import setup_indicator
else:
show_systray = False
if "on" in sys.argv:
if is_process_running("stunnel")[0] or is_process_running("openvpn")[0]:
exit("Airvpn is already activated")
turn_on(show_systray)
elif "off" in sys.argv:
if not is_process_running("stunnel")[0] and not is_process_running("openvpn")[0]:
exit("Airvpn is already deactivated")
turn_off(show_systray)
else:
print("USAGE: airvpn_toggler.py <on/off> <optional: show>")