2017-08-20 09:55:02 +08:00
|
|
|
package alias
|
|
|
|
|
|
|
|
import (
|
2021-01-24 09:20:40 +08:00
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
|
2017-09-16 18:20:21 +08:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2017-08-20 09:55:02 +08:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2017-09-16 18:20:21 +08:00
|
|
|
// Rewrite is plugin to rewrite requests internally before being handled.
|
2017-08-20 09:55:02 +08:00
|
|
|
type Alias struct {
|
2017-09-16 18:20:21 +08:00
|
|
|
Next plugin.Handler
|
2017-08-20 09:55:02 +08:00
|
|
|
}
|
|
|
|
|
2017-09-16 18:20:21 +08:00
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
2017-08-20 09:55:02 +08:00
|
|
|
func (al Alias) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
|
|
mw := NewResponseModifier(w)
|
2017-09-16 18:20:21 +08:00
|
|
|
return plugin.NextOrFailure(al.Name(), al.Next, ctx, mw, r)
|
2017-08-20 09:55:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements the Handler interface.
|
|
|
|
func (al Alias) Name() string { return "alias" }
|
|
|
|
|
|
|
|
type ResponseModifier struct {
|
|
|
|
dns.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a dns.Msg modifier that replaces CNAME on root zones with other records.
|
|
|
|
func NewResponseModifier(w dns.ResponseWriter) *ResponseModifier {
|
|
|
|
return &ResponseModifier{
|
|
|
|
ResponseWriter: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:45:39 +08:00
|
|
|
func min(a, b uint32) uint32 {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-08-20 09:55:02 +08:00
|
|
|
// WriteMsg records the status code and calls the
|
|
|
|
// underlying ResponseWriter's WriteMsg method.
|
|
|
|
func (r *ResponseModifier) WriteMsg(res *dns.Msg) error {
|
|
|
|
// Guess zone based on authority section.
|
|
|
|
var zone string
|
2018-01-30 23:57:48 +08:00
|
|
|
for _, rr := range res.Ns {
|
2017-08-20 09:55:02 +08:00
|
|
|
if rr.Header().Rrtype == dns.TypeNS {
|
|
|
|
zone = rr.Header().Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and delete CNAME record on that zone, storing the canonical name.
|
2018-01-31 00:01:13 +08:00
|
|
|
var (
|
2019-10-18 12:45:39 +08:00
|
|
|
cname string = zone
|
|
|
|
ttl uint32 = math.MaxUint32
|
2018-01-31 00:01:13 +08:00
|
|
|
)
|
2019-10-18 12:45:39 +08:00
|
|
|
for i := 0; i < len(res.Answer); {
|
|
|
|
rr := res.Answer[i]
|
|
|
|
if rr.Header().Rrtype == dns.TypeCNAME && rr.Header().Name == cname {
|
2017-08-20 09:55:02 +08:00
|
|
|
cname = rr.(*dns.CNAME).Target
|
2019-10-18 12:45:39 +08:00
|
|
|
ttl = min(ttl, rr.(*dns.CNAME).Header().Ttl)
|
2017-08-20 09:55:02 +08:00
|
|
|
// Remove the CNAME record
|
|
|
|
res.Answer = append(res.Answer[:i], res.Answer[i+1:]...)
|
2019-10-18 12:45:39 +08:00
|
|
|
continue
|
2017-08-20 09:55:02 +08:00
|
|
|
}
|
2019-10-18 12:45:39 +08:00
|
|
|
i++
|
2017-08-20 09:55:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename all the records with the above canonical name to the zone name
|
2018-01-30 23:57:48 +08:00
|
|
|
for _, rr := range res.Answer {
|
2017-08-20 09:55:02 +08:00
|
|
|
if rr.Header().Name == cname {
|
|
|
|
rr.Header().Name = zone
|
2019-10-18 12:45:39 +08:00
|
|
|
rr.Header().Ttl = min(ttl, rr.Header().Ttl)
|
2017-08-20 09:55:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is a wrapper that records the size of the message that gets written.
|
|
|
|
func (r *ResponseModifier) Write(buf []byte) (int, error) {
|
|
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements dns.Hijacker. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Hijack method if there is one, or returns an error.
|
|
|
|
func (r *ResponseModifier) Hijack() {
|
|
|
|
r.ResponseWriter.Hijack()
|
|
|
|
return
|
|
|
|
}
|