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/server | |
| parent | 682d70091af2044ff6d8b350da9dff13163232e2 (diff) | |
All: Document everything to pass revive lint
Diffstat (limited to 'internal/server')
| -rw-r--r-- | internal/server/custom.go | 9 | ||||
| -rw-r--r-- | internal/server/institute.go | 11 | ||||
| -rw-r--r-- | internal/server/secureinternet.go | 11 | ||||
| -rw-r--r-- | internal/server/server.go | 13 | ||||
| -rw-r--r-- | internal/server/servers.go | 36 | ||||
| -rw-r--r-- | internal/server/time.go | 3 |
6 files changed, 73 insertions, 10 deletions
diff --git a/internal/server/custom.go b/internal/server/custom.go index b4b81cb..10e9a28 100644 --- a/internal/server/custom.go +++ b/internal/server/custom.go @@ -10,6 +10,10 @@ import ( "github.com/jwijenbergh/eduoauth-go" ) +// AddCustom adds a custom server to the internal server list +// `ctx` is the context used for cancellation +// `id` is the identifier of the server, the base URL +// `na` specifies whether or not we want to add the server without doing authorization now func (s *Servers) AddCustom(ctx context.Context, id string, na bool) (*Server, error) { sd := api.ServerData{ ID: id, @@ -39,6 +43,11 @@ func (s *Servers) AddCustom(ctx context.Context, id string, na bool) (*Server, e return &cust, nil } +// GetCustom gets a custom server +// `ctx` is the context for cancellation +// `id` is the identifier of the server +// `tok` are the tokens such that we can initialize the API +// `disableAuth` is set to True when authorization should not be triggered func (s *Servers) GetCustom(ctx context.Context, id string, tok *eduoauth.Token, disableAuth bool) (*Server, error) { sd := api.ServerData{ ID: id, diff --git a/internal/server/institute.go b/internal/server/institute.go index 881f96d..aa032c7 100644 --- a/internal/server/institute.go +++ b/internal/server/institute.go @@ -11,6 +11,11 @@ import ( "github.com/jwijenbergh/eduoauth-go" ) +// AddInstitute adds an institute access server +// `ctx` is the context used for cancellation +// `disco` are the discovery servers +// `id` is the identifier for the server, the base url +// `na` is true when authorization should not be triggered func (s *Servers) AddInstitute(ctx context.Context, disco *discovery.Discovery, id string, na bool) (*Server, error) { // This is basically done to double check if the server is part of the institute access section of disco dsrv, err := disco.ServerByURL(id, "institute_access") @@ -43,6 +48,12 @@ func (s *Servers) AddInstitute(ctx context.Context, disco *discovery.Discovery, return &inst, nil } +// GetInstitute gets an institute access server +// `ctx` is the context used for cancellation +// `id` is the identifier for the server, the base url +// `disco` are the discovery servers +// `tok` are the tokens such that we do not have to trigger auth +// `disableAuth` is true when auth should never be triggered func (s *Servers) GetInstitute(ctx context.Context, id string, disco *discovery.Discovery, tok *eduoauth.Token, disableAuth bool) (*Server, error) { // This is basically done to double check if the server is part of the institute access section of disco dsrv, err := disco.ServerByURL(id, "institute_access") diff --git a/internal/server/secureinternet.go b/internal/server/secureinternet.go index 19e75a1..0d06a55 100644 --- a/internal/server/secureinternet.go +++ b/internal/server/secureinternet.go @@ -13,6 +13,11 @@ import ( "github.com/jwijenbergh/eduoauth-go" ) +// AddSecure adds a secure internet server +// `ctx` is the context used for cancellation +// `disco` are the discovery servers +// `orgID` is the organiztaion ID +// `na` specifies whether or not authorization should be triggered when adding func (s *Servers) AddSecure(ctx context.Context, disco *discovery.Discovery, orgID string, na bool) (*Server, error) { if s.config.HasSecureInternet() { return nil, errors.New("a secure internet server already exists") @@ -54,6 +59,12 @@ func (s *Servers) AddSecure(ctx context.Context, disco *discovery.Discovery, org return &sec, nil } +// GetSecure gets a secure internet server +// `ctx` is the context used for cancellation +// `orgID` is the organization ID that identifies the server +// `disco` are the discovery servers +// `tok` are the tokens such that the server can be found without triggering auth +// `disableAuth` is set to true when authorization should not be triggered func (s *Servers) GetSecure(ctx context.Context, orgID string, disco *discovery.Discovery, tok *eduoauth.Token, disableAuth bool) (*Server, error) { srv, err := s.config.GetServer(orgID, server.TypeSecureInternet) if err != nil { diff --git a/internal/server/server.go b/internal/server/server.go index bffeb2c..4b960a5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,3 +1,4 @@ +// Package server implements functions that have to deal with server interaction package server import ( @@ -13,6 +14,7 @@ import ( srvtypes "github.com/eduvpn/eduvpn-common/types/server" ) +// Server is the struct for a single server type Server struct { identifier string t srvtypes.Type @@ -20,8 +22,10 @@ type Server struct { storage *v2.V2 } +// ErrInvalidProfile is an error that is returned when an invalid profile has been chosen var ErrInvalidProfile = errors.New("invalid profile") +// NewServer creates a new server func (s *Servers) NewServer(identifier string, t srvtypes.Type, api *api.API) Server { return Server{ identifier: identifier, @@ -155,6 +159,7 @@ func (s *Server) connect(ctx context.Context, wgSupport bool, pTCP bool) (*srvty }, nil } +// Disconnect sends an API /disconnect to the server func (s *Server) Disconnect(ctx context.Context) error { a, err := s.api() if err != nil { @@ -170,6 +175,7 @@ func (s *Server) cfgServer() (*v2.Server, error) { return s.storage.GetServer(s.identifier, s.t) } +// SetProfileID sets the profile id `id` for the server func (s *Server) SetProfileID(id string) error { cs, err := s.cfgServer() if err != nil { @@ -179,6 +185,7 @@ func (s *Server) SetProfileID(id string) error { return nil } +// SetProfileList sets the profile list `prfs` for the server func (s *Server) SetProfileList(prfs srvtypes.Profiles) error { cs, err := s.cfgServer() if err != nil { @@ -188,6 +195,7 @@ func (s *Server) SetProfileList(prfs srvtypes.Profiles) error { return nil } +// SetExpireTime sets the time `et` when the VPN expires func (s *Server) SetExpireTime(et time.Time) error { cs, err := s.cfgServer() if err != nil { @@ -197,6 +205,7 @@ func (s *Server) SetExpireTime(et time.Time) error { return nil } +// ProfileID gets the profile ID for the server func (s *Server) ProfileID() (string, error) { cs, err := s.cfgServer() if err != nil { @@ -205,6 +214,7 @@ 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") @@ -217,11 +227,12 @@ func (s *Server) SetLocation(loc string) error { return nil } +// SetCurrent sets the current server in the state file to this one func (s *Server) SetCurrent() error { if s.storage == nil { return errors.New("no storage available") } - s.storage.LastChosen = &v2.ServerType{ + s.storage.LastChosen = &v2.ServerKey{ ID: s.identifier, T: s.t, } diff --git a/internal/server/servers.go b/internal/server/servers.go index fe2550c..64e64b6 100644 --- a/internal/server/servers.go +++ b/internal/server/servers.go @@ -12,23 +12,31 @@ import ( "github.com/jwijenbergh/eduoauth-go" ) +// Callbacks defines the interface for doing certain callback operations type Callbacks interface { + // api.Callbacks is the API callback interface api.Callbacks + // GettingConfig is called when the config is obtained GettingConfig() error + // InvalidProfile is called when an invalid profile is found InvalidProfile(context.Context, *Server) (string, error) } +// Servers is the main struct that contains information for configuring the servers type Servers struct { - clientID string - cb Callbacks + clientID string + cb Callbacks + // WGSupport defines whether or not wireguard support is enabled WGSupport bool config *v2.V2 } +// Remove removes a server with id `identifier` and type `t` func (s *Servers) Remove(identifier string, t srvtypes.Type) error { return s.config.RemoveServer(identifier, t) } +// NewServers creates a new servers struct func NewServers(name string, cb Callbacks, wgSupport bool, cfg *v2.V2) Servers { return Servers{ clientID: name, @@ -38,25 +46,31 @@ func NewServers(name string, cb Callbacks, wgSupport bool, cfg *v2.V2) Servers { } } +// CurrentServer contains the information for the current active server type CurrentServer struct { + // it embeds the state file server *v2.Server - T v2.ServerType + // Key is the server key + Key v2.ServerKey + // srvs refers to the original servers manager srvs *Servers } +// ServerWithCallbacks gets the current server as a server struct and triggers callbacks as needed func (cs *CurrentServer) ServerWithCallbacks(ctx context.Context, disco *discovery.Discovery, tokens *eduoauth.Token, disableAuth bool) (*Server, error) { - switch cs.T.T { + switch cs.Key.T { case srvtypes.TypeInstituteAccess: - return cs.srvs.GetInstitute(ctx, cs.T.ID, disco, tokens, disableAuth) + return cs.srvs.GetInstitute(ctx, cs.Key.ID, disco, tokens, disableAuth) case srvtypes.TypeSecureInternet: - return cs.srvs.GetSecure(ctx, cs.T.ID, disco, tokens, disableAuth) + return cs.srvs.GetSecure(ctx, cs.Key.ID, disco, tokens, disableAuth) case srvtypes.TypeCustom: - return cs.srvs.GetCustom(ctx, cs.T.ID, tokens, disableAuth) + return cs.srvs.GetCustom(ctx, cs.Key.ID, tokens, disableAuth) default: - return nil, fmt.Errorf("no such server type: %d", cs.T.T) + return nil, fmt.Errorf("no such server type: %d", cs.Key.T) } } +// GetServer gets a server from the state file func (s *Servers) GetServer(id string, t srvtypes.Type) (*v2.Server, error) { if s.config == nil { return nil, errors.New("no configuration available") @@ -64,6 +78,7 @@ func (s *Servers) GetServer(id string, t srvtypes.Type) (*v2.Server, error) { return s.config.GetServer(id, t) } +// CurrentServer gets the current server from the state file and wraps it into a neat type func (s *Servers) CurrentServer() (*CurrentServer, error) { curr, k, err := s.config.CurrentServer() if err != nil { @@ -71,15 +86,18 @@ func (s *Servers) CurrentServer() (*CurrentServer, error) { } return &CurrentServer{ Server: curr, - T: *k, + Key: *k, srvs: s, }, nil } +// PublicCurrent gets the current server into a type that we can return to the client func (s *Servers) PublicCurrent(disco *discovery.Discovery) (*srvtypes.Current, error) { return s.config.PublicCurrent(disco) } +// ConnectWithCallbacks handles the /connect flow +// It calls callbacks as needed func (s *Servers) ConnectWithCallbacks(ctx context.Context, srv *Server, pTCP bool) (*srvtypes.Configuration, error) { err := srv.SetCurrent() if err != nil { diff --git a/internal/server/time.go b/internal/server/time.go index 4e54ef6..7369f47 100644 --- a/internal/server/time.go +++ b/internal/server/time.go @@ -28,6 +28,7 @@ func RenewButtonTime(st time.Time, et time.Time) int64 { return t.Unix() } +// CountdownTime returns the time when the countdown timer should be shown func CountdownTime(st time.Time, et time.Time) int64 { d := et.Sub(st) @@ -44,6 +45,8 @@ func CountdownTime(st time.Time, et time.Time) int64 { return t.Unix() } +// NotificationTimes returns the list when the app should +// show a notification when the VPN is (about to) expire(d) func NotificationTimes(st time.Time, et time.Time) []int64 { last := []time.Duration{ time.Duration(0), |
