diff options
Diffstat (limited to 'internal/config/v2')
| -rw-r--r-- | internal/config/v2/convert.go | 13 | ||||
| -rw-r--r-- | internal/config/v2/v2.go | 67 | ||||
| -rw-r--r-- | internal/config/v2/v2_test.go | 4 |
3 files changed, 57 insertions, 27 deletions
diff --git a/internal/config/v2/convert.go b/internal/config/v2/convert.go index 0212749..21f39a9 100644 --- a/internal/config/v2/convert.go +++ b/internal/config/v2/convert.go @@ -15,11 +15,11 @@ func v1AuthTime(st time.Time, ost time.Time) time.Time { return ost } -func convertV1Server(list v1.InstituteServers, iscurrent bool, t server.Type) (map[ServerType]*Server, *ServerType) { - ret := make(map[ServerType]*Server) - var lc *ServerType +func convertV1Server(list v1.InstituteServers, iscurrent bool, t server.Type) (map[ServerKey]*Server, *ServerKey) { + ret := make(map[ServerKey]*Server) + var lc *ServerKey for k, v := range list.Map { - key := ServerType{ + key := ServerKey{ T: t, ID: k, } @@ -37,10 +37,11 @@ func convertV1Server(list v1.InstituteServers, iscurrent bool, t server.Type) (m return ret, lc } +// FromV1 converts a version 1 state struct into a v2 one func FromV1(ver1 *v1.V1) *V2 { gsrvs := ver1.Servers - var lc *ServerType + var lc *ServerKey cust, glc := convertV1Server(gsrvs.Custom, gsrvs.IsType == server.TypeCustom, server.TypeCustom) if lc == nil { lc = glc @@ -64,7 +65,7 @@ func FromV1(ver1 *v1.V1) *V2 { } v, ok := sec.BaseMap[sec.CurrentLocation] if v != nil && ok { - t := ServerType{ + t := ServerKey{ T: server.TypeSecureInternet, ID: sec.HomeOrganizationID, } diff --git a/internal/config/v2/v2.go b/internal/config/v2/v2.go index 9608d54..51f7a5f 100644 --- a/internal/config/v2/v2.go +++ b/internal/config/v2/v2.go @@ -1,3 +1,4 @@ +// Package v2 implements version 2 of the state file package v2 import ( @@ -10,41 +11,52 @@ import ( "github.com/eduvpn/eduvpn-common/types/server" ) +// Server is the struct for each server type Server struct { - Profiles server.Profiles `json:"profiles"` - LastAuthorizeTime time.Time `json:"last_authorize_time,omitempty"` - ExpireTime time.Time `json:"expire_time,omitempty"` + // Profiles are the list of profiles + Profiles server.Profiles `json:"profiles"` + // LastAuthorizeTime is the time we last authorized + // This is used for determining when to show e.g. the renew button + LastAuthorizeTime time.Time `json:"last_authorize_time,omitempty"` + // ExpireTime is the time at which the VPN expires + ExpireTime time.Time `json:"expire_time,omitempty"` - // In case of secure internet: + // CountryCode is the country code for the server in case of secure internet + // Otherwise it is an empty string CountryCode string `json:"country_code"` } -type ServerType struct { - T server.Type +// ServerKey is the key type of the server map +type ServerKey struct { + // T is the type of server, e.g. secure internet + T server.Type + // ID is the identifier for the server ID string } const keyFormat = "%d,%s" -func newServerType(key string) (*ServerType, error) { +func newServerType(key string) (*ServerKey, error) { var t server.Type var id string if _, err := fmt.Sscanf(key, keyFormat, &t, &id); err != nil { return nil, err } - return &ServerType{ + return &ServerKey{ T: t, ID: id, }, nil } -func (st ServerType) MarshalText() ([]byte, error) { +// MarshalText convers the server key into one that can be used in a map +func (st ServerKey) MarshalText() ([]byte, error) { k := fmt.Sprintf(keyFormat, st.T, st.ID) return []byte(k), nil } -func (st *ServerType) UnmarshalText(text []byte) error { +// UnmarshalText converts the marshaled key into a ServerType struct +func (st *ServerKey) UnmarshalText(text []byte) error { k := string(text) g, err := newServerType(k) if err != nil { @@ -54,14 +66,21 @@ func (st *ServerType) UnmarshalText(text []byte) error { return nil } +// V2 is the top-level struct for the state file type V2 struct { - List map[ServerType]*Server `json:"server_list,omitempty"` - LastChosen *ServerType `json:"last_chosen_id,omitempty"` - Discovery discovery.Discovery `json:"discovery"` + // List is the list of servers + List map[ServerKey]*Server `json:"server_list,omitempty"` + // LastChosen represents the key of the last chosen server + // A server is chosen if we got a config for it + LastChosen *ServerKey `json:"last_chosen_id,omitempty"` + // Discovery is the cached list of discovery JSON + Discovery discovery.Discovery `json:"discovery"` } +// RemoveServer removes a server with id `id` and type `t` from the V2 struct +// It returns an error if no such server exists func (cfg *V2) RemoveServer(id string, t server.Type) error { - k := ServerType{ + k := ServerKey{ ID: id, T: t, } @@ -78,22 +97,26 @@ func (cfg *V2) RemoveServer(id string, t server.Type) error { return errors.New("server does not exist") } -func (cfg *V2) getServerWithKey(k ServerType) (*Server, error) { +func (cfg *V2) getServerWithKey(k ServerKey) (*Server, error) { if v, ok := cfg.List[k]; ok { return v, nil } return nil, errors.New("server does not exist") } +// GetServer gets a server with id `id` and type `t` +// If the server doesn't exist it returns nil and an error func (cfg *V2) GetServer(id string, t server.Type) (*Server, error) { - k := ServerType{ + k := ServerKey{ ID: id, T: t, } return cfg.getServerWithKey(k) } -func (cfg *V2) CurrentServer() (*Server, *ServerType, error) { +// CurrentServer gets the last chosen server +// It returns the server, the server type and an error if it doesn't exist +func (cfg *V2) CurrentServer() (*Server, *ServerKey, error) { if cfg.LastChosen == nil { return nil, nil, errors.New("no server chosen before") } @@ -104,6 +127,8 @@ func (cfg *V2) CurrentServer() (*Server, *ServerType, error) { return srv, cfg.LastChosen, nil } +// HasSecureInternet returns true whether or not the state file +// has a secure internet server in it func (cfg *V2) HasSecureInternet() bool { for k := range cfg.List { if k.T == server.TypeSecureInternet { @@ -113,21 +138,24 @@ func (cfg *V2) HasSecureInternet() bool { return false } +// AddServer adds a server with id `id`, type `t` and server `srv` func (cfg *V2) AddServer(id string, t server.Type, srv Server) error { if cfg.HasSecureInternet() && t == server.TypeSecureInternet { return errors.New("a secure internet server already exists, remove the other secure internet server first") } - k := ServerType{ + k := ServerKey{ ID: id, T: t, } if cfg.List == nil { - cfg.List = make(map[ServerType]*Server) + cfg.List = make(map[ServerKey]*Server) } cfg.List[k] = &srv return nil } +// PublicCurrent gets the current server as a type that should be returned to the client +// It returns this server or nil and an error if it doesn't exist func (cfg *V2) PublicCurrent(disco *discovery.Discovery) (*server.Current, error) { curr, _, err := cfg.CurrentServer() if err != nil { @@ -207,6 +235,7 @@ func convertSecure(orgID string, countryCode string, disco *discovery.Discovery) }, nil } +// PublicList gets all the servers in a format that is returned to the client func (cfg *V2) PublicList(disco *discovery.Discovery) *server.List { ret := &server.List{} // TODO: profile information? diff --git a/internal/config/v2/v2_test.go b/internal/config/v2/v2_test.go index 5a4c2ea..ce8de01 100644 --- a/internal/config/v2/v2_test.go +++ b/internal/config/v2/v2_test.go @@ -36,7 +36,7 @@ func TestLoad(t *testing.T) { } `, want: &V2{ - List: map[ServerType]*Server{ + List: map[ServerKey]*Server{ {ID: "a", T: server.TypeInstituteAccess}: { Profiles: server.Profiles{ Map: map[string]server.Profile{ @@ -103,7 +103,7 @@ func TestLoad(t *testing.T) { } `, want: &V2{ - List: map[ServerType]*Server{ + List: map[ServerKey]*Server{ {ID: "a", T: server.TypeInstituteAccess}: { Profiles: server.Profiles{ Map: map[string]server.Profile{ |
