moved handle of blacklist domains

This commit is contained in:
Niklas 2020-12-28 00:36:54 +01:00
parent 9bc041ca7e
commit d77db9234e
2 changed files with 37 additions and 27 deletions

View File

@ -3,12 +3,18 @@ package main
import (
"io/ioutil"
"log"
"net"
"net/http"
"regexp"
"github.com/miekg/dns"
)
const blockTTL uint32 = 300
var nullIPv4 = net.IPv4(0, 0, 0, 0)
var nullIPv6 = net.ParseIP("::/0")
func loadBlacklist(config []configBlacklist) map[string]bool {
list := make([]string, 0)
for _, element := range config {
@ -77,3 +83,33 @@ func parseRawBlacklist(blacklist configBlacklist, raw string) []string {
return finalList
}
func handleBlockedDomain(w dns.ResponseWriter, r *dns.Msg) {
q := r.Question[0]
m := new(dns.Msg)
m.SetReply(r)
if q.Qtype == dns.TypeA {
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: blockTTL,
},
A: nullIPv4,
})
} else if q.Qtype == dns.TypeAAAA {
m.Answer = append(m.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: blockTTL,
},
AAAA: nullIPv6,
})
}
w.WriteMsg(m)
}

View File

@ -205,33 +205,7 @@ func createServer(zones zoneMap, config config, aclList map[string]*net.IPNet, b
}
if _, ok := blacklist[r.Question[0].Name]; ok {
// Domain is blocked
m := new(dns.Msg)
m.SetReply(r)
if r.Question[0].Qtype == dns.TypeA {
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: r.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1000,
},
A: net.IPv4(0, 0, 0, 0),
})
} else if r.Question[0].Qtype == dns.TypeAAAA {
m.Answer = append(m.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: r.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1000,
},
AAAA: net.ParseIP("::/0"),
})
}
w.WriteMsg(m)
handleBlockedDomain(w, r)
} else {
// Forward request
in, _, err := c.Exchange(r, config.Forward.Server)