diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-02-14 11:41:12 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-04-05 12:26:11 +0200 |
| commit | d306d7b27ee1df60bf32f03b6160856e78d1b02c (patch) | |
| tree | 17ead8a0b05fe02817c5be954782fdb53c5fb7ac | |
| parent | abe952080b01be4524fd03462236b6a3e63589f8 (diff) | |
Expose list retrieval by returning a JSON string
Signed-off-by: jwijenbergh <jeroenwijenbergh@protonmail.com>
| -rw-r--r-- | exports/exports.go | 28 | ||||
| -rw-r--r-- | src/server.go | 20 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/__init__.py | 3 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/discovery.py | 23 |
4 files changed, 58 insertions, 16 deletions
diff --git a/exports/exports.go b/exports/exports.go index acf02e1..c4943f1 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -1,6 +1,10 @@ package main +/* +#include <stdlib.h> +*/ import "C" +import "unsafe" import "github.com/jwijenbergh/eduvpn-common/src" @@ -8,15 +12,27 @@ import "github.com/jwijenbergh/eduvpn-common/src" // GetOrganizationsList gets the list of organizations from the disco server. // Returns the unix timestamp of the data. This is used as key for looking up data. //export GetOrganizationsList -//func GetOrganizationsList() uint64 { -// return eduvpn.GetOrganizationsList() -//} +func GetOrganizationsList() (*C.char, int8) { + body, err := eduvpn.GetOrganizationsList() + if err != nil { + return nil, int8(err.(eduvpn.RequestError).Code) + } + return C.CString(body), 0 +} + +//export FreeString +func FreeString(addr *C.char) { + C.free(unsafe.Pointer(addr)) +} // //// GetServerList gets the list of servers from the disco server. //// Returns the unix timestamp of the data. This is used as key for looking up data. -////export GetServerList -//func GetServerList() uint64 { -// return eduvpn.GetServerList() +//func GetServersList() ([]byte, int8) { +// body, err := eduvpn.GetServersList() +// if err != nil { +// return nil, int8(err.(eduvpn.RequestError).Code) +// } +// return body, 0 //} // Verify verifies a signature on a JSON file. See eduvpn.Verify for more details. diff --git a/src/server.go b/src/server.go index 37a8f62..e39f4c7 100644 --- a/src/server.go +++ b/src/server.go @@ -29,13 +29,13 @@ func getFileUrl(url string) ([]byte, error) { // Helper function that gets a disco json // TODO: Verify signature -func getDiscoFile(jsonFile string) ([]byte, error) { +func getDiscoFile(jsonFile string) (string, error) { // Get json data fileUrl := "https://disco.eduvpn.org/v2/" + jsonFile fileBody, error := getFileUrl(fileUrl) if error != nil { - return nil, error + return "", error } // Get signature @@ -43,7 +43,7 @@ func getDiscoFile(jsonFile string) ([]byte, error) { sigBody, error := getFileUrl(sigUrl) if error != nil { - return nil, error + return "", error } // Verify signature @@ -54,19 +54,23 @@ func getDiscoFile(jsonFile string) ([]byte, error) { verifySuccess, verifyErr := Verify(string(sigBody), fileBody, jsonFile, previousSigTime, forcePrehash) if !verifySuccess || verifyErr != nil { - return nil, detailedVPNError{errVerifySigError, "Signature is not valid", verifyErr} + return "", detailedVPNError{errVerifySigError, "Signature is not valid", verifyErr} } - return fileBody, nil + return string(fileBody), nil } // Get the organization list -func GetOrganizationsList() ([]byte, error) { - return getDiscoFile("organization_list.json") +func GetOrganizationsList() (string, error) { + body, err := getDiscoFile("organization_list.json") + if err != nil { + return "", err.(detailedRequestError).ToVerifyError() + } + return body, nil } // Get the server list -func getServersList() ([]byte, error) { +func GetServersList() (string, error) { return getDiscoFile("server_list.json") } diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py index e9d0284..b14b2af 100644 --- a/wrappers/python/eduvpncommon/__init__.py +++ b/wrappers/python/eduvpncommon/__init__.py @@ -28,3 +28,6 @@ class GoSlice(Structure): return GoSlice((c_char * len(bs))(*bs), len(bs), len(bs)) +class DataError(Structure): + _fields_ = [('data', c_void_p), + ('error', c_int64)] diff --git a/wrappers/python/eduvpncommon/discovery.py b/wrappers/python/eduvpncommon/discovery.py index 3ae967b..d7fa73b 100644 --- a/wrappers/python/eduvpncommon/discovery.py +++ b/wrappers/python/eduvpncommon/discovery.py @@ -1,11 +1,30 @@ -from . import lib, GoSlice +from . import lib, GoSlice, DataError from ctypes import * from enum import Enum -#lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], c_uint64 +# 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.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError +lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None + lib.Verify.argtypes, lib.Verify.restype = [GoSlice, GoSlice, GoSlice, c_uint64], c_int64 lib.InsecureTestingSetExtraKey.argtypes, lib.InsecureTestingSetExtraKey.restype = [GoSlice], None +class RequestErrorCode(Enum): + ErrRequestFileError = 1 # The request for the file has failed. + ErrVerifySigError = 2 # The signature failed to verify. + Unknown = -1 # Other unknown error. + +def getOrganizationsList(): + dataError = lib.GetOrganizationsList() + ptr = dataError.data + err = dataError.error + body = None + if not err: + body = cast(ptr, c_char_p).value + lib.FreeString(ptr) + return body + class VerifyErrorCode(Enum): ErrUnknownExpectedFileName = 1 # Unknown expected file name specified. The signature has not been verified. |
