-
Notifications
You must be signed in to change notification settings - Fork 1
/
protonvpn-applet.py
executable file
·377 lines (295 loc) · 12.5 KB
/
protonvpn-applet.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
#!/usr/bin/env python3
import sys
import subprocess
import functools
from enum import Enum
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QSystemTrayIcon, QMenu, QAction, qApp, QMessageBox
from PyQt5.QtCore import QSize, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from protonvpn_cli import utils, country_codes
from protonvpn_cli.utils import is_connected
PROTONVPN_APPLET_VERSION = "0.1.7"
class VPNStatusException(Exception):
"""General exception to throw when anything goes wrong
"""
class VPNCommand(Enum):
"""Commands to run the CLI
"""
status = 'protonvpn s'
connect_fastest = 'protonvpn c -f'
disconnect = 'protonvpn d'
version = 'protonvpn -v'
connect_random = 'protonvpn c -r'
connect_fastest_cc = 'protonvpn c --cc'
connect_fastest_p2p = 'protonvpn c --p2p'
connect_fastest_sc = 'protonvpn c --sc'
connect_fastest_tor = 'protonvpn c --tor'
reconnect = 'protonvpn r'
def check_single_instance():
"""Use pgrep to check if protonvpn-applet is already running
"""
pid = None
try:
pid = subprocess.run('pgrep protonvpn-applet'.split(), check=True, capture_output=True)
except subprocess.CalledProcessError:
try:
pid = subprocess.run('pgrep protonvpn-applet.py'.split(), check=True, capture_output=True)
except subprocess.CalledProcessError:
pass
if pid is not None:
print('There is an instance already running.')
sys.exit(1)
class Status(Enum):
"""Enum to keep track of the previous connection state
"""
connected = 'Connected'
disconnected = 'Disconnected'
class Polling(QThread):
"""Thread to check the VPN state every second and notifies on disconnection
"""
def __init__(self, applet):
QThread.__init__(self)
self.applet = applet
def __del__(self):
self.wait()
def run(self):
while self.applet.is_polling():
if is_connected():
self.applet.tray_icon.setIcon(QIcon('icons/16x16/protonvpn-connected.png'))
self.applet.previous_status = Status.connected
else:
# notify on disconnection
if self.applet.show_notifications() and self.applet.previous_status == Status.connected:
CheckStatus(self).start()
self.applet.tray_icon.setIcon(QIcon('icons/16x16/protonvpn-disconnected.png'))
self.applet.previous_status = Status.disconnected
self.sleep(1)
class ConnectVPN(QThread):
"""Thread to connect using the specified profile
"""
def __init__(self, applet, command):
QThread.__init__(self)
self.applet = applet
self.command = command
print(self.command)
def __del__(self):
self.wait()
def run(self):
subprocess.run([self.applet.auth] + self.command.split(), check=False)
self.applet.status_vpn()
class DisconnectVPN(QThread):
"""Thread to disconnect the VPN
"""
def __init__(self, applet):
QThread.__init__(self)
self.applet = applet
def __del__(self):
self.wait()
def run(self):
subprocess.run([self.applet.auth] + VPNCommand.disconnect.value.split(), check=False)
self.applet.status_vpn()
class ReconnectVPN(QThread):
"""Thread to connect using previously used profile
"""
def __init__(self, applet):
QThread.__init__(self)
self.applet = applet
def __del__(self):
self.wait()
def run(self):
subprocess.run([self.applet.auth] + VPNCommand.reconnect.value.split(), check=False)
self.applet.status_vpn()
class CheckStatus(QThread):
"""Thread to report ProtonVPN status
"""
def __init__(self, applet):
QThread.__init__(self)
self.applet = applet
def __del__(self):
self.wait()
def run(self):
result = subprocess.run(VPNCommand.status.value.split(), check=False, capture_output=True)
Notify.Notification.new(result.stdout.decode()).show()
class CheckProtonVPNVersion(QThread):
"""Thread to check version
"""
protonvpn_version_ready = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent=parent)
self.parent = parent
self.version = 'None'
def __del__(self):
self.wait()
def run(self):
self.version = subprocess.check_output(VPNCommand.version.value.split()).decode(sys.stdout.encoding)
self.protonvpn_version_ready.emit(self.version)
class PVPNApplet(QMainWindow):
"""Main applet body
"""
tray_icon = None
polling = True
previous_status = None
#auth = 'pkexec'
auth = 'sudo'
# Override the class constructor
def __init__(self):
super(PVPNApplet, self).__init__()
self.country_codes = country_codes # Keep a list of country codes
# Init QSystemTrayIcon
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon('icons/16x16/protonvpn-disconnected.png'))
# Init libnotify
Notify.init('ProtonVPN')
# Refresh server list, store the resulting servers so we can populate the menu
self.servers = self.update_available_servers()
# Menu actions
connect_fastest_action = QAction('Connect fastest', self)
reconnect_action = QAction('Reconnect', self)
disconnect_action = QAction('Disconnect', self)
status_action = QAction('Status', self)
connect_fastest_sc_action = QAction('Secure Core', self)
connect_fastest_p2p_action = QAction('P2P', self)
connect_fastest_tor_action = QAction('Tor', self)
connect_random_action = QAction('Random', self)
show_protonvpn_applet_version_action = QAction('About ProtonVPN-Applet', self)
show_protonvpn_version_action = QAction('About ProtonVPN', self)
quit_action = QAction('Exit', self)
self.show_notifications_action = QAction('Show Notifications')
self.show_notifications_action.setCheckable(True)
self.show_notifications_action.setChecked(False)
# Triggers
quit_action.triggered.connect(qApp.quit)
connect_fastest_action.triggered.connect(self.connect_fastest)
disconnect_action.triggered.connect(self.disconnect_vpn)
status_action.triggered.connect(self.status_vpn)
show_protonvpn_applet_version_action.triggered.connect(self.show_protonvpn_applet_version)
show_protonvpn_version_action.triggered.connect(self.get_protonvpn_version)
connect_fastest_sc_action.triggered.connect(self.connect_fastest_sc)
connect_fastest_p2p_action.triggered.connect(self.connect_fastest_p2p)
connect_fastest_tor_action.triggered.connect(self.connect_fastest_tor)
connect_random_action.triggered.connect(self.connect_random)
reconnect_action.triggered.connect(self.reconnect_vpn)
# Generate connection menu for specific countries
connect_country_actions = []
for country_name in self.get_available_countries(self.servers):
# Get the ISO-3166 Alpha-2 country code
country_name_to_code = {v: k for k, v in country_codes.country_codes.items()}
country_code = country_name_to_code[country_name]
# Dynamically create functions for connecting to each country; each function just passes its respective
# country code to `self.connect_fastest_cc()`
setattr(self, f'connect_fastest_{country_code}', functools.partial(self.connect_fastest_cc, country_code))
# Generate an action for each country; set up the trigger; append to actions list
country_action = QAction(f'{country_name}', self)
country_action.triggered.connect(getattr(self, f'connect_fastest_{country_code}'))
connect_country_actions.append(country_action)
# Create a scrollable country connection menu
connect_country_menu = QMenu("Country...", self)
connect_country_menu.setStyleSheet('QMenu { menu-scrollable: 1; }')
connect_country_menu.addActions(connect_country_actions)
# Generate connection menu
connection_menu = QMenu("Other connections...", self)
connection_menu.addMenu(connect_country_menu)
connection_menu.addAction(connect_fastest_sc_action)
connection_menu.addAction(connect_fastest_p2p_action)
connection_menu.addAction(connect_fastest_tor_action)
connection_menu.addAction(connect_random_action)
# Draw menu
tray_menu = QMenu()
tray_menu.addAction(connect_fastest_action)
tray_menu.addAction(reconnect_action)
tray_menu.addMenu(connection_menu)
tray_menu.addAction(disconnect_action)
tray_menu.addAction(status_action)
tray_menu.addSeparator()
tray_menu.addAction(self.show_notifications_action)
tray_menu.addAction(show_protonvpn_applet_version_action)
tray_menu.addAction(show_protonvpn_version_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
# Polling thread
self.start_polling()
def is_polling(self):
return self.polling
def kill_polling(self):
self.polling = False
def start_polling(self):
self.polling = True
self.polling_thread = Polling(self)
self.polling_thread.start()
def _connect_vpn(self, command):
self.kill_polling()
connect_thread = ConnectVPN(self, command)
connect_thread.finished.connect(self.start_polling)
connect_thread.start()
def connect_fastest(self):
self._connect_vpn(VPNCommand.connect_fastest.value)
def connect_fastest_p2p(self):
self._connect_vpn(VPNCommand.connect_fastest_p2p.value)
def connect_fastest_sc(self):
self._connect_vpn(VPNCommand.connect_fastest_sc.value)
def connect_fastest_cc(self, cc):
command = VPNCommand.connect_fastest_cc.value + f' {cc}'
self._connect_vpn(command)
def connect_fastest_tor(self):
self._connect_vpn(VPNCommand.connect_fastest_tor.value)
def connect_random(self):
self._connect_vpn(VPNCommand.connect_random.value)
def disconnect_vpn(self):
disconnect_thread = DisconnectVPN(self)
disconnect_thread.start()
def status_vpn(self):
status_thread = CheckStatus(self)
status_thread.start()
def reconnect_vpn(self):
reconnect_thread = ReconnectVPN(self)
reconnect_thread.start()
# Override closeEvent to intercept the window closing event
def closeEvent(self, event):
event.ignore()
self.hide()
def show_notifications(self):
return self.show_notifications_action.isChecked()
def show_protonvpn_applet_version(self):
"""Show the protonvpn-applet version.
"""
name = '© 2020 Dónal Murray'
email = 'dmurray654@gmail.com'
github = 'https://github.com/seadanda/protonvpn-applet'
info = [f'<center>Version: {PROTONVPN_APPLET_VERSION}',
f'{name}',
f"<a href='{email}'>{email}</a>",
f"<a href='{github}'>{github}</a></center>"]
centered_text = f'<center>{"<br>".join(info)}</center>'
QMessageBox.information(self, 'protonvpn-applet', centered_text)
def get_protonvpn_version(self):
"""Start the CheckProtonVPNVersion thread; when it gets the version, it will call `self.show_protonvpn_version`
"""
print('called get_protonvpn_version')
check_protonvpn_version_thread = CheckProtonVPNVersion(self)
check_protonvpn_version_thread.protonvpn_version_ready.connect(self.show_protonvpn_version)
check_protonvpn_version_thread.start()
def show_protonvpn_version(self, version):
"""
Show the ProtonVPN version in a QMessageBox.
Parameters
----------
version : str
Version number to be shown.
"""
print('called show_protonvpn_version')
QMessageBox.information(self, 'ProtonVPN Version', f'Version: {version}')
def update_available_servers(self):
utils.pull_server_data()
return utils.get_servers()
@staticmethod
def get_available_countries(servers):
return sorted(list({utils.get_country_name(server['ExitCountry']) for server in servers}))
if __name__ == '__main__':
check_single_instance()
app = QApplication(sys.argv)
mw = PVPNApplet()
sys.exit(app.exec())