diff options
author | drebs <drebs@leap.se> | 2013-02-09 22:27:22 -0200 |
---|---|---|
committer | drebs <drebs@leap.se> | 2013-02-09 22:27:22 -0200 |
commit | 46a3373295a683db9b66c233b07a7a948b8147bc (patch) | |
tree | 410a47871bfd8654ddabbb53f3c29a89c1550e71 /src/leap/base/checks.py | |
parent | e726f3df7c7efbe4069846468ffdbed6829ccfc4 (diff) | |
parent | b376948ccd59aa02022123102eaae359a536e4e6 (diff) |
Merge branch 'develop' into feature/soledad-api
Diffstat (limited to 'src/leap/base/checks.py')
-rw-r--r-- | src/leap/base/checks.py | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/leap/base/checks.py b/src/leap/base/checks.py index 0ebf4f2f..0bf44f59 100644 --- a/src/leap/base/checks.py +++ b/src/leap/base/checks.py @@ -5,8 +5,6 @@ import re import socket import netifaces -import ping -import requests import sh from leap.base import constants @@ -45,26 +43,25 @@ class LeapNetworkChecker(object): checker.parse_log_and_react([], ()) def check_internet_connection(self): - try: - # XXX remove this hardcoded random ip - # ping leap.se or eip provider instead...? - # XXX could use icmp instead.. - requests.get('http://216.172.161.165') - except requests.ConnectionError as e: - error = "Unidentified Connection Error" - if e.message == "[Errno 113] No route to host": + if _platform == "Linux": + try: + output = sh.ping("-c", "5", "-w", "5", ICMP_TARGET) + # XXX should redirect this to netcheck logger. + # and don't clutter main log. + logger.debug('Network appears to be up.') + except sh.ErrorReturnCode_1 as e: + packet_loss = re.findall("\d+% packet loss", e.message)[0] + logger.debug("Unidentified Connection Error: " + packet_loss) if not self.is_internet_up(): error = "No valid internet connection found." else: error = "Provider server appears to be down." - logger.error(error) - raise exceptions.NoInternetConnection(error) - except (requests.HTTPError, requests.RequestException) as e: - raise exceptions.NoInternetConnection(e.message) - # XXX should redirect this to netcheck logger. - # and don't clutter main log. - logger.debug('Network appears to be up.') + logger.error(error) + raise exceptions.NoInternetConnection(error) + + else: + raise NotImplementedError def is_internet_up(self): iface, gateway = self.get_default_interface_gateway() @@ -82,7 +79,7 @@ class LeapNetworkChecker(object): #toss out header route_table.pop(0) if not route_table: - raise exceptions.TunnelNotDefaultRouteError() + raise exceptions.NoDefaultInterfaceFoundError return route_table def _get_def_iface_osx(self): @@ -158,7 +155,7 @@ class LeapNetworkChecker(object): if _platform == "Linux": default_iface, gw = self._get_def_iface_linux() elif _platform == "Darwin": - default_iface, gw = self.get_def_iface_osx() + default_iface, gw = self._get_def_iface_osx() else: raise NotImplementedError @@ -167,7 +164,7 @@ class LeapNetworkChecker(object): if default_iface not in netifaces.interfaces(): raise exceptions.InterfaceNotFoundError - logger.debug('-- default iface', default_iface) + logger.debug('-- default iface %s', default_iface) return default_iface, gw def ping_gateway(self, gateway): @@ -178,13 +175,15 @@ class LeapNetworkChecker(object): # -- is it a domain? # -- can we resolve? -- raise NoDNSError if not. - # XXX -- needs review! - # We cannout use this ping implementation; it needs root. - # We need to look for another, poors-man implementation - # or wrap around system traceroute (using sh module, fi) - # -- kali - packet_loss = ping.quiet_ping(gateway)[0] - logger.debug('packet loss %s' % packet_loss) + # XXX -- sh.ping implemtation needs review! + try: + output = sh.ping("-c", "10", gateway).stdout + except sh.ErrorReturnCode_1 as e: + output = e.message + finally: + packet_loss = int(re.findall("(\d+)% packet loss", output)[0]) + + logger.debug('packet loss %s%%' % packet_loss) if packet_loss > constants.MAX_ICMP_PACKET_LOSS: raise exceptions.NoConnectionToGateway |