summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/vpn/gateways.py
blob: 92367c30d8c7dee738d42f63059bdda1b4f5e77c (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
# -*- coding: utf-8 -*-
# gateways.py
# Copyright (C) 2013-2017 LEAP Encryption Access Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Gateway Selection
"""
import copy
import logging
import os
import re
import time

from leap.common.config import get_path_prefix


def _normalized(label):
    return label.lower().replace(',', '_').replace(' ', '_')


class GatewaySelector(object):

    # http://www.timeanddate.com/time/map/
    equivalent_timezones = {13: -11, 14: -10}

    def __init__(self, gateways=None, locations=None, tz_offset=None, preferred=None):
        '''
        Constructor for GatewaySelector.

        By default, we will use a Time Zone Heuristic to choose the closest
        gateway to the user.

        If the user specified something in the 'vpn_prefs' section of
        bitmaskd.cfg, we will passed a dictionary here with entries for
        'countries' and 'locations', in the form of a list. The 'locations'
        entry has preference over the 'countries' one.

        :param gateways: an unordered list of all the available gateways, read
                         from the eip-config file.
        :type gateways: list
        :param locations: a dictionary with the locations, as read from the
                          eip-config file
        :type locations: dict
        :param tz_offset: use this offset as a local distance to GMT.
        :type tz_offset: int
        :param preferred: a dictionary containing the country code (cc) and the
                          locations (locations) manually preferred by the user.
        :type preferred: dict
        '''
        if gateways is None:
            gateways = []
        if locations is None:
            locations = {}
        if preferred is None:
            preferred = {}
        self.gateways = gateways
        self.locations = locations
        self.preferred = preferred

        self._local_offset = tz_offset
        if tz_offset is None:
            tz_offset = self._get_local_offset()
        if tz_offset in self.equivalent_timezones:
            tz_offset = self.equivalent_timezones[tz_offset]
        self._local_offset = tz_offset

    def select_gateways(self):
        """
        Returns the IPs top 4 preferred gateways, in order.
        """
        gateways = [gateway[1] for gateway in self.get_sorted_gateways()][:4]
        return gateways

    def get_sorted_gateways(self):
        """
        Returns a tuple with location-label, IP and Country Code with all the
        available gateways, sorted by order of preference.
        """
        gateways_timezones = []
        locations = self.locations

        for idx, gateway in enumerate(self.gateways):
            distance = 99  # if hasn't location -> should go last
            location = locations.get(gateway.get('location'))

            label = gateway.get('location', 'Unknown')
            country = 'XX'
            if location is not None:
                country = location.get('country_code', 'XX')
                label = location.get('name', label)
                timezone = location.get('timezone')
                if timezone is not None:
                    offset = int(timezone)
                    if offset in self.equivalent_timezones:
                        offset = self.equivalent_timezones[offset]
                    distance = self._get_timezone_distance(offset)
            ip = self.gateways[idx].get('ip_address')
            gateways_timezones.append((ip, distance, label, country))

        gateways_timezones = sorted(gateways_timezones, key=lambda gw: gw[1])

        result = []
        for ip, distance, label, country in gateways_timezones:
            result.append((label, ip, country))

        filtered = self.apply_user_preferences(result)
        return filtered 

    def apply_user_preferences(self, options):
        """
        We re-sort the pre-sorted list of gateway options, according with the
        user's preferences.

        Location has preference over the Country Code indication.
        """
        applied = []
        presorted = copy.copy(options)
        for location in self.preferred.get('loc', []):
            for index, data in enumerate(presorted):
                label, ip, country = data
                if _normalized(label) == _normalized(location):
                    applied.append((label, ip, country))
                    presorted.pop(index)

        for cc in self.preferred.get('cc', []):
            for index, data in enumerate(presorted):
                label, ip, country = data
                if _normalized(country) == _normalized(cc):
                    applied.append((label, ip, country))
                    presorted.pop(index)
        if presorted:
            applied += presorted 
        return applied

    def get_gateways_country_code(self):
        country_codes = {}
        locations = self.locations
        if not locations:
            return
        gateways = self.gateways

        for idx, gateway in enumerate(gateways):
            gateway_location = gateway.get('location')

            ip = self._eipconfig.get_gateway_ip(idx)
            if gateway_location is not None:
                ccode = locations[gateway['location']]['country_code']
                country_codes[ip] = ccode
        return country_codes

    def _get_timezone_distance(self, offset):
        '''
        Return the distance between the local timezone and
        the one with offset 'offset'.

        :param offset: the distance of a timezone to GMT.
        :type offset: int
        :returns: distance between local offset and param offset.
        :rtype: int
        '''
        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):
        '''
        Return the distance between GMT and the local timezone.

        :rtype: int
        '''
        local_offset = time.timezone
        if time.daylight:
            local_offset = time.altzone

        return -local_offset / 3600