summaryrefslogtreecommitdiff
path: root/src
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 /src
parenteb5a1f2e9d47530c1896f49a2c4e7ffc82bcce4f (diff)
Save a local copy of the disco list
Diffstat (limited to 'src')
-rw-r--r--src/discovery.go37
-rw-r--r--src/state.go12
2 files changed, 46 insertions, 3 deletions
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
}