-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
52 lines (47 loc) · 1.35 KB
/
client.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
package bifrost
import (
"crypto/tls"
"crypto/x509"
"io"
"net/http"
"net/url"
)
// Hosts is a map of hostnames to backend URLs.
type Hosts map[string]*url.URL
type mappedHostTransport struct {
hosts Hosts
transport http.RoundTripper
}
func (t *mappedHostTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if u, ok := t.hosts[req.Host]; ok {
req.URL.Scheme = u.Scheme
req.URL.Host = u.Host
path, err := url.JoinPath(u.Path, req.URL.Path)
if err != nil {
return nil, err
}
req.URL.Path = "/" + path
}
return t.transport.RoundTrip(req)
}
// HTTPClient returns a http.Client configured for mTLS with client authentication.
// The client will use the provided Hosts map to map hostnames to backend URLs.
// Client certificates are loaded from clientCert and rootCAs is used to
// validate the server certificate.
// If ssllog is not nil, the client will log TLS key material to it.
func HTTPClient(h Hosts, clientCert *tls.Certificate, rootCAs *x509.CertPool, ssllog io.Writer) *http.Client {
return &http.Client{
Transport: &mappedHostTransport{
hosts: h,
transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
ForceAttemptHTTP2: true,
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{*clientCert},
RootCAs: rootCAs,
KeyLogWriter: ssllog,
},
},
},
}
}