1 回答
TA贡献1799条经验 获得超9个赞
可行的。=)
您应该在 Transport 上设置 Dial 和 DialTLS 字段并在那里进行检查。
希望代码是不言自明的,请随时询问。
由于限制,不能在操场上工作!
https://play.golang.org/p/4TkczUEnKn
package main
import (
"fmt"
"net/http"
"net"
"time"
"crypto/tls"
)
func DialTLS(network, addr string) (net.Conn, error) {
conn, err := tls.Dial(network, addr, &tls.Config{
InsecureSkipVerify: true,
})
cs := conn.ConnectionState()
fmt.Println(cs.Version, cs.HandshakeComplete)
// insert your check here!
return conn, err
}
func Dial (network, addr string) (net.Conn, error) {
// Copied from DefaultTransport
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
conn, err := dialer.Dial(network, addr)
fmt.Println("unsecure")
return conn, err
}
func main() {
client := &http.Client{
Timeout: time.Second,
Transport: &http.Transport{
Dial: Dial,
// If DialTLS is set, the Dial hook is not used for HTTPS
// requests and the TLSClientConfig and TLSHandshakeTimeout
// are ignored. The returned net.Conn is assumed to already be
// past the TLS handshake.
// TLSClientConfig: &tls.Config{
// InsecureSkipVerify: true,
// },
DialTLS: DialTLS,
},
}
// Deep in transport it checks "tlsDial := t.DialTLS != nil && cm.targetScheme == "https" && cm.proxyURL == nil"
// https://golang.org/src/net/http/transport.go#L741
resp, err := client.Get("https://www.google.com")
fmt.Println(resp, err, "\n\n")
resp , err = client.Get("http://www.google.com")
fmt.Println(resp, err)
}
UPD
UPD2:下面的代码是个坏主意=)。回退逻辑混淆了 http.Clienterror = Get https://www.google.com: http: server gave HTTP response to HTTPS client或者我遗漏了一些东西,好吧,现在由你决定 =)
甚至更好(取决于您的逻辑)。
func DialTLSWithFallback(network, addr string) (net.Conn, error) {
conn, err := tls.Dial(network, addr, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
// obviously, usecure
// lets try net.Dial
// and return, so check bellow wont trigger on wrong connection type
return &(net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial(network, addr)
}
cs := conn.ConnectionState()
fmt.Println(cs.Version, cs.HandshakeComplete)
// its "secure" connection
// check is it strong enough here!
return conn, err
}
你运输缩小到
Transport: &http.Transport{
Dial: DialTLSWithFallback,
DialTLS: DialTLSWithFallback,
}
- 1 回答
- 0 关注
- 191 浏览
添加回答
举报
