无法在 Go 中与 httputil.ReverseProxy 同时使用自定义 RoundTrip 和 Dial

Unable to use custom RoundTrip and Dial at the same time with httputil.ReverseProxy in Go

我正在使用核心 net/http/httputil/ReverseProxy 库在 Go 中编写反向代理服务器。我已经使用了自定义 Director,我需要为传输定义自己的 RoundTrip 和 Dial。

我可以像这样使用自定义拨号:

transport := &http.Transport{
    Dial: func(network, addr string) (net.Conn, error) {
        lgr.Println("running custom magic below")
        // custom magic
        return net.Dial(network, addr)
    },
}
res := &httputil.ReverseProxy{Director: director, Transport: transport}
return res

所以我创建了一个嵌入 http.Transport 的自定义传输,如下所示:

type customTransport struct {
    http.Transport
}

func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    res, err := http.DefaultTransport.RoundTrip(req)
    lgr.Println("checking the response")
    // check the response
    return res, err
}

并且我尝试以这种方式使用它而不是默认传输:

transport := &customTransport{http.Transport{
    Dial: func(network, addr string) (net.Conn, error) {
        lgr.Println("running custom magic below")
        // custom magic
        return net.Dial(network, addr)
    },
}}
res := &httputil.ReverseProxy{Director: director, Transport: transport}

问题是虽然我的自定义 RoundTrip 工作正常,但 Dial 现在停止工作并且自定义魔法不再起作用。

当我在分配后立即登录 transport.Dial 时,我看到它在内存中的地址。当我在 res 中记录 RevereProxy.Transport 上面的函数 returns 时,我什至在那里看到相同的地址:

fmt.Printf("%#v", transport.Dial)
2016/02/18 12:11:48.401245 main.go:76: (func(string, string) (net.Conn, error))(0x4039a0)
fmt.Printf("%#v", proxy.Transport)
2016/02/18 12:11:48.401403 main.go:100: &main.customTransport{Transport:http.Transport{idleMu:sync.Mutex{state:0, sema:0x0}, wantIdle:false, idleConn:map[http.connectMethodKey][]*http.persistConn(nil), idleConnCh:map[http.connectMethodKey]chan *http.persistConn(nil), reqMu:sync.Mutex{state:0, sema:0x0}, reqCanceler:map[*http.Request]func()(nil), altMu:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0}, writerSem:0x0, readerSem:0x0, readerCount:0, readerWait:0}, altProto:map[string]http.RoundTripper(nil), Proxy:(func(*http.Request) (*url.URL, error))(nil), Dial:(func(string, string) (net.Conn, error))(0x4039a0), DialTLS:(func(string, string) (net.Conn, error))(nil), TLSClientConfig:(*tls.Config)(nil), TLSHandshakeTimeout:0, DisableKeepAlives:false, DisableCompression:false, MaxIdleConnsPerHost:0, ResponseHeaderTimeout:0, ExpectContinueTimeout:0, TLSNextProto:map[string]func(string, *tls.Conn) http.RoundTripper(nil), nextProtoOnce:sync.Once{m:sync.Mutex{state:0, sema:0x0}, done:0x0}, h2transport:(*http.http2Transport)(nil)}}

无论如何,似乎调用了核心 net.Dial 方法而不是我的自定义 Dial,我真的不知道为什么。 :-( 任何想法将不胜感激!谢谢!

您正在定义自定义传输,但在 http.DefaultTransport 上调用 RoundTrip。您需要在嵌入式传输上调用 RoundTrip

type customTransport struct {
    *http.Transport
}

func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    res, err := t.Transport.RoundTrip(req)
    lgr.Println("checking the response")
    // check the response
    return res, err
}

无需自定义 Dial,只需在 TLSClientConfig 中设置 InsecureSkipVerify 选项:

proxy.Transport = &customTransport{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },
}