-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
154 lines (116 loc) · 3.01 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
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
package shttp
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"os"
"time"
"github.com/galdor/go-ejson"
"github.com/galdor/go-log"
"github.com/galdor/go-service/pkg/utils"
)
const (
DefaultClientConnectionTimeout = 30 // seconds
DefaultClientRequestTimeout = 30 // seconds
)
type ClientCfg struct {
Log *log.Logger `json:"-"`
ConnectionTimeout *int `json:"connection_timeout"` // seconds
RequestTimeout *int `json:"request_timeout"` // seconds
LogRequests bool `json:"log_requests"`
DisableRedirections bool `json:"disable_redirections"`
TLS *TLSClientCfg `json:"tls"`
Header http.Header `json:"-"`
}
type TLSClientCfg struct {
CACertificates []string `json:"ca_certificates"`
}
type Client struct {
Cfg ClientCfg
Log *log.Logger
Client *http.Client
tlsCfg *tls.Config
}
func (cfg *ClientCfg) ValidateJSON(v *ejson.Validator) {
}
func NewClient(cfg ClientCfg) (*Client, error) {
if cfg.ConnectionTimeout == nil {
cfg.ConnectionTimeout = utils.Ref(DefaultClientConnectionTimeout)
}
if cfg.RequestTimeout == nil {
cfg.RequestTimeout = utils.Ref(DefaultClientRequestTimeout)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Duration(*cfg.ConnectionTimeout) * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 60 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
tlsCfg := &tls.Config{}
if cfg.TLS != nil {
caCertificatePool, err := LoadCertificates(cfg.TLS.CACertificates)
if err != nil {
return nil, err
}
tlsCfg.RootCAs = caCertificatePool
}
client := &http.Client{
Timeout: time.Duration(*cfg.RequestTimeout) * time.Second,
Transport: NewRoundTripper(transport, &cfg),
}
if cfg.DisableRedirections {
client.CheckRedirect =
func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
c := &Client{
Cfg: cfg,
Log: cfg.Log,
Client: client,
tlsCfg: tlsCfg,
}
transport.DialTLSContext = c.DialTLSContext
return c, nil
}
func (c *Client) CloseConnections() {
c.Client.CloseIdleConnections()
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.Client.Do(req)
}
func (c *Client) DialTLSContext(ctx context.Context, network, address string) (net.Conn, error) {
dialer := &tls.Dialer{
NetDialer: &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
},
Config: c.tlsCfg,
}
conn, err := dialer.DialContext(ctx, network, address)
if err != nil {
return nil, err
}
return conn, nil
}
func LoadCertificates(certificates []string) (*x509.CertPool, error) {
pool := x509.NewCertPool()
for _, certificate := range certificates {
data, err := os.ReadFile(certificate)
if err != nil {
return nil, fmt.Errorf("cannot read %q: %w", certificate, err)
}
if pool.AppendCertsFromPEM(data) == false {
return nil, fmt.Errorf("cannot load certificates from %q",
certificate)
}
}
return pool, nil
}