Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

IPv6 Requests now return AAAA record when blocked #16

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
var log = clog.NewWithPlugin("ads")

type DNSAdBlock struct {
Next plugin.Handler
BlockLists []string
TargetIP net.IP
RuleSet RuleSet
LogBlocks bool
blockMap BlockMap
updater *BlocklistUpdater
Next plugin.Handler
BlockLists []string
TargetIP net.IP
TargetIPv6 net.IP
RuleSet RuleSet
LogBlocks bool
blockMap BlockMap
updater *BlocklistUpdater
}

func (e *DNSAdBlock) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
Expand All @@ -48,7 +49,13 @@ func (e *DNSAdBlock) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
requestCountBySource.WithLabelValues(metrics.WithServer(ctx), state.IP()).Inc()

if !e.RuleSet.IsWhitelisted(qname) && (e.blockMap[qname] || e.RuleSet.IsBlacklisted(qname)) {
answers := a(state.Name(), []net.IP{e.TargetIP})
var answers []dns.RR
if state.QType() == dns.TypeAAAA {
answers = aaaa(state.Name(), []net.IP{e.TargetIPv6})
} else {
answers = a(state.Name(), []net.IP{e.TargetIP})
}

m := new(dns.Msg)
m.SetReply(r)
m.Authoritative, m.RecursionAvailable = true, true
Expand All @@ -73,7 +80,7 @@ func (e *DNSAdBlock) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
func (e *DNSAdBlock) Name() string { return "ads" }

func a(zone string, ips []net.IP) []dns.RR {
answers := []dns.RR{}
var answers []dns.RR
for _, ip := range ips {
r := new(dns.A)
r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeA,
Expand All @@ -83,3 +90,14 @@ func a(zone string, ips []net.IP) []dns.RR {
}
return answers
}
func aaaa(zone string, ips []net.IP) []dns.RR {
var answers []dns.RR
for _, ip := range ips {
r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeAAAA,
Class: dns.ClassINET, Ttl: 3600}
r.AAAA = ip
answers = append(answers, r)
}
return answers
}
27 changes: 27 additions & 0 deletions ads_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ import (
"testing"
)

func TestLookup_Block_IPv6(t *testing.T) {
blacklist := make([]string, 0)

testCases := make([]test.Case, 0)
for i := 0; i < 10; i++ {
qname := fmt.Sprintf("testhost-%09d.local.test.tld", i+1)
blacklist = append(blacklist, qname)

tcase := test.Case{
Qname: qname,
Qtype: dns.TypeAAAA,
Answer: []dns.RR{
test.AAAA(fmt.Sprintf("%s. 3600 IN AAAA fe80::9cbd:c3ff:fe28:e133", qname)),
},
}
testCases = append(testCases, tcase)
}

testCases = append(testCases, initAllowedTestCases()[10:]...)

p := initTestPlugin(t, BuildRuleset(make([]string, 0), blacklist))
ctx := context.TODO()

resolveTestCases(testCases, p, ctx, t)
}

func TestLookup_RegexBlacklist(t *testing.T) {
ruleset := getEmptyRuleset()

Expand Down Expand Up @@ -208,6 +234,7 @@ func initTestPlugin(t testing.TB, rs RuleSet) *DNSAdBlock {
updater: nil,
LogBlocks: true,
TargetIP: net.ParseIP("10.1.33.7"),
TargetIPv6: net.ParseIP("fe80::9cbd:c3ff:fe28:e133"),
}

return &p
Expand Down