diff --git a/README.md b/README.md index 1cec79e..cb510b5 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ Creates new RLPx object - `options.remoteClientIdFilter` - Optional list of client ID filter strings (e.g. `['go1.5', 'quorum']`). - `options.capabilities` - Upper layer protocol capabilities, e.g. `[devp2p.ETH.eth63, devp2p.ETH.eth62]`. - `options.listenPort` - The listening port for the server or `null` for default. +- `options.connectPort` - The default local connection port to connect with remote peers with: if `undefined`, connect from a random local port. - `options.dpt` - `DPT` object for the peers to connect to (default: `null`, no `DPT` peer management). #### `rlpx.connect(peer)` (`async`) diff --git a/src/rlpx/rlpx.ts b/src/rlpx/rlpx.ts index 14c65a3..8da873f 100644 --- a/src/rlpx/rlpx.ts +++ b/src/rlpx/rlpx.ts @@ -22,6 +22,7 @@ export interface RLPxOptions { remoteClientIdFilter?: string[] capabilities: Capabilities[] listenPort: number | null + connectPort?: number | undefined } export class RLPx extends EventEmitter { @@ -33,6 +34,7 @@ export class RLPx extends EventEmitter { _remoteClientIdFilter?: string[] _capabilities: Capabilities[] _listenPort: number | null + _connectPort: number | undefined _dpt: DPT _peersLRU: LRUCache _peersQueue: { peer: PeerInfo; ts: number }[] @@ -57,6 +59,7 @@ export class RLPx extends EventEmitter { this._remoteClientIdFilter = options.remoteClientIdFilter this._capabilities = options.capabilities this._listenPort = options.listenPort + this._connectPort = options.connectPort // DPT this._dpt = options.dpt || null @@ -134,7 +137,15 @@ export class RLPx extends EventEmitter { socket.once('error', deferred.reject) socket.setTimeout(this._timeout, () => deferred.reject(new Error('Connection timeout'))) - socket.connect(peer.tcpPort, peer.address, deferred.resolve) + socket.connect( + { + port: peer.tcpPort, + host: peer.address, + // use the connectionPort to connect from + localPort: this._connectPort, + }, + deferred.resolve, + ) await deferred.promise this._onConnect(socket, peer.id)