Use DialContext for tcp again

It got lost somewhere along the way.
This commit is contained in:
Matt Joiner 2018-06-15 14:34:58 +10:00
parent fd0fb0051b
commit 2fb1c022f5
1 changed files with 19 additions and 9 deletions

View File

@ -54,29 +54,39 @@ func listenTcp(network, address, proxyURL string) (s socket, err error) {
if err != nil {
return
}
defer func() {
if err != nil {
l.Close()
}
}()
// If we don't need the proxy - then we should return default net.Dialer,
// otherwise, let's try to parse the proxyURL and return proxy.Dialer
if len(proxyURL) != 0 {
// TODO: The error should be propagated, as proxy may be in use for
// security or privacy reasons. Also just pass proxy.Dialer in from
// the Config.
if dialer, err := getProxyDialer(proxyURL); err == nil {
return tcpSocket{l, dialer}, nil
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
defer perf.ScopeTimerErr(&err)()
return dialer.Dial(network, addr)
}}, nil
}
}
return tcpSocket{l, nil}, nil
dialer := net.Dialer{}
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
defer perf.ScopeTimerErr(&err)()
return dialer.DialContext(ctx, network, addr)
}}, nil
}
type tcpSocket struct {
net.Listener
d proxy.Dialer
d func(ctx context.Context, addr string) (net.Conn, error)
}
func (me tcpSocket) dial(ctx context.Context, addr string) (net.Conn, error) {
if me.d != nil {
return me.d.Dial(me.Addr().Network(), addr)
}
return net.Dial(me.Addr().Network(), addr)
return me.d(ctx, addr)
}
func setPort(addr string, port int) string {