This commit is contained in:
苏尹岚
2020-11-17 11:11:22 +08:00
parent 6c8d05e3c2
commit 9e178e0673
2 changed files with 9 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import (
"crypto/md5"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/xml"
"fmt"
"net/http"
@@ -396,7 +397,7 @@ func New(appID, appKey, mchID string, config ...*platformapi.APIConfig) *API {
}
}
func NewWithCertificate(appID, appKey, mchID string, certPEMBlock, keyPEMBlock interface{}, config ...*platformapi.APIConfig) (a *API) {
func NewWithCertificate(appID, appKey, mchID string, certPEMBlock, keyPEMBlock, rootCa interface{}, config ...*platformapi.APIConfig) (a *API) {
var certs tls.Certificate
var err error
if binCertPEMBlock, ok := certPEMBlock.([]byte); ok {
@@ -405,9 +406,12 @@ func NewWithCertificate(appID, appKey, mchID string, certPEMBlock, keyPEMBlock i
certs, err = tls.LoadX509KeyPair(certPEMBlock.(string), keyPEMBlock.(string))
}
if err == nil {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(rootCa.([]byte))
a = New(appID, appKey, mchID, config...)
a.client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
Certificates: []tls.Certificate{certs},
},
}