forked from buffrr/letsdane
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
167 lines (143 loc) · 4.05 KB
/
dialer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package sane
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"time"
"github.com/miekg/dns"
"github.com/randomlogin/sane/resolver"
)
type dialer struct {
net net.Dialer
resolver resolver.Resolver
}
var errBadHost = errors.New("bad host")
type addrList struct {
Host string
Port string
IPs []net.IP
}
func newDialer() *dialer {
return &dialer{
net: net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second,
},
}
}
// dialTLSContext attempts to connect to one of the dst addresses and initiates a TLS
// handshake, returning the resulting TLS connection.
func (d *dialer) dialTLSContext(ctx context.Context, network string, dst *addrList, config *tls.Config) (*tls.Conn, error) {
tlsDialer := &tls.Dialer{
NetDialer: &d.net,
Config: config,
}
for _, ip := range dst.IPs {
ipaddr := net.JoinHostPort(ip.String(), dst.Port)
conn, err := tlsDialer.DialContext(ctx, network, ipaddr)
if err != nil {
if err, ok := err.(*tlsError); ok {
return nil, err
}
continue
}
return conn.(*tls.Conn), nil
}
return nil, fmt.Errorf("could not reach any of %v", dst.IPs)
}
// dialContext attempts to connect to the given named address.
func (d *dialer) dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
addrs, err := d.resolveAddr(ctx, addr)
if err != nil {
return nil, err
}
return d.dialAddrList(ctx, network, addrs)
}
// dialAddrList attempts to connect to one of the dst addresses
func (d *dialer) dialAddrList(ctx context.Context, network string, dst *addrList) (net.Conn, error) {
for _, ip := range dst.IPs {
ipaddr := net.JoinHostPort(ip.String(), dst.Port)
conn, err := d.net.DialContext(ctx, network, ipaddr)
if err != nil {
continue
}
return conn, nil
}
return nil, fmt.Errorf("could not reach any of %v", dst.IPs)
}
// resolveAddr resolves the named address by performing a dns lookup returning a list
// of ipv4 and ipv6 addresses
func (d *dialer) resolveAddr(ctx context.Context, addr string) (addrs *addrList, err error) {
addrs = &addrList{}
addrs.Host, addrs.Port, err = net.SplitHostPort(addr)
if err != nil {
return
}
addrs.IPs, _, err = d.resolver.LookupIP(ctx, "ip4", addrs.Host)
if err != nil {
return
}
if len(addrs.IPs) == 0 {
err = fmt.Errorf("%s no such host", addr)
return
}
return
}
// resolveDANE resolves the given host by performing a dns lookup returning
// an address list of ipv4 and ipv6 addresses and TLSA resource records.
func (d *dialer) resolveDANE(ctx context.Context, network, host string, constraints map[string]struct{}) (addrs *addrList, tlsa []*dns.TLSA, err error) {
addrs = &addrList{}
tlsa = []*dns.TLSA{}
addrs.Host, addrs.Port, err = net.SplitHostPort(host)
if err != nil || addrs.Host == "" || addrs.Port == "" {
return nil, nil, errBadHost
}
if ip := net.ParseIP(addrs.Host); ip != nil {
addrs.IPs = []net.IP{ip}
return
}
done := make(chan struct{})
var tlsaErr, ipErr error
go func() {
addrs.IPs, _, ipErr = d.resolver.LookupIP(ctx, "ip", addrs.Host)
done <- struct{}{}
}()
if constraints == nil || !inConstraints(constraints, addrs.Host) {
var secure bool
tlsa, secure, tlsaErr = d.resolver.LookupTLSA(ctx, addrs.Port, network, addrs.Host)
if !secure {
tlsa = []*dns.TLSA{}
}
}
<-done
if ipErr != nil {
err = ipErr
return
}
err = tlsaErr
return
}
// httpOnlyRoundTripper creates a round tripper used for http requests (fails on https requests)
func httpOnlyRoundTripper(d *dialer) http.RoundTripper {
return &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, err error) {
return d.dialContext(ctx, network, addr)
},
DialTLSContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return nil, fmt.Errorf("dial tls for host %s not supported", address)
},
}
}
// tlsaSupported checks if there is a supported DANE usage
// from the given TLSA records. currently checks for usage EE(3).
func tlsaSupported(rrs []*dns.TLSA) bool {
for _, rr := range rrs {
if rr.Usage == 3 {
return true
}
}
return false
}