Skip to content

Commit

Permalink
#92: wildcard support for client names
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Aug 24, 2020
1 parent 9592cb3 commit 89f062f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ blocking:
default:
- ads
- special
# use client name or ip address or client subnet as CIDR notation
laptop.fritz.box:
# use client name (with wildcard support: * - sequence of any characters, [0-9] - range)
# or single ip address / client subnet as CIDR notation
laptop*:
- ads
192.168.178.1/24:
- special
Expand Down
5 changes: 3 additions & 2 deletions docs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ blocking:
default:
- ads
- special
# use client name or ip address or client subnet as CIDR notation
laptop.fritz.box:
# use client name (with wildcard support: * - sequence of any characters, [0-9] - range)
# or single ip address / client subnet as CIDR notation
laptop*:
- ads
192.168.178.1/24:
- special
Expand Down
13 changes: 10 additions & 3 deletions resolver/blocking_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net"
"net/http"
"path/filepath"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -340,9 +341,10 @@ func extractEntryToCheckFromResponse(rr dns.RR) (entryToCheck string, tName stri
func (r *BlockingResolver) groupsToCheckForClient(request *Request) (groups []string) {
// try client names
for _, cName := range request.ClientNames {
groupsByName, found := r.cfg.ClientGroupsBlock[cName]
if found {
groups = append(groups, groupsByName...)
for blockGroup, groupsByName := range r.cfg.ClientGroupsBlock {
if clientNameMatchesBlockGroup(blockGroup, cName) {
groups = append(groups, groupsByName...)
}
}
}

Expand Down Expand Up @@ -381,6 +383,11 @@ func cidrContainsIP(cidr string, ip net.IP) bool {
return ipnet.Contains(ip)
}

func clientNameMatchesBlockGroup(group string, clientName string) bool {
match, _ := filepath.Match(group, clientName)
return match
}

func (r *BlockingResolver) matches(groupsToCheck []string, m lists.Matcher,
domain string) (blocked bool, group string) {
if len(groupsToCheck) > 0 {
Expand Down
10 changes: 10 additions & 0 deletions resolver/blocking_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
. "blocky/helpertest"
"blocky/metrics"
"blocky/util"

"encoding/json"
"net/http"
"os"
Expand Down Expand Up @@ -85,6 +86,7 @@ badcnamedomain.com`)
"192.168.178.55": {"gr1"},
"altName": {"gr2"},
"10.43.8.67/28": {"gr1"},
"wildcard[0-9]*": {"gr1"},
"default": {"defaultGroup"},
},
BlockType: "ZeroIP",
Expand Down Expand Up @@ -162,6 +164,14 @@ badcnamedomain.com`)
Expect(resp.Res.Answer).Should(BeDNSRecord("blocked2.com.", dns.TypeA, 21600, "0.0.0.0"))
})
})
When("Client name matches wildcard", func() {
It("should block query if domain is in one group", func() {
resp, err = sut.Resolve(newRequestWithClient("domain1.com.", dns.TypeA, "1.2.1.2", "wildcard1name"))

Expect(resp.Reason).Should(Equal("BLOCKED (gr1)"))
Expect(resp.Res.Answer).Should(BeDNSRecord("domain1.com.", dns.TypeA, 21600, "0.0.0.0"))
})
})

When("Default group is defined", func() {
It("should block domains from default group for each client", func() {
Expand Down

0 comments on commit 89f062f

Please sign in to comment.