summaryrefslogtreecommitdiff
path: root/wrappers/python/eduvpn_common/loader.py
blob: f0f31d6fb16d194b1e62f74131ec7f78954ceb40 (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
import pathlib
import platform
from collections import defaultdict
from ctypes import CDLL, c_char_p, c_int, c_void_p, cdll

from eduvpn_common import __version__
from eduvpn_common.types import (
    cToken,
    DataError,
    ReadRxBytes,
    VPNStateChange,
)


def load_lib() -> CDLL:
    """The function that loads the Go shared library

    :meta private:

    :return: The Go shared library loaded with cdll.LoadLibrary from ctypes
    :rtype: CDLL
    """
    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}-{__version__}{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))

    return lib


def initialize_functions(lib: CDLL) -> None:
    """Initializes the Go shared library functions

    :param lib: CDLL: The Go shared library

    :meta private:
    """
    # 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.CancelOAuth.argtypes, lib.CancelOAuth.restype = [c_char_p], c_void_p
    lib.ChangeSecureLocation.argtypes, lib.ChangeSecureLocation.restype = [
        c_char_p
    ], c_void_p
    lib.Deregister.argtypes, lib.Deregister.restype = [c_char_p], None
    lib.FreeConfig.argtypes, lib.FreeConfig.restype = [c_void_p], None
    lib.FreeDiscoOrganizations.argtypes, lib.FreeDiscoOrganizations.restype = [
        c_void_p
    ], None
    lib.FreeDiscoServers.argtypes, lib.FreeDiscoServers.restype = [c_void_p], None
    lib.FreeError.argtypes, lib.FreeError.restype = [c_void_p], None
    lib.FreeProfiles.argtypes, lib.FreeProfiles.restype = [c_void_p], None
    lib.FreeSecureLocations.argtypes, lib.FreeSecureLocations.restype = [c_void_p], None
    lib.FreeServer.argtypes, lib.FreeServer.restype = [c_void_p], None
    lib.FreeServers.argtypes, lib.FreeServers.restype = [c_void_p], None
    lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None
    lib.GetConfigCustomServer.argtypes, lib.GetConfigCustomServer.restype = [
        c_char_p,
        c_char_p,
        c_int,
        cToken,
    ], DataError
    lib.GetConfigInstituteAccess.argtypes, lib.GetConfigInstituteAccess.restype = [
        c_char_p,
        c_char_p,
        c_int,
        cToken,
    ], DataError
    lib.GetConfigSecureInternet.argtypes, lib.GetConfigSecureInternet.restype = [
        c_char_p,
        c_char_p,
        c_int,
        cToken,
    ], DataError
    lib.GetDiscoOrganizations.argtypes, lib.GetDiscoOrganizations.restype = [
        c_char_p
    ], DataError
    lib.GetDiscoServers.argtypes, lib.GetDiscoServers.restype = [c_char_p], DataError
    lib.GetCurrentServer.argtypes, lib.GetCurrentServer.restype = [c_char_p], DataError
    lib.GetSavedServers.argtypes, lib.GetSavedServers.restype = [c_char_p], DataError
    lib.GoBack.argtypes, lib.GoBack.restype = [c_char_p], None
    lib.InFSMState.argtypes, lib.InFSMState.restype = [c_void_p, c_int], int
    lib.Register.argtypes, lib.Register.restype = [
        c_char_p,
        c_char_p,
        c_char_p,
        VPNStateChange,
        c_int,
    ], c_void_p
    lib.RemoveCustomServer.argtypes, lib.RemoveCustomServer.restype = [
        c_char_p,
        c_char_p,
    ], c_void_p
    lib.AddInstituteAccess.argtypes, lib.AddInstituteAccess.restype = [
        c_char_p,
        c_char_p,
    ], c_void_p
    (
        lib.AddSecureInternetHomeServer.argtypes,
        lib.AddSecureInternetHomeServer.restype,
    ) = [
        c_char_p,
        c_char_p,
    ], c_void_p
    lib.AddCustomServer.argtypes, lib.AddCustomServer.restype = [
        c_char_p,
        c_char_p,
    ], c_void_p
    lib.RemoveInstituteAccess.argtypes, lib.RemoveInstituteAccess.restype = [
        c_char_p,
        c_char_p,
    ], c_void_p
    lib.RemoveSecureInternet.argtypes, lib.RemoveSecureInternet.restype = [
        c_char_p
    ], c_void_p
    lib.RenewSession.argtypes, lib.RenewSession.restype = [c_char_p], c_void_p
    lib.SetConnected.argtypes, lib.SetConnected.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.SetDisconnecting.argtypes, lib.SetDisconnecting.restype = [c_char_p], c_void_p
    lib.SetProfileID.argtypes, lib.SetProfileID.restype = [c_char_p, c_char_p], c_void_p
    lib.SetSearchServer.argtypes, lib.SetSearchServer.restype = [c_char_p], c_void_p
    lib.SetSecureLocation.argtypes, lib.SetSecureLocation.restype = [
        c_char_p,
        c_char_p,
    ], c_void_p
    lib.SetSupportWireguard.argtypes, lib.SetSupportWireguard.restype = [
        c_char_p,
        c_int,
    ], c_void_p
    lib.ShouldRenewButton.argtypes, lib.ShouldRenewButton.restype = [], int
    lib.StartFailover.argtypes, lib.StartFailover.restype = [c_char_p, c_char_p, c_int, ReadRxBytes], DataError
    lib.CancelFailover.argtypes, lib.CancelFailover.restype = [c_char_p], c_void_p