Skip to content

Commit

Permalink
all: imp chlog
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Sep 1, 2023
1 parent a704325 commit 183f84a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 120 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ NOTE: Add new changes BELOW THIS COMMENT.

### Added

- Wildcard pattern support for ignored domains in logs and statistics ([#5720]).
- AdBlock-style syntax support for ignored domains in logs and statistics
([#5720]).
- [`Strict-Transport-Security`][hsts] header in the HTTP API and DNS-over-HTTPS
responses when HTTPS is forced ([#2998]). See [RFC 6979][rfc6797].
- UI for the schedule of the service-blocking pause ([#951]).
Expand Down
27 changes: 5 additions & 22 deletions internal/aghnet/ignore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aghnet

import (
"fmt"
"strings"

"github.com/AdguardTeam/urlfilter"
Expand All @@ -16,31 +15,15 @@ type IgnoreEngine struct {
// engine is the filtering engine that can match ignored hostnames.
engine *urlfilter.DNSEngine

// ignored is the sorted list of ignored hostnames.
// ignored is the list of ignored hostnames.
ignored []string
}

// NewIgnoreEngine creates a new instance of the IgnoreEngine and validates the
// NewIgnoreEngine creates a new instance of the IgnoreEngine and stores the
// list of ignored hostnames.
func NewIgnoreEngine(ignored []string) (e *IgnoreEngine, err error) {
list := make([]string, 0, len(ignored))
for i, host := range ignored {
if host == "" {
return nil, fmt.Errorf("at index %d: hostname is empty", i)
}

list = append(list, strings.ToLower(host))
}

slices.Sort(list)
for i := 0; i < len(list)-1; i++ {
if list[i] == list[i+1] {
return nil, fmt.Errorf("duplicate hostname %q", list[i])
}
}

ruleList := &filterlist.StringRuleList{
RulesText: strings.Join(list, "\n"),
RulesText: strings.ToLower(strings.Join(ignored, "\n")),
IgnoreCosmetic: true,
}
ruleStorage, err := filterlist.NewRuleStorage([]filterlist.RuleList{ruleList})
Expand All @@ -50,7 +33,7 @@ func NewIgnoreEngine(ignored []string) (e *IgnoreEngine, err error) {

return &IgnoreEngine{
engine: urlfilter.NewDNSEngine(ruleStorage),
ignored: list,
ignored: ignored,
}, nil
}

Expand All @@ -65,7 +48,7 @@ func (e *IgnoreEngine) Has(host string) (ignore bool) {
return ignore
}

// Values returs a sorted list of ignored hostnames.
// Values returs a copy of ignored hostnames list.
func (e *IgnoreEngine) Values() (ignored []string) {
return slices.Clone(e.ignored)
}
74 changes: 0 additions & 74 deletions internal/aghnet/ignore_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,12 @@
package aghnet_test

import (
"strings"
"testing"

"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

func TestIgnoreEngine_New(t *testing.T) {
testCases := []struct {
name string
wantErrMsg string
hostnames []string
}{{
name: "single",
hostnames: []string{
"example.com",
},
wantErrMsg: "",
}, {
name: "multiple",
hostnames: []string{
"example.com",
"example.org",
"example.net",
},
wantErrMsg: "",
}, {
name: "empty",
hostnames: []string{
"example.com",
"",
},
wantErrMsg: "at index 1: hostname is empty",
}, {
name: "dublicate",
hostnames: []string{
"example.com",
"example.com",
},
wantErrMsg: "duplicate hostname \"example.com\"",
}}

for _, tc := range testCases {
engine, err := aghnet.NewIgnoreEngine(tc.hostnames)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)

if err == nil {
require.NotNil(t, engine)
}
}
}

func TestIgnoreEngine_Has(t *testing.T) {
hostnames := []string{
"*.example.com",
Expand Down Expand Up @@ -92,29 +44,3 @@ func TestIgnoreEngine_Has(t *testing.T) {
require.Equal(t, tc.ignore, engine.Has(tc.host))
}
}

func TestIgnoreEngine_Values(t *testing.T) {
hostnames := []string{
"B.com.",
"d.com",
"A.com.",
"c.com",
}

engine, err := aghnet.NewIgnoreEngine(hostnames)
require.NotNil(t, engine)
require.NoError(t, err)

values := engine.Values()
require.Len(t, values, len(hostnames))

t.Run("sorted", func(t *testing.T) {
require.True(t, slices.IsSorted(values))
})

t.Run("lowercased", func(t *testing.T) {
for _, host := range values {
require.Equal(t, strings.ToLower(host), host)
}
})
}
1 change: 1 addition & 0 deletions internal/dnsforward/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type accessManager struct {
allowedClientIDs *stringutil.Set
blockedClientIDs *stringutil.Set

// TODO(s.chzhen): Use [aghnet.IgnoreEngine].
blockedHostsEng *urlfilter.DNSEngine

// TODO(a.garipov): Create a type for a set of IP networks.
Expand Down
23 changes: 0 additions & 23 deletions internal/stats/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,6 @@ func TestHandleStatsConfig(t *testing.T) {
},
wantCode: http.StatusOK,
wantErr: "",
}, {
name: "ignored_duplicate",
body: getConfigResp{
Enabled: aghalg.NBTrue,
Interval: float64(minIvl.Milliseconds()),
Ignored: []string{
"ignor.ed",
"ignor.ed",
},
},
wantCode: http.StatusUnprocessableEntity,
wantErr: "ignored: duplicate hostname \"ignor.ed\"\n",
}, {
name: "ignored_empty",
body: getConfigResp{
Enabled: aghalg.NBTrue,
Interval: float64(minIvl.Milliseconds()),
Ignored: []string{
"",
},
},
wantCode: http.StatusUnprocessableEntity,
wantErr: "ignored: at index 0: hostname is empty\n",
}, {
name: "enabled_is_null",
body: getConfigResp{
Expand Down

0 comments on commit 183f84a

Please sign in to comment.