-
Notifications
You must be signed in to change notification settings - Fork 5
/
http_client.go
52 lines (45 loc) · 1002 Bytes
/
http_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
package wxpay
import (
"crypto/tls"
"encoding/pem"
"net/http"
"time"
"golang.org/x/crypto/pkcs12"
)
var client = &http.Client{
Timeout: 60 * time.Second,
}
var tlsClient = &http.Client{
Timeout: 60 * time.Second,
}
func selectedClient(url string) *http.Client {
switch url {
case refundURL, reverseURL, transferURL, transferInfoURL, downloadFundFlowURL:
return tlsClient
default:
return client
}
}
// SetTLSClient tls client
func SetTLSClient(pfxData []byte, password string) error {
blocks, err := pkcs12.ToPEM(pfxData, password)
if err != nil {
globalLogger.printf("ToPEM err: %v", err)
return err
}
var pemData []byte
for _, b := range blocks {
pemData = append(pemData, pem.EncodeToMemory(b)...)
}
cert, err := tls.X509KeyPair(pemData, pemData)
if err != nil {
globalLogger.printf("X509KeyPair err: %v", err)
return err
}
tlsClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
return nil
}