Преглед на файлове

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	utils/logger/logger.go
marion преди 5 години
родител
ревизия
599e367c2c
променени са 2 файла, в които са добавени 26 реда и са изтрити 4 реда
  1. 23 3
      utils/http_utils.go
  2. 3 1
      utils/logger/logger.go

+ 23 - 3
utils/http_utils.go

@@ -5,6 +5,7 @@ import (
 	"compress/gzip"
 	"compress/zlib"
 	"context"
+	"crypto/tls"
 	"fmt"
 	"golang.org/x/net/proxy"
 	"io"
@@ -88,6 +89,7 @@ type RequestPromise struct {
 	proxy       func(*http.Request) (*url.URL, error)
 	dialContext func(ctx context.Context, network, addr string) (net.Conn, error)
 	client      *http.Client
+	isSkipTls   bool
 }
 
 // 返回一个http请求配置对象,默认带上压缩头
@@ -127,6 +129,12 @@ func NewPoolingHttpClient() *http.Client {
 	}
 }
 
+// 设置https忽略本地证书校验
+func (r *RequestPromise) SetSkipTls() *RequestPromise {
+	r.isSkipTls = true
+	return r
+}
+
 // 设置http header
 func (r *RequestPromise) SetHeader(key string, value string) *RequestPromise {
 	if len(strings.TrimSpace(key)) == 0 {
@@ -238,11 +246,23 @@ func (r *RequestPromise) initClient() {
 	if nil == r.client { // create new http client instance
 		r.client = &http.Client{Timeout: DefaultHttpTimeout} // default timeout
 	}
-	if r.timeout != 0 {
+	if r.timeout < 0 {
+		r.timeout = DefaultHttpTimeout // default timeout
+	}
+	if r.timeout > 0 {
 		r.client.Timeout = r.timeout
 	}
-	if r.client.Timeout < 0 {
-		r.client.Timeout = 0
+	if r.isSkipTls {
+		if nil == r.client.Transport {
+			r.client.Transport = &http.Transport{}
+		}
+		transport := (r.client.Transport).(*http.Transport)
+		transport.TLSClientConfig = &tls.Config{
+			InsecureSkipVerify: true,
+			//VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
+			//	return nil
+			//},
+		}
 	}
 	if nil != r.proxy || nil != r.dialContext {
 		if nil == r.client.Transport {

+ 3 - 1
utils/logger/logger.go

@@ -7,7 +7,6 @@ import (
 	"github.com/rifflock/lfshook"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/viper"
-	"gopkg.in/gemnasium/logrus-graylog-hook.v2"
 	"os"
 	"reflect"
 	"regexp"
@@ -358,6 +357,9 @@ func (logger *Logger) getSql(originSql string, params []interface{}) string {
 			formattedValues = append(formattedValues, "NULL")
 		}
 	}
+	if nil == formattedValues {
+		return ""
+	}
 
 	var sql string
 	// differentiate between $n placeholders or else treat like ?