summaryrefslogtreecommitdiff
path: root/src/server.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-04-19 15:02:45 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-19 15:02:45 +0200
commit723ecacc8528be0e96db42392f1781ddf5894bea (patch)
tree1debf1d6d0c50adb32939db3cc84e5130d1fb818 /src/server.go
parent5f40a8d10a17182f744cb7ac11087d170dd49560 (diff)
Profiles: Implement basic functionality for sending a profile_id
Diffstat (limited to 'src/server.go')
-rw-r--r--src/server.go55
1 files changed, 33 insertions, 22 deletions
diff --git a/src/server.go b/src/server.go
index 8a600d4..20d9136 100644
--- a/src/server.go
+++ b/src/server.go
@@ -10,6 +10,7 @@ type Server struct {
Endpoints *ServerEndpoints `json:"endpoints"`
OAuth *OAuth `json:"oauth"`
Profiles *ServerProfileInfo `json:"profiles"`
+ ProfilesRaw string `json:"profiles_raw"`
}
type ServerProfile struct {
@@ -20,7 +21,7 @@ type ServerProfile struct {
}
type ServerProfileInfo struct {
- Current uint8 `json:"current_profile"`
+ Current string `json:"current_profile"`
Info struct {
ProfileList []ServerProfile `json:"profile_list"`
} `json:"info"`
@@ -84,17 +85,6 @@ 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" {
@@ -104,12 +94,31 @@ func (profile *ServerProfile) supportsWireguard() bool {
return false
}
-func (server *Server) GetCurrentProfile() (*ServerProfile, error) {
- if server.Profiles == nil {
- return nil, errors.New("No server profiles found")
+func (server *Server) getProfileForID(profile_id string) (*ServerProfile, error) {
+ for _, profile := range server.Profiles.Info.ProfileList {
+ if profile.ID == profile_id {
+ return &profile, nil
+ }
}
+ return nil, errors.New("no profile found for id")
+}
+
+func (server *Server) getConfigWithProfile(profile_id string) (string, error) {
+ profile, profileErr := server.getProfileForID(profile_id)
- return server.Profiles.getCurrentProfile()
+ if profileErr != nil {
+ return "", profileErr
+ }
+
+ if profile.supportsWireguard() {
+ return server.WireguardGetConfig(profile_id)
+ }
+ return server.OpenVPNGetConfig(profile_id)
+}
+
+func (server *Server) askForProfileID() (string, error) {
+ _, profile_id := GetVPNState().GoTransition(ASK_PROFILE, server.ProfilesRaw)
+ return profile_id, nil
}
func (server *Server) GetConfig() (string, error) {
@@ -119,14 +128,16 @@ func (server *Server) GetConfig() (string, error) {
return "", infoErr
}
- profile, profileErr := server.GetCurrentProfile()
+ // Set the current profile if there is only one profile
+ if len(server.Profiles.Info.ProfileList) == 1 {
+ return server.getConfigWithProfile(server.Profiles.Info.ProfileList[0].ID)
+ }
+
+ profile_id, profileErr := server.askForProfileID()
if profileErr != nil {
- return "", profileErr
+ return "", nil
}
- if profile.supportsWireguard() {
- return server.WireguardGetConfig()
- }
- return server.OpenVPNGetConfig()
+ return server.getConfigWithProfile(profile_id)
}