diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-04-12 22:52:49 +0200 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2023-09-25 09:43:37 +0200 |
| commit | a23c3e61c5d89ef67973891b5b3a176c06e1b174 (patch) | |
| tree | f1eed03b047f8affd3d5123fa5c9e868ac7d8bec /internal/server/profile | |
| parent | ee95eb45708e1fa766a63866d26d05d13f23e8c9 (diff) | |
Refactor: Split internal server into multiple packages
- Pass contexts
- Have separate packages for e.g. custom, institute and secure
- internet servers, profiles....
- Return types from the public ./types package with a Public() method
Diffstat (limited to 'internal/server/profile')
| -rw-r--r-- | internal/server/profile/profile.go | 88 | ||||
| -rw-r--r-- | internal/server/profile/profile_test.go | 100 |
2 files changed, 188 insertions, 0 deletions
diff --git a/internal/server/profile/profile.go b/internal/server/profile/profile.go new file mode 100644 index 0000000..7a19685 --- /dev/null +++ b/internal/server/profile/profile.go @@ -0,0 +1,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} +} diff --git a/internal/server/profile/profile_test.go b/internal/server/profile/profile_test.go new file mode 100644 index 0000000..e246b5c --- /dev/null +++ b/internal/server/profile/profile_test.go @@ -0,0 +1,100 @@ +package profile + +import "testing" + +func Test_CurrentProfileIndex(t *testing.T) { + testCases := []struct { + profiles []Profile + current string + index int + }{ + { + profiles: []Profile{ + { + ID: "a", + DisplayName: "b", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + }, + current: "a", + index: 0, + }, + { + profiles: []Profile{ + { + ID: "a", + DisplayName: "a", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + { + ID: "b", + DisplayName: "b", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + }, + current: "b", + index: 1, + }, + { + profiles: []Profile{ + { + ID: "a", + DisplayName: "a", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + { + ID: "b", + DisplayName: "b", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + }, + current: "", + index: 0, + }, + { + profiles: []Profile{ + { + ID: "a", + DisplayName: "a", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + { + ID: "b", + DisplayName: "b", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + }, + current: "", + index: 0, + }, + { + profiles: []Profile{ + { + ID: "a", + DisplayName: "a", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + { + ID: "b", + DisplayName: "b", + VPNProtoList: []string{"openvpn", "wireguard"}, + }, + }, + current: "idonotexist", + index: 0, + }, + } + + for _, tc := range testCases { + pri := &Info{ + Current: tc.current, + Info: ListInfo{ + ProfileList: tc.profiles, + }, + } + got := pri.CurrentProfileIndex() + if got != tc.index { + t.Fatalf("failed getting profile index, got: '%v', want: '%v'", got, tc.index) + } + } +} |
