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
|
"""generic watcher object that keeps track of connection status"""
# This should be deprecated in favor of daemon mode + management
# interface. But we can leave it here for debug purposes.
class EIPConnectionStatus(object):
"""
Keep track of client (gui) and openvpn
states.
These are the OpenVPN states:
CONNECTING -- OpenVPN's initial state.
WAIT -- (Client only) Waiting for initial response
from server.
AUTH -- (Client only) Authenticating with server.
GET_CONFIG -- (Client only) Downloading configuration options
from server.
ASSIGN_IP -- Assigning IP address to virtual network
interface.
ADD_ROUTES -- Adding routes to system.
CONNECTED -- Initialization Sequence Completed.
RECONNECTING -- A restart has occurred.
EXITING -- A graceful exit is in progress.
We add some extra states:
DISCONNECTED -- GUI initial state.
UNRECOVERABLE -- An unrecoverable error has been raised
while invoking openvpn service.
"""
CONNECTING = 1
WAIT = 2
AUTH = 3
GET_CONFIG = 4
ASSIGN_IP = 5
ADD_ROUTES = 6
CONNECTED = 7
RECONNECTING = 8
EXITING = 9
# gui specific states:
UNRECOVERABLE = 11
DISCONNECTED = 0
def __init__(self, callbacks=None):
"""
EIPConnectionStatus is initialized with a tuple
of signals to be triggered.
:param callbacks: a tuple of (callable) observers
:type callbacks: tuple
"""
# (callbacks to connect to signals in Qt-land)
self.current = self.DISCONNECTED
self.previous = None
self.callbacks = callbacks
def get_readable_status(self):
# XXX DRY status / labels a little bit.
# think we'll want to i18n this.
human_status = {
0: 'disconnected',
1: 'connecting',
2: 'waiting',
3: 'authenticating',
4: 'getting config',
5: 'assigning ip',
6: 'adding routes',
7: 'connected',
8: 'reconnecting',
9: 'exiting',
11: 'unrecoverable error',
}
return human_status[self.current]
def get_state_icon(self):
"""
returns the high level icon
for each fine-grain openvpn state
"""
connecting = (self.CONNECTING,
self.WAIT,
self.AUTH,
self.GET_CONFIG,
self.ASSIGN_IP,
self.ADD_ROUTES)
connected = (self.CONNECTED,)
disconnected = (self.DISCONNECTED,
self.UNRECOVERABLE)
# this can be made smarter,
# but it's like it'll change,
# so +readability.
if self.current in connecting:
return "connecting"
if self.current in connected:
return "connected"
if self.current in disconnected:
return "disconnected"
def set_vpn_state(self, status):
"""
accepts a state string from the management
interface, and sets the internal state.
:param status: openvpn STATE (uppercase).
:type status: str
"""
if hasattr(self, status):
self.change_to(getattr(self, status))
def set_current(self, to):
"""
setter for the 'current' property
:param to: destination state
:type to: int
"""
self.current = to
def change_to(self, to):
"""
:param to: destination state
:type to: int
"""
if to == self.current:
return
changed = False
from_ = self.current
self.current = to
# We can add transition restrictions
# here to ensure no transitions are
# allowed outside the fsm.
self.set_current(to)
changed = True
#trigger signals (as callbacks)
#print('current state: %s' % self.current)
if changed:
self.previous = from_
if self.callbacks:
for cb in self.callbacks:
if callable(cb):
cb(self)
def status_watcher(cs, line):
"""
a wrapper that calls to ConnectionStatus object
:param cs: a EIPConnectionStatus instance
:type cs: EIPConnectionStatus object
:param line: a single line of the watched output
:type line: str
"""
#print('status watcher watching')
# from the mullvad code, should watch for
# things like:
# "Initialization Sequence Completed"
# "With Errors"
# "Tap-Win32"
if "Completed" in line:
cs.change_to(cs.CONNECTED)
return
if "Initial packet from" in line:
cs.change_to(cs.CONNECTING)
return
|