Skip to content

Commit

Permalink
Add ability to define remoteClientId filter as an rlpx option
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Dec 6, 2017
1 parent 5e83564 commit b47bb04
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
10 changes: 6 additions & 4 deletions examples/peer-communication.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const BOOTNODES = require('ethereum-common').bootstrapNodes.filter((node) => {
tcpPort: node.port
}
})
const REMOTE_CLIENTID_FILTER = ['go1.5', 'go1.6', 'go1.7', 'quorum', 'pirl', 'ubiq', 'gmc', 'gwhale', 'prichain']

const CHECK_BLOCK_TITLE = 'Byzantium Fork' // Only for debugging/console output
const CHECK_BLOCK_NR = 4370000
Expand Down Expand Up @@ -49,6 +50,7 @@ const rlpx = new devp2p.RLPx(PRIVATE_KEY, {
devp2p.ETH.eth63,
devp2p.ETH.eth62
],
remoteClientIdFilter: REMOTE_CLIENTID_FILTER,
listenPort: null
})

Expand Down Expand Up @@ -118,7 +120,7 @@ rlpx.on('peer:added', (peer) => {
headers.push(CHECK_BLOCK_HEADER)
}

if (requests.headers.length === 0 && requests.msgTypes[code] >= 5) {
if (requests.headers.length === 0 && requests.msgTypes[code] >= 8) {
peer.disconnect(devp2p.RLPx.DISCONNECT_REASONS.USELESS_PEER)
} else {
eth.sendMessage(devp2p.ETH.MESSAGE_CODES.BLOCK_HEADERS, headers)
Expand Down Expand Up @@ -168,7 +170,7 @@ rlpx.on('peer:added', (peer) => {
break

case devp2p.ETH.MESSAGE_CODES.GET_BLOCK_BODIES:
if (requests.headers.length === 0 && requests.msgTypes[code] >= 5) {
if (requests.headers.length === 0 && requests.msgTypes[code] >= 8) {
peer.disconnect(devp2p.RLPx.DISCONNECT_REASONS.USELESS_PEER)
} else {
eth.sendMessage(devp2p.ETH.MESSAGE_CODES.BLOCK_BODIES, [])
Expand Down Expand Up @@ -211,7 +213,7 @@ rlpx.on('peer:added', (peer) => {
break

case devp2p.ETH.MESSAGE_CODES.GET_NODE_DATA:
if (requests.headers.length === 0 && requests.msgTypes[code] >= 5) {
if (requests.headers.length === 0 && requests.msgTypes[code] >= 8) {
peer.disconnect(devp2p.RLPx.DISCONNECT_REASONS.USELESS_PEER)
} else {
eth.sendMessage(devp2p.ETH.MESSAGE_CODES.NODE_DATA, [])
Expand All @@ -222,7 +224,7 @@ rlpx.on('peer:added', (peer) => {
break

case devp2p.ETH.MESSAGE_CODES.GET_RECEIPTS:
if (requests.headers.length === 0 && requests.msgTypes[code] >= 5) {
if (requests.headers.length === 0 && requests.msgTypes[code] >= 8) {
peer.disconnect(devp2p.RLPx.DISCONNECT_REASONS.USELESS_PEER)
} else {
eth.sendMessage(devp2p.ETH.MESSAGE_CODES.RECEIPTS, [])
Expand Down
4 changes: 2 additions & 2 deletions src/eth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ class ETH extends EventEmitter {
getVersion () {
return this._version
}

_getStatusString (status) {
var sStr = `[V:${buffer2int(status[0])}, NID:${buffer2int(status[1])}, TD:${buffer2int(status[2])}`
sStr += `, BestH:${status[3].toString('hex')}, GenH:${status[4].toString('hex')}]`
return sStr
}

sendStatus (status) {
if (this._status !== null) return
this._status = [
Expand Down
2 changes: 2 additions & 0 deletions src/rlpx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RLPx extends EventEmitter {
this._timeout = options.timeout || ms('10s')
this._maxPeers = options.maxPeers || 10
this._clientId = Buffer.from(options.clientId || `Ethereum Node.js/${pVersion}`)
this._remoteClientIdFilter = options.remoteClientIdFilter
this._capabilities = options.capabilities
this._listenPort = options.listenPort

Expand Down Expand Up @@ -144,6 +145,7 @@ class RLPx extends EventEmitter {

timeout: this._timeout,
clientId: this._clientId,
remoteClientIdFilter: this._remoteClientIdFilter,
capabilities: this._capabilities,
port: this._listenPort
})
Expand Down
12 changes: 11 additions & 1 deletion src/rlpx/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Peer extends EventEmitter {
this._capabilities = options.capabilities
this._port = options.port
this._id = options.id
this._remoteClientIdFilter = options.remoteClientIdFilter

// ECIES session
this._remoteId = options.remoteId
Expand Down Expand Up @@ -165,10 +166,11 @@ class Peer extends EventEmitter {
}

const obj = this._getProtocol(code)
if (obj === undefined) return this.disconnect(Peer.DISCONNECT_REASONS.PROTOCOL_ERROR)

const msgCode = code - obj.offset
const prefix = this.getMsgPrefix(msgCode)
debug(`Received ${prefix} (message code: ${code} - ${obj.offset} = ${msgCode}) ${this._socket.remoteAddress}:${this._socket.remotePort}`)
if (obj === undefined) return this.disconnect(Peer.DISCONNECT_REASONS.PROTOCOL_ERROR)

try {
obj.protocol._handleMessage(msgCode, body.slice(1))
Expand Down Expand Up @@ -208,6 +210,14 @@ class Peer extends EventEmitter {
return this.disconnect(Peer.DISCONNECT_REASONS.INVALID_IDENTITY)
}

if (this._remoteClientIdFilter) {
for (let filterStr of this._remoteClientIdFilter) {
if (this._hello.clientId.toLowerCase().includes(filterStr.toLowerCase())) {
return this.disconnect(Peer.DISCONNECT_REASONS.USELESS_PEER)
}
}
}

const shared = {}
for (let item of this._hello.capabilities) {
for (let obj of this._capabilities) {
Expand Down

0 comments on commit b47bb04

Please sign in to comment.