Skip to content

Commit bb3c912

Browse files
committed
move proxyFromURL to client
1 parent 85fb2d8 commit bb3c912

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

client.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"net/url"
1818
"strings"
1919
"time"
20+
21+
"golang.org/x/net/proxy"
2022
)
2123

2224
// ErrBadHandshake is returned when the server response to opening handshake is
@@ -282,7 +284,21 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
282284
return nil, nil, err
283285
}
284286
if proxyURL != nil {
285-
netDial, err = proxyFromURL(proxyURL, netDial)
287+
netDial, err = func(proxyURL *url.URL, forwardDial netDialerFunc) (netDialerFunc, error) {
288+
if proxyURL.Scheme == "http" {
289+
return (&httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDial}).DialContext, nil
290+
}
291+
dialer, err := proxy.FromURL(proxyURL, forwardDial)
292+
if err != nil {
293+
return nil, err
294+
}
295+
if d, ok := dialer.(proxy.ContextDialer); ok {
296+
return d.DialContext, nil
297+
}
298+
return func(ctx context.Context, net, addr string) (net.Conn, error) {
299+
return dialer.Dial(net, addr)
300+
}, nil
301+
}(proxyURL, netDial)
286302
if err != nil {
287303
return nil, nil, err
288304
}

proxy.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"net/http"
1515
"net/url"
1616
"strings"
17-
18-
"golang.org/x/net/proxy"
1917
)
2018

2119
type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
@@ -28,22 +26,6 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
2826
return fn(ctx, network, addr)
2927
}
3028

31-
func proxyFromURL(proxyURL *url.URL, forwardDial netDialerFunc) (netDialerFunc, error) {
32-
if proxyURL.Scheme == "http" {
33-
return (&httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDial}).DialContext, nil
34-
}
35-
dialer, err := proxy.FromURL(proxyURL, forwardDial)
36-
if err != nil {
37-
return nil, err
38-
}
39-
if d, ok := dialer.(proxy.ContextDialer); ok {
40-
return d.DialContext, nil
41-
}
42-
return func(ctx context.Context, net, addr string) (net.Conn, error) {
43-
return dialer.Dial(net, addr)
44-
}, nil
45-
}
46-
4729
type httpProxyDialer struct {
4830
proxyURL *url.URL
4931
forwardDial netDialerFunc

0 commit comments

Comments
 (0)