-
Notifications
You must be signed in to change notification settings - Fork 71
/
vpn_indicator.py
executable file
·438 lines (360 loc) · 13.4 KB
/
vpn_indicator.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'duc_tin'
from queue import Empty, Queue
from threading import Thread
from subprocess import call, Popen, PIPE
import datetime
import select
import signal, os, sys
import socket
import time
def rep_time():
return str(datetime.datetime.now()).split('.')[0]
class InfoServer:
def __init__(self, port):
self.host = 'localhost'
self.port = port
self.backlog = 0
self.is_listening = False
self.is_connected = False
self.is_dead = False
self.client = None
self.sock = socket.socket()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_address = self.host, self.port
self.readlist = [self.sock] # use for select
def listen(self):
try:
self.sock.bind(self.server_address)
self.sock.listen(self.backlog)
print(rep_time(), 'listening')
return True
except OSError as e:
print(rep_time(), e)
return False
def accept_it(self):
if not self.is_connected:
self.client, addrr = self.sock.accept()
self.readlist.append(self.client)
self.is_connected = True
print(rep_time(), 'Server: Connected with %s:%s' % addrr)
return 'Connected'
else:
# reject all other request
client, addrr = self.sock.accept()
client.close()
return 'Another client tried to connect'
def recv_it(self):
data = []
while True:
char = self.client.recv(1)
if not char: # socket close signal
self.is_connected = False
self.readlist.remove(self.client)
print(rep_time(), 'main disconnected')
return ''
elif char == b'\n':
return b''.join(data).decode()
else:
data.append(char)
def check_io(self, q_info):
"""Receive information about vpn tunnel
:type q_info: Queue
"""
while True:
if self.is_dead:
return 0
# try to bind the socket, only run one time
while not self.is_listening:
self.is_listening = self.listen()
time.sleep(2)
# normal select protocol
readable, _, _ = select.select(self.readlist, [], [], 1)
for s in readable:
if s is self.sock:
# sigterm from indicator
if self.is_dead:
print(rep_time(), 'Server: Received dead signal')
self.sock.close()
# signal from client
else:
data = self.accept_it()
q_info.put(data)
else: # client sent something
try:
data = self.recv_it()
q_info.put(data)
except socket.error as e:
print(rep_time(), 'Client die unexpectedly')
self.is_connected = False
except Exception as e:
print(rep_time(), e)
def send(self, msg):
msg = msg.encode()
if msg == b'dead':
self.is_dead = True
if self.is_connected:
self.sock.shutdown(socket.SHUT_RDWR)
print("shutdown socket")
return True
elif self.is_connected:
try:
self.client.sendall(msg + b'\n')
return True
except socket.error:
return False
else:
return False
class InfoClient:
def __init__(self, port):
self.host = 'localhost'
self.port = port
self.sock = socket.socket()
self.server_address = self.host, port
self.is_connected = False
self.last_msg = ''
def connect(self):
while not self.is_connected:
try:
self.sock = socket.create_connection(self.server_address)
# print rep_time(), 'socket: connected'
self.is_connected = True
# update current status
if self.last_msg:
self.send(self.last_msg)
except socket.error as e:
# print rep_time(), str(e)
time.sleep(2)
def recv_it(self):
data = []
while True:
char = self.sock.recv(1)
if not char: # socket close signal
self.is_connected = False
return ''
elif char == b'\n':
return b''.join(data).decode()
else:
data.append(char)
def check_io(self, q_cmd):
"""Receive information about vpn tunnel
:type q_cmd: Queue
"""
while True:
if self.is_connected:
# check if there is cmd from indicator
readable, _, _ = select.select([self.sock], [], [])
try:
data = self.recv_it()
if data:
q_cmd.put(data)
except OSError as e:
print(rep_time(), 'Server die unexpectedly')
self.is_connected = False
else:
self.connect()
def send(self, msg):
self.last_msg = msg
if self.is_connected:
try:
msg = (msg + '\n').encode()
self.sock.sendall(msg)
return True
except OSError:
return False
else:
return False
class VPNIndicator:
def __init__(self, q_info, sender):
signal.signal(signal.SIGINT, self.handler)
signal.signal(signal.SIGTERM, self.handler)
# pipe for send/recv data to tcp server
self.q_info = q_info
self.send = sender
self.APPINDICATOR_ID = 'myappindicator'
self.icon1 = os.path.abspath('icon/connected.svg')
self.icon2 = os.path.abspath('icon/connectnot.svg')
self.icon345 = [os.path.abspath(icon) for icon in
['icon/connecting1.svg', 'icon/connecting2.svg', 'icon/connecting3.svg']]
self.hang = False
self.is_connecting = False
self.icon_th = 0
self.last_recv = ['']
self.indicator = appindicator.Indicator.new(self.APPINDICATOR_ID, self.icon2,
appindicator.IndicatorCategory.APPLICATION_STATUS)
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
# Add menu to indicator
self.indicator.set_menu(self.build_menu())
self.notifier = notify.Notification.new('', '', None)
self.notifier.set_timeout(2)
notify.init(self.APPINDICATOR_ID)
def run(self, *args):
GLib.timeout_add(1000, self.callback, *args)
GLib.timeout_add(500, self.blinking)
Gtk.main()
def blinking(self):
if self.is_connecting:
if self.icon_th == 3:
self.icon_th = 0
self.indicator.set_icon_full(self.icon345[self.icon_th], "")
self.icon_th += 1
return True
def reload(self, data_in):
if data_in:
print(rep_time(), data_in[:12])
self.last_recv = data_in.split(';')
if 'connecting' in data_in:
print(rep_time(), 'set blinking')
self.is_connecting = True
else:
self.is_connecting = False
if 'connected' in data_in:
self.hang = False
self.status('', self.last_recv)
elif 'successfully' in data_in:
self.indicator.set_icon_full(self.icon1, "")
self.status('', self.last_recv)
elif 'terminate' in data_in:
self.indicator.set_icon_full(self.icon2, "")
self.status('', ['terminate'])
elif 'Offline' in data_in and not self.hang:
self.indicator.set_icon_full(self.icon2, "")
self.status('', ["Offline"])
self.hang = True
elif 'main exit' in data_in:
self.quit()
# return True
def build_menu(self):
menu = Gtk.Menu()
# change focus to main program terminal
focus_main = Gtk.MenuItem(label='Show main')
focus_main.connect('activate', self.change_focus)
menu.append(focus_main)
menu.append(Gtk.SeparatorMenuItem())
# show status popup
current_status = Gtk.MenuItem(label='VPN Status')
current_status.connect('activate', self.status, self.last_recv)
menu.append(current_status)
# connect to the next vpn on the list
next_vpn = Gtk.MenuItem(label='Next VPN')
next_vpn.connect('activate', self.send_cmd, 'next')
menu.append(next_vpn)
menu.append(Gtk.SeparatorMenuItem())
# stop vpn
stop_vpn = Gtk.MenuItem(label='Stop VPN')
stop_vpn.connect('activate', self.send_cmd, 'stop')
menu.append(stop_vpn)
# reconnect the current one
reco_vpn = Gtk.MenuItem(label='ReConnect')
reco_vpn.connect('activate', self.send_cmd, 'reconnect')
menu.append(reco_vpn)
menu.append(Gtk.SeparatorMenuItem())
# quit button
item_quit = Gtk.MenuItem(label='Quit indicator')
item_quit.connect('activate', self.handler, '')
menu.append(item_quit)
menu.show_all()
return menu
def quit(self, source=None):
# send dead signal to tcp server
print('quit now 4')
self.send('dead')
print('quit now 3')
notify.uninit()
print('quit now 2')
Gtk.main_quit()
print('quit now 1')
def change_focus(self, menu_obj):
try:
call(['wmctrl', '-a', 'vpngate-with-proxy'])
except FileNotFoundError:
self.status('', ['wmctrl'])
def status(self, menu_obj, messages=''):
"""
:type messages: list
"""
if menu_obj:
messages = self.last_recv
self.notifier.close()
if 'connected' in messages[0]:
summary = 'Connected to main program'
body = ''
elif 'successfully' in messages[0]:
print(rep_time(), messages[1:])
tags = ['Ping:', 'Speed:', 'Up time:', 'Session:', 'Log:', 'Score:', 'Protocol:', 'Portal:']
msg = messages[1:3] + [item for items in zip(tags, messages[3:9]) for item in items]
summary = 'VPN tunnel established'
body = '''{:<20}{:<15} {:>19} {:<15}
{:<18} {:<8} Mbps
{:<18}{:<15} {:>20} {:<20}
{:<22}{:<15}
{:<21}{:<15}'''.format(*msg)
elif 'terminate' in messages[0]:
summary = 'VPN tunnel has broken'
body = 'Please choose a different server and try again'
elif 'Offline' in messages[0]:
summary = 'VPN program is offline'
body = "Click VPN indicator and choose 'Quit' to quit"
elif 'wmctrl' in messages[0]:
summary = "'wmctrl' not found"
body = "please do: sudo apt install wmctrl"
else:
summary = 'Status unknown'
body = "Waiting for connection from main program"
self.notifier.update(summary, body, icon=None)
self.notifier.show()
def handler(self, signal_num, frame):
if signal_num == signal.SIGINT:
print(rep_time(), 'Indicator: got SigInt')
else:
self.quit('')
def send_cmd(self, menu_obj, arg):
print(rep_time(), 'Indicator sent:', arg)
self.send(arg)
def callback(self):
try:
data = self.q_info.get_nowait()
self.reload(data)
except Empty:
pass
except Exception as e:
self.notifier.update("Error", str(e), icon=None)
self.notifier.show()
return True
if __name__ == '__main__':
try:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as appindicator
gi.require_version('Notify', '0.7')
from gi.repository import Notify as notify
satisfied = True
except (ImportError, ValueError):
print(' Lack of Gtk related modules! VPN indicator will not run!', file=sys.stderr)
print(' You should try "sudo apt-get install gir1.2-appindicator3-0.1 gir1.2-notify-0.7 python-gobject"',
file=sys.stderr)
satisfied = False
time.sleep(1)
if satisfied:
another_me = Popen(['pgrep', '-f', "vpn_indicator.py"], stdout=PIPE, universal_newlines=True).communicate()[0]
another_me = another_me.strip().split('\n')
if len(another_me) > 1:
print(rep_time(), 'exist another me', another_me[1:])
sys.exit()
# queue for interacting between indicator and server
q = Queue()
server = InfoServer(8088)
t = Thread(target=server.check_io, args=(q,)) # shouldn't be daemon
t.daemon = True
t.start()
indicator = VPNIndicator(q, server.send)
try:
indicator.run()
except Exception as e:
print(rep_time(), 'Indicator:', e)
if not server.is_dead:
server.send('dead')
t.join()