-
Notifications
You must be signed in to change notification settings - Fork 22
/
dns_utils.go
148 lines (125 loc) · 4.23 KB
/
dns_utils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
*/
package utils
import (
"fmt"
"github.com/go-acme/lego/v3/challenge/dns01"
"github.com/miekg/dns"
"time"
)
const defaultPath = "/etc/resolv.conf"
var defaultNameservers = []string{
// google-public-dns-a.google.com
"8.8.8.8:53",
// google-public-dns-b.google.com
"8.8.4.4:53",
}
// PreparePrecheckNameservers collects the nameservers for checking DNS propagation.
// If no nameservers are provided, it tries to read them from `/etc/resolv.conf`, and
// last resort is to use Google public DNS servers (8.8.8.8 and 8.8.4.4)
func PreparePrecheckNameservers(nameservers []string) []string {
if len(nameservers) == 0 || len(nameservers) == 1 && len(nameservers[0]) == 0 {
return getNameservers(defaultPath, defaultNameservers)
}
return dns01.ParseNameservers(nameservers)
}
// getNameservers attempts to get systems nameservers before falling back to the defaults
func getNameservers(path string, defaults []string) []string {
config, err := dns.ClientConfigFromFile(path)
if err != nil || len(config.Servers) == 0 {
return defaults
}
return dns01.ParseNameservers(config.Servers)
}
// CreateWrapPreCheckOption creates lego DNS ChallengeOption for custom Precheck function,
// checking the DNS propagation of the DNS challenge TXT record.
func CreateWrapPreCheckOption(nameservers []string) dns01.ChallengeOption {
return dns01.WrapPreCheck(func(domain, fqdn, value string, check dns01.PreCheckFunc) (b bool, err error) {
return CheckDNSPropagation(nameservers, fqdn, value)
})
}
// CheckDNSPropagation checks if the expected TXT record has been propagated to all authoritative nameservers.
func CheckDNSPropagation(nameservers []string, fqdn string, values ...string) (bool, error) {
// Initial attempt to resolve at the recursive NS
r, err := dnsQuery(fqdn, dns.TypeTXT, nameservers, true)
if err != nil {
return false, err
}
return checkTXTValue(r.Answer, values), nil
}
func checkTXTValue(answer []dns.RR, values []string) bool {
found := make([]bool, len(values))
for _, rr := range answer {
if v, ok := rr.(*dns.TXT); ok {
for _, txt := range v.Txt {
for i, value := range values {
if txt == value {
found[i] = true
}
}
}
}
}
for _, b := range found {
if !b {
return false
}
}
return true
}
// The following methods are copied from github.com/go-acme/lego/v3/challenge/dns01/nameserver.go
// dnsTimeout is used to override the default DNS timeout of 10 seconds.
var dnsTimeout = 10 * time.Second
func dnsQuery(fqdn string, rtype uint16, nameservers []string, recursive bool) (*dns.Msg, error) {
m := createDNSMsg(fqdn, rtype, recursive)
var in *dns.Msg
var err error
for _, ns := range nameservers {
in, err = sendDNSQuery(m, ns)
if err == nil && len(in.Answer) > 0 {
break
}
}
return in, err
}
func createDNSMsg(fqdn string, rtype uint16, recursive bool) *dns.Msg {
m := new(dns.Msg)
m.SetQuestion(fqdn, rtype)
m.SetEdns0(4096, false)
if !recursive {
m.RecursionDesired = false
}
return m
}
func sendDNSQuery(m *dns.Msg, ns string) (*dns.Msg, error) {
udp := &dns.Client{Net: "udp", Timeout: dnsTimeout}
in, _, err := udp.Exchange(m, ns)
// customization: try TCP if UDP fails
if err != nil || in != nil && in.Truncated {
tcp := &dns.Client{Net: "tcp", Timeout: dnsTimeout}
// If the TCP request succeeds, the err will reset to nil
var err2 error
in, _, err2 = tcp.Exchange(m, ns)
if err == nil {
err = err2
} else if err2 != nil {
err = fmt.Errorf("DNS lookup: udp failed with %s, tcp failed with %s", err, err2)
} else {
err = nil
}
}
return in, err
}