summaryrefslogtreecommitdiff
path: root/exports
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-15 16:12:48 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-05 12:26:14 +0200
commit5887d925486e7ce650c3440de6cd29dd2240e929 (patch)
tree6bfce1ddb6a1de5647f23fe7131098ebb421034e /exports
parenta019e95fdbaea3d7af2d8ad10903fd656bfc4466 (diff)
Add callback state change and simplify wrappers
The python wrapper contained lots of code that should not be exposed. The other wrappers I will update later
Diffstat (limited to 'exports')
-rw-r--r--exports/exports.go65
1 files changed, 22 insertions, 43 deletions
diff --git a/exports/exports.go b/exports/exports.go
index e6be488..f0883a8 100644
--- a/exports/exports.go
+++ b/exports/exports.go
@@ -2,27 +2,38 @@ package main
/*
#include <stdlib.h>
+
+typedef void (*PythonCB)(const char* oldstate, const char* newstate);
+
+// FIXME: Remove this, see: https://stackoverflow.com/questions/58606884/multiple-definition-when-using-cgo
+__attribute__((weak))
+void call_callback(PythonCB callback, const char* oldstate, const char* newstate)
+{
+ callback(oldstate, newstate);
+}
*/
import "C"
import "unsafe"
import "github.com/jwijenbergh/eduvpn-common/src"
-// Functions here should probably not take string parameters, see https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go
-// GetOrganizationsList gets the list of organizations from the disco server.
-// Returns the json data as a string and an error code. This is used as key for looking up data.
-//export GetOrganizationsList
-func GetOrganizationsList() (*C.char, *C.char) {
- body, err := eduvpn.GetOrganizationsList()
- if err != nil {
- return nil, C.CString(err.Error())
+var P_StateCallback C.PythonCB
+
+func StateCallback(old_state string, new_state string) {
+ if P_StateCallback == nil {
+ return
}
- return C.CString(body), nil
+ oldState_c := C.CString(old_state)
+ newState_c := C.CString(new_state)
+ C.call_callback(P_StateCallback, oldState_c, newState_c)
+ C.free(unsafe.Pointer(oldState_c))
+ C.free(unsafe.Pointer(newState_c))
}
//export Register
-func Register(name *C.char, url *C.char) {
- eduvpn.Register(eduvpn.GetVPNState(), C.GoString(name), C.GoString(url))
+func Register(name *C.char, url *C.char, stateCallback C.PythonCB) {
+ P_StateCallback = stateCallback
+ eduvpn.Register(eduvpn.GetVPNState(), C.GoString(name), C.GoString(url), StateCallback)
}
//export InitializeOAuth
@@ -34,42 +45,10 @@ func InitializeOAuth() (*C.char, *C.char) {
return C.CString(url), nil
}
-// GetServersList gets the list of servers from the disco server.
-// Returns the json data as a string and an error code. This is used as key for looking up data.
-//export GetServersList
-func GetServersList() (*C.char, *C.char) {
- body, err := eduvpn.GetServersList()
- if err != nil {
- return nil, C.CString(err.Error())
- }
- return C.CString(body), nil
-}
-
//export FreeString
func FreeString(addr *C.char) {
C.free(unsafe.Pointer(addr))
}
-// Verify verifies a signature on a JSON file. See eduvpn.Verify for more details.
-// It returns 0 for a valid signature and a nonzero eduvpn.VerifyErrorCode otherwise.
-// signatureFileContent must be UTF-8-encoded.
-//export Verify
-func Verify(signatureFileContent []byte, signedJson []byte, expectedFileName []byte, minSignTime uint64) (int8, *C.char) {
- valid, err := eduvpn.Verify(string(signatureFileContent), signedJson, string(expectedFileName), minSignTime, false)
- if valid {
- return 0, nil
- } else {
- return 1, C.CString(err.Error())
- }
-}
-
-// InsecureTestingSetExtraKey adds an extra allowed key for verification with Verify.
-// ONLY USE FOR TESTING. Not Thread-safe. Do not call in parallel to Verify.
-// keyString must be an ASCII Base64-encoded key.
-//export InsecureTestingSetExtraKey
-func InsecureTestingSetExtraKey(keyString []byte) {
- eduvpn.InsecureTestingSetExtraKey(string(keyString))
-}
-
// Not used in library, but needed to compile.
func main() { panic("compile with -buildmode=c-shared") }