-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
settings.go
75 lines (59 loc) · 1.77 KB
/
settings.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
package update
import (
"errors"
"fmt"
"net/netip"
"regexp"
"github.com/miekg/dns"
"github.com/qdm12/gosettings/validate"
"github.com/qdm12/gotree"
)
type Settings struct {
// FqdnHostnames is a list of fully qualified domain names
// to filter out.
FqdnHostnames []string
// IPs is a list of IP addresses to filter out.
IPs []netip.Addr
// IPPrefixes is a list of IP prefixes to filter out.
IPPrefixes []netip.Prefix
}
func (s *Settings) SetDefaults() {}
var fqdnHostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])(\.([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9]))*\.$`) //nolint:lll
var (
ErrFqdnHostnameNotValid = errors.New("fqdn hostname is not valid")
)
func (s Settings) Validate() (err error) {
err = validate.AllMatchRegex(s.FqdnHostnames, fqdnHostRegex)
if err != nil {
return fmt.Errorf("FQDN hostnames: %w", err)
}
return nil
}
// BlockHostnames transforms the slice of hostnames given to
// FQDN hostnames and sets these to the settings.
func (s *Settings) BlockHostnames(hostnames []string) {
s.FqdnHostnames = make([]string, len(hostnames))
for i := range hostnames {
s.FqdnHostnames[i] = dns.Fqdn(hostnames[i])
}
}
func (s *Settings) String() string {
return s.ToLinesNode().String()
}
func (s *Settings) ToLinesNode() (node *gotree.Node) {
if len(s.IPs) == 0 && len(s.FqdnHostnames) == 0 &&
len(s.IPPrefixes) == 0 {
return gotree.New("Filter update: disabled")
}
node = gotree.New("Filter update settings:")
if len(s.IPs) > 0 {
node.Appendf("IP addresses blocked: %d", len(s.IPs))
}
if len(s.IPPrefixes) > 0 {
node.Appendf("IP networks blocked: %d", len(s.IPPrefixes))
}
if len(s.FqdnHostnames) > 0 {
node.Appendf("Hostnames blocked: %d", len(s.FqdnHostnames))
}
return node
}