summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-22 14:42:20 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-22 14:42:20 +0100
commit22f9d7bafc60f56259e74df98e544b5820cfca5b (patch)
treefdc4886d035d04623666cb3132f3054bda77e889
parenteb5a1f2e9d47530c1896f49a2c4e7ffc82bcce4f (diff)
Save a local copy of the disco list
-rw-r--r--exports/exports.go23
-rw-r--r--src/discovery.go37
-rw-r--r--src/state.go12
-rw-r--r--wrappers/python/eduvpncommon/__init__.py20
-rw-r--r--wrappers/python/eduvpncommon/main.py7
5 files changed, 95 insertions, 4 deletions
diff --git a/exports/exports.go b/exports/exports.go
index 0bed7ba..c6a6a91 100644
--- a/exports/exports.go
+++ b/exports/exports.go
@@ -37,6 +37,29 @@ func Register(name *C.char, config_directory *C.char, stateCallback C.PythonCB)
eduvpn.Register(eduvpn.GetVPNState(), C.GoString(name), C.GoString(config_directory), StateCallback)
}
+func ErrorToString(error error) string {
+ if error == nil {
+ return ""
+ }
+
+ return error.Error()
+}
+
+//export GetOrganizationsList
+func GetOrganizationsList() (*C.char, *C.char) {
+ state := eduvpn.GetVPNState()
+ organizations, organizationsErr := state.GetOrganizationsList()
+ return C.CString(organizations), C.CString(ErrorToString(organizationsErr))
+}
+
+
+//export GetServersList
+func GetServersList() (*C.char, *C.char) {
+ state := eduvpn.GetVPNState()
+ servers, serversErr := state.GetServersList()
+ return C.CString(servers), C.CString(ErrorToString(serversErr))
+}
+
//export FreeString
func FreeString(addr *C.char) {
C.free(unsafe.Pointer(addr))
diff --git a/src/discovery.go b/src/discovery.go
index ced7716..fa109c2 100644
--- a/src/discovery.go
+++ b/src/discovery.go
@@ -32,6 +32,11 @@ func (e *DiscoVerifyError) Error() string {
return fmt.Sprintf("failed verifying file %s with signature %s due to error %v", e.File, e.Sigfile, e.Err)
}
+type DiscoList struct {
+ Organizations *string `json:"organizations"`
+ Servers *string `json:"servers"`
+}
+
// Helper function that gets a disco json
func getDiscoFile(jsonFile string) (string, error) {
// Get json data
@@ -75,22 +80,50 @@ func (e *GetListError) Error() string {
return fmt.Sprintf("failed getting disco list file %s with error %v", e.File, e.Err)
}
+// FIXME: Implement these properly based on version and time info
+func (eduvpn *VPNState) DetermineOrganizationsUpdate() bool {
+ return eduvpn.DiscoList == nil || eduvpn.DiscoList.Organizations == nil
+}
+
+func (eduvpn *VPNState) DetermineServersUpdate() bool {
+ return eduvpn.DiscoList == nil || eduvpn.DiscoList.Servers == nil
+}
+
+func (eduvpn *VPNState) EnsureDisco() {
+ if eduvpn.DiscoList == nil {
+ eduvpn.DiscoList = &DiscoList{}
+ }
+}
+
// Get the organization list
-func GetOrganizationsList() (string, error) {
+func (eduvpn *VPNState) GetOrganizationsList() (string, error) {
+ if !eduvpn.DetermineOrganizationsUpdate() {
+ return *eduvpn.DiscoList.Organizations, nil
+ }
file := "organization_list.json"
body, err := getDiscoFile(file)
if err != nil {
return "", &GetListError{File: file, Err: err}
}
+ eduvpn.EnsureDisco()
+ eduvpn.DiscoList.Organizations = &body
+ eduvpn.WriteConfig()
return body, nil
}
// Get the server list
-func GetServersList() (string, error) {
+func (eduvpn *VPNState) GetServersList() (string, error) {
+ if !eduvpn.DetermineServersUpdate() {
+ fmt.Println("DO NOT NEED UPDATE")
+ return *eduvpn.DiscoList.Servers, nil
+ }
file := "server_list.json"
body, err := getDiscoFile("server_list.json")
if err != nil {
return "", &GetListError{File: file, Err: err}
}
+ eduvpn.EnsureDisco()
+ eduvpn.DiscoList.Servers = &body
+ eduvpn.WriteConfig()
return body, nil
}
diff --git a/src/state.go b/src/state.go
index 6f06860..ba485b2 100644
--- a/src/state.go
+++ b/src/state.go
@@ -7,13 +7,23 @@ type VPNState struct {
// The chosen server
Server *Server `json:"server"`
+
+ // The list of servers and organizations from disco
+ DiscoList *DiscoList `json:"disco"`
}
func Register(state *VPNState, name string, directory string, stateCallback func(string, string, string)) error {
state.Name = name
state.ConfigDirectory = directory
- stateCallback("START", "REGISTERED", "test data")
+ stateCallback("START", "REGISTERED", "app registered")
+
+ // Try to load the previous configuration
+
+ if state.LoadConfig() != nil {
+ // This error can be safely ignored, as when the config does not load, the struct will not be filled
+ // Make sure to log this when we have implemented a good logging system
+ }
return nil
}
diff --git a/wrappers/python/eduvpncommon/__init__.py b/wrappers/python/eduvpncommon/__init__.py
index b79c980..ff329a0 100644
--- a/wrappers/python/eduvpncommon/__init__.py
+++ b/wrappers/python/eduvpncommon/__init__.py
@@ -19,10 +19,30 @@ _libfile = f"{_lib_prefixes[_os]}{_libname}{_lib_suffixes[_os]}"
# Library should have been copied to the lib/ folder
lib = cdll.LoadLibrary(str(pathlib.Path(__file__).parent / "lib" / _libfile))
+class DataError(Structure):
+ _fields_ = [('data', c_void_p),
+ ('error', c_void_p)]
+
VPNStateChange = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p)
# Exposed functions
lib.Register.argtypes, lib.Register.restype = [c_char_p, c_char_p, VPNStateChange], None
+lib.GetOrganizationsList.argtypes, lib.GetOrganizationsList.restype = [], DataError
+lib.GetServersList.argtypes, lib.GetServersList.restype = [], DataError
# 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.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None
+
+def GetPtrString(ptr):
+ if ptr:
+ string = cast(ptr, c_char_p).value
+ lib.FreeString(ptr)
+ if string:
+ return string.decode()
+ return ""
+
+
+def GetDataError(data_error):
+ data = GetPtrString(data_error.data)
+ error = GetPtrString(data_error.error)
+ return data, error
diff --git a/wrappers/python/eduvpncommon/main.py b/wrappers/python/eduvpncommon/main.py
index 3bb0c48..ac13344 100644
--- a/wrappers/python/eduvpncommon/main.py
+++ b/wrappers/python/eduvpncommon/main.py
@@ -1,4 +1,4 @@
-from . import lib, VPNStateChange
+from . import lib, VPNStateChange, GetDataError
from ctypes import *
@VPNStateChange
@@ -14,3 +14,8 @@ def Register(name, config_directory, state_callback):
dir_bytes = config_directory.encode('utf-8')
lib.Register(name_bytes, dir_bytes, state_callback)
+def GetDiscoServers():
+ servers, serversErr = GetDataError(lib.GetServersList())
+ organizations, organizationsErr = GetDataError(lib.GetOrganizationsList())
+ return servers, serversErr, organizations, organizationsErr
+