无法使用Go中的httputil.ReverseProxy同时使用自定义往返和拨号
我正在使用core net/http/httputil/reverseeproxy库在Go中编写一个反向代理服务器。我已经使用了自定义控制器,我需要为传输定义自己的往返和拨号 我可以像这样使用自定义拨号:无法使用Go中的httputil.ReverseProxy同时使用自定义往返和拨号,go,reverse-proxy,Go,Reverse Proxy,我正在使用core net/http/httputil/reverseeproxy库在Go中编写一个反向代理服务器。我已经使用了自定义控制器,我需要为传输定义自己的往返和拨号 我可以像这样使用自定义拨号: transport := &http.Transport{ Dial: func(network, addr string) (net.Conn, error) { lgr.Println("running custom magic below")
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
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
}
因此,我创建了一个嵌入http.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
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}
问题是,虽然我的自定义往返工作正常,但拨号盘现在停止工作,不再执行自定义魔法
当我记录传输时。分配后立即拨号,我会在内存中看到它的地址。当我记录我的RevereProxy.Transport(上面的函数以res返回)时,我甚至在那里看到了相同的地址:
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)}}
无论如何,似乎调用了core net.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
}
您正在定义自定义传输,但在
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
}
这里有完整的来源:这里有完整的来源:谢谢,这很有意义,现在我用重新定义的拨号盘打电话给我的传输…我最初也注意到这条线,并认为问题是DefaultTransport,但我真的无法想象我还应该打什么电话。谢谢!:-)谢谢,这很有意义,现在我用重新定义的拨号盘呼叫我的传输…我最初也注意到这条线路,并认为问题在于DefaultTransport,但我真的无法想象我还应该呼叫什么。谢谢!:-)