Skip to content

Commit

Permalink
fix: make broker URL retrieval method safer
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Oct 5, 2023
1 parent 0f7170a commit 12482fd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,18 @@ export function getBrokerUrl(
): string {
const protocol = getClientTransport(request)
if (!request?.connDetails) {
const address = !request
? (stream as NetSocket)?.address()
: request.socket.address()
let address = {}
if (isNetSocket(stream) && isNetSocketAddress(stream.address())) {
address = stream.address()
} else if (
typeof request?.socket?.address === 'function' &&
isNetSocketAddress(request.socket.address())
) {
address = request.socket.address()
}

return isNetSocketAddress(address)
? `${protocol}://${address?.address}:${address.port}`
? `${protocol}://${address.address}:${address.port}`
: `${protocol}://localhost:1883`
}
const { isTls, isWebsocket, serverIpAddress, serverPort } =
Expand Down

0 comments on commit 12482fd

Please sign in to comment.