diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-28 14:09:28 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-28 14:12:48 +0100 |
| commit | 59e6ccd051452162fab852a25deb4f0f8a9e22b2 (patch) | |
| tree | f4d8168b5b696f0eae26dbfc0e6cae514cbe65e7 /internal/server | |
| parent | 279c0de75629de5868c3ac1b3272a2850e6b62f7 (diff) | |
Refactor: Fix revive linter errors by deleting redundant prefixes
Diffstat (limited to 'internal/server')
| -rw-r--r-- | internal/server/api.go | 22 | ||||
| -rw-r--r-- | internal/server/common.go | 88 | ||||
| -rw-r--r-- | internal/server/instituteaccess.go | 4 | ||||
| -rw-r--r-- | internal/server/secureinternet.go | 26 |
4 files changed, 70 insertions, 70 deletions
diff --git a/internal/server/api.go b/internal/server/api.go index 65aadca..21ba6f4 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -13,7 +13,7 @@ import ( "github.com/eduvpn/eduvpn-common/types" ) -func APIGetEndpoints(baseURL string) (*ServerEndpoints, error) { +func APIGetEndpoints(baseURL string) (*Endpoints, error) { errorMessage := "failed getting server endpoints" url, urlErr := url.Parse(baseURL) if urlErr != nil { @@ -23,13 +23,13 @@ func APIGetEndpoints(baseURL string) (*ServerEndpoints, error) { wellKnownPath := "/.well-known/vpn-user-portal" url.Path = path.Join(url.Path, wellKnownPath) - _, body, bodyErr := httpw.HTTPGet(url.String()) + _, body, bodyErr := httpw.Get(url.String()) if bodyErr != nil { return nil, types.NewWrappedError(errorMessage, bodyErr) } - endpoints := &ServerEndpoints{} + endpoints := &Endpoints{} jsonErr := json.Unmarshal(body, endpoints) if jsonErr != nil { @@ -43,12 +43,12 @@ func apiAuthorized( server Server, method string, endpoint string, - opts *httpw.HTTPOptionalParams, + opts *httpw.OptionalParams, ) (http.Header, []byte, error) { errorMessage := "failed API authorized" // Ensure optional is not nil as we will fill it with headers if opts == nil { - opts = &httpw.HTTPOptionalParams{} + opts = &httpw.OptionalParams{} } base, baseErr := server.Base() @@ -76,20 +76,20 @@ func apiAuthorized( } else { opts.Headers = http.Header{headerKey: {headerValue}} } - return httpw.HTTPMethodWithOpts(method, url.String(), opts) + return httpw.MethodWithOpts(method, url.String(), opts) } func apiAuthorizedRetry( server Server, method string, endpoint string, - opts *httpw.HTTPOptionalParams, + opts *httpw.OptionalParams, ) (http.Header, []byte, error) { errorMessage := "failed authorized API retry" header, body, bodyErr := apiAuthorized(server, method, endpoint, opts) if bodyErr != nil { - var error *httpw.HTTPStatusError + var error *httpw.StatusError // Only retry authorized if we get a HTTP 401 if errors.As(bodyErr, &error) && error.Status == 401 { @@ -112,7 +112,7 @@ func APIInfo(server Server) error { if bodyErr != nil { return types.NewWrappedError(errorMessage, bodyErr) } - structure := ServerProfileInfo{} + structure := ProfileInfo{} jsonErr := json.Unmarshal(body, &structure) if jsonErr != nil { @@ -168,7 +168,7 @@ func APIConnectWireguard( server, http.MethodPost, "/connect", - &httpw.HTTPOptionalParams{Headers: headers, Body: urlForm}, + &httpw.OptionalParams{Headers: headers, Body: urlForm}, ) if connectErr != nil { return "", "", time.Time{}, types.NewWrappedError( @@ -209,7 +209,7 @@ func APIConnectOpenVPN(server Server, profileID string, preferTCP bool) (string, server, http.MethodPost, "/connect", - &httpw.HTTPOptionalParams{Headers: headers, Body: urlForm}, + &httpw.OptionalParams{Headers: headers, Body: urlForm}, ) if connectErr != nil { return "", time.Time{}, types.NewWrappedError(errorMessage, connectErr) diff --git a/internal/server/common.go b/internal/server/common.go index 7f6599a..e8c8e51 100644 --- a/internal/server/common.go +++ b/internal/server/common.go @@ -11,21 +11,21 @@ import ( ) // The base type for servers. -type ServerBase struct { +type Base struct { URL string `json:"base_url"` DisplayName map[string]string `json:"display_name"` SupportContact []string `json:"support_contact"` - Endpoints ServerEndpoints `json:"endpoints"` - Profiles ServerProfileInfo `json:"profiles"` + Endpoints Endpoints `json:"endpoints"` + Profiles ProfileInfo `json:"profiles"` StartTime time.Time `json:"start_time"` EndTime time.Time `json:"expire_time"` Type string `json:"server_type"` } -type ServerType int8 +type Type int8 const ( - CustomServerType ServerType = iota + CustomServerType Type = iota InstituteAccessServerType SecureInternetServerType ) @@ -35,7 +35,7 @@ type Servers struct { CustomServers InstituteAccessServers `json:"custom_servers"` InstituteServers InstituteAccessServers `json:"institute_servers"` SecureInternetHomeServer SecureInternetHomeServer `json:"secure_internet_home"` - IsType ServerType `json:"is_secure_internet"` + IsType Type `json:"is_secure_internet"` } type Server interface { @@ -45,48 +45,48 @@ type Server interface { TemplateAuth() func(string) string // Gets the server base - Base() (*ServerBase, error) + Base() (*Base, error) } -type ServerProfile struct { +type Profile struct { ID string `json:"profile_id"` DisplayName string `json:"display_name"` VPNProtoList []string `json:"vpn_proto_list"` DefaultGateway bool `json:"default_gateway"` } -type ServerProfileListInfo struct { - ProfileList []ServerProfile `json:"profile_list"` +type ProfileListInfo struct { + ProfileList []Profile `json:"profile_list"` } -type ServerProfileInfo struct { +type ProfileInfo struct { Current string `json:"current_profile"` - Info ServerProfileListInfo `json:"info"` + Info ProfileListInfo `json:"info"` } -func (info ServerProfileInfo) GetCurrentProfileIndex() int { +func (info ProfileInfo) GetCurrentProfileIndex() int { index := 0 for _, profile := range info.Info.ProfileList { if profile.ID == info.Current { return index } - index += 1 + index++ } // Default is 'first' profile return 0 } -type ServerEndpointList struct { +type EndpointList struct { API string `json:"api_endpoint"` Authorization string `json:"authorization_endpoint"` Token string `json:"token_endpoint"` } // Struct that defines the json format for /.well-known/vpn-user-portal". -type ServerEndpoints struct { +type Endpoints struct { API struct { - V2 ServerEndpointList `json:"http://eduvpn.org/api#2"` - V3 ServerEndpointList `json:"http://eduvpn.org/api#3"` + V2 EndpointList `json:"http://eduvpn.org/api#2"` + V3 EndpointList `json:"http://eduvpn.org/api#3"` } `json:"api"` V string `json:"v"` } @@ -97,7 +97,7 @@ func (servers *Servers) GetCurrentServer() (Server, error) { if !servers.HasSecureLocation() { return nil, types.NewWrappedError( errorMessage, - &ServerGetCurrentNotFoundError{}, + &CurrentNotFoundError{}, ) } return &servers.SecureInternetHomeServer, nil @@ -113,7 +113,7 @@ func (servers *Servers) GetCurrentServer() (Server, error) { if bases == nil { return nil, types.NewWrappedError( errorMessage, - &ServerGetCurrentNoMapError{}, + &CurrentNoMapError{}, ) } server, exists := bases[currentServerURL] @@ -121,7 +121,7 @@ func (servers *Servers) GetCurrentServer() (Server, error) { if !exists || server == nil { return nil, types.NewWrappedError( errorMessage, - &ServerGetCurrentNotFoundError{}, + &CurrentNotFoundError{}, ) } return server, nil @@ -283,7 +283,7 @@ func CancelOAuth(server Server) { server.OAuth().Cancel() } -func (profile *ServerProfile) supportsProtocol(protocol string) bool { +func (profile *Profile) supportsProtocol(protocol string) bool { for _, proto := range profile.VPNProtoList { if proto == protocol { return true @@ -292,15 +292,15 @@ func (profile *ServerProfile) supportsProtocol(protocol string) bool { return false } -func (profile *ServerProfile) supportsWireguard() bool { +func (profile *Profile) supportsWireguard() bool { return profile.supportsProtocol("wireguard") } -func (profile *ServerProfile) supportsOpenVPN() bool { +func (profile *Profile) supportsOpenVPN() bool { return profile.supportsProtocol("openvpn") } -func Profile(server Server) (*ServerProfile, error) { +func CurrentProfile(server Server) (*Profile, error) { errorMessage := "failed getting current profile" base, baseErr := server.Base() @@ -316,11 +316,11 @@ func Profile(server Server) (*ServerProfile, error) { return nil, types.NewWrappedError( errorMessage, - &ServerGetCurrentProfileNotFoundError{ProfileID: profileID}, + &CurrentProfileNotFoundError{ProfileID: profileID}, ) } -func (base *ServerBase) InitializeEndpoints() error { +func (base *Base) InitializeEndpoints() error { errorMessage := "failed initializing endpoints" endpoints, endpointsErr := APIGetEndpoints(base.URL) if endpointsErr != nil { @@ -330,8 +330,8 @@ func (base *ServerBase) InitializeEndpoints() error { return nil } -func (base *ServerBase) ValidProfiles(clientSupportsWireguard bool) ServerProfileInfo { - var validProfiles []ServerProfile +func (base *Base) ValidProfiles(clientSupportsWireguard bool) ProfileInfo { + var validProfiles []Profile for _, profile := range base.Profiles.Info.ProfileList { // Not a valid profile because it does not support openvpn // Also the client does not support wireguard @@ -340,10 +340,10 @@ func (base *ServerBase) ValidProfiles(clientSupportsWireguard bool) ServerProfil } validProfiles = append(validProfiles, profile) } - return ServerProfileInfo{Current: base.Profiles.Current, Info: ServerProfileListInfo{ProfileList: validProfiles}} + return ProfileInfo{Current: base.Profiles.Current, Info: ProfileListInfo{ProfileList: validProfiles}} } -func ValidProfiles(server Server, clientSupportsWireguard bool) (*ServerProfileInfo, error) { +func ValidProfiles(server Server, clientSupportsWireguard bool) (*ProfileInfo, error) { errorMessage := "failed to get valid profiles" // No error wrapping here otherwise we wrap it too much base, baseErr := server.Base() @@ -438,7 +438,7 @@ func HasValidProfile(server Server, clientSupportsWireguard bool) (bool, error) // If there was a profile chosen and it doesn't exist anymore, reset it if base.Profiles.Current != "" { - _, existsProfileErr := Profile(server) + _, existsProfileErr := CurrentProfile(server) if existsProfileErr != nil { base.Profiles.Current = "" } @@ -450,7 +450,7 @@ func HasValidProfile(server Server, clientSupportsWireguard bool) (bool, error) if base.Profiles.Current == "" { base.Profiles.Current = base.Profiles.Info.ProfileList[0].ID } - profile, profileErr := Profile(server) + profile, profileErr := CurrentProfile(server) // shouldn't happen if profileErr != nil { return false, types.NewWrappedError(errorMessage, profileErr) @@ -486,7 +486,7 @@ func RefreshEndpoints(server Server) error { func Config(server Server, clientSupportsWireguard bool, preferTCP bool) (string, string, error) { errorMessage := "failed getting an OpenVPN/WireGuard configuration" - profile, profileErr := Profile(server) + profile, profileErr := CurrentProfile(server) if profileErr != nil { return "", "", types.NewWrappedError(errorMessage, profileErr) } @@ -522,34 +522,34 @@ func Disconnect(server Server) { APIDisconnect(server) } -type ServerGetCurrentProfileNotFoundError struct { +type CurrentProfileNotFoundError struct { ProfileID string } -func (e *ServerGetCurrentProfileNotFoundError) Error() string { +func (e *CurrentProfileNotFoundError) Error() string { return fmt.Sprintf("failed to get current profile, profile with ID: %s not found", e.ProfileID) } -type ServerGetConfigForceTCPError struct{} +type ConfigPreferTCPError struct{} -func (e *ServerGetConfigForceTCPError) Error() string { +func (e *ConfigPreferTCPError) Error() string { return "failed to get config, prefer TCP is on but the server does not support OpenVPN" } -type ServerEnsureServerEmptyURLError struct{} +type EmptyURLError struct{} -func (e *ServerEnsureServerEmptyURLError) Error() string { +func (e *EmptyURLError) Error() string { return "failed ensuring server, empty url provided" } -type ServerGetCurrentNoMapError struct{} +type CurrentNoMapError struct{} -func (e *ServerGetCurrentNoMapError) Error() string { +func (e *CurrentNoMapError) Error() string { return "failed getting current server, no servers available" } -type ServerGetCurrentNotFoundError struct{} +type CurrentNotFoundError struct{} -func (e *ServerGetCurrentNotFoundError) Error() string { +func (e *CurrentNotFoundError) Error() string { return "failed getting current server, not found" } diff --git a/internal/server/instituteaccess.go b/internal/server/instituteaccess.go index c0594a7..f76323c 100644 --- a/internal/server/instituteaccess.go +++ b/internal/server/instituteaccess.go @@ -14,7 +14,7 @@ type InstituteAccessServer struct { Auth oauth.OAuth `json:"oauth"` // Embed the server base - Basic ServerBase `json:"base"` + Basic Base `json:"base"` } type InstituteAccessServers struct { @@ -69,7 +69,7 @@ func (institute *InstituteAccessServer) TemplateAuth() func(string) string { } } -func (institute *InstituteAccessServer) Base() (*ServerBase, error) { +func (institute *InstituteAccessServer) Base() (*Base, error) { return &institute.Basic, nil } diff --git a/internal/server/secureinternet.go b/internal/server/secureinternet.go index c6a353b..fa4c9c9 100644 --- a/internal/server/secureinternet.go +++ b/internal/server/secureinternet.go @@ -16,7 +16,7 @@ type SecureInternetHomeServer struct { DisplayName map[string]string `json:"display_name"` // The home server has a list of info for each configured server location - BaseMap map[string]*ServerBase `json:"base_map"` + BaseMap map[string]*Base `json:"base_map"` // We have the authorization URL template, the home organization ID and the current location AuthorizationTemplate string `json:"authorization_template"` @@ -64,12 +64,12 @@ func (server *SecureInternetHomeServer) TemplateAuth() func(string) string { } } -func (server *SecureInternetHomeServer) Base() (*ServerBase, error) { +func (server *SecureInternetHomeServer) Base() (*Base, error) { errorMessage := "failed getting current secure internet home base" if server.BaseMap == nil { return nil, types.NewWrappedError( errorMessage, - &ServerSecureInternetMapNotFoundError{}, + &SecureInternetMapNotFoundError{}, ) } @@ -78,7 +78,7 @@ func (server *SecureInternetHomeServer) Base() (*ServerBase, error) { if !exists { return nil, types.NewWrappedError( errorMessage, - &ServerSecureInternetBaseNotFoundError{Current: server.CurrentLocation}, + &SecureInternetBaseNotFoundError{Current: server.CurrentLocation}, ) } return base, nil @@ -94,11 +94,11 @@ func (servers *Servers) HasSecureLocation() bool { func (server *SecureInternetHomeServer) addLocation( locationServer *types.DiscoveryServer, -) (*ServerBase, error) { +) (*Base, error) { errorMessage := "failed adding a location" // Initialize the base map if it is non-nil if server.BaseMap == nil { - server.BaseMap = make(map[string]*ServerBase) + server.BaseMap = make(map[string]*Base) } // Add the location to the base map @@ -106,7 +106,7 @@ func (server *SecureInternetHomeServer) addLocation( if !exists || base == nil { // Create the base to be added to the map - base = &ServerBase{} + base = &Base{} base.URL = locationServer.BaseURL base.DisplayName = server.DisplayName base.SupportContact = locationServer.SupportContact @@ -152,22 +152,22 @@ func (server *SecureInternetHomeServer) init( return nil } -type ServerGetSecureInternetHomeError struct{} +type SecureInternetHomeNotFoundError struct{} -func (e *ServerGetSecureInternetHomeError) Error() string { +func (e *SecureInternetHomeNotFoundError) Error() string { return "failed to get secure internet home server, not found" } -type ServerSecureInternetMapNotFoundError struct{} +type SecureInternetMapNotFoundError struct{} -func (e *ServerSecureInternetMapNotFoundError) Error() string { +func (e *SecureInternetMapNotFoundError) Error() string { return "secure internet map not found" } -type ServerSecureInternetBaseNotFoundError struct { +type SecureInternetBaseNotFoundError struct { Current string } -func (e *ServerSecureInternetBaseNotFoundError) Error() string { +func (e *SecureInternetBaseNotFoundError) Error() string { return fmt.Sprintf("secure internet base not found with current location: %s", e.Current) } |
