-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.py
93 lines (76 loc) · 3.24 KB
/
setup.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
import os
import wx
import PySimpleGUIWx as sg
from ultrasurf import Proxy
class ProxyConnection(object):
def __init__(self):
super(ProxyConnection, self).__init__()
self.CONNECT = 'Connect'
self.DISCONNECT = 'Disconnect'
self.EXIT = 'Exit'
# tray icon details
self.icon_dir = os.path.join(os.getcwd().replace(os.sep, '/')+'/assets/icons')
self.on_icon = os.path.join(self.icon_dir+'/on.ico')
self.off_icon = os.path.join(self.icon_dir+'/off.ico')
# ##
# system tray items
self.context_items = [self.CONNECT, self.DISCONNECT, '---', self.EXIT]
self.menu_def = ['UNUSED', self.context_items]
self.tray = sg.SystemTray(menu=self.menu_def, filename=self.off_icon)
# ##
# proxy details
self.proxy = Proxy()
# ##
# flags
self.is_connected = False
def _toggle_tray_icon(self, is_connected=True):
tooltip_msg = 'Cloud {}'.format('connected' if is_connected else 'disconnected')
self.tray.Update(menu=self.menu_def, filename=self.on_icon if is_connected else self.off_icon, tooltip=tooltip_msg)
self.is_connected = is_connected
return True
def _connect(self):
try:
if self.is_connected:
self.tray.ShowMessage('Already Connected', 'Start exploring', filename=self.on_icon)
return True
# show tray notification and try to connect with proxy
self.tray.ShowMessage('Connecting', 'Please wait..')
self._toggle_tray_icon(is_connected=True)
self.proxy._run()
self.tray.ShowMessage('Connected', 'Start exploring', filename=self.on_icon)
except Exception as ex:
self.tray.ShowMessage('Error', '{}'.format(ex))
raise ex
def _disconnect(self):
try:
if not self.is_connected:
self.tray.ShowMessage('Disconnected', 'Please connect to explore', filename=self.on_icon)
return True
# on disconnecting revert system proxy settings and disconnect
self.tray.ShowMessage('Reverting', 'Please wait..', filename=self.off_icon)
self._toggle_tray_icon(is_connected=False)
self.proxy._revert_system_proxy()
self.tray.ShowMessage('Disconnected', 'All settings reverted', filename=self.off_icon)
except Exception as ex:
self.tray.ShowMessage('Error', '{}'.format(ex))
raise ex
def _start(self):
if not self.is_connected:
self._connect()
while True:
event = self.tray.Read()
if event == self.EXIT:
self._disconnect()
self.tray.ShowMessage('See you soon!', 'Bye', filename=self.off_icon)
break
elif event == self.CONNECT:
self._connect()
elif event == self.DISCONNECT:
self._disconnect()
if __name__ == "__main__":
proxy_conn = ProxyConnection()
try:
proxy_conn.proxy._revert_system_proxy()
proxy_conn._start()
except Exception as ex:
proxy_conn.proxy._revert_system_proxy()