From 1f9acbe3366d08c280b9076274f612efabde3870 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Fri, 21 Jun 2013 17:35:46 -0300 Subject: Bugfix: return the correct gateway. After this fix we always returned the first gateway, no matter what the user asked for. --- src/leap/services/eip/eipconfig.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/leap/services/eip/eipconfig.py') diff --git a/src/leap/services/eip/eipconfig.py b/src/leap/services/eip/eipconfig.py index a85fe64a..ff98bf55 100644 --- a/src/leap/services/eip/eipconfig.py +++ b/src/leap/services/eip/eipconfig.py @@ -176,7 +176,7 @@ class EIPConfig(BaseConfig): index = 0 logger.warning("Provided an unknown gateway index %s, " + "defaulting to 0") - ip_addr_str = gateways[0]["ip_address"] + ip_addr_str = gateways[index]["ip_address"] try: ipaddr.IPAddress(ip_addr_str) -- cgit v1.2.3 From 4c54df049b3ef23b29c1e4e2c42201012843c8a1 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Fri, 21 Jun 2013 17:38:03 -0300 Subject: Allow to create the class using a specific offset. This is useful for testing purposes, so we can be consistent with the distance calculation. --- src/leap/services/eip/eipconfig.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/leap/services/eip/eipconfig.py') diff --git a/src/leap/services/eip/eipconfig.py b/src/leap/services/eip/eipconfig.py index ff98bf55..e79314ce 100644 --- a/src/leap/services/eip/eipconfig.py +++ b/src/leap/services/eip/eipconfig.py @@ -39,17 +39,21 @@ class VPNGatewaySelector(object): VPN Gateway selector. """ - def __init__(self, eipconfig): + def __init__(self, eipconfig, tz_offset=None): ''' Constructor for VPNGatewaySelector. :param eipconfig: a valid EIP Configuration. :type eipconfig: EIPConfig + :param tz_offset: use this offset as a local distance to GMT. + :type tz_offset: datetime.timedelta ''' leap_assert_type(eipconfig, EIPConfig) - self._local_offset = 0 # defaults to GMT - self._local_timezone = None - self._set_local_offset() + + self._local_offset = tz_offset + if tz_offset is None: + self._local_offset = self._get_local_offset() + self._eipconfig = eipconfig def get_gateways(self): @@ -95,15 +99,17 @@ class VPNGatewaySelector(object): hours = diff.seconds / (60 * 60) return hours - def _set_local_offset(self): + def _get_local_offset(self): ''' - Sets the distance between GMT and the local timezone. + Returns the distance between GMT and the local timezone. + + :rtype: datetime.timedelta ''' local_offset = time.timezone if time.daylight: local_offset = time.altzone - self._local_offset = datetime.timedelta(seconds=-local_offset) + return datetime.timedelta(seconds=-local_offset) class EIPConfig(BaseConfig): @@ -233,6 +239,7 @@ if __name__ == "__main__": if eipconfig.load("leap/providers/bitmask.net/eip-service.json"): print eipconfig.get_clusters() print eipconfig.get_gateways() + print eipconfig.get_locations() print eipconfig.get_openvpn_configuration() print eipconfig.get_serial() print eipconfig.get_version() -- cgit v1.2.3 From edda5a3c4762c7eeb3bdeda19ddfa0c72d98f387 Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Mon, 24 Jun 2013 12:21:18 -0300 Subject: Bugfix: timezone calculation. Also use int notation instead of datetime.timedelta. --- src/leap/services/eip/eipconfig.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/leap/services/eip/eipconfig.py') diff --git a/src/leap/services/eip/eipconfig.py b/src/leap/services/eip/eipconfig.py index e79314ce..97eb3dfb 100644 --- a/src/leap/services/eip/eipconfig.py +++ b/src/leap/services/eip/eipconfig.py @@ -21,7 +21,6 @@ Provider configuration import logging import os import re -import datetime import time import ipaddr @@ -46,7 +45,7 @@ class VPNGatewaySelector(object): :param eipconfig: a valid EIP Configuration. :type eipconfig: EIPConfig :param tz_offset: use this offset as a local distance to GMT. - :type tz_offset: datetime.timedelta + :type tz_offset: int ''' leap_assert_type(eipconfig, EIPConfig) @@ -93,23 +92,29 @@ class VPNGatewaySelector(object): :returns: distance between local offset and param offset. :rtype: int ''' - delta1 = datetime.timedelta(hours=offset) - delta2 = self._local_offset - diff = abs(delta1 - delta2) - hours = diff.seconds / (60 * 60) - return hours + timezones = range(-11, 13) + tz1 = offset + tz2 = self._local_offset + distance = abs(timezones.index(tz1) - timezones.index(tz2)) + if distance > 12: + if tz1 < 0: + distance = timezones.index(tz1) + timezones[::-1].index(tz2) + else: + distance = timezones[::-1].index(tz1) + timezones.index(tz2) + + return distance def _get_local_offset(self): ''' Returns the distance between GMT and the local timezone. - :rtype: datetime.timedelta + :rtype: int ''' local_offset = time.timezone if time.daylight: local_offset = time.altzone - return datetime.timedelta(seconds=-local_offset) + return local_offset / 3600 class EIPConfig(BaseConfig): -- cgit v1.2.3