From 40053552852cf8c27fe7658b64df001d65b0a65e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Wed, 23 Mar 2022 18:15:13 +0100 Subject: Add getting profiles and add openvpn config support --- src/server.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/server.go') diff --git a/src/server.go b/src/server.go index 8f8706e..1dc0f88 100644 --- a/src/server.go +++ b/src/server.go @@ -1,6 +1,7 @@ package eduvpn import ( + "errors" "encoding/json" ) @@ -8,6 +9,21 @@ type Server struct { BaseURL string `json:"base_url"` Endpoints *ServerEndpoints `json:"endpoints"` OAuth *OAuth `json:"oauth"` + Profiles *ServerProfileInfo `json:"profiles"` +} + +type ServerProfile struct { + ID string `json:"profile_id"` + DisplayName string `json:"display_name"` + VPNProtoList []string `json:"vpn_proto_list"` + DefaultGateway bool `json:"default_gateway"` +} + +type ServerProfileInfo struct { + Current uint8 `json:"current_profile"` + Info struct { + ProfileList []ServerProfile `json:"profile_list"` + } `json:"info"` } type ServerEndpointList struct { @@ -58,3 +74,50 @@ func (server *Server) GetEndpoints() error { return nil } + +func (profiles *ServerProfileInfo) getCurrentProfile() (*ServerProfile, error) { + if profiles.Info.ProfileList == nil { + return nil, errors.New("No server profiles") + } + + if (int)(profiles.Current) >= len(profiles.Info.ProfileList) { + return nil, errors.New("Invalid profile") + } + return &profiles.Info.ProfileList[profiles.Current], nil +} + +func (profile *ServerProfile) supportsWireguard() bool { + for _, proto := range profile.VPNProtoList { + if proto == "wireguard" { + return true + } + } + return false +} + +func (server *Server) GetCurrentProfile() (*ServerProfile, error) { + if server.Profiles == nil { + return nil, errors.New("No server profiles found") + } + + return server.Profiles.getCurrentProfile() +} + +func (server *Server) GetConfig() (string, error) { + infoErr := server.APIInfo() + + if infoErr != nil { + return "", infoErr + } + + profile, profileErr := server.GetCurrentProfile() + + if profileErr != nil { + return "", profileErr + } + + if profile.supportsWireguard() { + return server.WireguardGetConfig() + } + return server.OpenVPNGetConfig() +} -- cgit v1.2.3