-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
settings.go
96 lines (77 loc) · 2.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package blockbuilder
import (
"fmt"
"net/http"
"net/netip"
"regexp"
"strings"
"github.com/qdm12/gosettings"
"github.com/qdm12/gosettings/validate"
"github.com/qdm12/gotree"
)
type Settings struct {
Client *http.Client
BlockMalicious *bool
BlockAds *bool
BlockSurveillance *bool
AllowedHosts []string
AllowedIPs []netip.Addr
AllowedIPPrefixes []netip.Prefix
AddBlockedHosts []string
AddBlockedIPs []netip.Addr
AddBlockedIPPrefixes []netip.Prefix
}
func (s *Settings) SetDefaults() {
s.Client = gosettings.DefaultComparable(s.Client, http.DefaultClient)
s.BlockMalicious = gosettings.DefaultPointer(s.BlockMalicious, false)
s.BlockAds = gosettings.DefaultPointer(s.BlockAds, false)
s.BlockSurveillance = gosettings.DefaultPointer(s.BlockSurveillance, false)
}
var hostRegex = 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
func (s Settings) Validate() (err error) {
err = validate.AllMatchRegex(s.AllowedHosts, hostRegex)
if err != nil {
return fmt.Errorf("allowed hosts: %w", err)
}
err = validate.AllMatchRegex(s.AddBlockedHosts, hostRegex)
if err != nil {
return fmt.Errorf("additional blocked hosts: %w", err)
}
return nil
}
func (s *Settings) String() string {
return s.ToLinesNode().String()
}
func (s *Settings) ToLinesNode() (node *gotree.Node) {
node = gotree.New("Filter build settings:")
var blockedCategories []string
if *s.BlockMalicious {
blockedCategories = append(blockedCategories, "malicious")
}
if *s.BlockSurveillance {
blockedCategories = append(blockedCategories, "surveillance")
}
if *s.BlockAds {
blockedCategories = append(blockedCategories, "ads")
}
node.Appendf("Blocked categories: %s", strings.Join(blockedCategories, ", "))
if len(s.AllowedHosts) > 0 {
node.Appendf("Hostnames unblocked: %d", len(s.AllowedHosts))
}
if len(s.AllowedIPs) > 0 {
node.Appendf("IP addresses unblocked: %d", len(s.AllowedIPs))
}
if len(s.AllowedIPPrefixes) > 0 {
node.Appendf("IP networks unblocked: %d", len(s.AllowedIPPrefixes))
}
if len(s.AddBlockedHosts) > 0 {
node.Appendf("Additional hostnames blocked: %d", len(s.AddBlockedHosts))
}
if len(s.AddBlockedIPs) > 0 {
node.Appendf("Additional IP addresses blocked: %d", len(s.AddBlockedIPs))
}
if len(s.AddBlockedIPPrefixes) > 0 {
node.Appendf("Additional IP networks blocked: %d", len(s.AddBlockedIPPrefixes))
}
return node
}