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,23 +19,31 @@ 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 {
if rr.Header().Rrtype == dns.TypeA { overideRR(r, rr)
a := rr.(*dns.A) }
if a.A.Equal(r.Search) {
a.A = r.Override for _, rr := range res.Extra {
} overideRR(r, rr)
} else if rr.Header().Rrtype == dns.TypeAAAA {
a := rr.(*dns.AAAA)
if a.AAAA.Equal(r.Search) {
a.AAAA = r.Override
}
}
} }
} }
return r.ResponseWriter.WriteMsg(res) return r.ResponseWriter.WriteMsg(res)
} }
func overideRR(r Rule, rr dns.RR) {
if rr.Header().Rrtype == dns.TypeA {
a := rr.(*dns.A)
if a.A.Equal(r.Search) {
a.A = r.Override
}
} else if rr.Header().Rrtype == dns.TypeAAAA {
a := rr.(*dns.AAAA)
if a.AAAA.Equal(r.Search) {
a.AAAA = r.Override
}
}
}
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")
return r.ResponseWriter.Write(buf) return r.ResponseWriter.Write(buf)