]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use DialContext for tcp again
authorMatt Joiner <anacrolix@gmail.com>
Fri, 15 Jun 2018 04:34:58 +0000 (14:34 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 15 Jun 2018 04:34:58 +0000 (14:34 +1000)
It got lost somewhere along the way.

socket.go

index e7d54fb0e5cc5532939031f2df286f3d882e63b3..33a19bc242e0afe3bb170f62b6bce849adf4b667 100644 (file)
--- a/socket.go
+++ b/socket.go
@@ -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 {