From ddf5e546916ad94c62b1e42b6f03831f906b2f29 Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 24 Sep 2012 17:34:25 -0400 Subject: improved network checks on the way to a network checker. --- src/leap/eip/checks.py | 15 +++++++-------- src/leap/eip/exceptions.py | 2 ++ src/leap/eip/tests/test_checks.py | 22 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 9b7b1cee..82940fd3 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -70,21 +70,20 @@ class LeapNetworkChecker(object): checker.is_internet_up() checker.ping_gateway() - def test_internet_connection(self): - # XXX we're not passing the error anywhere. - # XXX we probably should raise an exception here? - # unless we use this as smoke test + def check_internet_connection(self): try: # XXX remove this hardcoded random ip requests.get('http://216.172.161.165') except (requests.HTTPError, requests.RequestException) as e: - self.error = e.message - except requests.ConenctionError as e: + raise eipexceptions.NoInternetConnection(e.message) + except requests.ConnectionError as e: + error = "Unidentified Connection Error" if e.message == "[Errno 113] No route to host": if not self.is_internet_up(): - self.error = "No valid internet connection found." + error = "No valid internet connection found." else: - self.error = "Provider server appears to be down." + error = "Provider server appears to be down." + raise eipexceptions.NoInternetConnection(error) def is_internet_up(self): iface, gateway = self.get_default_interface_gateway() diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index f048621f..f883a173 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -136,6 +136,8 @@ class NoConnectionToGateway(EIPClientError): message = "no connection to gateway" usermessage = "Looks like there are problems with your internet connection" +class NoInternetConnection(EIPClientError): + message = "No Internet connection found" # # Errors that probably we don't need anymore diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 19b54c04..f412dbec 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -52,7 +52,7 @@ class LeapNetworkCheckTest(BaseLeapTest): def test_checker_should_implement_check_methods(self): checker = eipchecks.LeapNetworkChecker() - self.assertTrue(hasattr(checker, "test_internet_connection"), + self.assertTrue(hasattr(checker, "check_internet_connection"), "missing meth") self.assertTrue(hasattr(checker, "is_internet_up"), "missing meth") @@ -64,7 +64,7 @@ class LeapNetworkCheckTest(BaseLeapTest): mc = Mock() checker.run_all(checker=mc) - self.assertTrue(mc.test_internet_connection.called, "not called") + self.assertTrue(mc.check_internet_connection.called, "not called") self.assertTrue(mc.ping_gateway.called, "not called") self.assertTrue(mc.is_internet_up.called, "not called") @@ -86,6 +86,24 @@ class LeapNetworkCheckTest(BaseLeapTest): mocked_ping.return_value = [11, "", ""] checker.ping_gateway("4.2.2.2") + def test_check_internet_connection_failures(self): + checker = eipchecks.LeapNetworkChecker() + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.HTTPError + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.RequestException + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + + #TODO: Mock possible errors that can be raised by is_internet_up + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.ConnectionError + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + @unittest.skipUnless(_uid == 0, "root only") def test_ping_gateway(self): checker = eipchecks.LeapNetworkChecker() -- cgit v1.2.3 From 4c183e81074066eb2b064896fbb741e99c50286d Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 24 Sep 2012 17:44:21 -0400 Subject: Missed a renaming of test_internet_connection to check_internet_connection. --- src/leap/eip/checks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 82940fd3..20d1296d 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -66,7 +66,7 @@ class LeapNetworkChecker(object): self.error = None # ? # for MVS - checker.test_internet_connection() + checker.check_internet_connection() checker.is_internet_up() checker.ping_gateway() -- cgit v1.2.3 From 021e7ea900c64af9577412f11349f69e01634c0c Mon Sep 17 00:00:00 2001 From: antialias Date: Tue, 25 Sep 2012 15:48:40 -0400 Subject: fixed typo. --- src/leap/util/coroutines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/leap/util/coroutines.py b/src/leap/util/coroutines.py index e7ccfacf..b9d0a98b 100644 --- a/src/leap/util/coroutines.py +++ b/src/leap/util/coroutines.py @@ -72,7 +72,7 @@ def watch_output(out, observers): :type out: fd :param observers: tuple of coroutines to send data\ for each event - :type ovservers: tuple + :type observers: tuple """ observer_dict = dict(((observer, process_events(observer)) for observer in observers)) -- cgit v1.2.3 From 15b017656e6865b7b85ae389ab3b462c562a1e42 Mon Sep 17 00:00:00 2001 From: antialias Date: Tue, 25 Sep 2012 16:05:02 -0400 Subject: moved LeapNetworkChecker and test in base. --- src/leap/base/checks.py | 80 +++++++++++++++++++++++++++++++++++ src/leap/base/exceptions.py | 19 +++++++++ src/leap/base/tests/test_checks.py | 86 ++++++++++++++++++++++++++++++++++++++ src/leap/eip/checks.py | 76 --------------------------------- src/leap/eip/exceptions.py | 18 -------- src/leap/eip/tests/test_checks.py | 78 ---------------------------------- 6 files changed, 185 insertions(+), 172 deletions(-) create mode 100644 src/leap/base/checks.py create mode 100644 src/leap/base/tests/test_checks.py diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py new file mode 100644 index 00000000..c5438b09 --- /dev/null +++ b/src/leap/base/checks.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- + +import platform + +import ping +import requests + +from leap.base import constants +from leap.base import exceptions + + +class LeapNetworkChecker(object): + """ + all network related checks + """ + # TODO eventually, use a more portable solution + # like psutil + + def run_all(self, checker=None): + if not checker: + checker = self + self.error = None # ? + + # for MVS + checker.check_internet_connection() + checker.is_internet_up() + checker.ping_gateway() + + def check_internet_connection(self): + try: + # XXX remove this hardcoded random ip + requests.get('http://216.172.161.165') + except (requests.HTTPError, requests.RequestException) as e: + raise exceptions.NoInternetConnection(e.message) + except requests.ConnectionError as e: + error = "Unidentified Connection Error" + if e.message == "[Errno 113] No route to host": + if not self.is_internet_up(): + error = "No valid internet connection found." + else: + error = "Provider server appears to be down." + raise exceptions.NoInternetConnection(error) + + def is_internet_up(self): + iface, gateway = self.get_default_interface_gateway() + self.ping_gateway(self) + + def get_default_interface_gateway(self): + """only impletemented for linux so far.""" + if not platform.system() == "Linux": + raise NotImplementedError + + f = open("/proc/net/route") + route_table = f.readlines() + f.close() + #toss out header + route_table.pop(0) + + default_iface = None + gateway = None + while route_table: + line = route_table.pop(0) + iface, destination, gateway = line.split('\t')[0:3] + if destination == '00000000': + default_iface = iface + break + + if not default_iface: + raise exceptions.NoDefaultInterfaceFoundError + + if default_iface not in netifaces.interfaces(): + raise exceptions.InterfaceNotFoundError + + return default_iface, gateway + + def ping_gateway(self, gateway): + #TODO: Discuss how much packet loss (%) is acceptable. + packet_loss = ping.quiet_ping(gateway)[0] + if packet_loss > constants.MAX_ICMP_PACKET_LOSS: + raise exceptions.NoConnectionToGateway diff --git a/src/leap/base/exceptions.py b/src/leap/base/exceptions.py index 9c4aa77b..7771d1f9 100644 --- a/src/leap/base/exceptions.py +++ b/src/leap/base/exceptions.py @@ -4,3 +4,22 @@ class MissingConfigFileError(Exception): class ImproperlyConfigured(Exception): pass + + +class NoDefaultInterfaceFoundError(Exception): + message = "no default interface found" + usermessage = "Looks like your computer is not connected to the internet" + + +class InterfaceNotFoundError(Exception): + # XXX should take iface arg on init maybe? + message = "interface not found" + + +class NoConnectionToGateway(Exception): + message = "no connection to gateway" + usermessage = "Looks like there are problems with your internet connection" + + +class NoInternetConnection(Exception): + message = "No Internet connection found" diff --git a/src/leap/base/tests/test_checks.py b/src/leap/base/tests/test_checks.py new file mode 100644 index 00000000..a3b3ea91 --- /dev/null +++ b/src/leap/base/tests/test_checks.py @@ -0,0 +1,86 @@ +try: + import unittest2 as unittest +except ImportError: + import unittest +import os + +from mock import (patch, Mock) +from StringIO import StringIO + +import ping +import requests + +from leap.base import checks +from leap.base import exceptions +from leap.testing.basetest import BaseLeapTest + +_uid = os.getuid() + + +class LeapNetworkCheckTest(BaseLeapTest): + __name__ = "leap_network_check_tests" + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_checker_should_implement_check_methods(self): + checker = checks.LeapNetworkChecker() + + self.assertTrue(hasattr(checker, "check_internet_connection"), + "missing meth") + self.assertTrue(hasattr(checker, "is_internet_up"), + "missing meth") + self.assertTrue(hasattr(checker, "ping_gateway"), + "missing meth") + + def test_checker_should_actually_call_all_tests(self): + checker = checks.LeapNetworkChecker() + + mc = Mock() + checker.run_all(checker=mc) + self.assertTrue(mc.check_internet_connection.called, "not called") + self.assertTrue(mc.ping_gateway.called, "not called") + self.assertTrue(mc.is_internet_up.called, "not called") + + def test_get_default_interface_no_interface(self): + checker = checks.LeapNetworkChecker() + with patch('leap.base.checks.open', create=True) as mock_open: + with self.assertRaises(exceptions.NoDefaultInterfaceFoundError): + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT") + checker.get_default_interface_gateway() + + def test_ping_gateway_fail(self): + checker = checks.LeapNetworkChecker() + with patch.object(ping, "quiet_ping") as mocked_ping: + with self.assertRaises(exceptions.NoConnectionToGateway): + mocked_ping.return_value = [11, "", ""] + checker.ping_gateway("4.2.2.2") + + def test_check_internet_connection_failures(self): + checker = checks.LeapNetworkChecker() + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.HTTPError + with self.assertRaises(exceptions.NoInternetConnection): + checker.check_internet_connection() + + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.RequestException + with self.assertRaises(exceptions.NoInternetConnection): + checker.check_internet_connection() + + #TODO: Mock possible errors that can be raised by is_internet_up + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.ConnectionError + with self.assertRaises(exceptions.NoInternetConnection): + checker.check_internet_connection() + + @unittest.skipUnless(_uid == 0, "root only") + def test_ping_gateway(self): + checker = checks.LeapNetworkChecker() + checker.ping_gateway("4.2.2.2") diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 20d1296d..9872f8d8 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -39,10 +39,6 @@ into base.tests to be invoked by the base leap init routines. However, I'm testing them alltogether for the sake of having the whole unit reachable and testable as a whole. -LeapNetworkChecker ------------------- -Network checks. To be moved to base. -docs TBD """ @@ -52,78 +48,6 @@ def get_ca_cert(): return certs.where(ca_file) -class LeapNetworkChecker(object): - """ - all network related checks - """ - # XXX to be moved to leap.base.checks - # TODO eventually, use a more portable solution - # like psutil - - def run_all(self, checker=None): - if not checker: - checker = self - self.error = None # ? - - # for MVS - checker.check_internet_connection() - checker.is_internet_up() - checker.ping_gateway() - - def check_internet_connection(self): - try: - # XXX remove this hardcoded random ip - requests.get('http://216.172.161.165') - except (requests.HTTPError, requests.RequestException) as e: - raise eipexceptions.NoInternetConnection(e.message) - except requests.ConnectionError as e: - error = "Unidentified Connection Error" - if e.message == "[Errno 113] No route to host": - if not self.is_internet_up(): - error = "No valid internet connection found." - else: - error = "Provider server appears to be down." - raise eipexceptions.NoInternetConnection(error) - - def is_internet_up(self): - iface, gateway = self.get_default_interface_gateway() - self.ping_gateway(self) - - def get_default_interface_gateway(self): - """only impletemented for linux so far.""" - if not platform.system() == "Linux": - raise NotImplementedError - - f = open("/proc/net/route") - route_table = f.readlines() - f.close() - #toss out header - route_table.pop(0) - - default_iface = None - gateway = None - while route_table: - line = route_table.pop(0) - iface, destination, gateway = line.split('\t')[0:3] - if destination == '00000000': - default_iface = iface - break - - if not default_iface: - raise eipexceptions.NoDefaultInterfaceFoundError - - if default_iface not in netifaces.interfaces(): - raise eipexceptions.InterfaceNotFoundError - - return default_iface, gateway - - def ping_gateway(self, gateway): - #TODO: Discuss how much packet loss (%) is acceptable. - packet_loss = ping.quiet_ping(gateway)[0] - if packet_loss > baseconstants.MAX_ICMP_PACKET_LOSS: - raise eipexceptions.NoConnectionToGateway - - class ProviderCertChecker(object): """ Several checks needed for getting diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index f883a173..6b4ee6aa 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -121,24 +121,6 @@ class EIPInitBadProviderError(EIPClientError): class EIPConfigurationError(EIPClientError): pass - -class NoDefaultInterfaceFoundError(EIPClientError): - message = "no default interface found" - usermessage = "Looks like your computer is not connected to the internet" - - -class InterfaceNotFoundError(EIPClientError): - # XXX should take iface arg on init maybe? - message = "interface not found" - - -class NoConnectionToGateway(EIPClientError): - message = "no connection to gateway" - usermessage = "Looks like there are problems with your internet connection" - -class NoInternetConnection(EIPClientError): - message = "No Internet connection found" - # # Errors that probably we don't need anymore # chase down for them and check. diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index f412dbec..06133825 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -9,10 +9,8 @@ import os import time import urlparse -from StringIO import StringIO from mock import (patch, Mock) -import ping import requests from leap.base import config as baseconfig @@ -26,8 +24,6 @@ from leap.testing.basetest import BaseLeapTest from leap.testing.https_server import BaseHTTPSServerTestCase from leap.testing.https_server import where as where_cert -_uid = os.getuid() - class NoLogRequestHandler: def log_message(self, *args): @@ -38,78 +34,6 @@ class NoLogRequestHandler: return '' -class LeapNetworkCheckTest(BaseLeapTest): - # XXX to be moved to base.checks - - __name__ = "leap_network_check_tests" - - def setUp(self): - pass - - def tearDown(self): - pass - - def test_checker_should_implement_check_methods(self): - checker = eipchecks.LeapNetworkChecker() - - self.assertTrue(hasattr(checker, "check_internet_connection"), - "missing meth") - self.assertTrue(hasattr(checker, "is_internet_up"), - "missing meth") - self.assertTrue(hasattr(checker, "ping_gateway"), - "missing meth") - - def test_checker_should_actually_call_all_tests(self): - checker = eipchecks.LeapNetworkChecker() - - mc = Mock() - checker.run_all(checker=mc) - self.assertTrue(mc.check_internet_connection.called, "not called") - self.assertTrue(mc.ping_gateway.called, "not called") - self.assertTrue(mc.is_internet_up.called, - "not called") - - def test_get_default_interface_no_interface(self): - checker = eipchecks.LeapNetworkChecker() - with patch('leap.eip.checks.open', create=True) as mock_open: - with self.assertRaises(eipexceptions.NoDefaultInterfaceFoundError): - mock_open.return_value = StringIO( - "Iface\tDestination Gateway\t" - "Flags\tRefCntd\tUse\tMetric\t" - "Mask\tMTU\tWindow\tIRTT") - checker.get_default_interface_gateway() - - def test_ping_gateway_fail(self): - checker = eipchecks.LeapNetworkChecker() - with patch.object(ping, "quiet_ping") as mocked_ping: - with self.assertRaises(eipexceptions.NoConnectionToGateway): - mocked_ping.return_value = [11, "", ""] - checker.ping_gateway("4.2.2.2") - - def test_check_internet_connection_failures(self): - checker = eipchecks.LeapNetworkChecker() - with patch.object(requests, "get") as mocked_get: - mocked_get.side_effect = requests.HTTPError - with self.assertRaises(eipexceptions.NoInternetConnection): - checker.check_internet_connection() - - with patch.object(requests, "get") as mocked_get: - mocked_get.side_effect = requests.RequestException - with self.assertRaises(eipexceptions.NoInternetConnection): - checker.check_internet_connection() - - #TODO: Mock possible errors that can be raised by is_internet_up - with patch.object(requests, "get") as mocked_get: - mocked_get.side_effect = requests.ConnectionError - with self.assertRaises(eipexceptions.NoInternetConnection): - checker.check_internet_connection() - - @unittest.skipUnless(_uid == 0, "root only") - def test_ping_gateway(self): - checker = eipchecks.LeapNetworkChecker() - checker.ping_gateway("4.2.2.2") - - class EIPCheckTest(BaseLeapTest): __name__ = "eip_check_tests" @@ -149,8 +73,6 @@ class EIPCheckTest(BaseLeapTest): "not called") self.assertTrue(mc.check_complete_eip_config.called, "not called") - #self.assertTrue(mc.ping_gateway.called, - #"not called") # test individual check methods -- cgit v1.2.3 From 3fd7b55de96484e02accb991fb2c0c3ce0aa9883 Mon Sep 17 00:00:00 2001 From: antialias Date: Tue, 25 Sep 2012 17:37:48 -0400 Subject: First check for threaded network checks. TODO: tests. --- src/leap/base/constants.py | 2 ++ src/leap/base/network.py | 55 +++++++++++++++++++++++++++++++++++++++++ src/leap/baseapp/mainwindow.py | 3 +++ src/leap/baseapp/network.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 src/leap/base/network.py create mode 100644 src/leap/baseapp/network.py diff --git a/src/leap/base/constants.py b/src/leap/base/constants.py index 7a1415fb..8a76b6b4 100644 --- a/src/leap/base/constants.py +++ b/src/leap/base/constants.py @@ -28,3 +28,5 @@ DEFAULT_PROVIDER_DEFINITION = { u'version': u'0.1.0'} MAX_ICMP_PACKET_LOSS = 10 + +ROUTE_CHECK_INTERVAL = 120 diff --git a/src/leap/base/network.py b/src/leap/base/network.py new file mode 100644 index 00000000..58f903e1 --- /dev/null +++ b/src/leap/base/network.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +from __future__ import (print_function) + +from leap.base.checks import LeapNetworkChecker +from leap.base.constants import ROUTE_CHECK_INTERVAL +from leap.util.coroutines import (launch_thread, process_events) + +from time import sleep + +class NetworkChecker(object): + """ + Manages network checking thread that makes sure we have a working network + connection. + """ + def __init__(self, *args, **kwargs): + self.status_signals = kwargs.pop('status_signals', None) + self.watcher_cb = kwargs.pop('status_signals', None) + + def start(self): + self._launch_recurrent_network_checks((self.watcher_cb,)) + + def stop(self): + raise NotImplementedError + + def run_checks(self): + pass + + #private methods + + #here all the observers in fail_callbacks expect one positional argument, + #which is exception so we can try by passing a lambda with logger to + #check it works. + def _network_checks_thread(self, fail_callbacks): + print('fail_callbacks: %s' % fail_callbacks) + print(len(fail_callbacks)) + observer_dict = dict((( + observer, process_events(observer)) for observer in fail_callbacks)) + netchecker = LeapNetworkChecker() + while True: + try: + netchecker.check_internet_connection() + sleep(ROUTE_CHECK_INTERVAL) + except Exception as exc: + for obs in observer_dict: + observer_dict[obs].send(exc) + + + def _launch_recurrent_network_checks(fail_callbacks): + print(type(fail_callbacks)) + watcher = launch_thread( + network_checks_thread, + (fail_callbacks,)) + return watcher + + diff --git a/src/leap/baseapp/mainwindow.py b/src/leap/baseapp/mainwindow.py index 10b23d9a..7b2ecb1d 100644 --- a/src/leap/baseapp/mainwindow.py +++ b/src/leap/baseapp/mainwindow.py @@ -8,6 +8,7 @@ from PyQt4 import QtGui from leap.baseapp.eip import EIPConductorAppMixin from leap.baseapp.log import LogPaneMixin from leap.baseapp.systray import StatusAwareTrayIconMixin +from leap.baseapp.network import NetworkCheckerAppMixin from leap.baseapp.leap_app import MainWindowMixin logger = logging.getLogger(name=__name__) @@ -16,6 +17,7 @@ logger = logging.getLogger(name=__name__) class LeapWindow(QtGui.QMainWindow, MainWindowMixin, EIPConductorAppMixin, StatusAwareTrayIconMixin, + NetworkCheckerAppMixin, LogPaneMixin): """ main window for the leap app. @@ -36,6 +38,7 @@ class LeapWindow(QtGui.QMainWindow, self.createLogBrowser() EIPConductorAppMixin.__init__(self, opts=opts) StatusAwareTrayIconMixin.__init__(self) + NetworkCheckerAppMixin.__init__(self) MainWindowMixin.__init__(self) # bind signals diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py new file mode 100644 index 00000000..42a42fcd --- /dev/null +++ b/src/leap/baseapp/network.py @@ -0,0 +1,56 @@ +from __future__ import print_function +import logging +import time +logger = logging.getLogger(name=__name__) + +from leap.base.network import NetworkChecker +from leap.baseapp.dialogs import ErrorDialog + + +class NetworkCheckerAppMixin(object): + """ + initialize an instance of the Network Checker, + which gathers error and passes them on. + """ + + def __init__(self, *args, **kwargs): + opts = kwargs.pop('opts', None) + config_file = getattr(opts, 'config_file', None) + + self.network_checker_started = False + + self.network_checker = NetworkChecker( + watcher_cb=self.newLogLine.emit, + status_signals=(self.statusChange.emit, ), + debug=self.debugmode) + + self.network_checker.run_checks() + self.error_check() + + def error_check(self): + """ + consumes the conductor error queue. + pops errors, and acts accordingly (launching user dialogs). + """ + logger.debug('error check') + + errq = self.conductor.error_queue + while errq.qsize() != 0: + logger.debug('%s errors left in conductor queue', errq.qsize()) + # we get exception and original traceback from queue + error, tb = errq.get() + + # redundant log, debugging the loop. + logger.error('%s: %s', error.__class__.__name__, error.message) + + if issubclass(error.__class__, eip_exceptions.EIPClientError): + self.handle_eip_error(error) + + else: + # deprecated form of raising exception. + raise error, None, tb + + if error.failfirst is True: + break + + -- cgit v1.2.3 From 202345940e12d4633c5d46d5ed21c3a433573d48 Mon Sep 17 00:00:00 2001 From: antialias Date: Fri, 28 Sep 2012 18:15:47 -0400 Subject: Added check that default route is tun0. --- src/leap/base/checks.py | 27 +++++++++++++++++++++++++++ src/leap/base/exceptions.py | 4 ++++ src/leap/base/tests/test_checks.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py index c5438b09..a775e162 100644 --- a/src/leap/base/checks.py +++ b/src/leap/base/checks.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import logging import platform import ping @@ -8,6 +9,7 @@ import requests from leap.base import constants from leap.base import exceptions +logger = logging.getLogger(name=__name__) class LeapNetworkChecker(object): """ @@ -22,6 +24,7 @@ class LeapNetworkChecker(object): self.error = None # ? # for MVS + checker.check_tunnel_default_interface() checker.check_internet_connection() checker.is_internet_up() checker.ping_gateway() @@ -40,11 +43,35 @@ class LeapNetworkChecker(object): else: error = "Provider server appears to be down." raise exceptions.NoInternetConnection(error) + logger.debug('Network appears to be up.') def is_internet_up(self): iface, gateway = self.get_default_interface_gateway() self.ping_gateway(self) + def check_tunnel_default_interface(self): + """ + Raises an TunnelNotDefaultRouteError + (including when no routes are present) + """ + if not platform.system() == "Linux": + raise NotImplementedError + + f = open("/proc/net/route") + route_table = f.readlines() + f.close() + #toss out header + route_table.pop(0) + + if not route_table: + raise exceptions.TunnelNotDefaultRouteError() + + line = route_table.pop(0) + iface, destination = line.split('\t')[0:2] + if not destination == '00000000' or not iface == 'tun0': + raise exceptions.TunnelNotDefaultRouteError() + + def get_default_interface_gateway(self): """only impletemented for linux so far.""" if not platform.system() == "Linux": diff --git a/src/leap/base/exceptions.py b/src/leap/base/exceptions.py index 7771d1f9..48d827f5 100644 --- a/src/leap/base/exceptions.py +++ b/src/leap/base/exceptions.py @@ -23,3 +23,7 @@ class NoConnectionToGateway(Exception): class NoInternetConnection(Exception): message = "No Internet connection found" + + +class TunnelNotDefaultRouteError(Exception): + message = "VPN Maybe be down." diff --git a/src/leap/base/tests/test_checks.py b/src/leap/base/tests/test_checks.py index a3b3ea91..30746991 100644 --- a/src/leap/base/tests/test_checks.py +++ b/src/leap/base/tests/test_checks.py @@ -31,6 +31,8 @@ class LeapNetworkCheckTest(BaseLeapTest): self.assertTrue(hasattr(checker, "check_internet_connection"), "missing meth") + self.assertTrue(hasattr(checker, "check_tunnel_default_interface"), + "missing meth") self.assertTrue(hasattr(checker, "is_internet_up"), "missing meth") self.assertTrue(hasattr(checker, "ping_gateway"), @@ -42,6 +44,7 @@ class LeapNetworkCheckTest(BaseLeapTest): mc = Mock() checker.run_all(checker=mc) self.assertTrue(mc.check_internet_connection.called, "not called") + self.assertTrue(mc.check_tunnel_default_interface.called, "not called") self.assertTrue(mc.ping_gateway.called, "not called") self.assertTrue(mc.is_internet_up.called, "not called") @@ -55,6 +58,33 @@ class LeapNetworkCheckTest(BaseLeapTest): "Mask\tMTU\tWindow\tIRTT") checker.get_default_interface_gateway() + def test_check_tunnel_default_interface(self): + checker = checks.LeapNetworkChecker() + with patch('leap.base.checks.open', create=True) as mock_open: + with self.assertRaises(exceptions.TunnelNotDefaultRouteError): + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT") + checker.check_tunnel_default_interface() + + with patch('leap.base.checks.open', create=True) as mock_open: + with self.assertRaises(exceptions.TunnelNotDefaultRouteError): + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT\n" + "wlan0\t00000000\t0102A8C0\t0003\t0\t0\t0\t00000000\t0\t0\t0") + checker.check_tunnel_default_interface() + + with patch('leap.base.checks.open', create=True) as mock_open: + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT\n" + "tun0\t00000000\t01002A0A\t0003\t0\t0\t0\t00000080\t0\t0\t0") + checker.check_tunnel_default_interface() + def test_ping_gateway_fail(self): checker = checks.LeapNetworkChecker() with patch.object(ping, "quiet_ping") as mocked_ping: -- cgit v1.2.3 From 58344bb28c1c0f25ed37624ff487cc8f24821d52 Mon Sep 17 00:00:00 2001 From: antialias Date: Fri, 28 Sep 2012 18:16:47 -0400 Subject: Functionality to shutdown network checker when openvpn is stopped. But thread not being successfully killed. --- src/leap/base/network.py | 40 +++++++++++++++++++++++++++++----------- src/leap/baseapp/eip.py | 2 ++ src/leap/baseapp/network.py | 7 ++++--- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/leap/base/network.py b/src/leap/base/network.py index 58f903e1..a1e7c880 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -1,12 +1,18 @@ # -*- coding: utf-8 -*- from __future__ import (print_function) +import logging +import threading from leap.base.checks import LeapNetworkChecker from leap.base.constants import ROUTE_CHECK_INTERVAL -from leap.util.coroutines import (launch_thread, process_events) +from leap.base.exceptions import TunnelNotDefaultRouteError +from leap.util.coroutines import (launch_thread_no_daemon, process_events) from time import sleep +logger = logging.getLogger(name=__name__) + + class NetworkChecker(object): """ Manages network checking thread that makes sure we have a working network @@ -15,12 +21,17 @@ class NetworkChecker(object): def __init__(self, *args, **kwargs): self.status_signals = kwargs.pop('status_signals', None) self.watcher_cb = kwargs.pop('status_signals', None) + self.excp_logger = lambda exc: logger.error("%s", exc.message) + self.checker = LeapNetworkChecker() def start(self): - self._launch_recurrent_network_checks((self.watcher_cb,)) + self.process_handle = self._launch_recurrent_network_checks((self.excp_logger,)) def stop(self): - raise NotImplementedError + #TODO: Thread still not being stopped when openvpn is stopped. + logger.debug("stopping network checker...") + self.process_handle._Thread__stop() + logger.debug("network checked stopped.") def run_checks(self): pass @@ -31,24 +42,31 @@ class NetworkChecker(object): #which is exception so we can try by passing a lambda with logger to #check it works. def _network_checks_thread(self, fail_callbacks): - print('fail_callbacks: %s' % fail_callbacks) - print(len(fail_callbacks)) + #TODO: replace this with waiting for a signal from openvpn + while True: + try: + self.checker.check_tunnel_default_interface() + break + except TunnelNotDefaultRouteError: + sleep(1) + observer_dict = dict((( observer, process_events(observer)) for observer in fail_callbacks)) - netchecker = LeapNetworkChecker() while True: try: - netchecker.check_internet_connection() + self.checker.check_tunnel_default_interface() + self.checker.check_internet_connection() sleep(ROUTE_CHECK_INTERVAL) except Exception as exc: for obs in observer_dict: observer_dict[obs].send(exc) + sleep(ROUTE_CHECK_INTERVAL) - def _launch_recurrent_network_checks(fail_callbacks): - print(type(fail_callbacks)) - watcher = launch_thread( - network_checks_thread, + def _launch_recurrent_network_checks(self, fail_callbacks): + #we need to wrap the fail callback in a turple + watcher = launch_thread_no_daemon( + self._network_checks_thread, (fail_callbacks,)) return watcher diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py index b0e14be7..ad074abc 100644 --- a/src/leap/baseapp/eip.py +++ b/src/leap/baseapp/eip.py @@ -224,9 +224,11 @@ class EIPConductorAppMixin(object): # we could bring Timer Init to this Mixin # or to its own Mixin. self.timer.start(constants.TIMER_MILLISECONDS) + self.network_checker.start() return if self.eip_service_started is True: + self.network_checker.stop() self.conductor.disconnect() if self.debugmode: self.startStopButton.setText('&Connect') diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index 42a42fcd..75690cc9 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -36,7 +36,7 @@ class NetworkCheckerAppMixin(object): errq = self.conductor.error_queue while errq.qsize() != 0: - logger.debug('%s errors left in conductor queue', errq.qsize()) + logger.debug('%s errors left in network queue', errq.qsize()) # we get exception and original traceback from queue error, tb = errq.get() @@ -44,7 +44,7 @@ class NetworkCheckerAppMixin(object): logger.error('%s: %s', error.__class__.__name__, error.message) if issubclass(error.__class__, eip_exceptions.EIPClientError): - self.handle_eip_error(error) + self.handle_network_error(error) else: # deprecated form of raising exception. @@ -53,4 +53,5 @@ class NetworkCheckerAppMixin(object): if error.failfirst is True: break - + def handle_network_error(self, error): + pass -- cgit v1.2.3 From c75b27484a999f70a6e28e521f236cf2d403edd1 Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 1 Oct 2012 11:02:40 -0400 Subject: Checker starting and stopping correctly. --- src/leap/base/network.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/leap/base/network.py b/src/leap/base/network.py index a1e7c880..92fb7635 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -6,7 +6,7 @@ import threading from leap.base.checks import LeapNetworkChecker from leap.base.constants import ROUTE_CHECK_INTERVAL from leap.base.exceptions import TunnelNotDefaultRouteError -from leap.util.coroutines import (launch_thread_no_daemon, process_events) +from leap.util.coroutines import (launch_thread, process_events) from time import sleep @@ -22,6 +22,7 @@ class NetworkChecker(object): self.status_signals = kwargs.pop('status_signals', None) self.watcher_cb = kwargs.pop('status_signals', None) self.excp_logger = lambda exc: logger.error("%s", exc.message) + self.shutdown = threading.Event() self.checker = LeapNetworkChecker() def start(self): @@ -29,8 +30,7 @@ class NetworkChecker(object): def stop(self): #TODO: Thread still not being stopped when openvpn is stopped. - logger.debug("stopping network checker...") - self.process_handle._Thread__stop() + self.shutdown.set() logger.debug("network checked stopped.") def run_checks(self): @@ -52,7 +52,7 @@ class NetworkChecker(object): observer_dict = dict((( observer, process_events(observer)) for observer in fail_callbacks)) - while True: + while not self.shutdown.is_set(): try: self.checker.check_tunnel_default_interface() self.checker.check_internet_connection() @@ -61,11 +61,12 @@ class NetworkChecker(object): for obs in observer_dict: observer_dict[obs].send(exc) sleep(ROUTE_CHECK_INTERVAL) - + #reset event + self.shutdown.clear() def _launch_recurrent_network_checks(self, fail_callbacks): #we need to wrap the fail callback in a turple - watcher = launch_thread_no_daemon( + watcher = launch_thread( self._network_checks_thread, (fail_callbacks,)) return watcher -- cgit v1.2.3 From 95ce59c8833cb2ba951630080cdbc1e6d756a666 Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 1 Oct 2012 15:10:55 -0400 Subject: Still some QT related problems. Hand off to kali to fix. --- src/leap/base/network.py | 8 +++----- src/leap/baseapp/mainwindow.py | 14 ++++++++++++++ src/leap/baseapp/network.py | 36 +----------------------------------- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/src/leap/base/network.py b/src/leap/base/network.py index 92fb7635..159e9b21 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -21,15 +21,15 @@ class NetworkChecker(object): def __init__(self, *args, **kwargs): self.status_signals = kwargs.pop('status_signals', None) self.watcher_cb = kwargs.pop('status_signals', None) - self.excp_logger = lambda exc: logger.error("%s", exc.message) + self.error_cb = kwargs.pop('error_cb', + lambda exc: logger.error("%s", exc.message)) self.shutdown = threading.Event() self.checker = LeapNetworkChecker() def start(self): - self.process_handle = self._launch_recurrent_network_checks((self.excp_logger,)) + self.process_handle = self._launch_recurrent_network_checks((self.error_cb,)) def stop(self): - #TODO: Thread still not being stopped when openvpn is stopped. self.shutdown.set() logger.debug("network checked stopped.") @@ -70,5 +70,3 @@ class NetworkChecker(object): self._network_checks_thread, (fail_callbacks,)) return watcher - - diff --git a/src/leap/baseapp/mainwindow.py b/src/leap/baseapp/mainwindow.py index 7b2ecb1d..000db8c9 100644 --- a/src/leap/baseapp/mainwindow.py +++ b/src/leap/baseapp/mainwindow.py @@ -28,6 +28,7 @@ class LeapWindow(QtGui.QMainWindow, newLogLine = QtCore.pyqtSignal([str]) statusChange = QtCore.pyqtSignal([object]) + networkError = QtCore.pyqtSignal([object]) def __init__(self, opts): logger.debug('init leap window') @@ -57,3 +58,16 @@ class LeapWindow(QtGui.QMainWindow, # eipapp should catch that if self.conductor.autostart: self.start_or_stopVPN() + + #TODO: Put all Dialogs in one place + @QtCore.pyqtSlot() + def raise_Network_Error(self, exc): + message = exc.message + + # XXX + # check headless = False before + # launching dialog. + # (so Qt tests can assert stuff) + + dialog = ErrorDialog() + dialog.warningMessage(message, 'error') diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index 75690cc9..c73e8062 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -14,44 +14,10 @@ class NetworkCheckerAppMixin(object): """ def __init__(self, *args, **kwargs): - opts = kwargs.pop('opts', None) - config_file = getattr(opts, 'config_file', None) - - self.network_checker_started = False - self.network_checker = NetworkChecker( watcher_cb=self.newLogLine.emit, - status_signals=(self.statusChange.emit, ), + error_cb=self.handle_network_error, debug=self.debugmode) self.network_checker.run_checks() - self.error_check() - - def error_check(self): - """ - consumes the conductor error queue. - pops errors, and acts accordingly (launching user dialogs). - """ - logger.debug('error check') - - errq = self.conductor.error_queue - while errq.qsize() != 0: - logger.debug('%s errors left in network queue', errq.qsize()) - # we get exception and original traceback from queue - error, tb = errq.get() - - # redundant log, debugging the loop. - logger.error('%s: %s', error.__class__.__name__, error.message) - - if issubclass(error.__class__, eip_exceptions.EIPClientError): - self.handle_network_error(error) - - else: - # deprecated form of raising exception. - raise error, None, tb - - if error.failfirst is True: - break - def handle_network_error(self, error): - pass -- cgit v1.2.3 From 31c0afa5eb9bc7566ca39099520e8adc7b531e22 Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 5 Oct 2012 20:15:12 +0900 Subject: pep8 --- src/leap/base/checks.py | 5 +++-- src/leap/base/network.py | 13 ++++++++----- src/leap/base/tests/test_checks.py | 3 ++- src/leap/baseapp/mainwindow.py | 5 +++-- src/leap/baseapp/network.py | 5 ++--- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py index a775e162..d02c9a3d 100644 --- a/src/leap/base/checks.py +++ b/src/leap/base/checks.py @@ -3,6 +3,7 @@ import logging import platform +import netifaces import ping import requests @@ -11,6 +12,7 @@ from leap.base import exceptions logger = logging.getLogger(name=__name__) + class LeapNetworkChecker(object): """ all network related checks @@ -51,7 +53,7 @@ class LeapNetworkChecker(object): def check_tunnel_default_interface(self): """ - Raises an TunnelNotDefaultRouteError + Raises an TunnelNotDefaultRouteError (including when no routes are present) """ if not platform.system() == "Linux": @@ -71,7 +73,6 @@ class LeapNetworkChecker(object): if not destination == '00000000' or not iface == 'tun0': raise exceptions.TunnelNotDefaultRouteError() - def get_default_interface_gateway(self): """only impletemented for linux so far.""" if not platform.system() == "Linux": diff --git a/src/leap/base/network.py b/src/leap/base/network.py index 159e9b21..4a90f2f4 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -21,13 +21,15 @@ class NetworkChecker(object): def __init__(self, *args, **kwargs): self.status_signals = kwargs.pop('status_signals', None) self.watcher_cb = kwargs.pop('status_signals', None) - self.error_cb = kwargs.pop('error_cb', - lambda exc: logger.error("%s", exc.message)) + self.error_cb = kwargs.pop( + 'error_cb', + lambda exc: logger.error("%s", exc.message)) self.shutdown = threading.Event() self.checker = LeapNetworkChecker() def start(self): - self.process_handle = self._launch_recurrent_network_checks((self.error_cb,)) + self.process_handle = self._launch_recurrent_network_checks( + (self.error_cb,)) def stop(self): self.shutdown.set() @@ -51,7 +53,8 @@ class NetworkChecker(object): sleep(1) observer_dict = dict((( - observer, process_events(observer)) for observer in fail_callbacks)) + observer, + process_events(observer)) for observer in fail_callbacks)) while not self.shutdown.is_set(): try: self.checker.check_tunnel_default_interface() @@ -65,7 +68,7 @@ class NetworkChecker(object): self.shutdown.clear() def _launch_recurrent_network_checks(self, fail_callbacks): - #we need to wrap the fail callback in a turple + #we need to wrap the fail callback in a tuple watcher = launch_thread( self._network_checks_thread, (fail_callbacks,)) diff --git a/src/leap/base/tests/test_checks.py b/src/leap/base/tests/test_checks.py index 30746991..bec09ce6 100644 --- a/src/leap/base/tests/test_checks.py +++ b/src/leap/base/tests/test_checks.py @@ -74,7 +74,8 @@ class LeapNetworkCheckTest(BaseLeapTest): "Iface\tDestination Gateway\t" "Flags\tRefCntd\tUse\tMetric\t" "Mask\tMTU\tWindow\tIRTT\n" - "wlan0\t00000000\t0102A8C0\t0003\t0\t0\t0\t00000000\t0\t0\t0") + "wlan0\t00000000\t0102A8C0\t" + "0003\t0\t0\t0\t00000000\t0\t0\t0") checker.check_tunnel_default_interface() with patch('leap.base.checks.open', create=True) as mock_open: diff --git a/src/leap/baseapp/mainwindow.py b/src/leap/baseapp/mainwindow.py index 000db8c9..e48666a4 100644 --- a/src/leap/baseapp/mainwindow.py +++ b/src/leap/baseapp/mainwindow.py @@ -10,6 +10,7 @@ from leap.baseapp.log import LogPaneMixin from leap.baseapp.systray import StatusAwareTrayIconMixin from leap.baseapp.network import NetworkCheckerAppMixin from leap.baseapp.leap_app import MainWindowMixin +from leap.baseapp import dialogs logger = logging.getLogger(name=__name__) @@ -68,6 +69,6 @@ class LeapWindow(QtGui.QMainWindow, # check headless = False before # launching dialog. # (so Qt tests can assert stuff) - - dialog = ErrorDialog() + + dialog = dialogs.ErrorDialog() dialog.warningMessage(message, 'error') diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index c73e8062..f1859c7a 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -1,10 +1,10 @@ from __future__ import print_function + import logging -import time logger = logging.getLogger(name=__name__) from leap.base.network import NetworkChecker -from leap.baseapp.dialogs import ErrorDialog +#from leap.baseapp.dialogs import ErrorDialog class NetworkCheckerAppMixin(object): @@ -20,4 +20,3 @@ class NetworkCheckerAppMixin(object): debug=self.debugmode) self.network_checker.run_checks() - -- cgit v1.2.3 From 1cbf954d9eda71cabfa58811c09bc63cfe9465d5 Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 5 Oct 2012 21:21:22 +0900 Subject: add comments to netchecks --- src/leap/base/checks.py | 40 +++++++++++++++++++++++++++++++++++++--- src/leap/base/network.py | 15 ++++++++++----- src/leap/baseapp/network.py | 9 ++++++--- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py index d02c9a3d..0dbb2846 100644 --- a/src/leap/base/checks.py +++ b/src/leap/base/checks.py @@ -17,8 +17,15 @@ class LeapNetworkChecker(object): """ all network related checks """ - # TODO eventually, use a more portable solution - # like psutil + # TODO refactor to use psutil --- + + # #718 + # XXX get provider gateway as a parameter + # for constructor. + # def __init__(self, *args, **kwargs): + # ... + # provider_gw = kwargs.pop('provider_gw', None) + # self.provider_gateway = provider_gw def run_all(self, checker=None): if not checker: @@ -29,12 +36,23 @@ class LeapNetworkChecker(object): checker.check_tunnel_default_interface() checker.check_internet_connection() checker.is_internet_up() + + # XXX We are pinging the default gateway for our connection right? + # kali: 2012-10-05 20:59 -- I think we should get + # also the default gateway and ping it instead. checker.ping_gateway() + # something like: ? + # see __init__ above + # if self.provider_gateway: + # checker.ping_gateway(self.provider_gateway) + def check_internet_connection(self): try: # XXX remove this hardcoded random ip + # ping leap.se or eip provider instead...? requests.get('http://216.172.161.165') + except (requests.HTTPError, requests.RequestException) as e: raise exceptions.NoInternetConnection(e.message) except requests.ConnectionError as e: @@ -44,6 +62,7 @@ class LeapNetworkChecker(object): error = "No valid internet connection found." else: error = "Provider server appears to be down." + logger.error(error) raise exceptions.NoInternetConnection(error) logger.debug('Network appears to be up.') @@ -78,6 +97,7 @@ class LeapNetworkChecker(object): if not platform.system() == "Linux": raise NotImplementedError + # XXX use psutil f = open("/proc/net/route") route_table = f.readlines() f.close() @@ -102,7 +122,21 @@ class LeapNetworkChecker(object): return default_iface, gateway def ping_gateway(self, gateway): - #TODO: Discuss how much packet loss (%) is acceptable. + # TODO: Discuss how much packet loss (%) is acceptable. + + # XXX -- validate gateway + # -- is it a valid ip? (there's something in util) + # -- is it a domain? + # -- can we resolve? -- raise NoDNSError if not. packet_loss = ping.quiet_ping(gateway)[0] if packet_loss > constants.MAX_ICMP_PACKET_LOSS: raise exceptions.NoConnectionToGateway + + # XXX check for name resolution servers + # dunno what's the best way to do this... + # check for etc/resolv entries or similar? + # just try to resolve? + # is there something in psutil? + + # def check_name_resolution(self): + # pass diff --git a/src/leap/base/network.py b/src/leap/base/network.py index 4a90f2f4..e90139c4 100644 --- a/src/leap/base/network.py +++ b/src/leap/base/network.py @@ -13,18 +13,22 @@ from time import sleep logger = logging.getLogger(name=__name__) -class NetworkChecker(object): +class NetworkCheckerThread(object): """ Manages network checking thread that makes sure we have a working network connection. """ def __init__(self, *args, **kwargs): self.status_signals = kwargs.pop('status_signals', None) - self.watcher_cb = kwargs.pop('status_signals', None) + #self.watcher_cb = kwargs.pop('status_signals', None) self.error_cb = kwargs.pop( 'error_cb', lambda exc: logger.error("%s", exc.message)) self.shutdown = threading.Event() + + # XXX get provider_gateway and pass it to checker + # see in eip.config for function + # #718 self.checker = LeapNetworkChecker() def start(self): @@ -50,9 +54,10 @@ class NetworkChecker(object): self.checker.check_tunnel_default_interface() break except TunnelNotDefaultRouteError: + # XXX ??? why do we sleep here??? sleep(1) - observer_dict = dict((( + fail_observer_dict = dict((( observer, process_events(observer)) for observer in fail_callbacks)) while not self.shutdown.is_set(): @@ -61,8 +66,8 @@ class NetworkChecker(object): self.checker.check_internet_connection() sleep(ROUTE_CHECK_INTERVAL) except Exception as exc: - for obs in observer_dict: - observer_dict[obs].send(exc) + for obs in fail_observer_dict: + fail_observer_dict[obs].send(exc) sleep(ROUTE_CHECK_INTERVAL) #reset event self.shutdown.clear() diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index f1859c7a..fbf9376f 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -3,7 +3,7 @@ from __future__ import print_function import logging logger = logging.getLogger(name=__name__) -from leap.base.network import NetworkChecker +from leap.base.network import NetworkCheckerThread #from leap.baseapp.dialogs import ErrorDialog @@ -14,9 +14,12 @@ class NetworkCheckerAppMixin(object): """ def __init__(self, *args, **kwargs): - self.network_checker = NetworkChecker( + self.network_checker = NetworkCheckerThread( + # XXX watcher? remove ----- watcher_cb=self.newLogLine.emit, - error_cb=self.handle_network_error, + # XXX what callback? ------ + error_cb=None, debug=self.debugmode) + # XXX move run_checks to slot self.network_checker.run_checks() -- cgit v1.2.3 From 6cd947041b3352bebddf3863a86b0a15f8222bcf Mon Sep 17 00:00:00 2001 From: kali Date: Fri, 5 Oct 2012 21:22:36 +0900 Subject: fix seticon call breakage when interface dies --- src/leap/baseapp/systray.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/leap/baseapp/systray.py b/src/leap/baseapp/systray.py index 39a23f49..adcfe9b9 100644 --- a/src/leap/baseapp/systray.py +++ b/src/leap/baseapp/systray.py @@ -162,9 +162,10 @@ class StatusAwareTrayIconMixin(object): oldlayout.itemAt(new).widget().show() def setIcon(self, name): - icon = self.Icons.get(name)(self) - self.trayIcon.setIcon(icon) - #self.setWindowIcon(icon) + icon_fun = self.Icons.get(name) + if icon_fun and callable(icon_fun): + icon = icon_fun(self) + self.trayIcon.setIcon(icon) def getIcon(self, icon_name): return self.states.get(icon_name, None) -- cgit v1.2.3 From 6728eb9afb21bad867e4052a6190a9bdb34c928a Mon Sep 17 00:00:00 2001 From: kali Date: Mon, 8 Oct 2012 07:50:24 +0900 Subject: popup dialog error when network error happens we are shutting down for now. we should be acting upon failures in the near future. lowered the recurrent checks interval to 10 seconds. --- src/leap/base/checks.py | 3 --- src/leap/base/constants.py | 2 +- src/leap/base/exceptions.py | 55 +++++++++++++++++++++++++++++++++++++----- src/leap/baseapp/mainwindow.py | 2 ++ src/leap/baseapp/network.py | 25 +++++++++++++++---- src/leap/eip/checks.py | 6 ++--- src/leap/eip/exceptions.py | 1 - src/leap/util/coroutines.py | 6 +++-- 8 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py index 0dbb2846..84f9dd46 100644 --- a/src/leap/base/checks.py +++ b/src/leap/base/checks.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- - import logging import platform @@ -17,8 +16,6 @@ class LeapNetworkChecker(object): """ all network related checks """ - # TODO refactor to use psutil --- - # #718 # XXX get provider gateway as a parameter # for constructor. diff --git a/src/leap/base/constants.py b/src/leap/base/constants.py index 8a76b6b4..3f32176f 100644 --- a/src/leap/base/constants.py +++ b/src/leap/base/constants.py @@ -29,4 +29,4 @@ DEFAULT_PROVIDER_DEFINITION = { MAX_ICMP_PACKET_LOSS = 10 -ROUTE_CHECK_INTERVAL = 120 +ROUTE_CHECK_INTERVAL = 10 diff --git a/src/leap/base/exceptions.py b/src/leap/base/exceptions.py index 48d827f5..f12a49d5 100644 --- a/src/leap/base/exceptions.py +++ b/src/leap/base/exceptions.py @@ -1,3 +1,43 @@ +""" +Exception attributes and their meaning/uses +------------------------------------------- + +* critical: if True, will abort execution prematurely, + after attempting any cleaning + action. + +* failfirst: breaks any error_check loop that is examining + the error queue. + +* message: the message that will be used in the __repr__ of the exception. + +* usermessage: the message that will be passed to user in ErrorDialogs + in Qt-land. +""" + + +class LeapException(Exception): + """ + base LeapClient exception + sets some parameters that we will check + during error checking routines + """ + critical = False + failfirst = False + warning = False + + +class CriticalError(LeapException): + """ + we cannot do anything about it + """ + critical = True + failfirst = True + + +# In use ??? +# don't thing so. purge if not... + class MissingConfigFileError(Exception): pass @@ -6,24 +46,27 @@ class ImproperlyConfigured(Exception): pass -class NoDefaultInterfaceFoundError(Exception): +class NoDefaultInterfaceFoundError(LeapException): message = "no default interface found" usermessage = "Looks like your computer is not connected to the internet" -class InterfaceNotFoundError(Exception): +class InterfaceNotFoundError(LeapException): # XXX should take iface arg on init maybe? message = "interface not found" -class NoConnectionToGateway(Exception): +class NoConnectionToGateway(CriticalError): message = "no connection to gateway" usermessage = "Looks like there are problems with your internet connection" -class NoInternetConnection(Exception): +class NoInternetConnection(CriticalError): message = "No Internet connection found" + usermessage = "It looks like there is no internet connection." + # and now we try to connect to our web to troubleshoot LOL :P -class TunnelNotDefaultRouteError(Exception): - message = "VPN Maybe be down." +class TunnelNotDefaultRouteError(CriticalError): + message = "Tunnel connection dissapeared. VPN down?" + usermessage = "The Encrypted Connection was lost. Shutting down..." diff --git a/src/leap/baseapp/mainwindow.py b/src/leap/baseapp/mainwindow.py index e48666a4..fdbaf693 100644 --- a/src/leap/baseapp/mainwindow.py +++ b/src/leap/baseapp/mainwindow.py @@ -52,6 +52,8 @@ class LeapWindow(QtGui.QMainWindow, lambda status: self.onStatusChange(status)) self.timer.timeout.connect( lambda: self.onTimerTick()) + self.networkError.connect( + lambda exc: self.onNetworkError(exc)) # ... all ready. go! diff --git a/src/leap/baseapp/network.py b/src/leap/baseapp/network.py index fbf9376f..077d5164 100644 --- a/src/leap/baseapp/network.py +++ b/src/leap/baseapp/network.py @@ -1,10 +1,13 @@ from __future__ import print_function import logging + logger = logging.getLogger(name=__name__) +from PyQt4 import QtCore + +from leap.baseapp.dialogs import ErrorDialog from leap.base.network import NetworkCheckerThread -#from leap.baseapp.dialogs import ErrorDialog class NetworkCheckerAppMixin(object): @@ -15,11 +18,23 @@ class NetworkCheckerAppMixin(object): def __init__(self, *args, **kwargs): self.network_checker = NetworkCheckerThread( - # XXX watcher? remove ----- - watcher_cb=self.newLogLine.emit, - # XXX what callback? ------ - error_cb=None, + error_cb=self.networkError.emit, debug=self.debugmode) # XXX move run_checks to slot self.network_checker.run_checks() + + @QtCore.pyqtSlot(object) + def onNetworkError(self, exc): + """ + slot that receives a network exceptions + and raises a user error message + """ + logger.debug('handling network exception') + logger.error(exc.message) + dialog = ErrorDialog(parent=self) + + if exc.critical: + dialog.criticalMessage(exc.usermessage, "network error") + else: + dialog.warningMessage(exc.usermessage, "network error") diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 9872f8d8..b68ee23a 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -1,12 +1,12 @@ import logging import ssl -import platform +#import platform import time import os from gnutls import crypto -import netifaces -import ping +#import netifaces +#import ping import requests from leap import __branding as BRANDING diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index 6b4ee6aa..24c9bfe8 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -28,7 +28,6 @@ TODO: * EIPClientError: Should inherit from LeapException - and move basic attrs there * gettext / i18n for user messages. diff --git a/src/leap/util/coroutines.py b/src/leap/util/coroutines.py index b9d0a98b..0657fc04 100644 --- a/src/leap/util/coroutines.py +++ b/src/leap/util/coroutines.py @@ -4,10 +4,13 @@ from __future__ import division, print_function +import logging from subprocess import PIPE, Popen import sys from threading import Thread +logger = logging.getLogger(__name__) + ON_POSIX = 'posix' in sys.builtin_module_names @@ -38,8 +41,7 @@ for each event if callable(callback): callback(m) else: - #XXX log instead - print('not a callable passed') + logger.debug('not a callable passed') except GeneratorExit: return -- cgit v1.2.3