-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcnamechain.go
39 lines (35 loc) · 908 Bytes
/
cnamechain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cdnfinder
import (
"time"
"github.com/miekg/dns"
)
func detectcnamesretry(hostname, server string, retry bool) ([]string, error) {
out := make([]string, 0)
out = append(out, hostname) //Include question hostname as first item in chain
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{hostname, dns.TypeA, dns.ClassINET}
c := new(dns.Client)
c.Timeout = time.Second * 5
msg, _, err := c.Exchange(m1, server)
if err != nil {
return out, err
}
for _, ans := range msg.Answer {
if c, ok := ans.(*dns.CNAME); ok {
out = append(out, c.Target)
}
}
if err != nil {
if retry {
//If fail retry once
return detectcnamesretry(hostname, server, false)
}
}
return out, err
}
func detectcnames(hostname, server string) ([]string, error) {
return detectcnamesretry(hostname, server, true)
}