Skip to content

Commit aa46640

Browse files
committed
fix issue #479
1 parent b65e629 commit aa46640

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

client.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ type Dialer struct {
9999
// If Jar is nil, cookies are not sent in requests and ignored
100100
// in responses.
101101
Jar http.CookieJar
102+
103+
// Custom proxy connect header
104+
ProxyConnectHeader http.Header
102105
}
103106

104107
// Dial creates a new client connection by calling DialContext with a background context.
@@ -274,7 +277,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
274277
return nil, nil, err
275278
}
276279
if proxyURL != nil {
277-
dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
280+
dialer, err := proxy_FromURL(proxyURL, &netDialer{d.ProxyConnectHeader, netDial})
278281
if err != nil {
279282
return nil, nil, err
280283
}

client_server_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ func TestProxyDial(t *testing.T) {
156156

157157
cstDialer := cstDialer // make local copy for modification on next line.
158158
cstDialer.Proxy = http.ProxyURL(surl)
159+
cstDialer.ProxyConnectHeader = map[string][]string{
160+
"User-Agents": {"xxx"},
161+
}
159162

160163
connect := false
161164
origHandler := s.Server.Config.Handler
@@ -166,6 +169,10 @@ func TestProxyDial(t *testing.T) {
166169
if r.Method == "CONNECT" {
167170
connect = true
168171
w.WriteHeader(http.StatusOK)
172+
if r.Header.Get("User-Agents") != "xxx" {
173+
t.Log("xxx not found in the request header")
174+
http.Error(w, "header xxx not found", http.StatusMethodNotAllowed)
175+
}
169176
return
170177
}
171178

proxy.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,29 @@ import (
1414
"strings"
1515
)
1616

17-
type netDialerFunc func(network, addr string) (net.Conn, error)
17+
type netDialer struct {
18+
proxyHeader http.Header
19+
f func(network, addr string) (net.Conn, error)
20+
}
1821

19-
func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
20-
return fn(network, addr)
22+
func (n netDialer) Dial(network, addr string) (net.Conn, error) {
23+
return n.f(network, addr)
2124
}
2225

2326
func init() {
2427
proxy_RegisterDialerType("http", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) {
25-
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial}, nil
28+
p, ok := forwardDialer.(*netDialer)
29+
if !ok {
30+
return nil, errors.New("type assertion failed when ini proxy info")
31+
}
32+
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial, proxyHeader: p.proxyHeader}, nil
2633
})
2734
}
2835

2936
type httpProxyDialer struct {
3037
proxyURL *url.URL
3138
forwardDial func(network, addr string) (net.Conn, error)
39+
proxyHeader http.Header
3240
}
3341

3442
func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
@@ -47,6 +55,10 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
4755
}
4856
}
4957

58+
for k, v := range hpd.proxyHeader {
59+
connectHeader[k] = v
60+
}
61+
5062
connectReq := &http.Request{
5163
Method: "CONNECT",
5264
URL: &url.URL{Opaque: addr},

0 commit comments

Comments
 (0)