Skip to content

Commit 5d15f58

Browse files
committed
DialHost must connect to the requested host
When a hostname resolves to multiple hosts, multiple HostInfos are generated. DialHost must connect to the host received as parameter. If the hostname is used to establish the connection, the dns could resolve to another host. The hostname should still be used to verify the TLS connection.
1 parent 1c9ec8f commit 5d15f58

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

dial.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ func (hd *defaultHostDialer) DialHost(ctx context.Context, host *HostInfo) (*Dia
4545
return nil, fmt.Errorf("host missing port: %v", port)
4646
}
4747

48-
addr := host.HostnameAndPort()
49-
conn, err := hd.dialer.DialContext(ctx, "tcp", addr)
48+
connAddr := host.ConnectAddressAndPort()
49+
conn, err := hd.dialer.DialContext(ctx, "tcp", connAddr)
5050
if err != nil {
5151
return nil, err
5252
}
53+
addr := host.HostnameAndPort()
5354
return WrapTLS(ctx, conn, addr, hd.tlsConfig)
5455
}
5556

host_source.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ func (h *HostInfo) HostnameAndPort() string {
401401
return net.JoinHostPort(h.hostname, strconv.Itoa(h.port))
402402
}
403403

404+
func (h *HostInfo) ConnectAddressAndPort() string {
405+
h.mu.Lock()
406+
defer h.mu.Unlock()
407+
addr, _ := h.connectAddressLocked()
408+
return net.JoinHostPort(addr.String(), strconv.Itoa(h.port))
409+
}
410+
404411
func (h *HostInfo) String() string {
405412
h.mu.RLock()
406413
defer h.mu.RUnlock()

0 commit comments

Comments
 (0)