我正在使用核心net / http / httputil / ReverseProxy库在Go中编写反向代理服务器。我已经使用了自定义Director,我需要定义自己的RoundTrip和Dial for the Transport。
我可以像这样使用自定义拨号:
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时,我甚至在那里看到相同的地址:
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方法被调用而不是我的自定义拨号,我真的不知道为什么。 :-(任何想法都将受到高度赞赏!谢谢!
答案 0 :(得分:2)
您正在定义自定义传输,但在RoundTrip
上调用http.DefaultTransport
。您需要在嵌入式传输上调用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
}