3 changed files with 118 additions and 4 deletions
@ -0,0 +1,69 @@
|
||||
/* |
||||
------------------------------------------------------------------------------------------------------------------------ |
||||
####### 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 |
||||
} |
||||
|
||||
/* |
||||
######################################################################################################## @(°_°)@ ####### |
||||
*/ |
Loading…
Reference in new issue