-
Notifications
You must be signed in to change notification settings - Fork 21
/
dnsone.go
86 lines (75 loc) · 2.17 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
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
package challtestsrv
import (
"context"
"net/http"
"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}
}
type doh struct {
*http.Server
tlsCert, tlsCertKey string
}
func (s *doh) Shutdown() error {
return s.Server.Shutdown(context.Background())
}
func (s *doh) ListenAndServe() error {
return s.Server.ListenAndServeTLS(s.tlsCert, s.tlsCertKey)
}
// dohServer creates a DoH server.
func dohServer(address string, tlsCert, tlsCertKey string, handler http.Handler) (challengeServer, error) {
return &doh{
&http.Server{
Handler: handler,
Addr: address,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
},
tlsCert,
tlsCertKey,
}, nil
}