summaryrefslogtreecommitdiff
path: root/wrappers/python/eduvpn_common/server.py
blob: 6760b405efaaf5ce080e952fc4167fe961461c9f (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from ctypes import CDLL, POINTER, c_void_p, cast
from datetime import datetime
from typing import List, Optional, Type

from eduvpn_common.types import (
    cConfig,
    cServer,
    cServerLocations,
    cServerProfiles,
    cServers,
    cToken,
)


class Profile:
    """The class that represents a server profile.

    :param: identifier: str: The identifier (id) of the profile
    :param: display_name: str: The display name of the profile
    :param: default_gateway: str: Whether or not this profile should have the default gateway set
    :param: search_domains: List[str]: The list of DNS search domains
    """

    def __init__(
        self,
        identifier: str,
        display_name: str,
        default_gateway: bool,
        dns_search_domains: List[str],
    ):
        self.identifier = identifier
        self.display_name = display_name
        self.default_gateway = default_gateway
        self.dns_search_domains = dns_search_domains

    def __str__(self):
        return self.display_name


class Token:
    """The class that represents oauth Tokens

    :param: access: str: The access token
    :param: refresh: str: The refresh token
    :param: expired: int: The expire unix time
    """

    def __init__(self, access: str, refresh: str, expired: int):
        self.access = access
        self.refresh = refresh
        self.expires = expired


class Config:
    """The class that represents an OpenVPN/WireGuard config

    :param: config: str: The config string
    :param: config_type: str: The type of config, openvpn/wireguard
    :param: tokens: Optional[Token]: The tokens
    """

    def __init__(self, config: str, config_type: str, tokens: Optional[Token]):
        self.config = config
        self.config_type = config_type
        self.tokens = tokens

    def __str__(self):
        return self.config


class Profiles:
    """The class that represents a list of profiles

    :param: profiles: List[Profile]: A list of profiles
    :param: current: int: The current profile index
    """

    def __init__(self, profiles: List[Profile], current: int):
        self.profiles = profiles
        self.current_index = current

    @property
    def current(self) -> Optional[Profile]:
        """Get the current profile if there is any

        :return: The profile if there is a current one (meaning the index is valid)
        :rtype: Optional[Profile]
        """
        if self.current_index < len(self.profiles):
            return self.profiles[self.current_index]
        return None


class Server:
    """The class that represents a server. Use this for a custom server

    :param: url: str: The base URL of the server. In case of secure internet (supertype) this is the organisation ID URL
    :param: display_name: str: The display name of the server
    :param: profiles: Optional[Profiles]: The profiles if there are any already obtained, defaults to None
    :param: expire_time: int: The expiry time in a Unix timestamp, defaults to 0
    """

    def __init__(
        self,
        url: str,
        display_name: str,
        profiles: Optional[Profiles] = None,
        expire_time: int = 0,
    ):
        self.url = url
        self.display_name = display_name
        self.profiles = profiles
        self.expire_time = datetime.fromtimestamp(expire_time)

    def __str__(self):
        return self.display_name

    @property
    def category(self) -> str:
        """Return the category of the server as a string

        :return: The category string, "Custom Server"
        :rtype: str
        """
        return "Custom Server"


class InstituteServer(Server):
    """The class that represents an Institute Access Server

    :param: url: str: The base URL of the Institute Access Server
    :param: display_name: str: The display name of the Institute Access Server
    :param: support_contact: List[str]: The list of support contacts
    :param: profiles: Optional[Profiles]: The profiles of the server if there are any
    :param: expire_time: int: The expiry time in a Unix timestamp
    """

    def __init__(
        self,
        url: str,
        display_name: str,
        support_contact: List[str],
        profiles: Optional[Profiles],
        expire_time: int,
    ):
        super().__init__(url, display_name, profiles, expire_time)
        self.support_contact = support_contact

    @property
    def category(self) -> str:
        """Return the category of the institute server as a string

        :return: The category string, "Institute Access Server"
        :rtype: str
        """
        return "Institute Access Server"


class SecureInternetServer(Server):
    """The class that represents a Secure Internet Server

    :param: org_id: str: The organization ID of the Secure Internet Server as returned by Discovery
    :param: display_name: str: The display name of the server
    :param: support_contact: List[str]: The list of support contacts of the server
    :param: locations: List[str]: The list of secure internet locations
    :param: profiles: Optional[Profiles]: The list of profiles if there are any
    :param: expire_time: int: The expiry time in a Unix timestamp
    :param: country_code: str: The country code of the server
    """

    def __init__(
        self,
        org_id: str,
        display_name: str,
        support_contact: List[str],
        locations: List[str],
        profiles: Optional[Profiles],
        expire_time: int,
        country_code: str,
    ):
        super().__init__(org_id, display_name, profiles, expire_time)
        self.org_id = org_id
        self.support_contact = support_contact
        self.locations = locations
        self.country_code = country_code

    @property
    def category(self) -> str:
        """Return the category of the secure internet server as a string

        :return: The category string, "Secure Internet Server"
        :rtype: str
        """
        return "Secure Internet Server"


def get_type_for_str(type_str: str) -> Type[Server]:
    """Get the right class type for a certain string input

    :param type_str: str: The string that represents the type of server, one of secure_internet, institute_access, custom_server

    :return: The server, defaults to Institute Server if an invalid input is given
    :rtype: Type[Server]
    """
    if type_str == "secure_internet":
        return SecureInternetServer
    if type_str == "custom_server":
        return Server
    return InstituteServer


def get_locations_from_ptr(ptr) -> List[str]:
    """Get the locations from the Go shared library and convert it to a Python usable structure

    :param ptr: The pointer to the List[str] locations as returned by the Go library

    :meta private:

    :return: Locations if there are any
    :rtype: List[str]
    """
    if not ptr:
        return []
    locations = cast(ptr, POINTER(cServerLocations)).contents
    location_list = []
    for i in range(locations.total_locations):
        location_list.append(locations.locations[i].decode("utf-8"))
    return location_list


def get_profiles(ptr) -> Optional[Profiles]:
    """Get the profiles from the Go shared library and convert it to a Python usable structure

    :param ptr: The pointer to the Profiles as returned by the Go library

    :meta private:

    :return: Profiles if there are any
    :rtype: Optional[Profiles]
    """
    if not ptr:
        return None
    profiles = []
    _profiles = ptr.contents
    current_profile = _profiles.current
    if not _profiles.profiles:
        return None
    for i in range(_profiles.total_profiles):
        if not _profiles.profiles[i]:
            continue
        profile = _profiles.profiles[i].contents
        dns_search_domains = []
        for j in range(profile.total_dns_search_domains):
            dns_search_domains.append(profile.dns_search_domains[j].decode("utf-8"))
        profiles.append(
            Profile(
                profile.identifier.decode("utf-8"),
                profile.display_name.decode("utf-8"),
                profile.default_gateway == 1,
                dns_search_domains,
            )
        )
    return Profiles(profiles, current_profile)


def get_server(ptr, _type: Optional[str] = None) -> Optional[Server]:
    """Get the server from the Go shared library and convert it to a Python usable structure

    :param ptr: The pointer as returned by the Go library
    :param _type:  (Default value = None): The optional parameter that represents whether or not the type is enforced to the input. If None it is automatically determined

    :meta private:

    :return: Server if there is any
    :rtype: Optional[Server]
    """
    if not ptr:
        return None

    current_server = ptr.contents
    if _type is None:
        _type = get_type_for_str(current_server.server_type.decode("utf-8"))

    identifier = current_server.identifier.decode("utf-8")
    display_name = current_server.display_name.decode("utf-8")

    if _type is not Server:
        support_contact = []
        for i in range(current_server.total_support_contact):
            support_contact.append(current_server.support_contact[i].decode("utf-8"))
    locations = get_locations_from_ptr(current_server.locations)
    profiles = get_profiles(current_server.profiles)
    if profiles is None:
        profiles = Profiles([], 0)
    if _type is SecureInternetServer:
        return SecureInternetServer(
            identifier,
            display_name,
            support_contact,
            locations,
            profiles,
            current_server.expire_time,
            current_server.country_code.decode("utf-8"),
        )
    if _type is InstituteServer:
        return InstituteServer(
            identifier,
            display_name,
            support_contact,
            profiles,
            current_server.expire_time,
        )
    return Server(identifier, display_name, profiles, current_server.expire_time)


def get_transition_server(lib: CDLL, ptr: c_void_p) -> Optional[Server]:
    """Get a server from a transition event

    :param lib: CDLL: The Go shared library
    :param ptr: c_void_p: The Go's returned C pointer that represents the Server

    :meta private:

    :return: The server if there is any
    :rtype: Optional[Server]
    """
    if ptr:
        server = get_server(cast(ptr, POINTER(cServer)))
        lib.FreeServer(ptr)
        return server
    return None


def get_transition_profiles(lib: CDLL, ptr: c_void_p) -> Optional[Profiles]:
    """Get profiles from a transition event

    :param lib: CDLL: The Go shared library
    :param ptr: c_void_p: The Go's returned C pointer that represents the profiles

    :meta private:

    :return: The profiles if there is any
    :rtype: Optional[Profiles]
    """
    if ptr:
        profiles = get_profiles(cast(ptr, POINTER(cServerProfiles)))
        lib.FreeProfiles(ptr)
        return profiles
    return None


def get_servers(lib: CDLL, ptr: c_void_p) -> Optional[List[Server]]:
    """Get servers from the Go library as a C structure and return a Python usable structure

    :param lib: CDLL: The Go shared library
    :param ptr: c_void_p: The C pointer to the servers structure

    :meta private:

    :return: The list of Servers if there is any
    :rtype: Optional[List[Server]]
    """
    if ptr:
        returned = []
        servers = cast(ptr, POINTER(cServers)).contents
        if servers.custom_servers:
            for i in range(servers.total_custom):
                current = get_server(servers.custom_servers[i], Server)
                if current is None:
                    continue
                returned.append(current)

        if servers.institute_servers:
            for i in range(servers.total_institute):
                current = get_server(servers.institute_servers[i], InstituteServer)
                if current is None:
                    continue
                returned.append(current)

        if servers.secure_internet:
            current = get_server(servers.secure_internet, SecureInternetServer)
            if current is not None:
                returned.append(current)
        lib.FreeServers(ptr)
        return returned
    return None


def get_locations(lib: CDLL, ptr: c_void_p) -> Optional[List[str]]:
    """Get locations from the Go library as a C structure and return a Python usable structure

    :param lib: CDLL: The Go shared library
    :param ptr: c_void_p: The C pointer to the locations structure

    :meta private:

    :return: The list of servers if there are any
    :rtype: Optional[List[str]]
    """
    if ptr:
        location_list = get_locations_from_ptr(ptr)
        lib.FreeSecureLocations(ptr)
        return location_list
    return None


def get_tokens(lib: CDLL, ptr: c_void_p) -> Optional[Token]:
    if ptr:
        toks = cast(ptr, POINTER(cToken)).contents
        access = toks.access.decode("utf-8")
        refresh = toks.refresh.decode("utf-8")
        expired = toks.expired
        lib.FreeTokens(ptr)
        return Token(access, refresh, expired)
    return None


def get_config(lib: CDLL, ptr: c_void_p) -> Optional[Config]:
    """Get the config from the Go library as a C structure and return a Python usable structure

    :param lib: CDLL: The Go shared library
    :param ptr: c_void_p: The C pointer to the confg structure

    :meta private:

    :return: The configuration if there is any
    :rtype: Optional[Config]
    """
    if ptr:
        config = cast(ptr, POINTER(cConfig)).contents
        cfg = config.config.decode("utf-8")
        cfg_type = config.config_type.decode("utf-8")
        tokens = None
        if config.token:
            token_struct = config.token.contents
            tokens = Token(
                token_struct.access.decode("utf-8"),
                token_struct.refresh.decode("utf-8"),
                token_struct.expired,
            )

        config_class = Config(cfg, cfg_type, tokens)
        lib.FreeConfig(ptr)
        return config_class
    return None


def encode_tokens(arg: Optional[Token]) -> cToken:
    if arg is None:
        return cToken("".encode("utf-8"), "".encode("utf-8"), 0)
    return cToken(arg.access.encode("utf-8"), arg.refresh.encode("utf-8"), arg.expires)