Skip to content

Commit

Permalink
DialHost must connect to the requested host
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laurovenancio committed Mar 24, 2023
1 parent 95c4d5d commit 5f78076
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ func (hd *defaultHostDialer) DialHost(ctx context.Context, host *HostInfo) (*Dia
return nil, fmt.Errorf("host missing port: %v", port)
}

addr := host.HostnameAndPort()
conn, err := hd.dialer.DialContext(ctx, "tcp", addr)
connAddr := host.ConnectAddressAndPort()
conn, err := hd.dialer.DialContext(ctx, "tcp", connAddr)
if err != nil {
return nil, err
}
addr := host.HostnameAndPort()
return WrapTLS(ctx, conn, addr, hd.tlsConfig)
}

Expand Down
7 changes: 7 additions & 0 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ func (h *HostInfo) HostnameAndPort() string {
return net.JoinHostPort(h.hostname, strconv.Itoa(h.port))
}

func (h *HostInfo) ConnectAddressAndPort() string {
h.mu.Lock()
defer h.mu.Unlock()
addr, _ := h.connectAddressLocked()
return net.JoinHostPort(addr.String(), strconv.Itoa(h.port))
}

func (h *HostInfo) String() string {
h.mu.RLock()
defer h.mu.RUnlock()
Expand Down

0 comments on commit 5f78076

Please sign in to comment.