-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathclient.go
More file actions
159 lines (139 loc) · 5.02 KB
/
client.go
File metadata and controls
159 lines (139 loc) · 5.02 KB
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
package acme
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
)
// Client is the interface used to verify ACME challenges.
type Client interface {
// Get issues an HTTP GET to the specified URL.
Get(url string) (*http.Response, error)
// LookupTXT returns the DNS TXT records for the given domain name.
LookupTxt(name string) ([]string, error)
// TLSDial connects to the given network address using net.Dialer and then
// initiates a TLS handshake, returning the resulting TLS connection.
TLSDial(network, addr string, config *tls.Config) (*tls.Conn, error)
}
type clientKey struct{}
// NewClientContext adds the given client to the context.
func NewClientContext(ctx context.Context, c Client) context.Context {
return context.WithValue(ctx, clientKey{}, c)
}
// ClientFromContext returns the current client from the given context.
func ClientFromContext(ctx context.Context) (c Client, ok bool) {
c, ok = ctx.Value(clientKey{}).(Client)
return
}
// MustClientFromContext returns the current client from the given context. It will
// return a new instance of the client if it does not exist.
func MustClientFromContext(ctx context.Context) Client {
c, ok := ClientFromContext(ctx)
if !ok {
return NewClient()
}
return c
}
type client struct {
http *http.Client
dialer *net.Dialer
// resolver is used for DNS lookups; defaults to net.DefaultResolver
resolver *net.Resolver
}
// ClientOption configures the ACME client.
type ClientOption func(*client)
// WithProxyURL configures the HTTP(S) proxy to use for ACME HTTP requests.
// Example: WithProxyURL("http://proxy.local:3128") or WithProxyURL("socks5://...").
func WithProxyURL(proxyURL string) ClientOption {
return func(c *client) {
if tr, ok := c.http.Transport.(*http.Transport); ok {
if u, err := url.Parse(proxyURL); err == nil {
tr.Proxy = http.ProxyURL(u)
}
}
}
}
// WithProxyFunc sets a custom proxy selection function, overriding environment variables.
func WithProxyFunc(fn func(*http.Request) (*url.URL, error)) ClientOption {
return func(c *client) {
if tr, ok := c.http.Transport.(*http.Transport); ok {
tr.Proxy = fn
}
}
}
// WithResolver sets a custom DNS resolver to be used for both TXT lookups and dialing.
func WithResolver(r *net.Resolver) ClientOption {
return func(c *client) {
c.resolver = r
c.dialer.Resolver = r
}
}
// WithDNS configures the client to use a specific DNS server for all lookups and dialing.
// The address should be in host:port form, e.g. "8.8.8.8:53".
func WithDNS(addr string) ClientOption {
return func(c *client) {
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := &net.Dialer{Timeout: 5 * time.Second}
return d.DialContext(ctx, network, addr)
},
}
c.resolver = r
c.dialer.Resolver = r
}
}
// NewClientWithOptions returns an implementation of Client for verifying ACME challenges.
// It accepts optional ClientOptions to override proxy and DNS resolver behavior.
func NewClientWithOptions(opts ...ClientOption) Client {
d := &net.Dialer{Timeout: 30 * time.Second}
// Default transport uses environment proxy and our dialer so that custom resolver applies to HTTP too.
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: d.DialContext,
TLSClientConfig: &tls.Config{
//nolint:gosec // used on tls-alpn-01 challenge
InsecureSkipVerify: true, // lgtm[go/disabled-certificate-check]
},
}
c := &client{
http: &http.Client{
Timeout: 30 * time.Second,
Transport: tr,
},
dialer: d,
resolver: net.DefaultResolver,
}
// Apply options
for _, opt := range opts {
opt(c)
}
// Ensure transport dialer is bound (in case options replaced dialer.resolver)
if tr2, ok := c.http.Transport.(*http.Transport); ok {
tr2.DialContext = c.dialer.DialContext
}
return c
}
// NewClient returns an implementation of Client with default settings
// (proxy from environment and system DNS resolver). For custom configuration
// use NewClientWithOptions.
func NewClient(opts ...ClientOption) Client { // keep signature source-compatible for callers without options
return NewClientWithOptions(opts...)
}
func (c *client) Get(url string) (*http.Response, error) {
return c.http.Get(url)
}
func (c *client) LookupTxt(name string) ([]string, error) {
// Prefer custom resolver with a bounded timeout
if c.resolver != nil {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return c.resolver.LookupTXT(ctx, name)
}
return net.LookupTXT(name)
}
func (c *client) TLSDial(network, addr string, config *tls.Config) (*tls.Conn, error) {
return tls.DialWithDialer(c.dialer, network, addr, config)
}