summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-03-20 15:56:33 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-09-25 09:43:37 +0200
commit2388b826cc8c0507bc840a728d005450d91adf4b (patch)
tree01efd5c44d9ce9cee30f291d8d5ae5db726a5732
parent19882f158fec139622ffe5b52bc9e834a9d3246e (diff)
Exports + Python: Use an enum for server type
-rw-r--r--exports/exports.go32
-rw-r--r--wrappers/python/eduvpn_common/loader.py6
-rw-r--r--wrappers/python/eduvpn_common/main.py24
3 files changed, 35 insertions, 27 deletions
diff --git a/exports/exports.go b/exports/exports.go
index 5daa05f..19be174 100644
--- a/exports/exports.go
+++ b/exports/exports.go
@@ -178,20 +178,20 @@ func CancelOAuth() *C.char {
}
//export AddServer
-func AddServer(_type *C.char, id *C.char) *C.char {
+func AddServer(_type C.int, id *C.char) *C.char {
// TODO: type
state, stateErr := getVPNState()
if stateErr != nil {
return getCError(stateErr)
}
- t := C.GoString(_type)
+ t := int(_type)
var err error
switch t {
- case "institute_access":
- err = state.AddInstituteServer(C.GoString(id))
- case "secure_internet":
+ case int(srvtypes.TypeInstituteAccess):
+ err = state.AddInstituteServer(C.GoString(id))
+ case int(srvtypes.TypeSecureInternet):
err = state.AddSecureInternetHomeServer(C.GoString(id))
- case "custom_server":
+ case int(srvtypes.TypeCustom):
err = state.AddCustomServer(C.GoString(id))
default:
err = errors.Errorf("invalid type: %v", t)
@@ -200,19 +200,19 @@ func AddServer(_type *C.char, id *C.char) *C.char {
}
//export RemoveServer
-func RemoveServer(_type *C.char, id *C.char) *C.char {
+func RemoveServer(_type C.int, id *C.char) *C.char {
state, stateErr := getVPNState()
if stateErr != nil {
return getCError(stateErr)
}
- t := C.GoString(_type)
+ t := int(_type)
var err error
switch t {
- case "institute_access":
+ case int(srvtypes.TypeInstituteAccess):
err = state.RemoveInstituteAccess(C.GoString(id))
- case "secure_internet":
+ case int(srvtypes.TypeSecureInternet):
err = state.RemoveSecureInternet()
- case "custom_server":
+ case int(srvtypes.TypeCustom):
err = state.RemoveCustomServer(C.GoString(id))
default:
err = errors.Errorf("invalid type: %v", t)
@@ -255,7 +255,7 @@ func ServerList() (*C.char, *C.char) {
}
//export GetConfig
-func GetConfig(_type *C.char, id *C.char, pTCP C.int, tokens *C.char) (*C.char, *C.char) {
+func GetConfig(_type C.int, id *C.char, pTCP C.int, tokens *C.char) (*C.char, *C.char) {
state, stateErr := getVPNState()
if stateErr != nil {
return nil, getCError(stateErr)
@@ -265,14 +265,14 @@ func GetConfig(_type *C.char, id *C.char, pTCP C.int, tokens *C.char) (*C.char,
if err != nil {
return nil, getCError(err)
}
- t := C.GoString(_type)
+ t := int(_type)
var cfg *srvtypes.Configuration
switch t {
- case "institute_access":
+ case int(srvtypes.TypeInstituteAccess):
cfg, err = state.GetConfigInstituteAccess(C.GoString(id), preferTCPBool, tok)
- case "secure_internet":
+ case int(srvtypes.TypeSecureInternet):
cfg, err = state.GetConfigSecureInternet(C.GoString(id), preferTCPBool, tok)
- case "custom_server":
+ case int(srvtypes.TypeCustom):
cfg, err = state.GetConfigCustomServer(C.GoString(id), preferTCPBool, tok)
default:
err = errors.Errorf("invalid type: %v", t)
diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py
index c5709fb..e57e81d 100644
--- a/wrappers/python/eduvpn_common/loader.py
+++ b/wrappers/python/eduvpn_common/loader.py
@@ -64,18 +64,18 @@ def initialize_functions(lib: CDLL) -> None:
lib.DiscoOrganizations.argtypes, lib.DiscoOrganizations.restype = [], DataError
lib.DiscoServers.argtypes, lib.DiscoServers.restype = [], DataError
lib.GetConfig.argtypes, lib.GetConfig.restype = [
- c_char_p,
+ c_int,
c_char_p,
c_int,
c_char_p,
], DataError
lib.AddServer.argtypes, lib.AddServer.restype = [
- c_char_p,
+ c_int,
c_char_p,
], c_char_p
lib.CurrentServer.argtypes, lib.CurrentServer.restype = [], DataError
lib.RemoveServer.argtypes, lib.RemoveServer.restype = [
- c_char_p,
+ c_int,
c_char_p,
], c_char_p
lib.ServerList.argtypes, lib.ServerList.restype = [], DataError
diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py
index f22e4d3..87de430 100644
--- a/wrappers/python/eduvpn_common/main.py
+++ b/wrappers/python/eduvpn_common/main.py
@@ -1,3 +1,4 @@
+from enum import IntEnum
from typing import Any, Callable, Iterator, Optional
from eduvpn_common.loader import initialize_functions, load_lib
@@ -16,6 +17,13 @@ def forwardError(error: bytes | str):
raise WrappedError(error.decode("utf-8"))
+class ServerType(IntEnum):
+ UNKNOWN = 0
+ INSTITUTE_ACCESS = 1
+ SECURE_INTERNET = 2
+ CUSTOM = 3
+
+
class EduVPN(object):
"""The main class used to communicate with the Go library.
It registers the client with the library and then calls the needed appropriate functions
@@ -93,15 +101,15 @@ class EduVPN(object):
if register_err:
forwardError(register_err)
- def add_server(self, _type: str, _id: str) -> None:
+ def add_server(self, _type: ServerType, _id: str) -> None:
"""Add a server
- :param _type: str: The type of server: "institute_access", "secure_internet" or "custom_server"
+ :param _type: ServerType: The type of server e.g. SERVER.INSTITUTE_ACCESS
:param _id: str: The identifier of the server, e.g. "https://vpn.example.com/"
:raises WrappedError: An error by the Go library
"""
- add_err = self.go_function(self.lib.AddServer, _type, _id)
+ add_err = self.go_function(self.lib.AddServer, int(_type), _id)
if add_err:
forwardError(add_err)
@@ -140,25 +148,25 @@ class EduVPN(object):
forwardError(servers_err)
return servers
- def remove_server(self, _type: str, _id: str) -> None:
+ def remove_server(self, _type: ServerType, _id: str) -> None:
"""Remove a server
- :param _type: str: The type of server: "institute_access", "secure_internet" or "custom_server"
+ :param _type: ServerType: The type of server e.g. SERVER.INSTITUTE_ACCESS
:param _id: str: The identifier of the server, e.g. "https://vpn.example.com/"
:raises WrappedError: An error by the Go library
"""
- remove_err = self.go_function(self.lib.RemoveServer, _type, _id)
+ remove_err = self.go_function(self.lib.RemoveServer, int(_type), _id)
if remove_err:
forwardError(remove_err)
def get_config(
- self, _type: str, identifier: str, prefer_tcp: bool = False, tokens: str = "{}"
+ self, _type: ServerType, identifier: str, prefer_tcp: bool = False, tokens: str = "{}"
) -> Optional[str]:
"""Get an OpenVPN/WireGuard configuration from the server
- :param _type: str: The type of server: "institute_access", "secure_internet" or "custom_server"
+ :param _type: ServerType: The type of server e.g. SERVER.INSTITUTE_ACCESS
:param identifier: str: The identifier of the server, e.g. URL or ORG ID
:param prefer_tcp: bool: (Default value = False): Whether or not to prefer TCP
:param tokens: str (Defualt value = ""): The OAuth tokens if available