diff options
| -rw-r--r-- | cli/main.go | 4 | ||||
| -rw-r--r-- | exports/exports.go | 14 | ||||
| -rw-r--r-- | src/state.go | 23 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/__init__.py | 12 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/auth.py | 14 | ||||
| -rw-r--r-- | wrappers/python/eduvpncommon/discovery.py | 9 |
6 files changed, 62 insertions, 14 deletions
diff --git a/cli/main.go b/cli/main.go index 4d334ac..c3b6258 100644 --- a/cli/main.go +++ b/cli/main.go @@ -28,7 +28,9 @@ func main() { urlString = "https://" + urlString } - state := eduvpn.Register("org.eduvpn.app.linux", urlString) + state := eduvpn.GetVPNState() + + eduvpn.Register(state, "org.eduvpn.app.linux", urlString) authURL, err := eduvpn.InitializeOAuth(state) if err != nil { log.Fatal(err) diff --git a/exports/exports.go b/exports/exports.go index 3efc820..fea638a 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -20,6 +20,20 @@ func GetOrganizationsList() (*C.char, int8) { return C.CString(body), 0 } +//export Register +func Register(name *C.char, url *C.char) { + eduvpn.Register(eduvpn.GetVPNState(), C.GoString(name), C.GoString(url)) +} + +//export InitializeOAuth +func InitializeOAuth() (*C.char) { + url, err := eduvpn.InitializeOAuth(eduvpn.GetVPNState()) + if err != nil { + panic(err) + } + return C.CString(url) +} + // 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 diff --git a/src/state.go b/src/state.go index a03733a..22dcaaf 100644 --- a/src/state.go +++ b/src/state.go @@ -12,13 +12,28 @@ type EduVPNState struct { Server string } -func Register(name string, server string) *EduVPNState { - state := &EduVPNState{Name: name, Server: server} +func Register(state *EduVPNState, name string, server string) error { + state.Name = name + state.Server = server + endpoints, err := APIGetEndpoints(state) if err != nil { - panic(err) + return err } + state.Endpoints = endpoints - return state + return nil } + + +var VPNStateInstance *EduVPNState + +func GetVPNState() *EduVPNState { + if VPNStateInstance == nil { + VPNStateInstance = &EduVPNState{} + } + return VPNStateInstance +} + + diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py index 52efa05..911c671 100644 --- a/wrappers/python/eduvpncommon/__init__.py +++ b/wrappers/python/eduvpncommon/__init__.py @@ -31,3 +31,15 @@ class GoSlice(Structure): class DataError(Structure): _fields_ = [('data', c_void_p), ('error', c_int64)] + + +# 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.Register.argtypes, lib.Register.restype = [c_char_p, c_char_p], None +lib.InitializeOAuth.argtypes, lib.InitializeOAuth.restype = [], c_void_p +lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError +lib.GetServersList.argtypes, lib.GetServersList.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 + diff --git a/wrappers/python/eduvpncommon/auth.py b/wrappers/python/eduvpncommon/auth.py new file mode 100644 index 0000000..b0d1410 --- /dev/null +++ b/wrappers/python/eduvpncommon/auth.py @@ -0,0 +1,14 @@ +from . import lib +from ctypes import * + +def Register(name, url): + name_bytes = name.encode('utf-8') + url_bytes = url.encode('utf-8') + lib.Register(name_bytes, url_bytes) + +def InitializeOAuth(): + ptr = lib.InitializeOAuth() + value = cast(ptr, c_char_p).value + authURL = value.decode() + lib.FreeString(ptr) + return authURL diff --git a/wrappers/python/eduvpncommon/discovery.py b/wrappers/python/eduvpncommon/discovery.py index 7705968..4820ca2 100644 --- a/wrappers/python/eduvpncommon/discovery.py +++ b/wrappers/python/eduvpncommon/discovery.py @@ -5,15 +5,6 @@ from typing import Callable, List, Dict, Any from enum import Enum import json -# 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.GetServersList.argtypes, lib.GetServersList.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 - def getList(func: Callable) -> List[Dict[str, Any]]: dataError = func() ptr = dataError.data |
