From a84050a5e93f5fb9f5bbb79ca21b37e8359cf289 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Tue, 6 Feb 2024 16:27:45 +0100 Subject: Server: Refactor internal server package to use new state file This completely rewrites the internal server package. Some advantages: - Caches less - Uses a callback interface so that the client package does not get so convoluted - Introduce a new API package that only deals with the server API and uses github.com/jwijenbergh/eduoauth-go --- internal/api/profiles/profiles.go | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 internal/api/profiles/profiles.go (limited to 'internal/api/profiles') diff --git a/internal/api/profiles/profiles.go b/internal/api/profiles/profiles.go new file mode 100644 index 0000000..2f4fed7 --- /dev/null +++ b/internal/api/profiles/profiles.go @@ -0,0 +1,82 @@ +package profiles + +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"` + DNSSearchDomains []string `json:"dns_search_domain_list"` +} + +type ListInfo struct { + ProfileList []Profile `json:"profile_list"` +} + +type Info struct { + Info ListInfo `json:"info"` +} + +func (i Info) Len() int { + return len(i.Info.ProfileList) +} + +func (i Info) Get(id string) *Profile { + for _, p := range i.Info.ProfileList { + if p.ID == id { + return &p + } + } + return nil +} + +func (i Info) MustIndex(n int) Profile { + return i.Info.ProfileList[n] +} + +func hasProtocol(protos []string, proto protocol.Protocol) bool { + for _, p := range protos { + if protocol.New(p) == proto { + return true + } + } + return false +} + +func (p *Profile) HasOpenVPN() bool { + return hasProtocol(p.VPNProtoList, protocol.OpenVPN) +} + +func (p *Profile) HasWireGuard() bool { + return hasProtocol(p.VPNProtoList, protocol.WireGuard) +} + +func (i Info) FilterWireGuard() *Info { + var ret []Profile + for _, p := range i.Info.ProfileList { + if !p.HasOpenVPN() { + continue + } + } + return &Info{ + Info: ListInfo{ + ProfileList: ret, + }, + } +} + +func (i Info) Public() server.Profiles { + m := make(map[string]server.Profile) + for _, p := range i.Info.ProfileList { + m[p.ID] = server.Profile{ + DisplayName: map[string]string{ + "en": p.DisplayName, + }, + } + } + return server.Profiles{Map: m} +} -- cgit v1.2.3