mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 06:32:19 +00:00
Per the nodeJS documentation, a Net socket.remoteAddress value may be undefined if the socket is destroyed, as by a client disconnect. A multiaddr cannot be created for an invalid IP address (such as the undefined remote address of a destroyed socket). Currently the attempt results in a crash that can be triggered remotely. This commit catches the exception in get-multiaddr and returns an undefined value to listener rather than throwing an exception when trying to process defective or destroyed socket data. Listener then terminates processing of the incoming p2p connections that generate this error condition. fixes: https://github.com/libp2p/js-libp2p-tcp/issues/93 fixes: https://github.com/ipfs/js-ipfs/issues/1447
34 lines
764 B
JavaScript
34 lines
764 B
JavaScript
'use strict'
|
|
|
|
const multiaddr = require('multiaddr')
|
|
const Address6 = require('ip-address').Address6
|
|
const debug = require('debug')
|
|
const log = debug('libp2p:tcp:get-multiaddr')
|
|
|
|
module.exports = (socket) => {
|
|
let ma
|
|
|
|
try {
|
|
if (socket.remoteFamily === 'IPv6') {
|
|
const addr = new Address6(socket.remoteAddress)
|
|
|
|
if (addr.v4) {
|
|
const ip4 = addr.to4().correctForm()
|
|
ma = multiaddr('/ip4/' + ip4 +
|
|
'/tcp/' + socket.remotePort
|
|
)
|
|
} else {
|
|
ma = multiaddr('/ip6/' + socket.remoteAddress +
|
|
'/tcp/' + socket.remotePort
|
|
)
|
|
}
|
|
} else {
|
|
ma = multiaddr('/ip4/' + socket.remoteAddress +
|
|
'/tcp/' + socket.remotePort)
|
|
}
|
|
} catch (err) {
|
|
log(err)
|
|
}
|
|
return ma
|
|
}
|