mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add tls configuration for ghttp.Client
This commit is contained in:
@ -245,12 +245,57 @@ func Test_Duration(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_UnmarshalValue(t *testing.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gvar.Var
|
||||
}
|
||||
func Test_UnmarshalJson(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gvar.Var
|
||||
}
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "v",
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.String(), "v")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
Var gvar.Var
|
||||
}
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "v",
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.String(), "v")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_UnmarshalValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
Var *gvar.Var
|
||||
}
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "v",
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Var.String(), "v")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type V struct {
|
||||
Name string
|
||||
Var gvar.Var
|
||||
}
|
||||
var v *V
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
|
||||
@ -8,9 +8,12 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/gogf/gf"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"golang.org/x/net/proxy"
|
||||
"net"
|
||||
@ -198,8 +201,8 @@ func (c *Client) SetProxy(proxyURL string) {
|
||||
return
|
||||
}
|
||||
if _proxy.Scheme == "http" {
|
||||
if _, ok := c.Transport.(*http.Transport); ok {
|
||||
c.Transport.(*http.Transport).Proxy = http.ProxyURL(_proxy)
|
||||
if v, ok := c.Transport.(*http.Transport); ok {
|
||||
v.Proxy = http.ProxyURL(_proxy)
|
||||
}
|
||||
} else {
|
||||
var auth = &proxy.Auth{}
|
||||
@ -227,11 +230,55 @@ func (c *Client) SetProxy(proxyURL string) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, ok := c.Transport.(*http.Transport); ok {
|
||||
c.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
|
||||
if v, ok := c.Transport.(*http.Transport); ok {
|
||||
v.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
|
||||
return dialer.Dial(network, addr)
|
||||
}
|
||||
}
|
||||
//c.SetTimeout(10*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// SetTlsKeyCrt sets the certificate and key file for TLS configuration of client.
|
||||
func (c *Client) SetTLSKeyCrt(crtFile, keyFile string) error {
|
||||
tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if v, ok := c.Transport.(*http.Transport); ok {
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
v.TLSClientConfig = tlsConfig
|
||||
return nil
|
||||
}
|
||||
return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`)
|
||||
}
|
||||
|
||||
// SetTlsConfig sets the TLS configuration of client.
|
||||
func (c *Client) SetTLSConfig(tlsConfig *tls.Config) error {
|
||||
if v, ok := c.Transport.(*http.Transport); ok {
|
||||
v.TLSClientConfig = tlsConfig
|
||||
return nil
|
||||
}
|
||||
return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`)
|
||||
}
|
||||
|
||||
// LoadKeyCrt creates and returns a TLS configuration object with given certificate and key files.
|
||||
func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error) {
|
||||
crtPath, err := gfile.Search(crtFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyPath, err := gfile.Search(keyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
crt, err := tls.LoadX509KeyPair(crtPath, keyPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig := &tls.Config{}
|
||||
tlsConfig.Certificates = []tls.Certificate{crt}
|
||||
tlsConfig.Time = time.Now
|
||||
tlsConfig.Rand = rand.Reader
|
||||
return tlsConfig, nil
|
||||
}
|
||||
|
||||
@ -258,8 +258,8 @@ func (c *Client) prepareRequest(method, url string, data ...interface{}) (req *h
|
||||
}
|
||||
}
|
||||
// It's necessary set the req.Host if you want to custom the host value of the request.
|
||||
// It uses the "Host" value from header if it's not set in the request.
|
||||
if host := req.Header.Get("Host"); host != "" && req.Host == "" {
|
||||
// It uses the "Host" value from header if it's not empty.
|
||||
if host := req.Header.Get("Host"); host != "" {
|
||||
req.Host = host
|
||||
}
|
||||
// Custom Cookie.
|
||||
|
||||
Reference in New Issue
Block a user