move overide rr into func

This commit is contained in:
Djeeberjr 2022-04-03 01:33:22 +02:00
parent f6b13f423a
commit 81ce0257c4

View File

@ -3,12 +3,9 @@ package override
import ( import (
"net" "net"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
var log = clog.NewWithPlugin("overide")
type Rule struct { type Rule struct {
Search net.IP Search net.IP
Override net.IP Override net.IP
@ -22,6 +19,18 @@ type ResponseWrapper struct {
func (r *ResponseWrapper) WriteMsg(res *dns.Msg) error { func (r *ResponseWrapper) WriteMsg(res *dns.Msg) error {
for _, r := range r.Rules { for _, r := range r.Rules {
for _, rr := range res.Answer { for _, rr := range res.Answer {
overideRR(r, rr)
}
for _, rr := range res.Extra {
overideRR(r, rr)
}
}
return r.ResponseWriter.WriteMsg(res)
}
func overideRR(r Rule, rr dns.RR) {
if rr.Header().Rrtype == dns.TypeA { if rr.Header().Rrtype == dns.TypeA {
a := rr.(*dns.A) a := rr.(*dns.A)
if a.A.Equal(r.Search) { if a.A.Equal(r.Search) {
@ -34,10 +43,6 @@ func (r *ResponseWrapper) WriteMsg(res *dns.Msg) error {
} }
} }
} }
}
return r.ResponseWriter.WriteMsg(res)
}
func (r *ResponseWrapper) Write(buf []byte) (int, error) { func (r *ResponseWrapper) Write(buf []byte) (int, error) {
log.Warning("ResponseWrapper called with Write: not ensuring overide") log.Warning("ResponseWrapper called with Write: not ensuring overide")