summaryrefslogtreecommitdiff
path: root/src/leap/eip/eipconnection.py
blob: d012c56724e3e44d841b014f29d39526314ec5e2 (plain)
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
"""
EIP Connection Class
"""
from __future__ import (absolute_import,)
import logging
import Queue
import sys
import time

from dateutil.parser import parse as dateparse

from leap.eip.checks import ProviderCertChecker
from leap.eip.checks import EIPConfigChecker
from leap.eip import config as eipconfig
from leap.eip import exceptions as eip_exceptions
from leap.eip.openvpnconnection import OpenVPNConnection

logger = logging.getLogger(name=__name__)


class StatusMixIn(object):

    # a bunch of methods related with querying the connection
    # state/status and displaying useful info.
    # Needs to get clear on what is what, and
    # separate functions.
    # Should separate EIPConnectionStatus (self.status)
    # from the OpenVPN state/status command and parsing.

    ERR_CONNREFUSED = False

    def connection_state(self):
        """
        returns the current connection state
        """
        return self.status.current

    def get_icon_name(self):
        """
        get icon name from status object
        """
        return self.status.get_state_icon()

    def get_leap_status(self):
        return self.status.get_leap_status()

    def poll_connection_state(self):
        """
        """
        try:
            state = self.get_connection_state()
        except eip_exceptions.ConnectionRefusedError:
            # connection refused. might be not ready yet.
            if not self.ERR_CONNREFUSED:
                logger.warning('connection refused')
                self.ERR_CONNREFUSED = True
            return
        if not state:
            #logger.debug('no state')
            return
        (ts, status_step,
         ok, ip, remote) = state
        self.status.set_vpn_state(status_step)
        status_step = self.status.get_readable_status()
        return (ts, status_step, ok, ip, remote)

    def make_error(self):
        """
        capture error and wrap it in an
        understandable format
        """
        # mostly a hack to display errors in the debug UI
        # w/o breaking the polling.
        #XXX get helpful error codes
        self.with_errors = True
        now = int(time.time())
        return '%s,LAUNCHER ERROR,ERROR,-,-' % now

    def state(self):
        """
        Sends OpenVPN command: state
        """
        state = self._send_command("state")
        if not state:
            return None
        if isinstance(state, str):
            return state
        if isinstance(state, list):
            if len(state) == 1:
                return state[0]
            else:
                return state[-1]

    def vpn_status(self):
        """
        OpenVPN command: status
        """
        status = self._send_command("status")
        return status

    def vpn_status2(self):
        """
        OpenVPN command: last 2 statuses
        """
        return self._send_command("status 2")

    #
    # parse  info as the UI expects
    #

    def get_status_io(self):
        status = self.vpn_status()
        if isinstance(status, str):
            lines = status.split('\n')
        if isinstance(status, list):
            lines = status
        try:
            (header, when, tun_read, tun_write,
             tcp_read, tcp_write, auth_read) = tuple(lines)
        except ValueError:
            return None

        when_ts = dateparse(when.split(',')[1]).timetuple()
        sep = ','
        # XXX clean up this!
        tun_read = tun_read.split(sep)[1]
        tun_write = tun_write.split(sep)[1]
        tcp_read = tcp_read.split(sep)[1]
        tcp_write = tcp_write.split(sep)[1]
        auth_read = auth_read.split(sep)[1]

        # XXX this could be a named tuple. prettier.
        return when_ts, (tun_read, tun_write, tcp_read, tcp_write, auth_read)

    def get_connection_state(self):
        state = self.state()
        if state is not None:
            ts, status_step, ok, ip, remote = state.split(',')
            ts = time.gmtime(float(ts))
            # XXX this could be a named tuple. prettier.
            return ts, status_step, ok, ip, remote


class EIPConnection(OpenVPNConnection, StatusMixIn):
    """
    Aka conductor.
    Manages the execution of the OpenVPN process, auto starts, monitors the
    network connection, handles configuration, fixes leaky hosts, handles
    errors, etc.
    Status updates (connected, bandwidth, etc) are signaled to the GUI.
    """

    # XXX change name to EIPConductor ??

    def __init__(self,
                 provider_cert_checker=ProviderCertChecker,
                 config_checker=EIPConfigChecker,
                 *args, **kwargs):
        #self.settingsfile = kwargs.get('settingsfile', None)
        #self.logfile = kwargs.get('logfile', None)
        self.provider = kwargs.pop('provider', None)
        self._providercertchecker = provider_cert_checker
        self._configchecker = config_checker

        self.error_queue = Queue.Queue()

        status_signals = kwargs.pop('status_signals', None)
        self.status = EIPConnectionStatus(callbacks=status_signals)

        checker_signals = kwargs.pop('checker_signals', None)
        self.checker_signals = checker_signals

        self.init_checkers()

        host = eipconfig.get_socket_path()
        kwargs['host'] = host

        super(EIPConnection, self).__init__(*args, **kwargs)

    def connect(self, **kwargs):
        """
        entry point for connection process
        """
        # in OpenVPNConnection
        self.try_openvpn_connection()

    def disconnect(self, shutdown=False):
        """
        disconnects client
        """
        self.terminate_openvpn_connection(shutdown=shutdown)
        self.status.change_to(self.status.DISCONNECTED)

    def has_errors(self):
        return True if self.error_queue.qsize() != 0 else False

    def init_checkers(self):
        """
        initialize checkers
        """
        self.provider_cert_checker = self._providercertchecker(
            domain=self.provider)
        self.config_checker = self._configchecker(domain=self.provider)

    def set_provider_domain(self, domain):
        """
        sets the provider domain.
        used from the first run wizard when we launch the run_checks
        and connect process after having initialized the conductor.
        """
        # This looks convoluted, right.
        # We have to reinstantiate checkers cause we're passing
        # the domain param that we did not know at the beginning
        # (only for the firstrunwizard case)
        self.provider = domain
        self.init_checkers()

    def run_checks(self, skip_download=False, skip_verify=False):
        """
        run all eip checks previous to attempting a connection
        """
        logger.debug('running conductor checks')

        def push_err(exc):
            # keep the original traceback!
            exc_traceback = sys.exc_info()[2]
            self.error_queue.put((exc, exc_traceback))

        try:
            # network (1)
            if self.checker_signals:
                for signal in self.checker_signals:
                    signal('checking encryption keys')
            self.provider_cert_checker.run_all(skip_verify=skip_verify)
        except Exception as exc:
            push_err(exc)
        try:
            if self.checker_signals:
                for signal in self.checker_signals:
                    signal('checking provider config')
            self.config_checker.run_all(skip_download=skip_download)
        except Exception as exc:
            push_err(exc)
        try:
            self.run_openvpn_checks()
        except Exception as exc:
            push_err(exc)


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
        """
        self.current = self.DISCONNECTED
        self.previous = None
        # (callbacks to connect to signals in Qt-land)
        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_leap_status(self):
        # XXX improve nomenclature
        leap_status = {
            0: 'disconnected',
            1: 'connecting to gateway',
            2: 'connecting to gateway',
            3: 'authenticating',
            4: 'establishing network encryption',
            5: 'establishing network encryption',
            6: 'establishing network encryption',
            7: 'connected',
            8: 'reconnecting',
            9: 'exiting',
            11: 'unrecoverable error',
        }
        return leap_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)