mirror of https://github.com/dexidp/dex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
|
4 years ago
|
package httpclient
|
||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/tls"
|
||
|
|
"crypto/x509"
|
||
|
|
"fmt"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, error) {
|
||
|
|
pool, err := x509.SystemCertPool()
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
tlsConfig := tls.Config{RootCAs: pool, InsecureSkipVerify: insecureSkipVerify}
|
||
|
|
for _, rootCA := range rootCAs {
|
||
|
|
rootCABytes, err := os.ReadFile(rootCA)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to read root-ca: %v", err)
|
||
|
|
}
|
||
|
|
if !tlsConfig.RootCAs.AppendCertsFromPEM(rootCABytes) {
|
||
|
|
return nil, fmt.Errorf("no certs found in root CA file %q", rootCA)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return &http.Client{
|
||
|
|
Transport: &http.Transport{
|
||
|
|
TLSClientConfig: &tlsConfig,
|
||
|
|
Proxy: http.ProxyFromEnvironment,
|
||
|
|
DialContext: (&net.Dialer{
|
||
|
|
Timeout: 30 * time.Second,
|
||
|
|
KeepAlive: 30 * time.Second,
|
||
|
|
DualStack: true,
|
||
|
|
}).DialContext,
|
||
|
|
MaxIdleConns: 100,
|
||
|
|
IdleConnTimeout: 90 * time.Second,
|
||
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
||
|
|
ExpectContinueTimeout: 1 * time.Second,
|
||
|
|
},
|
||
|
|
}, nil
|
||
|
|
}
|