allow setting forward IP address for blocked domains
parent
cd553b4fad
commit
fd928abe9e
28
dns.go
28
dns.go
|
|
@ -13,24 +13,24 @@ import (
|
|||
)
|
||||
|
||||
type dnsHandler struct {
|
||||
Config Configuration
|
||||
httpBindIP net.IP
|
||||
logger *log.Logger
|
||||
Resolver DNSResolver
|
||||
Blocklist BlocklistManager
|
||||
Cache DNSCacher
|
||||
Config Configuration
|
||||
blockForwardIP net.IP
|
||||
logger *log.Logger
|
||||
Resolver DNSResolver
|
||||
Blocklist BlocklistManager
|
||||
Cache DNSCacher
|
||||
}
|
||||
|
||||
//NewDNSHandler creates a new DNS server handler
|
||||
func NewDNSHandler(httpBindIP string, cache DNSCacher, blocklist BlocklistManager, r DNSResolver, cfg Configuration) (dns.Handler, error) {
|
||||
func NewDNSHandler(blockForwardIP string, cache DNSCacher, blocklist BlocklistManager, r DNSResolver, cfg Configuration) (dns.Handler, error) {
|
||||
l := log.New(os.Stdout, "[DNS Server] ", log.LUTC|log.Lmicroseconds|log.Lshortfile)
|
||||
return &dnsHandler{
|
||||
logger: l,
|
||||
httpBindIP: net.ParseIP(httpBindIP),
|
||||
Resolver: r,
|
||||
Config: cfg,
|
||||
Blocklist: blocklist,
|
||||
Cache: cache,
|
||||
logger: l,
|
||||
blockForwardIP: net.ParseIP(blockForwardIP),
|
||||
Resolver: r,
|
||||
Config: cfg,
|
||||
Blocklist: blocklist,
|
||||
Cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +68,7 @@ func (h *dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
|
|||
}
|
||||
} else if list, block := h.Blocklist.IsBlacklisted(domain); block {
|
||||
h.logger.Printf("blocked dns query for '%s' from list '%s'", domain, list)
|
||||
msg.Answer = append(msg.Answer, &dns.A{Hdr: header, A: h.httpBindIP})
|
||||
msg.Answer = append(msg.Answer, &dns.A{Hdr: header, A: h.blockForwardIP})
|
||||
} else if cacheAnswers, ok := h.Cache.Get(domain); ok && len(cacheAnswers) > 0 {
|
||||
msg.Answer = append(msg.Answer, cacheAnswers...)
|
||||
} else {
|
||||
|
|
|
|||
9
main.go
9
main.go
|
|
@ -12,9 +12,10 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
configFilePath = flag.String("config", "./config.json", "config file path")
|
||||
httpAddress = flag.String("bind-http", "127.0.0.1", "interface to bind the HTTP server to (0.0.0.0 for all)")
|
||||
dnsAddress = flag.String("bind-dns", "127.0.0.1", "interface to bind the DNS server to (0.0.0.0 for all)")
|
||||
configFilePath = flag.String("config", "./config.json", "config file path")
|
||||
httpAddress = flag.String("bind-http", "127.0.0.1", "interface to bind the HTTP server to (0.0.0.0 for all)")
|
||||
dnsAddress = flag.String("bind-dns", "127.0.0.1", "interface to bind the DNS server to (0.0.0.0 for all)")
|
||||
blockForwardAddr = flag.String("block-forward-ip", "127.0.0.1", "IP address to forward blocked traffic to. Preferably has https running.")
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -47,7 +48,7 @@ func main() {
|
|||
ips = append(ips, net.ParseIP(strIP))
|
||||
}
|
||||
|
||||
handler, err := NewDNSHandler(*httpAddress, &memoryDNSCacher{TTL: time.Minute}, domainBlacklist, DNSResolver(ips), *cfg)
|
||||
handler, err := NewDNSHandler(*blockForwardAddr, &memoryDNSCacher{TTL: time.Minute}, domainBlacklist, DNSResolver(ips), *cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue