-
Notifications
You must be signed in to change notification settings - Fork 322
/
domains.go
54 lines (45 loc) · 1.08 KB
/
domains.go
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
package domains
import (
"sort"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/vouch/vouch-proxy/pkg/cfg"
)
var domains = cfg.Cfg.Domains
func init() {
sort.Sort(ByLengthDesc(domains))
}
// Matches returns one of the domains we're configured for
// TODO return all matches
// Matches return the first match of the
func Matches(s string) string {
for i, v := range domains {
if strings.Contains(s, v) {
log.Debugf("domain %s matched array value at [%d]=%v", s, i, v)
return v
}
}
log.Warnf("domain %s not found in any domains %v", s, domains)
return ""
}
// IsUnderManagement check if string contains a vouch managed domain
func IsUnderManagement(s string) bool {
match := Matches(s)
if match != "" {
return true
}
return false
}
// ByLengthDesc sort from
// https://play.golang.org/p/N6GbEgBffd
type ByLengthDesc []string
func (s ByLengthDesc) Len() int {
return len(s)
}
func (s ByLengthDesc) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// this differs by offing the longest first
func (s ByLengthDesc) Less(i, j int) bool {
return len(s[j]) < len(s[i])
}