-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitesfilter.go
79 lines (69 loc) · 1.34 KB
/
sitesfilter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package filterschedule
import (
"strings"
"time"
"os"
"k8s.io/apimachinery/pkg/util/yaml"
)
type SitesFilter struct {
Description string
Filter Sites
For Targets
When Schedule
}
type Sites struct {
Everything bool
Sites []string
}
type Targets struct {
All bool
Hosts []string
}
func LoadFromFile(filename string) ([]SitesFilter, error) {
var f []SitesFilter
content, err := os.ReadFile(filename)
if err == nil {
err = yaml.Unmarshal(content, &f)
}
return f, err
}
func (sf *SitesFilter) IsMatching(name string, clientIP string, t time.Time) bool {
if sf.Filter.Everything {
if sf.isApplicableTo(clientIP) && sf.isApplicableAt(t) {
return true
}
} else {
for _, p := range sf.Filter.Sites {
if strings.Contains(name, p) {
if sf.isApplicableTo(clientIP) && sf.isApplicableAt(t) {
return true
}
}
}
}
return false
}
func (sf *SitesFilter) isApplicableTo(clientIP string) bool {
if sf.For.All {
return true
} else {
for _, ip := range sf.For.Hosts {
if ip == clientIP {
return true
}
}
}
return false
}
func (sf *SitesFilter) isApplicableAt(t time.Time) bool {
if sf.When.Always {
return true
} else {
for _, se := range sf.When.Schedule {
if isApplicableAtDays(se.Days, t) && isApplicableAtHours(se.Hours, t) {
return true
}
}
}
return false
}