From da19f40ab42a7570d5a288239cc4dade56139082 Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Thu, 15 Jun 2017 01:16:24 +0200 Subject: [bug] reset traffic when stopping vpn also, refactor a bit VPNManagement so that the `connect_to_management` method does not receive connection details. I plan to refactor management so that it is a separate object from the control object (but we need to stablish better its relation with the status parsing class). - Resolves: #8834 --- src/leap/bitmask/vpn/_control.py | 2 + src/leap/bitmask/vpn/_management.py | 74 ++++++++++++++----------------------- src/leap/bitmask/vpn/manager.py | 6 +-- src/leap/bitmask/vpn/process.py | 13 ++++--- 4 files changed, 39 insertions(+), 56 deletions(-) (limited to 'src/leap/bitmask/vpn') diff --git a/src/leap/bitmask/vpn/_control.py b/src/leap/bitmask/vpn/_control.py index bf33b1bc..53d4d315 100644 --- a/src/leap/bitmask/vpn/_control.py +++ b/src/leap/bitmask/vpn/_control.py @@ -125,6 +125,8 @@ class VPNControl(object): else: log.debug('VPN is not running.') + self._vpnproc.traffic_status = (0, 0) + return True @property diff --git a/src/leap/bitmask/vpn/_management.py b/src/leap/bitmask/vpn/_management.py index 6e24f478..18e52ed7 100644 --- a/src/leap/bitmask/vpn/_management.py +++ b/src/leap/bitmask/vpn/_management.py @@ -15,7 +15,7 @@ except ImportError: from psutil import AccessDenied as psutil_AccessDenied PSUTIL_2 = True -from ._telnet import UDSTelnet +from leap.bitmask.vpn._telnet import UDSTelnet class OpenVPNAlreadyRunning(Exception): @@ -47,6 +47,17 @@ class VPNManagement(object): self._tn = None self.aborted = False + def set_connection(self, host, port): + """ + :param host: either socket path (unix) or socket IP + :type host: str + + :param port: either string "unix" if it's a unix socket, or port + otherwise + """ + self._host = host + self._port = port + def _seek_to_eof(self): """ Read as much as available. Position seek pointer to end of stream @@ -71,7 +82,6 @@ class VPNManagement(object): :return: response read :rtype: list """ - # leap_assert(self._tn, "We need a tn connection!") try: self._tn.write("%s\n" % (command,)) @@ -108,23 +118,17 @@ class VPNManagement(object): self._tn.get_socket().close() self._tn = None - def _connect_management(self, socket_host, socket_port): + def connect_to_management(self): """ - Connects to the management interface on the specified - socket_host socket_port. - - :param socket_host: either socket path (unix) or socket IP - :type socket_host: str + Connects to the management interface. - :param socket_port: either string "unix" if it's a unix - socket, or port otherwise :type socket_port: str """ if self.is_connected(): self._close_management_socket() try: - self._tn = UDSTelnet(socket_host, socket_port) + self._tn = UDSTelnet(self._host, self._port) # XXX make password optional # specially for win. we should generate @@ -138,42 +142,16 @@ class VPNManagement(object): self._tn.read_eager() except Exception as e: + print "ERROR", e self.log.warn('Could not connect to OpenVPN yet: %r' % (e,)) self._tn = None - def _connectCb(self, *args): - """ - Callback for connection. - - :param args: not used - """ - if not self._tn: - self.log.warn('Cannot connect to management...') - - def _connectErr(self, failure): - """ - Errorback for connection. - - :param failure: Failure - """ - self.log.failure('Error while connecting to management!') - - def connect_to_management(self, host, port): - """ - Connect to a management interface. - - :param host: the host of the management interface - :type host: str - - :param port: the port of the management interface - :type port: str - - :returns: a deferred - """ - self.connectd = defer.maybeDeferred( - self._connect_management, host, port) - self.connectd.addCallbacks(self._connectCb, self._connectErr) - return self.connectd + if self._tn: + return True + else: + print "ERROR!" + #self.log.failure('Error while connecting to management!') + return False def is_connected(self): """ @@ -205,7 +183,7 @@ class VPNManagement(object): 'not alive.') return if not self.aborted and not self.is_connected(): - self.connect_to_management(self._socket_host, self._socket_port) + self.connect_to_management() reactor.callLater( self.CONNECTION_RETRY_TIME, self.try_to_connect_to_management, retry + 1) @@ -235,6 +213,8 @@ class VPNManagement(object): if state != self._last_state: # XXX this status object is the vpn status observer if self._status: + # XXX DEBUG ----------------------- + print "SETTING STATUS", state self._status.set_status(state, None) self._last_state = state @@ -316,7 +296,7 @@ class VPNManagement(object): """ if self._socket_port == "unix": self.log.debug('Cleaning socket file temp folder') - tempfolder = _first(os.path.split(self._socket_host)) + tempfolder = _first(os.path.split(self._host)) if tempfolder and os.path.isdir(tempfolder): try: shutil.rmtree(tempfolder) @@ -397,7 +377,7 @@ class VPNManagement(object): port = cmdline[index + 2] self.log.debug("Trying to connect to %s:%s" % (host, port)) - self.connect_to_management(host, port) + self.connect_to_management() # XXX this has a problem with connections to different # remotes. So the reconnection will only work when we are diff --git a/src/leap/bitmask/vpn/manager.py b/src/leap/bitmask/vpn/manager.py index 1c07976f..2b113a75 100644 --- a/src/leap/bitmask/vpn/manager.py +++ b/src/leap/bitmask/vpn/manager.py @@ -27,16 +27,14 @@ from ._config import _TempVPNConfig, _TempProviderConfig from .constants import IS_WIN -# TODO this is very badly named. There is another class that is called -# manager. This +# TODO this is very badly named. There is another class that is called manager. +# TODO Call it Tunnel? Tunnel = vpn + firewall class TunnelManager(object): def __init__(self, provider, remotes, cert_path, key_path, ca_path, extra_flags): """ - Initialize the VPNManager object. - :param remotes: a list of gateways tuple (ip, port) looking like this: ((ip1, portA), (ip2, portB), ...) :type remotes: tuple of tuple(str, int) diff --git a/src/leap/bitmask/vpn/process.py b/src/leap/bitmask/vpn/process.py index 844cd0ff..d6e6129b 100644 --- a/src/leap/bitmask/vpn/process.py +++ b/src/leap/bitmask/vpn/process.py @@ -76,13 +76,12 @@ class _VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): openvpn invocation :type openvpn_verb: int """ + # TODO handle management as a component _management.VPNManagement.__init__(self) + self.set_connection(socket_host, socket_port) self._vpnconfig = vpnconfig self._providerconfig = providerconfig - self._socket_host = socket_host - self._socket_port = socket_port - self._launcher = get_vpn_launcher() self._last_state = None @@ -107,6 +106,10 @@ class _VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): def traffic_status(self): return self._status.get_traffic_status() + @traffic_status.setter + def traffic_status(self, value): + self._status.set_traffic_status(value) + # processProtocol methods def connectionMade(self): @@ -203,8 +206,8 @@ class _VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): command = self._launcher.get_vpn_command( vpnconfig=self._vpnconfig, providerconfig=self._providerconfig, - socket_host=self._socket_host, - socket_port=self._socket_port, + socket_host=self._host, + socket_port=self._port, openvpn_verb=self._openvpn_verb, remotes=self._remotes) -- cgit v1.2.3