-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdotls.go
60 lines (50 loc) · 1.45 KB
/
dotls.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
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// DNS-over-TLS implementation
//
package dnscore
import (
"context"
"crypto/tls"
"net"
"github.com/miekg/dns"
)
// dialTLSContext is a helper function that dials a network address using the
// given dialer or the default dialer if the given dialer is nil.
func (t *Transport) dialTLSContext(ctx context.Context, network, address string) (net.Conn, error) {
if t.DialTLSContext != nil {
return t.DialTLSContext(ctx, network, address)
}
// Fill in a default TLS config
hostname, _, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
config := &tls.Config{
InsecureSkipVerify: false,
NextProtos: []string{"dot"},
RootCAs: t.RootCAs,
ServerName: hostname,
}
// Defer to the stdlib TLS dialer
dialer := &tls.Dialer{Config: config}
return dialer.DialContext(ctx, network, address)
}
// queryTLS implements [*Transport.Query] for DNS over TLS.
func (t *Transport) queryTLS(ctx context.Context,
addr *ServerAddr, query *dns.Msg) (*dns.Msg, error) {
// 0. immediately fail if the context is already done, which
// is useful to write unit tests
if ctx.Err() != nil {
return nil, ctx.Err()
}
// 1. Dial the TLS connection
conn, err := t.dialTLSContext(ctx, "tcp", addr.Address)
// 2. Handle dialing failure
if err != nil {
return nil, err
}
// 3. Transfer conn ownership and perform the round trip
return t.queryStream(ctx, addr, query, conn)
}