From 948d986fda345ff41da699adf2dbf26144269145 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 14 Mar 2024 17:19:25 +0100 Subject: Client + Server: Cache secure internet profile choice per location --- CHANGES.md | 1 + client/client.go | 10 ++++++++++ internal/config/v2/v2.go | 3 +++ internal/server/server.go | 21 ++++++++------------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ca22adb..f58a26f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ * Add a server internally before authorizing and remove it again if authorization has failed. This makes sure the internal state is always up-to-date with what is happening. This also allows us to move to the main state when authorization is done as previously it could be the case where authorization was done but the server was not added yet * Fix previous state not being set correctly when getting a config and an error happens * Make WireGuard support mandatory +* Cache secure internet profile choice per location # 1.99.1 (2024-03-11) * Disable type annotation for global eduVPN class as it gave a `SyntaxError` on some Python versions. See https://bugs.python.org/issue34939 diff --git a/client/client.go b/client/client.go index 73f94d3..cfb95eb 100644 --- a/client/client.go +++ b/client/client.go @@ -517,6 +517,16 @@ func (c *Client) SetSecureLocation(orgID string, countryCode string) error { return i18nerr.Wrapf(err, "Failed to get the secure internet server with id: '%s' for setting a location", orgID) } srv.CountryCode = countryCode + + // no cached location profiles + if srv.LocationProfiles == nil { + return nil + } + + // restore profile from the location + if v, ok := srv.LocationProfiles[srv.CountryCode]; ok { + srv.Profiles.Current = v + } return nil } diff --git a/internal/config/v2/v2.go b/internal/config/v2/v2.go index 050eadd..143d5f2 100644 --- a/internal/config/v2/v2.go +++ b/internal/config/v2/v2.go @@ -25,6 +25,9 @@ type Server struct { // CountryCode is the country code for the server in case of secure internet // Otherwise it is an empty string CountryCode string `json:"country_code,omitempty"` + + // LocationProfiles are current profiles for each secure internet location + LocationProfiles map[string]string `json:"location_profiles,omitempty"` } // ServerKey is the key type of the server map diff --git a/internal/server/server.go b/internal/server/server.go index 1182f15..2148f85 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -178,7 +178,15 @@ func (s *Server) SetProfileID(id string) error { if err != nil { return err } + oldP := cs.Profiles.Current cs.Profiles.Current = id + + if s.t == srvtypes.TypeSecureInternet { + if cs.LocationProfiles == nil { + cs.LocationProfiles = make(map[string]string) + } + cs.LocationProfiles[cs.CountryCode] = oldP + } return nil } @@ -211,19 +219,6 @@ func (s *Server) ProfileID() (string, error) { return cs.Profiles.Current, nil } -// SetLocation sets the secure internet location for the server -func (s *Server) SetLocation(loc string) error { - if s.t != srvtypes.TypeSecureInternet { - return errors.New("changing secure internet location is only possible when the server is a secure location") - } - cs, err := s.cfgServer() - if err != nil { - return err - } - cs.CountryCode = loc - return nil -} - // SetCurrent sets the current server in the state file to this one func (s *Server) SetCurrent() error { if s.storage == nil { -- cgit v1.2.3