summaryrefslogtreecommitdiff
path: root/internal/discovery
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-05-07 11:51:34 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-05-29 14:36:10 +0200
commit3ecfc012e2db8b464596faf2c3bd4db1cab8697b (patch)
treeb234ee9aa6729e56232786fee83f92cc5bbb63e3 /internal/discovery
parentb1033a6a39fe21fec99be5318ba3536af148a79b (diff)
Discovery: Implement search and do not return keywords
This patch implements search by adding a second argument to DiscoOrganizations and DiscoServers. A search string of = "" returns everything. This also makes the subset that is returned to the client even fewer, no keywords.
Diffstat (limited to 'internal/discovery')
-rw-r--r--internal/discovery/discovery.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go
index 4a9ab92..6b7b961 100644
--- a/internal/discovery/discovery.go
+++ b/internal/discovery/discovery.go
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "strings"
"time"
"github.com/eduvpn/eduvpn-common/internal/http"
@@ -34,6 +35,25 @@ type Organization struct {
// SecureInternetHome is the secure internet home server that belongs to this organization
// Omitted if none is defined
SecureInternetHome string `json:"secure_internet_home"`
+ // KeywordList is the list of keywords
+ // Omitted if none is defined
+ KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"`
+}
+
+// Matches returns if the search query `str` matches with this organization
+func (s *Organization) Matches(str string) bool {
+ var catalog strings.Builder
+ for _, v := range s.DisplayName {
+ // length and nil error is returned
+ _, _ = catalog.WriteString(strings.ToLower(v))
+ _, _ = catalog.WriteString(" ")
+ }
+ for _, v := range s.KeywordList {
+ // length and nil error is returned
+ _, _ = catalog.WriteString(strings.ToLower(v))
+ _, _ = catalog.WriteString(" ")
+ }
+ return strings.Contains(catalog.String(), str)
}
// Servers are the list of servers from https://disco.eduvpn.org/v2/server_list.json
@@ -55,12 +75,30 @@ type Server struct {
AuthenticationURLTemplate string `json:"authentication_url_template,omitempty"`
// CountryCode is the country code for the server in case of secure internet, e.g. NL
CountryCode string `json:"country_code,omitempty"`
+ // KeywordList are the keywords of the server, omitted if empty
+ KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"`
// PublicKeyList are the public keys of the server. Currently not used in this lib but returned by the upstream discovery server
PublicKeyList []string `json:"public_key_list,omitempty"`
// SupportContact is the list/slice of support contacts
SupportContact []string `json:"support_contact,omitempty"`
}
+// Matches returns if the search query `str` matches with this server
+func (s *Server) Matches(str string) bool {
+ var catalog strings.Builder
+ for _, v := range s.DisplayName {
+ // length and nil error is returned
+ _, _ = catalog.WriteString(strings.ToLower(v))
+ _, _ = catalog.WriteString(" ")
+ }
+ for _, v := range s.KeywordList {
+ // length and nil error is returned
+ _, _ = catalog.WriteString(strings.ToLower(v))
+ _, _ = catalog.WriteString(" ")
+ }
+ return strings.Contains(catalog.String(), str)
+}
+
// Discovery is the main structure used for this package.
type Discovery struct {
// The httpClient for sending HTTP requests