summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-03-20 14:14:17 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-09-25 09:43:37 +0200
commit19882f158fec139622ffe5b52bc9e834a9d3246e (patch)
tree322aa99e52bc5d2f9ac97d9e69cc4d3747e1d2e0
parent3618f2337bf0099d1fe8e4782cda3677ea4175be (diff)
Types: Split server into subpackage
-rw-r--r--client/client.go62
-rw-r--r--client/server.go34
-rw-r--r--exports/exports.go6
-rw-r--r--types/server/server.go (renamed from types/types.go)54
4 files changed, 75 insertions, 81 deletions
diff --git a/client/client.go b/client/client.go
index 78b451d..295fa04 100644
--- a/client/client.go
+++ b/client/client.go
@@ -14,7 +14,7 @@ import (
"github.com/eduvpn/eduvpn-common/internal/log"
"github.com/eduvpn/eduvpn-common/internal/oauth"
"github.com/eduvpn/eduvpn-common/internal/server"
- "github.com/eduvpn/eduvpn-common/types"
+ srvtypes "github.com/eduvpn/eduvpn-common/types/server"
discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
"github.com/eduvpn/eduvpn-common/types/protocol"
"github.com/go-errors/errors"
@@ -283,7 +283,7 @@ func (c *Client) DiscoServers() (dss *discotypes.Servers, err error) {
// - The time starting at which the countdown button should be shown, less than 24 hours
// - The list of times where notifications should be shown
// These times are reset when the VPN gets disconnected
-func (c *Client) ExpiryTimes() (*types.Expiry, error) {
+func (c *Client) ExpiryTimes() (*srvtypes.Expiry, error) {
// Get current expiry time
srv, err := c.Servers.GetCurrentServer()
if err != nil {
@@ -299,7 +299,7 @@ func (c *Client) ExpiryTimes() (*types.Expiry, error) {
bT := b.RenewButtonTime()
cT := b.CountdownTime()
nT := b.NotificationTimes()
- return &types.Expiry{
+ return &srvtypes.Expiry{
StartTime: b.StartTime.Unix(),
EndTime: b.EndTime.Unix(),
ButtonTime: bT,
@@ -308,30 +308,30 @@ func (c *Client) ExpiryTimes() (*types.Expiry, error) {
}, nil
}
-func convertProfiles(profiles server.ProfileInfo) types.Profiles {
- m := make(map[string]types.Profile)
+func convertProfiles(profiles server.ProfileInfo) srvtypes.Profiles {
+ m := make(map[string]srvtypes.Profile)
for _, p := range profiles.Info.ProfileList {
var protocols []protocol.Protocol
// loop through all protocol strings
for _, ps := range p.VPNProtoList {
protocols = append(protocols, protocol.New(ps))
}
- m[p.ID] = types.Profile{
+ m[p.ID] = srvtypes.Profile{
DisplayName: map[string]string{
"en": p.DisplayName,
},
Protocols: protocols,
}
}
- return types.Profiles{Map: m, Current: profiles.Current}
+ return srvtypes.Profiles{Map: m, Current: profiles.Current}
}
-func convertGeneric(server server.InstituteAccessServer) (*types.GenericServer, error) {
+func convertGeneric(server server.InstituteAccessServer) (*srvtypes.Server, error) {
b, err := server.Base()
if err != nil {
return nil, err
}
- return &types.GenericServer{
+ return &srvtypes.Server{
DisplayName: b.DisplayName,
Identifier: b.URL,
Profiles: convertProfiles(b.Profiles),
@@ -339,9 +339,9 @@ func convertGeneric(server server.InstituteAccessServer) (*types.GenericServer,
}
// TODO: CLEAN THIS UP
-func (c *Client) ServerList() (*types.ServerList, error) {
+func (c *Client) ServerList() (*srvtypes.List, error) {
custom := c.Servers.CustomServers
- var customServers []types.GenericServer
+ var customServers []srvtypes.Server
for _, v := range custom.Map {
if v == nil {
return nil, errors.New("found nil value in custom server map")
@@ -353,7 +353,7 @@ func (c *Client) ServerList() (*types.ServerList, error) {
customServers = append(customServers, *conv)
}
institute := c.Servers.InstituteServers
- var instituteServers []types.InstituteServer
+ var instituteServers []srvtypes.Institute
for _, v := range institute.Map {
if v == nil {
return nil, errors.New("found nil value in institute server map")
@@ -362,25 +362,25 @@ func (c *Client) ServerList() (*types.ServerList, error) {
if err != nil {
return nil, errors.Errorf("failed to convert institute server for public type: %v", err)
}
- instituteServers = append(instituteServers, types.InstituteServer{
- GenericServer: *conv,
+ instituteServers = append(instituteServers, srvtypes.Institute{
+ Server: *conv,
// TODO: delisted
Delisted: false,
})
}
- var secureInternet *types.SecureInternetServer
+ var secureInternet *srvtypes.SecureInternet
if c.Servers.HasSecureInternet() {
b, err := c.Servers.SecureInternetHomeServer.Base()
if err == nil {
- generic := types.GenericServer{
+ generic := srvtypes.Server{
DisplayName: b.DisplayName,
Identifier: b.URL,
Profiles: convertProfiles(b.Profiles),
}
cc := c.Servers.SecureInternetHomeServer.CurrentLocation
- secureInternet = &types.SecureInternetServer{
- GenericServer: generic,
+ secureInternet = &srvtypes.SecureInternet{
+ Server: generic,
CountryCode: cc,
// TODO: delisted
Delisted: false,
@@ -388,7 +388,7 @@ func (c *Client) ServerList() (*types.ServerList, error) {
}
}
- return &types.ServerList{
+ return &srvtypes.List{
Institutes: instituteServers,
SecureInternet: secureInternet,
Custom: customServers,
@@ -396,7 +396,7 @@ func (c *Client) ServerList() (*types.ServerList, error) {
}
// TODO: CLEAN THIS UP
-func (c *Client) CurrentServer() (*types.CurrentServer, error) {
+func (c *Client) CurrentServer() (*srvtypes.Current, error) {
srvs := c.Servers
switch srvs.IsType {
@@ -409,13 +409,13 @@ func (c *Client) CurrentServer() (*types.CurrentServer, error) {
if err != nil {
return nil, err
}
- return &types.CurrentServer{
- Institute: &types.InstituteServer{
- GenericServer: *conv,
+ return &srvtypes.Current{
+ Institute: &srvtypes.Institute{
+ Server: *conv,
// TODO: delisted
Delisted: false,
},
- Type: types.SERVER_INSTITUTE_ACCESS,
+ Type: srvtypes.TypeInstituteAccess,
}, nil
case server.CustomServerType:
curr, err := srvs.GetCustomServer(srvs.CustomServers.CurrentURL)
@@ -426,29 +426,29 @@ func (c *Client) CurrentServer() (*types.CurrentServer, error) {
if err != nil {
return nil, err
}
- return &types.CurrentServer{
+ return &srvtypes.Current{
Custom: conv,
- Type: types.SERVER_CUSTOM,
+ Type: srvtypes.TypeCustom,
}, nil
case server.SecureInternetServerType:
b, err := c.Servers.SecureInternetHomeServer.Base()
if err != nil {
return nil, err
}
- generic := types.GenericServer{
+ generic := srvtypes.Server{
DisplayName: b.DisplayName,
Identifier: c.Servers.SecureInternetHomeServer.HomeOrganizationID,
Profiles: convertProfiles(b.Profiles),
}
cc := c.Servers.SecureInternetHomeServer.CurrentLocation
- return &types.CurrentServer{
- SecureInternet: &types.SecureInternetServer{
- GenericServer: generic,
+ return &srvtypes.Current{
+ SecureInternet: &srvtypes.SecureInternet{
+ Server: generic,
CountryCode: cc,
// TODO: delisted
Delisted: false,
},
- Type: types.SERVER_SECURE_INTERNET,
+ Type: srvtypes.TypeSecureInternet,
}, nil
default:
return nil, errors.New("current server not found")
diff --git a/client/server.go b/client/server.go
index 74caff1..684d780 100644
--- a/client/server.go
+++ b/client/server.go
@@ -8,15 +8,15 @@ import (
"github.com/eduvpn/eduvpn-common/internal/log"
"github.com/eduvpn/eduvpn-common/internal/oauth"
"github.com/eduvpn/eduvpn-common/internal/server"
- "github.com/eduvpn/eduvpn-common/types"
discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
+ srvtypes "github.com/eduvpn/eduvpn-common/types/server"
"github.com/eduvpn/eduvpn-common/types/protocol"
"github.com/go-errors/errors"
)
// TODO: This should not be reliant on an internal type
-func getTokens(tok oauth.Token) types.Tokens {
- return types.Tokens{
+func getTokens(tok oauth.Token) srvtypes.Tokens {
+ return srvtypes.Tokens{
Access: tok.Access,
Refresh: tok.Refresh,
Expires: tok.ExpiredTimestamp.Unix(),
@@ -25,7 +25,7 @@ func getTokens(tok oauth.Token) types.Tokens {
// getConfigAuth gets a config with authorization and authentication.
// It also asks for a profile if no valid profile is found.
-func (c *Client) getConfigAuth(srv server.Server, preferTCP bool, t types.Tokens) (*types.Configuration, error) {
+func (c *Client) getConfigAuth(srv server.Server, preferTCP bool, t srvtypes.Tokens) (*srvtypes.Configuration, error) {
err := c.ensureLogin(srv, t)
if err != nil {
return nil, err
@@ -59,7 +59,7 @@ func (c *Client) getConfigAuth(srv server.Server, preferTCP bool, t types.Tokens
return nil, err
}
- pCfg := &types.Configuration{
+ pCfg := &srvtypes.Configuration{
VPNConfig: cfg.Config,
Protocol: protocol.New(cfg.Type),
DefaultGateway: p.DefaultGateway,
@@ -71,7 +71,7 @@ func (c *Client) getConfigAuth(srv server.Server, preferTCP bool, t types.Tokens
// retryConfigAuth retries the getConfigAuth function if the tokens are invalid.
// If OAuth is cancelled, it makes sure that we only forward the error as additional info.
-func (c *Client) retryConfigAuth(srv server.Server, preferTCP bool, t types.Tokens) (*types.Configuration, error) {
+func (c *Client) retryConfigAuth(srv server.Server, preferTCP bool, t srvtypes.Tokens) (*srvtypes.Configuration, error) {
cfg, err := c.getConfigAuth(srv, preferTCP, t)
if err == nil {
return cfg, nil
@@ -80,7 +80,7 @@ func (c *Client) retryConfigAuth(srv server.Server, preferTCP bool, t types.Toke
tErr := &oauth.TokensInvalidError{}
if errors.As(err, &tErr) {
// TODO: Is passing empty tokens correct here?
- cfg, err = c.getConfigAuth(srv, preferTCP, types.Tokens{})
+ cfg, err = c.getConfigAuth(srv, preferTCP, srvtypes.Tokens{})
if err == nil {
return cfg, nil
}
@@ -90,7 +90,7 @@ func (c *Client) retryConfigAuth(srv server.Server, preferTCP bool, t types.Toke
}
// getConfig gets an OpenVPN/WireGuard configuration by contacting the server, moving the FSM towards the DISCONNECTED state and then saving the local configuration file.
-func (c *Client) getConfig(srv server.Server, preferTCP bool, t types.Tokens) (*types.Configuration, error) {
+func (c *Client) getConfig(srv server.Server, preferTCP bool, t srvtypes.Tokens) (*srvtypes.Configuration, error) {
if c.InFSMState(StateDeregistered) {
return nil, errors.Errorf("getConfig attempt in '%v'", StateDeregistered)
}
@@ -121,7 +121,7 @@ func (c *Client) getConfig(srv server.Server, preferTCP bool, t types.Tokens) (*
}
// Cleanup cleans up the VPN connection by sending a /disconnect to the server
-func (c *Client) Cleanup(ct types.Tokens) error {
+func (c *Client) Cleanup(ct srvtypes.Tokens) error {
srv, err := c.Servers.GetCurrentServer()
if err != nil {
c.logError(err)
@@ -290,7 +290,7 @@ func (c *Client) AddInstituteServer(url string) (err error) {
c.FSM.GoTransition(StateChosenServer)
// Authorize it
- if err = c.ensureLogin(srv, types.Tokens{}); err != nil {
+ if err = c.ensureLogin(srv, srvtypes.Tokens{}); err != nil {
// Removing is best effort
_ = c.RemoveInstituteAccess(url)
return err
@@ -355,7 +355,7 @@ func (c *Client) AddSecureInternetHomeServer(orgID string) (err error) {
c.FSM.GoTransition(StateChosenServer)
// Authorize it
- if err = c.ensureLogin(srv, types.Tokens{}); err != nil {
+ if err = c.ensureLogin(srv, srvtypes.Tokens{}); err != nil {
// Removing is best effort
_ = c.RemoveSecureInternet()
return err
@@ -402,7 +402,7 @@ func (c *Client) AddCustomServer(url string) (err error) {
c.FSM.GoTransition(StateChosenServer)
// Authorize it
- if err = c.ensureLogin(srv, types.Tokens{}); err != nil {
+ if err = c.ensureLogin(srv, srvtypes.Tokens{}); err != nil {
// removing is best effort
_ = c.RemoveCustomServer(url)
return err
@@ -415,7 +415,7 @@ func (c *Client) AddCustomServer(url string) (err error) {
// GetConfigInstituteAccess gets a configuration for an Institute Access Server.
// It ensures that the Institute Access Server exists by creating or using an existing one with the url.
// `preferTCP` indicates that the client wants to use TCP (through OpenVPN) to establish the VPN tunnel.
-func (c *Client) GetConfigInstituteAccess(url string, preferTCP bool, t types.Tokens) (cfg *types.Configuration, err error) {
+func (c *Client) GetConfigInstituteAccess(url string, preferTCP bool, t srvtypes.Tokens) (cfg *srvtypes.Configuration, err error) {
defer func() {
if err != nil {
c.logError(err)
@@ -457,7 +457,7 @@ func (c *Client) GetConfigInstituteAccess(url string, preferTCP bool, t types.To
// GetConfigSecureInternet gets a configuration for a Secure Internet Server.
// It ensures that the Secure Internet Server exists by creating or using an existing one with the orgID.
// `preferTCP` indicates that the client wants to use TCP (through OpenVPN) to establish the VPN tunnel.
-func (c *Client) GetConfigSecureInternet(orgID string, preferTCP bool, t types.Tokens) (cfg *types.Configuration, err error) {
+func (c *Client) GetConfigSecureInternet(orgID string, preferTCP bool, t srvtypes.Tokens) (cfg *srvtypes.Configuration, err error) {
defer func() {
if err != nil {
c.logError(err)
@@ -500,7 +500,7 @@ func (c *Client) GetConfigSecureInternet(orgID string, preferTCP bool, t types.T
// GetConfigCustomServer gets a configuration for a Custom Server.
// It ensures that the Custom Server exists by creating or using an existing one with the url.
// `preferTCP` indicates that the client wants to use TCP (through OpenVPN) to establish the VPN tunnel.
-func (c *Client) GetConfigCustomServer(url string, preferTCP bool, t types.Tokens) (cfg *types.Configuration, err error) {
+func (c *Client) GetConfigCustomServer(url string, preferTCP bool, t srvtypes.Tokens) (cfg *srvtypes.Configuration, err error) {
defer func() {
if err != nil {
c.logError(err)
@@ -584,7 +584,7 @@ func (c *Client) RenewSession() (err error) {
}
server.MarkTokensForRenew(srv)
- return c.ensureLogin(srv, types.Tokens{})
+ return c.ensureLogin(srv, srvtypes.Tokens{})
}
// ShouldRenewButton returns true if the renew button should be shown
@@ -602,7 +602,7 @@ func (c *Client) ShouldRenewButton() bool {
// ensureLogin logs the user back in if needed.
// It runs the FSM transitions to ask for user input.
-func (c *Client) ensureLogin(srv server.Server, t types.Tokens) (err error) {
+func (c *Client) ensureLogin(srv server.Server, t srvtypes.Tokens) (err error) {
// Relogin with oauth
// This moves the state to authorized
if !server.NeedsRelogin(srv) {
diff --git a/exports/exports.go b/exports/exports.go
index d8fd6ea..5daa05f 100644
--- a/exports/exports.go
+++ b/exports/exports.go
@@ -27,7 +27,7 @@ import (
"github.com/go-errors/errors"
"github.com/eduvpn/eduvpn-common/client"
- "github.com/eduvpn/eduvpn-common/types"
+ srvtypes "github.com/eduvpn/eduvpn-common/types/server"
)
var (
@@ -35,7 +35,7 @@ var (
VPNState *client.Client
)
-func getTokens(tokens *C.char) (t types.Tokens, err error) {
+func getTokens(tokens *C.char) (t srvtypes.Tokens, err error) {
err = json.Unmarshal([]byte(C.GoString(tokens)), &t)
return t, err
}
@@ -266,7 +266,7 @@ func GetConfig(_type *C.char, id *C.char, pTCP C.int, tokens *C.char) (*C.char,
return nil, getCError(err)
}
t := C.GoString(_type)
- var cfg *types.Configuration
+ var cfg *srvtypes.Configuration
switch t {
case "institute_access":
cfg, err = state.GetConfigInstituteAccess(C.GoString(id), preferTCPBool, tok)
diff --git a/types/types.go b/types/server/server.go
index bb04fdd..db2dd8b 100644
--- a/types/types.go
+++ b/types/server/server.go
@@ -1,8 +1,14 @@
-// package types lists the various public types that are returned to clients
-package types
+package server
-import (
- "github.com/eduvpn/eduvpn-common/types/protocol"
+import "github.com/eduvpn/eduvpn-common/types/protocol"
+
+type Type int8
+
+const (
+ TypeUnknown Type = iota
+ TypeInstituteAccess
+ TypeSecureInternet
+ TypeCustom
)
type Expiry struct {
@@ -30,27 +36,27 @@ type Tokens struct {
Expires int64 `json:"expires_in"`
}
-type GenericServer struct {
+type Server struct {
DisplayName map[string]string `json:"display_name,omitempty"`
Identifier string `json:"identifier"`
Profiles Profiles `json:"profiles"`
}
-type InstituteServer struct {
- GenericServer
+type Institute struct {
+ Server
Delisted bool `json:"delisted"`
}
-type SecureInternetServer struct {
- GenericServer
+type SecureInternet struct {
+ Server
CountryCode string `json:"country_code"`
Delisted bool `json:"delisted"`
}
-type ServerList struct {
- Institutes []InstituteServer `json:"institute_access_servers,omitempty"`
- SecureInternet *SecureInternetServer `json:"secure_internet_server,omitempty"`
- Custom []GenericServer `json:"custom_servers,omitempty"`
+type List struct {
+ Institutes []Institute `json:"institute_access_servers,omitempty"`
+ SecureInternet *SecureInternet `json:"secure_internet_server,omitempty"`
+ Custom []Server `json:"custom_servers,omitempty"`
}
type Configuration struct {
@@ -60,21 +66,9 @@ type Configuration struct {
Tokens Tokens `json:"tokens"`
}
-type ServerType int8
-
-const (
- SERVER_UNKNOWN ServerType = iota
-
- SERVER_INSTITUTE_ACCESS
-
- SERVER_SECURE_INTERNET
-
- SERVER_CUSTOM
-)
-
-type CurrentServer struct {
- Institute *InstituteServer `json:"institute_access_server,omitempty"`
- SecureInternet *SecureInternetServer `json:"secure_internet_server,omitempty"`
- Custom *GenericServer `json:"custom_server,omitempty"`
- Type ServerType `json:"server_type"`
+type Current struct {
+ Institute *Institute `json:"institute_access_server,omitempty"`
+ SecureInternet *SecureInternet `json:"secure_internet_server,omitempty"`
+ Custom *Server `json:"custom_server,omitempty"`
+ Type Type `json:"server_type"`
}