-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvpn-indicator
executable file
·235 lines (182 loc) · 7.91 KB
/
vpn-indicator
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
#!/usr/bin/python
import os
import glob
import time
import dbus
import gobject
import gtk
import pynotify
import appindicator
class GuiBuilder(object):
def __init__(self, path):
file_path = os.path.join(path)
self.builder = gtk.Builder()
self.builder.set_translation_domain('ubuntu-tweak')
self.builder.add_from_file(file_path)
self.builder.connect_signals(self)
for o in self.builder.get_objects():
if issubclass(type(o), gtk.Buildable):
name = gtk.Buildable.get_name(o)
setattr(self, name, o)
else:
print("WARNING: can not set name for non Buildable object: %s" % o)
def get_object(self, name):
return self.builder.get_object(name)
class LogWindow(gtk.Window, GuiBuilder):
def __init__(self):
gtk.Window.__init__(self)
if __file__ == '/usr/bin/vpn-indicator':
GuiBuilder.__init__(self, '/usr/share/vpn-indicator/vpn-indicator.ui')
else:
GuiBuilder.__init__(self, 'data/vpn-indicator.ui')
self.set_position(gtk.WIN_POS_CENTER)
self.set_size_request(800, 600)
self.set_title('VPN Indicator')
self.hpaned1.reparent(self)
self._update_model()
self.connect('delete-event', self.on_delete_event)
def on_delete_event(self, widget, event):
self.hide()
return True
def _update_model(self):
configs = glob.glob('/etc/openvpn/*.ovpn')
configs.sort()
for f in configs:
iter = self.config_model.append((f, os.path.splitext(os.path.basename(f))[0]))
# if it is the current config, select it
if os.path.realpath('/etc/openvpn/openvpn.conf') == f:
self.config_view.get_selection().select_iter(iter)
self.show_log()
def show_log(self):
data = os.popen('cat /var/log/syslog|grep "openvpn"').read()
text_buffer = self.log_view.get_buffer()
text_buffer.set_text(data)
iter_start, iter_end = text_buffer.get_bounds()
mark = text_buffer.create_mark(None, iter_end, False)
self.log_view.scroll_mark_onscreen(mark)
text_buffer.delete_mark(mark)
class VpnIndicator(appindicator.Indicator):
INTERFACE = "me.imtx.vpndaemon"
(STATUS_CONNECTING,
STATUS_CONNECTED,
STATUS_DISCONNECTED) = range(3)
_window = None
_current_config = None
_current_status = None
# animate_seq = ['changes-allow-symbolic', 'nm-vpn-standalone-lock']
animate_seq = ['nm-signal-00-secure', 'nm-signal-25-secure', 'nm-signal-50-secure', 'nm-signal-75-secure', 'nm-signal-100-secure']
def __init__(self):
appindicator.Indicator.__init__(self,
"vpn-indicator",
"changes-allow-symbolic",
appindicator.CATEGORY_APPLICATION_STATUS)
self.set_status(appindicator.STATUS_ACTIVE)
self.set_attention_icon("indicatr-messages-new")
self._init_daemon()
self._create_menu()
self._current_status = self.STATUS_CONNECTING
gobject.timeout_add(1000, self.checkvpn_status)
def checkvpn_status(self):
if self._current_config:
if not self.is_vpn_connected() and self.is_vpn_runned():
animate_icon = self.animate_seq[0]
self.animate_seq.append(self.animate_seq.pop(0))
self.set_icon(animate_icon)
elif not self.is_vpn_connected() and not self.is_vpn_runned():
self.set_icon("changes-allow-symbolic")
if self._current_status != self.STATUS_DISCONNECTED:
pynotify.Notification(self._current_config, 'VPN disconnected', 'vpn-indicator').show()
self._current_status = self.STATUS_DISCONNECTED
elif self.is_vpn_connected() and self.is_vpn_runned():
if self._current_status != self.STATUS_CONNECTED:
pynotify.Notification(self._current_config, 'VPN Connection Established', 'vpn-indicator').show()
self.set_icon('nm-vpn-standalone-lock')
self._current_status = self.STATUS_CONNECTED
return True
def is_vpn_connected(self):
return 'tun' in os.popen('ifconfig -s').read()
def is_vpn_runned(self):
return 'is running' in os.popen('service openvpn status').read()
def _create_menu(self):
menu = gtk.Menu()
first_item = None
configs = glob.glob('/etc/openvpn/*.ovpn')
configs.sort()
for f in configs:
short_name = os.path.splitext(os.path.basename(f))[0]
menu_items = gtk.RadioMenuItem(first_item,
short_name)
if first_item == None:
first_item = menu_items
if os.path.realpath('/etc/openvpn/openvpn.conf') == f:
self._current_config = short_name
menu_items.set_active(True)
menu.append(menu_items)
menu_items.connect("activate", self.on_vpnmenu_activated, f)
menu.append(gtk.SeparatorMenuItem())
start_menu = gtk.MenuItem('Start VPN')
start_menu.connect('activate', self.on_start_menu_activated)
menu.append(start_menu)
stop_menu = gtk.MenuItem('Stop VPN')
stop_menu.connect('activate', self.on_stop_menu_activated)
menu.append(stop_menu)
menu.append(gtk.SeparatorMenuItem())
log_menu = gtk.MenuItem('VPN Status...')
log_menu.connect('activate', self.on_log_menu_activated)
menu.append(log_menu)
quit_menu = gtk.MenuItem('Quit')
quit_menu.connect('activate', self.on_quit_menu_activated)
menu.append(quit_menu)
menu.show_all()
self.set_menu(menu)
def on_log_menu_activated(self, widget):
if self._window:
self._window.present()
else:
self._window = LogWindow()
self._window.show_all()
def on_quit_menu_activated(self, widget):
gtk.main_quit()
def on_start_menu_activated(self, widget):
self.do_start_vpn()
def on_stop_menu_activated(self, widget):
self.daemon.stop_vpn(dbus_interface='me.imtx.vpndaemon')
def on_vpnmenu_activated(self, widget, config_path):
if widget.get_active():
short_name = os.path.splitext(os.path.basename(config_path))[0]
self._current_config = short_name
if self.is_vpn_connected():
dialog = gtk.MessageDialog(buttons=gtk.BUTTONS_YES_NO)
dialog.set_markup('<b><big>VPN "%s" is connected</big></b>\n\nWould you like to turn off it, then connect to "%s"?' % (
self._current_config, short_name))
response = dialog.run()
dialog.destroy()
if response == gtk.RESPONSE_YES:
self.do_start_vpn(config_path)
return True
else:
self.do_start_vpn(config_path)
def do_start_vpn(self, config_path=None):
if config_path:
print 'load config: %s' % config_path
self.daemon.load_config(config_path, dbus_interface='me.imtx.vpndaemon')
self.daemon.stop_vpn(dbus_interface='me.imtx.vpndaemon')
while True:
if not self.is_vpn_runned():
print 'do_start_vpn'
self.daemon.start_vpn(dbus_interface='me.imtx.vpndaemon')
break
else:
print 'vpn is running, check status'
time.sleep(1)
def _init_daemon(self):
try:
systembus = dbus.SystemBus()
self.daemon = systembus.get_object('me.imtx.vpndaemon', '/Daemon')
except Exception, e:
print e
self.daemon = None
if __name__ == "__main__":
pynotify.init('vpn-indicator')
VpnIndicator()
gtk.main()