diff options
author | Julen Garcia Leunda <hicom150@gmail.com> | 2021-07-06 19:51:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-06 19:51:09 +0300 |
commit | 100a2aebe23420dd70842b3948896f8fecfee235 (patch) | |
tree | 6e651b5a6cd359c83ecaf3a148661b08221ff578 /lib/torrent.js | |
parent | 149c12d73725c07b86cb3498290f5a6953280559 (diff) |
fix: ensure uTP peer address is IPv4 (#2125)
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index b46cde3..8b96e14 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -762,8 +762,9 @@ class Torrent extends EventEmitter { if (this.destroyed) throw new Error('torrent is destroyed') if (!this.infoHash) throw new Error('addPeer() must not be called before the `infoHash` event') + let host + if (this.client.blocked) { - let host if (typeof peer === 'string') { let parts try { @@ -787,7 +788,10 @@ class Torrent extends EventEmitter { } // if the utp connection fails to connect, then it is replaced with a tcp connection to the same ip:port - const wasAdded = !!this._addPeer(peer, this.client.utp ? 'utp' : 'tcp') + + const type = (this.client.utp && this._isIPv4(host)) ? 'utp' : 'tcp' + const wasAdded = !!this._addPeer(peer, type) + if (wasAdded) { this.emit('peer', peer) } else { @@ -1800,7 +1804,9 @@ class Torrent extends EventEmitter { const reconnectTimeout = setTimeout(() => { if (this.destroyed) return - const newPeer = this._addPeer(peer.addr, this.client.utp ? 'utp' : 'tcp') + const host = addrToIPPort(peer.addr)[0] + const type = (this.client.utp && this._isIPv4(host)) ? 'utp' : 'tcp' + const newPeer = this._addPeer(peer.addr, type) if (newPeer) newPeer.retries = peer.retries + 1 }, ms) if (reconnectTimeout.unref) reconnectTimeout.unref() @@ -1824,6 +1830,16 @@ class Torrent extends EventEmitter { return port > 0 && port < 65535 && !(host === '127.0.0.1' && port === this.client.torrentPort) } + + /** + * Return `true` if string is a valid IPv4 address. + * @param {string} addr + * @return {boolean} + */ + _isIPv4 (addr) { + const IPv4Pattern = /^((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/ + return IPv4Pattern.test(addr) + } } function getBlockPipelineLength (wire, duration) { |