summaryrefslogtreecommitdiff
path: root/internal/api/profiles
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-06 16:27:45 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commita84050a5e93f5fb9f5bbb79ca21b37e8359cf289 (patch)
treeecdf0cea81b0bd6a3cf669f2b31c45a222d1c5f5 /internal/api/profiles
parent3152078aec8334357a61171838f664eb03299211 (diff)
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
Diffstat (limited to 'internal/api/profiles')
-rw-r--r--internal/api/profiles/profiles.go82
1 files changed, 82 insertions, 0 deletions
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}
+}