1
0
Fork 0

Merge pull request #4 from section-io/cname-chain

Collapse CNAME chain
pull/5/head
Ambrose Chua 2019-10-23 13:05:52 +08:00 committed by GitHub
commit c307d2c587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
"math"
"golang.org/x/net/context"
)
@ -33,6 +34,13 @@ func NewResponseModifier(w dns.ResponseWriter) *ResponseModifier {
}
}
func min(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
// WriteMsg records the status code and calls the
// underlying ResponseWriter's WriteMsg method.
func (r *ResponseModifier) WriteMsg(res *dns.Msg) error {
@ -46,24 +54,26 @@ func (r *ResponseModifier) WriteMsg(res *dns.Msg) error {
// Find and delete CNAME record on that zone, storing the canonical name.
var (
cname string
ttl uint32
cname string = zone
ttl uint32 = math.MaxUint32
)
for i, rr := range res.Answer {
if rr.Header().Rrtype == dns.TypeCNAME && rr.Header().Name == zone {
for i := 0; i < len(res.Answer); {
rr := res.Answer[i]
if rr.Header().Rrtype == dns.TypeCNAME && rr.Header().Name == cname {
cname = rr.(*dns.CNAME).Target
ttl = rr.(*dns.CNAME).Header().Ttl
ttl = min(ttl, rr.(*dns.CNAME).Header().Ttl)
// Remove the CNAME record
res.Answer = append(res.Answer[:i], res.Answer[i+1:]...)
break
continue
}
i++
}
// Rename all the records with the above canonical name to the zone name
for _, rr := range res.Answer {
if rr.Header().Name == cname {
rr.Header().Name = zone
rr.Header().Ttl = ttl
rr.Header().Ttl = min(ttl, rr.Header().Ttl)
}
}

16
go.mod Normal file
View File

@ -0,0 +1,16 @@
module github.com/serverwentdown/alias
go 1.13
require (
github.com/caddyserver/caddy v1.0.3
github.com/coredns/coredns v1.6.4
github.com/miekg/dns v1.1.17
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
)
replace (
github.com/Azure/go-autorest => github.com/Azure/go-autorest v13.0.0+incompatible
github.com/miekg/dns v1.1.3 => github.com/miekg/dns v1.1.17
golang.org/x/net v0.0.0-20190813000000-74dc4d7220e7 => golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
)

View File

@ -4,7 +4,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/mholt/caddy"
"github.com/caddyserver/caddy"
)
func init() {