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
package networkdial
import (
"cmp"
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/nettransport"
)
const (
DefaultDialTimeout = 10 * time.Second
DefaultKeepAlive = 30 * time.Second
DefaultHTTPTimeout = 30 * time.Second
DefaultIdleConnTimeout = 90 * time.Second
DefaultHandshakeTimeout = 15 * time.Second
DefaultMaxIdleConns = 100
)
type DialOptions struct {
Hosts Resolver
TLSConfig *tls.Config
AllowSystemDNS bool
}
type Dialer struct {
resolver Resolver
tlsConfig *tls.Config
dialer net.Dialer
allowSystemDNS bool
}
func NewDialer(hosts Resolver, tlsConfig *tls.Config) (*Dialer, error) {
return NewDialerWithOptions(DialOptions{Hosts: hosts, TLSConfig: tlsConfig})
}
func NewDialerWithOptions(opts DialOptions) (*Dialer, error) {
if opts.Hosts == nil {
return nil, ErrNoResolver
}
return &Dialer{
resolver: opts.Hosts,
tlsConfig: cloneTLSConfig(opts.TLSConfig),
dialer: net.Dialer{
Timeout: DefaultDialTimeout,
KeepAlive: DefaultKeepAlive,
},
allowSystemDNS: opts.AllowSystemDNS,
}, nil
}
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if d == nil || d.resolver == nil {
return nil, ErrNoResolver
}
target, err := d.address(address)
if err != nil {
return nil, err
}
conn, err := d.dialer.DialContext(ctx, network, target)
if err != nil {
return nil, fmt.Errorf("networkdial: dialing %s for %s: %w", target, address, err)
}
return conn, nil
}
func (d *Dialer) Resolve(address string) (string, error) {
if d == nil || d.resolver == nil {
return "", ErrNoResolver
}
return d.address(address)
}
func (d *Dialer) TLSConfig() *tls.Config {
if d == nil {
return cloneTLSConfig(nil)
}
return cloneTLSConfig(d.tlsConfig)
}
func (d *Dialer) address(address string) (string, error) {
host, _, err := net.SplitHostPort(address)
if err != nil {
return "", fmt.Errorf("networkdial: %q is not host:port: %w", address, err)
}
if mapped, ok := d.resolver.Address(address); ok {
return mapped, nil
}
if net.ParseIP(host) != nil {
return address, nil
}
if d.allowSystemDNS {
return address, nil
}
return "", fmt.Errorf("%w: %w: %s", ErrUnknownHost, ErrSystemDNSBlocked, address)
}
func NewHTTPTransport(hosts Resolver, tlsConfig *tls.Config) (*http.Transport, error) {
return NewHTTPTransportWithOptions(DialOptions{Hosts: hosts, TLSConfig: tlsConfig})
}
func NewHTTPTransportWithOptions(opts DialOptions) (*http.Transport, error) {
d, err := NewDialerWithOptions(opts)
if err != nil {
return nil, err
}
return nettransport.WithDial(&http.Transport{
TLSClientConfig: d.TLSConfig(),
ForceAttemptHTTP2: true,
MaxIdleConns: DefaultMaxIdleConns,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: DefaultIdleConnTimeout,
TLSHandshakeTimeout: DefaultHandshakeTimeout,
ExpectContinueTimeout: time.Second,
}, d.DialContext), nil
}
func NewHTTPClient(hosts Resolver, tlsConfig *tls.Config) (*http.Client, error) {
return NewHTTPClientWithOptions(DialOptions{Hosts: hosts, TLSConfig: tlsConfig})
}
func NewHTTPClientWithOptions(opts DialOptions) (*http.Client, error) {
tr, err := NewHTTPTransportWithOptions(opts)
if err != nil {
return nil, err
}
return &http.Client{
Transport: tr,
Timeout: DefaultHTTPTimeout,
}, nil
}
func NewNetworkDialer(hosts Resolver, caBundlePEM []byte) (*Dialer, error) {
cfg, err := TLSConfigFromCA(caBundlePEM, "")
if err != nil {
return nil, err
}
return NewDialerWithOptions(DialOptions{Hosts: hosts, TLSConfig: cfg})
}
func NewNetworkHTTPClient(hosts Resolver, caBundlePEM []byte) (*http.Client, error) {
cfg, err := TLSConfigFromCA(caBundlePEM, "")
if err != nil {
return nil, err
}
return NewHTTPClientWithOptions(DialOptions{Hosts: hosts, TLSConfig: cfg})
}
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
if cfg == nil {
return &tls.Config{MinVersion: tls.VersionTLS12}
}
out := cfg.Clone()
out.MinVersion = cmp.Or(out.MinVersion, tls.VersionTLS12)
return out
}