forked from letsencrypt/challtestsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsone.go
57 lines (50 loc) · 1.58 KB
/
dnsone.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
package challtestsrv
import (
"time"
"github.com/miekg/dns"
)
// AddDNSOneChallenge adds a TXT record for the given host with the given
// content.
func (s *ChallSrv) AddDNSOneChallenge(host, content string) {
s.challMu.Lock()
defer s.challMu.Unlock()
s.dnsOne[host] = append(s.dnsOne[host], content)
}
// DeleteDNSOneChallenge deletes a TXT record for the given host.
func (s *ChallSrv) DeleteDNSOneChallenge(host string) {
s.challMu.Lock()
defer s.challMu.Unlock()
delete(s.dnsOne, host)
}
// GetDNSOneChallenge returns a slice of TXT record values for the given host.
// If the host does not exist in the challenge response data then nil is
// returned.
func (s *ChallSrv) GetDNSOneChallenge(host string) []string {
s.challMu.RLock()
defer s.challMu.RUnlock()
return s.dnsOne[host]
}
type dnsHandler func(dns.ResponseWriter, *dns.Msg)
// dnsOneServer creates an ACME DNS-01 challenge server. The provided dns
// handler will be registered with the `miekg/dns` package to
// handle DNS requests. Because the DNS server runs both a UDP and a TCP
// listener two `server` objects are returned.
func dnsOneServer(address string, handler dnsHandler) []challengeServer {
// Register the dnsHandler
dns.HandleFunc(".", handler)
// Create a UDP DNS server
udpServer := &dns.Server{
Addr: address,
Net: "udp",
ReadTimeout: time.Second,
WriteTimeout: time.Second,
}
// Create a TCP DNS server
tcpServer := &dns.Server{
Addr: address,
Net: "tcp",
ReadTimeout: time.Second,
WriteTimeout: time.Second,
}
return []challengeServer{udpServer, tcpServer}
}