AFAIRE
sdk
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.
sdk/client/endpoint.go

70 lines
1.4 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package client
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"time"
)
type endpoint struct {
endpoint *Endpoint
client *http.Client
}
func buildTransport(ce *Endpoint, tlsCert *tls.Certificate) (http.RoundTripper, error) {
if ce.CA == "" {
return nil, nil
}
buf, err := os.ReadFile(ce.CA)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(buf)
t := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
}
if tlsCert != nil {
t.TLSClientConfig.Certificates = []tls.Certificate{*tlsCert}
}
return t, nil
}
func newEndpoint(cfg *Config, ce *Endpoint, tlsCert *tls.Certificate) (*endpoint, error) {
t, err := buildTransport(ce, tlsCert)
if err != nil {
return nil, err
}
c := &http.Client{
Transport: t,
Timeout: 10 * time.Second, // AFINIR
}
ep := &endpoint{
endpoint: ce,
client: c,
}
return ep, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/