Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

[RLPx] add option to connect to remote hosts from a predefined local port #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
13 changes: 12 additions & 1 deletion src/rlpx/rlpx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface RLPxOptions {
remoteClientIdFilter?: string[]
capabilities: Capabilities[]
listenPort: number | null
connectPort?: number | undefined
}

export class RLPx extends EventEmitter {
Expand All @@ -33,6 +34,7 @@ export class RLPx extends EventEmitter {
_remoteClientIdFilter?: string[]
_capabilities: Capabilities[]
_listenPort: number | null
_connectPort: number | undefined
_dpt: DPT
_peersLRU: LRUCache<string, boolean>
_peersQueue: { peer: PeerInfo; ts: number }[]
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down