summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-11-28 15:14:17 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-11-28 15:14:17 +0100
commit5143ed35f2af2fe0e725c2a466c86f1cf929333c (patch)
treeb6c19f466226fa1c25534404f9935464af7cb314
parent3934d75ac516a530ceb9f494f7ce0de12cb1b5de (diff)
Server: Split more into multiple implementation files
-rw-r--r--internal/server/base.go45
-rw-r--r--internal/server/profile.go46
-rw-r--r--internal/server/server.go (renamed from internal/server/common.go)214
-rw-r--r--internal/server/servers.go138
4 files changed, 229 insertions, 214 deletions
diff --git a/internal/server/base.go b/internal/server/base.go
new file mode 100644
index 0000000..bb88eb3
--- /dev/null
+++ b/internal/server/base.go
@@ -0,0 +1,45 @@
+package server
+
+import (
+ "time"
+
+ "github.com/eduvpn/eduvpn-common/types"
+)
+
+// The base type for servers.
+type Base struct {
+ URL string `json:"base_url"`
+ DisplayName map[string]string `json:"display_name"`
+ SupportContact []string `json:"support_contact"`
+ 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"`
+}
+
+func (base *Base) InitializeEndpoints() error {
+ errorMessage := "failed initializing endpoints"
+ endpoints, endpointsErr := APIGetEndpoints(base.URL)
+ if endpointsErr != nil {
+ return types.NewWrappedError(errorMessage, endpointsErr)
+ }
+ base.Endpoints = *endpoints
+ return nil
+}
+
+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
+ if !profile.supportsOpenVPN() && !clientSupportsWireguard {
+ continue
+ }
+ validProfiles = append(validProfiles, profile)
+ }
+ return ProfileInfo{
+ Current: base.Profiles.Current,
+ Info: ProfileListInfo{ProfileList: validProfiles},
+ }
+}
diff --git a/internal/server/profile.go b/internal/server/profile.go
new file mode 100644
index 0000000..42f0e52
--- /dev/null
+++ b/internal/server/profile.go
@@ -0,0 +1,46 @@
+package server
+
+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 ProfileListInfo struct {
+ ProfileList []Profile `json:"profile_list"`
+}
+
+type ProfileInfo struct {
+ Current string `json:"current_profile"`
+ Info ProfileListInfo `json:"info"`
+}
+
+func (info ProfileInfo) GetCurrentProfileIndex() int {
+ index := 0
+ for _, profile := range info.Info.ProfileList {
+ if profile.ID == info.Current {
+ return index
+ }
+ index++
+ }
+ // Default is 'first' profile
+ return 0
+}
+
+func (profile *Profile) supportsProtocol(protocol string) bool {
+ for _, proto := range profile.VPNProtoList {
+ if proto == protocol {
+ return true
+ }
+ }
+ return false
+}
+
+func (profile *Profile) supportsWireguard() bool {
+ return profile.supportsProtocol("wireguard")
+}
+
+func (profile *Profile) supportsOpenVPN() bool {
+ return profile.supportsProtocol("openvpn")
+}
diff --git a/internal/server/common.go b/internal/server/server.go
index f0274ed..95244d5 100644
--- a/internal/server/common.go
+++ b/internal/server/server.go
@@ -10,18 +10,6 @@ import (
"github.com/eduvpn/eduvpn-common/types"
)
-// The base type for servers.
-type Base struct {
- URL string `json:"base_url"`
- DisplayName map[string]string `json:"display_name"`
- SupportContact []string `json:"support_contact"`
- 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 Type int8
const (
@@ -30,14 +18,6 @@ const (
SecureInternetServerType
)
-type Servers struct {
- // A custom server is just an institute access server under the hood
- CustomServers InstituteAccessServers `json:"custom_servers"`
- InstituteServers InstituteAccessServers `json:"institute_servers"`
- SecureInternetHomeServer SecureInternetHomeServer `json:"secure_internet_home"`
- IsType Type `json:"is_secure_internet"`
-}
-
type Server interface {
OAuth() *oauth.OAuth
@@ -48,34 +28,6 @@ type Server interface {
Base() (*Base, error)
}
-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 ProfileListInfo struct {
- ProfileList []Profile `json:"profile_list"`
-}
-
-type ProfileInfo struct {
- Current string `json:"current_profile"`
- Info ProfileListInfo `json:"info"`
-}
-
-func (info ProfileInfo) GetCurrentProfileIndex() int {
- index := 0
- for _, profile := range info.Info.ProfileList {
- if profile.ID == info.Current {
- return index
- }
- index++
- }
- // Default is 'first' profile
- return 0
-}
-
type EndpointList struct {
API string `json:"api_endpoint"`
Authorization string `json:"authorization_endpoint"`
@@ -91,129 +43,6 @@ type Endpoints struct {
V string `json:"v"`
}
-func (servers *Servers) GetCurrentServer() (Server, error) {
- errorMessage := "failed getting current server"
- if servers.IsType == SecureInternetServerType {
- if !servers.HasSecureLocation() {
- return nil, types.NewWrappedError(
- errorMessage,
- &CurrentNotFoundError{},
- )
- }
- return &servers.SecureInternetHomeServer, nil
- }
-
- serversStruct := &servers.InstituteServers
-
- if servers.IsType == CustomServerType {
- serversStruct = &servers.CustomServers
- }
- currentServerURL := serversStruct.CurrentURL
- bases := serversStruct.Map
- if bases == nil {
- return nil, types.NewWrappedError(
- errorMessage,
- &CurrentNoMapError{},
- )
- }
- server, exists := bases[currentServerURL]
-
- if !exists || server == nil {
- return nil, types.NewWrappedError(
- errorMessage,
- &CurrentNotFoundError{},
- )
- }
- return server, nil
-}
-
-func (servers *Servers) addInstituteAndCustom(
- discoServer *types.DiscoveryServer,
- isCustom bool,
-) (Server, error) {
- url := discoServer.BaseURL
- errorMessage := fmt.Sprintf("failed adding institute access server: %s", url)
- toAddServers := &servers.InstituteServers
- serverType := InstituteAccessServerType
-
- if isCustom {
- toAddServers = &servers.CustomServers
- serverType = CustomServerType
- }
-
- if toAddServers.Map == nil {
- toAddServers.Map = make(map[string]*InstituteAccessServer)
- }
-
- server, exists := toAddServers.Map[url]
-
- // initialize the server if it doesn't exist yet
- if !exists {
- server = &InstituteAccessServer{}
- }
-
- instituteInitErr := server.init(
- url,
- discoServer.DisplayName,
- discoServer.Type,
- discoServer.SupportContact,
- )
- if instituteInitErr != nil {
- return nil, types.NewWrappedError(errorMessage, instituteInitErr)
- }
- toAddServers.Map[url] = server
- servers.IsType = serverType
- return server, nil
-}
-
-func (servers *Servers) AddInstituteAccessServer(
- instituteServer *types.DiscoveryServer,
-) (Server, error) {
- return servers.addInstituteAndCustom(instituteServer, false)
-}
-
-func (servers *Servers) AddCustomServer(
- customServer *types.DiscoveryServer,
-) (Server, error) {
- return servers.addInstituteAndCustom(customServer, true)
-}
-
-func (servers *Servers) GetSecureLocation() string {
- return servers.SecureInternetHomeServer.CurrentLocation
-}
-
-func (servers *Servers) SetSecureLocation(
- chosenLocationServer *types.DiscoveryServer,
-) error {
- errorMessage := "failed to set secure location"
- // Make sure to add the current location
- _, addLocationErr := servers.SecureInternetHomeServer.addLocation(chosenLocationServer)
-
- if addLocationErr != nil {
- return types.NewWrappedError(errorMessage, addLocationErr)
- }
-
- servers.SecureInternetHomeServer.CurrentLocation = chosenLocationServer.CountryCode
- return nil
-}
-
-func (servers *Servers) AddSecureInternet(
- secureOrg *types.DiscoveryOrganization,
- secureServer *types.DiscoveryServer,
-) (Server, error) {
- errorMessage := "failed adding secure internet server"
- // If we have specified an organization ID
- // We also need to get an authorization template
- initErr := servers.SecureInternetHomeServer.init(secureOrg, secureServer)
-
- if initErr != nil {
- return nil, types.NewWrappedError(errorMessage, initErr)
- }
-
- servers.IsType = SecureInternetServerType
- return &servers.SecureInternetHomeServer, nil
-}
-
func ShouldRenewButton(server Server) bool {
base, baseErr := server.Base()
@@ -283,23 +112,6 @@ func CancelOAuth(server Server) {
server.OAuth().Cancel()
}
-func (profile *Profile) supportsProtocol(protocol string) bool {
- for _, proto := range profile.VPNProtoList {
- if proto == protocol {
- return true
- }
- }
- return false
-}
-
-func (profile *Profile) supportsWireguard() bool {
- return profile.supportsProtocol("wireguard")
-}
-
-func (profile *Profile) supportsOpenVPN() bool {
- return profile.supportsProtocol("openvpn")
-}
-
func CurrentProfile(server Server) (*Profile, error) {
errorMessage := "failed getting current profile"
base, baseErr := server.Base()
@@ -320,32 +132,6 @@ func CurrentProfile(server Server) (*Profile, error) {
)
}
-func (base *Base) InitializeEndpoints() error {
- errorMessage := "failed initializing endpoints"
- endpoints, endpointsErr := APIGetEndpoints(base.URL)
- if endpointsErr != nil {
- return types.NewWrappedError(errorMessage, endpointsErr)
- }
- base.Endpoints = *endpoints
- return nil
-}
-
-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
- if !profile.supportsOpenVPN() && !clientSupportsWireguard {
- continue
- }
- validProfiles = append(validProfiles, profile)
- }
- return ProfileInfo{
- Current: base.Profiles.Current,
- Info: ProfileListInfo{ProfileList: validProfiles},
- }
-}
-
func ValidProfiles(server Server, clientSupportsWireguard bool) (*ProfileInfo, error) {
errorMessage := "failed to get valid profiles"
// No error wrapping here otherwise we wrap it too much
diff --git a/internal/server/servers.go b/internal/server/servers.go
new file mode 100644
index 0000000..a076770
--- /dev/null
+++ b/internal/server/servers.go
@@ -0,0 +1,138 @@
+package server
+
+import (
+ "fmt"
+
+ "github.com/eduvpn/eduvpn-common/types"
+)
+
+type Servers struct {
+ // A custom server is just an institute access server under the hood
+ CustomServers InstituteAccessServers `json:"custom_servers"`
+ InstituteServers InstituteAccessServers `json:"institute_servers"`
+ SecureInternetHomeServer SecureInternetHomeServer `json:"secure_internet_home"`
+ IsType Type `json:"is_secure_internet"`
+}
+
+func (servers *Servers) AddSecureInternet(
+ secureOrg *types.DiscoveryOrganization,
+ secureServer *types.DiscoveryServer,
+) (Server, error) {
+ errorMessage := "failed adding secure internet server"
+ // If we have specified an organization ID
+ // We also need to get an authorization template
+ initErr := servers.SecureInternetHomeServer.init(secureOrg, secureServer)
+
+ if initErr != nil {
+ return nil, types.NewWrappedError(errorMessage, initErr)
+ }
+
+ servers.IsType = SecureInternetServerType
+ return &servers.SecureInternetHomeServer, nil
+}
+
+func (servers *Servers) GetCurrentServer() (Server, error) {
+ errorMessage := "failed getting current server"
+ if servers.IsType == SecureInternetServerType {
+ if !servers.HasSecureLocation() {
+ return nil, types.NewWrappedError(
+ errorMessage,
+ &CurrentNotFoundError{},
+ )
+ }
+ return &servers.SecureInternetHomeServer, nil
+ }
+
+ serversStruct := &servers.InstituteServers
+
+ if servers.IsType == CustomServerType {
+ serversStruct = &servers.CustomServers
+ }
+ currentServerURL := serversStruct.CurrentURL
+ bases := serversStruct.Map
+ if bases == nil {
+ return nil, types.NewWrappedError(
+ errorMessage,
+ &CurrentNoMapError{},
+ )
+ }
+ server, exists := bases[currentServerURL]
+
+ if !exists || server == nil {
+ return nil, types.NewWrappedError(
+ errorMessage,
+ &CurrentNotFoundError{},
+ )
+ }
+ return server, nil
+}
+
+func (servers *Servers) addInstituteAndCustom(
+ discoServer *types.DiscoveryServer,
+ isCustom bool,
+) (Server, error) {
+ url := discoServer.BaseURL
+ errorMessage := fmt.Sprintf("failed adding institute access server: %s", url)
+ toAddServers := &servers.InstituteServers
+ serverType := InstituteAccessServerType
+
+ if isCustom {
+ toAddServers = &servers.CustomServers
+ serverType = CustomServerType
+ }
+
+ if toAddServers.Map == nil {
+ toAddServers.Map = make(map[string]*InstituteAccessServer)
+ }
+
+ server, exists := toAddServers.Map[url]
+
+ // initialize the server if it doesn't exist yet
+ if !exists {
+ server = &InstituteAccessServer{}
+ }
+
+ instituteInitErr := server.init(
+ url,
+ discoServer.DisplayName,
+ discoServer.Type,
+ discoServer.SupportContact,
+ )
+ if instituteInitErr != nil {
+ return nil, types.NewWrappedError(errorMessage, instituteInitErr)
+ }
+ toAddServers.Map[url] = server
+ servers.IsType = serverType
+ return server, nil
+}
+
+func (servers *Servers) AddInstituteAccessServer(
+ instituteServer *types.DiscoveryServer,
+) (Server, error) {
+ return servers.addInstituteAndCustom(instituteServer, false)
+}
+
+func (servers *Servers) AddCustomServer(
+ customServer *types.DiscoveryServer,
+) (Server, error) {
+ return servers.addInstituteAndCustom(customServer, true)
+}
+
+func (servers *Servers) GetSecureLocation() string {
+ return servers.SecureInternetHomeServer.CurrentLocation
+}
+
+func (servers *Servers) SetSecureLocation(
+ chosenLocationServer *types.DiscoveryServer,
+) error {
+ errorMessage := "failed to set secure location"
+ // Make sure to add the current location
+ _, addLocationErr := servers.SecureInternetHomeServer.addLocation(chosenLocationServer)
+
+ if addLocationErr != nil {
+ return types.NewWrappedError(errorMessage, addLocationErr)
+ }
+
+ servers.SecureInternetHomeServer.CurrentLocation = chosenLocationServer.CountryCode
+ return nil
+}