diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-12 19:18:05 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | 74e36f0ead717105f26087c2cab08b41ba5a7ce8 (patch) | |
| tree | 1eb2b7516bea705c9b5a50ce0965e170414ed880 /internal/config | |
| parent | 682d70091af2044ff6d8b350da9dff13163232e2 (diff) | |
All: Document everything to pass revive lint
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 12 | ||||
| -rw-r--r-- | internal/config/v1/v1.go | 61 | ||||
| -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 |
5 files changed, 113 insertions, 44 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 59d47e0..ed424f8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,19 +16,23 @@ import ( const stateFile = "state.json" +// Config represents the config state file type Config struct { directory string - V2 *v2.V2 + // V2 indicates we are version 2 + V2 *v2.V2 } func (c *Config) filename() string { return path.Join(c.directory, stateFile) } +// Discovery gets the discovery list from the state file func (c *Config) Discovery() *discovery.Discovery { return &c.V2.Discovery } +// Save saves the state file to disk func (c *Config) Save() error { if err := util.EnsureDirectory(c.directory); err != nil { return err @@ -45,6 +49,7 @@ func (c *Config) Save() error { return nil } +// Load loads the state file from disk func (c *Config) Load() error { bts, err := os.ReadFile(c.filename()) if err != nil { @@ -64,11 +69,16 @@ func (c *Config) Load() error { return nil } +// Versioned is the final top-level state file that is written to disk type Versioned struct { + // V1 is the version 1 state file that is no longer used but converted from V1 *v1.V1 `json:"v1,omitempty"` + // V2 is the version 2 state file V2 *v2.V2 `json:"v2,omitempty"` } +// NewFromDirectory creates a new config struct from a directory +// It does this by loading the JSON file from disk func NewFromDirectory(dir string) *Config { cfg := Config{ directory: dir, diff --git a/internal/config/v1/v1.go b/internal/config/v1/v1.go index 1e374b1..1d96d13 100644 --- a/internal/config/v1/v1.go +++ b/internal/config/v1/v1.go @@ -12,49 +12,78 @@ import ( "github.com/eduvpn/eduvpn-common/types/server" ) +// Profiles is the list of profiles type Profiles struct { profiles.Info Current string `json:"current_profile"` } +// Base is the base type of a server type Base struct { - BaseURL string `json:"base_url"` - Profiles Profiles `json:"profiles"` - StartTime time.Time `json:"start_time"` + // BaseURL is the base_url from discovery + BaseURL string `json:"base_url"` + // profiles is the last of profile + Profiles Profiles `json:"profiles"` + // StartTime is the time when we started the connection + StartTime time.Time `json:"start_time"` + // StartTimeOAuth is the time when we last started OAuth StartTimeOAuth time.Time `json:"start_time_oauth"` - ExpireTime time.Time `json:"expire_time"` + // ExpireTime is the time when the connection expires + ExpireTime time.Time `json:"expire_time"` } +// InstituteServer is the struct that represents an institute access server type InstituteServer struct { - Base Base `json:"base"` + // Base is the base of the server + Base Base `json:"base"` + // Profiles are the list of profiles Profiles Profiles `json:"profiles"` } +// InstituteServers is a list of institute access servers type InstituteServers struct { - Map map[string]InstituteServer `json:"map"` - CurrentURL string `json:"current_url"` + // Map is the map from base url to an institute access server + Map map[string]InstituteServer `json:"map"` + // CurrentURL is the URL of the currently configured server + CurrentURL string `json:"current_url"` } type ( - CustomServer = InstituteServer + // CustomServer is an alias to InstituteServer + CustomServer = InstituteServer + // CustomServers is an alias to InstituteServers CustomServers = InstituteServers ) +// SecureInternetHome represents a secure internet home server type SecureInternetHome struct { - BaseMap map[string]*Base `json:"base_map"` - DisplayName map[string]string `json:"display_name"` - HomeOrganizationID string `json:"home_organization_id"` - CurrentLocation string `json:"current_location"` + // BaseMap is the map from country code to a server base + BaseMap map[string]*Base `json:"base_map"` + // DisplayName is the map from language code to UI name + DisplayName map[string]string `json:"display_name"` + // HomeOrganizationID is the identifier of the home organization + HomeOrganizationID string `json:"home_organization_id"` + // CurrentLocation is the country code of the currently configured server + CurrentLocation string `json:"current_location"` } +// Servers represents the list of servers type Servers struct { - Custom CustomServers `json:"custom_servers"` - Institute InstituteServers `json:"institute_servers"` + // Custom are the "custom" servers; the servers that are added by the user + Custom CustomServers `json:"custom_servers"` + // Institute are the institute access servers configured from discovery + Institute InstituteServers `json:"institute_servers"` + // SecureInternetHome is the secure internet home server + // Also obtained through discovery SecureInternetHome SecureInternetHome `json:"secure_internet_home"` - IsType server.Type `json:"is_secure_internet"` + // IsType represents which server type was last configured + IsType server.Type `json:"is_secure_internet"` } +// V1 is the top-level struct for the first version of the state file type V1 struct { + // Discovery is the list of discovery servers Discovery discovery.Discovery `json:"discovery"` - Servers Servers `json:"servers"` + // Servers is the list of servers in the app + Servers Servers `json:"servers"` } 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{ |
