Skip to content

Commit

Permalink
fix(pkg/blockbuilder): make settings fields nilable
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 14, 2023
1 parent 55c6711 commit 42276e5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
6 changes: 3 additions & 3 deletions internal/setup/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func BuildBlockBuilder(userSettings settings.Block,
client *http.Client) (blockBuilder *blockbuilder.Builder) {
settings := blockbuilder.Settings{
Client: client,
BlockMalicious: *userSettings.BlockMalicious,
BlockAds: *userSettings.BlockAds,
BlockSurveillance: *userSettings.BlockSurveillance,
BlockMalicious: userSettings.BlockMalicious,
BlockAds: userSettings.BlockAds,
BlockSurveillance: userSettings.BlockSurveillance,
}

settings.AllowedHosts = make([]string, len(userSettings.AllowedHosts))
Expand Down
48 changes: 24 additions & 24 deletions pkg/blockbuilder/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
}{
"none blocked": {
settings: Settings{
BlockMalicious: false,
BlockAds: false,
BlockSurveillance: false,
BlockMalicious: ptrTo(false),
BlockAds: ptrTo(false),
BlockSurveillance: ptrTo(false),
},
},
"all blocked without lists": {
settings: Settings{
BlockMalicious: true,
BlockAds: true,
BlockSurveillance: true,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(true),
BlockSurveillance: ptrTo(true),
},
},
"all blocked with lists": {
settings: Settings{
BlockMalicious: true,
BlockAds: true,
BlockSurveillance: true,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(true),
BlockSurveillance: ptrTo(true),
},
maliciousHosts: httpCase{
content: []byte("malicious.com"),
Expand All @@ -77,9 +77,9 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
},
"all blocked with allowed hostnames": {
settings: Settings{
BlockMalicious: true,
BlockAds: true,
BlockSurveillance: true,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(true),
BlockSurveillance: ptrTo(true),
AllowedHosts: []string{"ads.com"},
},
maliciousHosts: httpCase{
Expand All @@ -105,9 +105,9 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
},
"blocked with additional blocked IP addresses": {
settings: Settings{
BlockMalicious: true,
BlockAds: false,
BlockSurveillance: false,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(false),
BlockSurveillance: ptrTo(false),
AddBlockedIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 7})},
},
maliciousHosts: httpCase{
Expand All @@ -121,9 +121,9 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
},
"all blocked with lists and one error": {
settings: Settings{
BlockMalicious: true,
BlockAds: true,
BlockSurveillance: true,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(true),
BlockSurveillance: ptrTo(true),
},
maliciousHosts: httpCase{
content: []byte("malicious.com"),
Expand Down Expand Up @@ -151,9 +151,9 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
},
"all blocked with errors": {
settings: Settings{
BlockMalicious: true,
BlockAds: true,
BlockSurveillance: true,
BlockMalicious: ptrTo(true),
BlockAds: ptrTo(true),
BlockSurveillance: ptrTo(true),
},
maliciousHosts: httpCase{
err: errors.New("malicious hostnames"),
Expand Down Expand Up @@ -195,15 +195,15 @@ func Test_Builder_BuildAll(t *testing.T) { //nolint:cyclop,maintidx
}{
m: make(map[string]int),
}
if tc.settings.BlockMalicious {
if *tc.settings.BlockMalicious {
clientCalls.m[maliciousBlockListIPsURL] = 0
clientCalls.m[maliciousBlockListHostnamesURL] = 0
}
if tc.settings.BlockAds {
if *tc.settings.BlockAds {
clientCalls.m[adsBlockListIPsURL] = 0
clientCalls.m[adsBlockListHostnamesURL] = 0
}
if tc.settings.BlockSurveillance {
if *tc.settings.BlockSurveillance {
clientCalls.m[surveillanceBlockListIPsURL] = 0
clientCalls.m[surveillanceBlockListHostnamesURL] = 0
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/blockbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func New(settings Settings) *Builder {

return &Builder{
client: settings.Client,
blockMalicious: settings.BlockMalicious,
blockAds: settings.BlockAds,
blockSurveillance: settings.BlockSurveillance,
blockMalicious: *settings.BlockMalicious,
blockAds: *settings.BlockAds,
blockSurveillance: *settings.BlockSurveillance,
allowedHosts: settings.AllowedHosts,
allowedIPs: settings.AllowedIPs,
allowedIPPrefixes: settings.AllowedIPPrefixes,
Expand Down
4 changes: 4 additions & 0 deletions pkg/blockbuilder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"net/netip"
)

func ptrTo[T any](x T) *T {
return &x
}

func convertIPsToString(ips []netip.Addr) (ipStrings []string) {
ipStrings = make([]string, len(ips))
for i := range ips {
Expand Down
15 changes: 9 additions & 6 deletions pkg/blockbuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

type Settings struct {
Client *http.Client
BlockMalicious bool
BlockAds bool
BlockSurveillance bool
BlockMalicious *bool
BlockAds *bool
BlockSurveillance *bool
AllowedHosts []string
AllowedIPs []netip.Addr
AllowedIPPrefixes []netip.Prefix
Expand All @@ -27,6 +27,9 @@ type Settings struct {

func (s *Settings) SetDefaults() {
s.Client = gosettings.DefaultPointerRaw(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
Expand Down Expand Up @@ -60,13 +63,13 @@ func (s *Settings) ToLinesNode() (node *gotree.Node) {
node = gotree.New("Filter build settings:")

var blockedCategories []string
if s.BlockMalicious {
if *s.BlockMalicious {
blockedCategories = append(blockedCategories, "malicious")
}
if s.BlockSurveillance {
if *s.BlockSurveillance {
blockedCategories = append(blockedCategories, "surveillance")
}
if s.BlockAds {
if *s.BlockAds {
blockedCategories = append(blockedCategories, "ads")
}

Expand Down

0 comments on commit 42276e5

Please sign in to comment.