summaryrefslogtreecommitdiff
path: root/wrappers/python/src/__init__.py
blob: ece4b468c3f38583f0e2a8d7ca4a9fc4094269c3 (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
from ctypes import *
from collections import defaultdict
from enum import Enum
import pathlib
import platform
from typing import Tuple, Optional
import json

_lib_prefixes = defaultdict(
    lambda: "lib",
    {
        "windows": "",
    },
)

_lib_suffixes = defaultdict(
    lambda: ".so",
    {
        "windows": ".dll",
        "darwin": ".dylib",
    },
)

_os = platform.system().lower()

_libname = "eduvpn_common"
_libfile = f"{_lib_prefixes[_os]}{_libname}{_lib_suffixes[_os]}"

lib = None

# Try to load in the normal path
try:
    lib = cdll.LoadLibrary(_libfile)
# Otherwise, library should have been copied to the lib/ folder
except:
    lib = cdll.LoadLibrary(str(pathlib.Path(__file__).parent / "lib" / _libfile))


class ErrorLevel(Enum):
    ERR_OTHER = 0
    ERR_INFO = 1

class cServerLocations(Structure):
    _fields_ = [
        ("locations", POINTER(c_char_p)),
        ("total_locations", c_size_t)
    ]

class cDiscoveryOrganization(Structure):
    _fields_ = [
        ("display_name", c_char_p),
        ("org_id", c_char_p),
        ("secure_internet_home", c_char_p),
        ("keyword_list", c_char_p),
    ]

class cDiscoveryOrganizations(Structure):
    _fields_ = [
        ("version", c_ulonglong),
        ("organizations", POINTER(POINTER(cDiscoveryOrganization))),
        ("total_organizations", c_size_t),
    ]

class cServerProfile(Structure):
    _fields_ = [
        ("identifier", c_char_p),
        ("display_name", c_char_p),
        ("default_gateway", c_int),
    ]

class cServerProfiles(Structure):
    _fields_ = [
        ("current", c_int),
        ("profiles", POINTER(POINTER(cServerProfile))),
        ("total_profiles", c_size_t),
    ]

class cServer(Structure):
    _fields_ = [
        ("identifier", c_char_p),
        ("display_name", c_char_p),
        ("country_code", c_char_p),
        ("support_contact", POINTER(c_char_p)),
        ("total_support_contact", c_size_t),
        ("profiles", POINTER(cServerProfiles)),
        ("expire_time", c_ulonglong),
    ]

class cServers(Structure):
    _fields_ = [
        ("custom_servers", POINTER(POINTER(cServer))),
        ("total_custom", c_size_t),
        ("institute_servers", POINTER(POINTER(cServer))),
        ("total_institute", c_size_t),
        ("secure_internet", POINTER(cServer)),
    ]

class DataError(Structure):
    _fields_ = [("data", c_void_p), ("error", c_void_p)]


VPNStateChange = CFUNCTYPE(None, c_char_p, c_int, c_int, c_void_p)

# Exposed functions
# We have to use c_void_p instead of c_char_p to free it properly
# See https://stackoverflow.com/questions/13445568/python-ctypes-how-to-free-memory-getting-invalid-pointer-error
lib.RemoveSecureInternet.argtypes, lib.RemoveSecureInternet.restype = [c_char_p], c_void_p
lib.RemoveInstituteAccess.argtypes, lib.RemoveInstituteAccess.restype = [c_char_p, c_char_p], c_void_p
lib.RemoveCustomServer.argtypes, lib.RemoveCustomServer.restype = [c_char_p, c_char_p], c_void_p
lib.GetConfigSecureInternet.argtypes, lib.GetConfigSecureInternet.restype = [
    c_char_p,
    c_char_p,
    c_int,
], DataError
lib.GetConfigInstituteAccess.argtypes, lib.GetConfigInstituteAccess.restype = [
    c_char_p,
    c_char_p,
    c_int,
], DataError
lib.GetConfigCustomServer.argtypes, lib.GetConfigCustomServer.restype = [
    c_char_p,
    c_char_p,
    c_int,
], DataError
lib.Deregister.argtypes, lib.Deregister.restype = [c_char_p], None
lib.Register.argtypes, lib.Register.restype = [
    c_char_p,
    c_char_p,
    VPNStateChange,
    c_int,
], c_void_p
lib.GetDiscoOrganizations.argtypes, lib.GetDiscoOrganizations.restype = [
    c_char_p
], c_void_p
lib.GetDiscoServers.argtypes, lib.GetDiscoServers.restype = [c_char_p], DataError
lib.GoBack.argtypes, lib.GoBack.restype = [c_char_p], None
lib.CancelOAuth.argtypes, lib.CancelOAuth.restype = [c_char_p], c_void_p
lib.SetProfileID.argtypes, lib.SetProfileID.restype = [c_char_p, c_char_p], c_void_p
lib.ChangeSecureLocation.argtypes, lib.ChangeSecureLocation.restype = [
    c_char_p
], c_void_p
lib.SetSecureLocation.argtypes, lib.SetSecureLocation.restype = [
    c_char_p,
    c_char_p,
], c_void_p
lib.SetConnected.argtypes, lib.SetConnected.restype = [c_char_p], c_void_p
lib.SetDisconnecting.argtypes, lib.SetDisconnecting.restype = [c_char_p], c_void_p
lib.SetConnecting.argtypes, lib.SetConnecting.restype = [c_char_p], c_void_p
lib.SetDisconnected.argtypes, lib.SetDisconnected.restype = [c_char_p, c_int], c_void_p
lib.SetSearchServer.argtypes, lib.SetSearchServer.restype = [c_char_p], c_void_p
lib.ShouldRenewButton.argtypes, lib.ShouldRenewButton.restype = [], int
lib.RenewSession.argtypes, lib.RenewSession.restype = [c_char_p], c_void_p
lib.FreeSecureLocations.argtypes, lib.FreeSecureLocations.restype = [c_void_p], None
lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None
lib.FreeDiscoOrganizations.argtypes, lib.FreeDiscoOrganizations.restype = [c_void_p], None
lib.FreeServers.argtypes, lib.FreeServers.restype = [c_void_p], None
lib.InFSMState.argtypes, lib.InFSMState.restype = [c_void_p, c_int], int
lib.GetSavedServers.argtypes, lib.GetSavedServers.restype = [c_char_p], c_void_p


class WrappedError:
    def __init__(self, traceback: str, cause: str, level: ErrorLevel):
        self.traceback = traceback
        self.cause = cause
        self.level = level


def encode_args(args, types):
    for arg, t in zip(args, types):
        # c_char_p needs the str to be encoded to bytes
        if t is c_char_p:
            arg = arg.encode("utf-8")
        yield arg


def decode_res(t):
    return decode_map.get(t, lambda x: x)


def get_ptr_string(ptr: c_void_p) -> str:
    if ptr:
        string = cast(ptr, c_char_p).value
        lib.FreeString(ptr)
        if string:
            return string.decode()
    return ""


def get_ptr_error(ptr: c_void_p) -> Optional[WrappedError]:
    error_string = get_ptr_string(ptr)

    if not error_string:
        return None

    error_json = json.loads(error_string)

    if not error_json:
        return None

    if "level" not in error_json:
        return error_string
    level = error_json["level"]
    traceback = error_json["traceback"]
    cause = error_json["cause"]
    return WrappedError(traceback, cause, ErrorLevel(level))


def get_error(ptr: c_void_p) -> str:
    error = get_ptr_error(ptr)
    if not error:
        return ""

    if not isinstance(error, WrappedError):
        return error
    return error.cause


def get_data_error(data_error: DataError) -> Tuple[str, str]:
    data = get_ptr_string(data_error.data)
    error = get_error(data_error.error)
    return data, error


def get_bool(boolInt: c_int) -> bool:
    return boolInt == 1

decode_map = {
    c_int: get_bool,
    c_void_p: get_error,
    DataError: get_data_error,
}