summaryrefslogtreecommitdiff
path: root/internal/server/profile/profile.go
blob: 7a196858589e136006c6eebd5dfb76a5deb111b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package profile

import (
	"github.com/eduvpn/eduvpn-common/types/protocol"
	"github.com/eduvpn/eduvpn-common/types/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 ListInfo struct {
	ProfileList []Profile `json:"profile_list"`
}

type Info struct {
	Current string   `json:"current_profile"`
	Info    ListInfo `json:"info"`
}

func (info Info) CurrentProfileIndex() int {
	for i, profile := range info.Info.ProfileList {
		if profile.ID == info.Current {
			return i
		}
	}
	// 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")
}

func (info Info) Supported(wireguardSupport bool) []Profile {
	var valid []Profile
	for _, p := range info.Info.ProfileList {
		// Not a valid profile because it does not support openvpn
		// Also the client does not support wireguard
		if !p.SupportsOpenVPN() && !wireguardSupport {
			continue
		}
		valid = append(valid, p)
	}
	return valid
}

func (info Info) Has(id string) bool {
	for _, p := range info.Info.ProfileList {
		if p.ID == id {
			return true
		}
	}
	return false
}

func (info Info) Public() server.Profiles {
	m := make(map[string]server.Profile)
	for _, p := range info.Info.ProfileList {
		var protocols []protocol.Protocol
		for _, ps := range p.VPNProtoList {
			protocols = append(protocols, protocol.New(ps))
		}
		m[p.ID] = server.Profile{
			DisplayName: map[string]string{
				"en": p.DisplayName,
			},
			Protocols: protocols,
		}
	}
	return server.Profiles{Map: m, Current: info.Current}
}