forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.ts
584 lines (522 loc) · 15.4 KB
/
peer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import { EventEmitter } from 'events'
import * as rlp from 'rlp'
import * as util from '../util'
import BufferList = require('bl')
import ms from 'ms'
import { debug as createDebugLogger } from 'debug'
import Common from '@ethereumjs/common'
import { ECIES } from './ecies'
import { ETH, LES } from '../'
import { int2buffer, buffer2int, formatLogData } from '../util'
import { Socket } from 'net'
const debug = createDebugLogger('devp2p:rlpx:peer')
const verbose = createDebugLogger('verbose').enabled
export const BASE_PROTOCOL_VERSION = 4
export const BASE_PROTOCOL_LENGTH = 16
export const PING_INTERVAL = ms('15s')
export enum PREFIXES {
HELLO = 0x00,
DISCONNECT = 0x01,
PING = 0x02,
PONG = 0x03,
}
export enum DISCONNECT_REASONS {
DISCONNECT_REQUESTED = 0x00,
NETWORK_ERROR = 0x01,
PROTOCOL_ERROR = 0x02,
USELESS_PEER = 0x03,
TOO_MANY_PEERS = 0x04,
ALREADY_CONNECTED = 0x05,
INCOMPATIBLE_VERSION = 0x06,
INVALID_IDENTITY = 0x07,
CLIENT_QUITTING = 0x08,
UNEXPECTED_IDENTITY = 0x09,
SAME_IDENTITY = 0x0a,
TIMEOUT = 0x0b,
SUBPROTOCOL_ERROR = 0x10,
}
export type HelloMsg = {
0: Buffer
1: Buffer
2: Buffer[][]
3: Buffer
4: Buffer
length: 5
}
export interface ProtocolDescriptor {
protocol?: any
offset: number
length?: number
}
export interface ProtocolConstructor {
new (...args: any[]): any
}
export interface Capabilities {
name: string
version: number
length: number
constructor: ProtocolConstructor
}
export interface Hello {
protocolVersion: number
clientId: string
capabilities: Capabilities[]
port: number
id: Buffer
}
export class Peer extends EventEmitter {
_clientId: Buffer
_capabilities?: Capabilities[]
_common: Common
_port: number
_id: Buffer
_remoteClientIdFilter: any
_remoteId: Buffer
_EIP8: Buffer
_eciesSession: ECIES
_state: string
_weHello: HelloMsg | null
_hello: Hello | null
_nextPacketSize: number
_socket: Socket
_socketData: BufferList
_pingIntervalId: NodeJS.Timeout | null
_pingTimeoutId: NodeJS.Timeout | null
_closed: boolean
_connected: boolean
_disconnectReason?: DISCONNECT_REASONS
_disconnectWe: any
_pingTimeout: number
_protocols: ProtocolDescriptor[]
constructor(options: any) {
super()
// hello data
this._clientId = options.clientId
this._capabilities = options.capabilities
this._common = options.common
this._port = options.port
this._id = options.id
this._remoteClientIdFilter = options.remoteClientIdFilter
// ECIES session
this._remoteId = options.remoteId
this._EIP8 = options.EIP8 !== undefined ? options.EIP8 : true
this._eciesSession = new ECIES(options.privateKey, this._id, this._remoteId)
// Auth, Ack, Header, Body
this._state = 'Auth'
this._weHello = null
this._hello = null
this._nextPacketSize = 307
// socket
this._socket = options.socket
this._socketData = new BufferList()
this._socket.on('data', this._onSocketData.bind(this))
this._socket.on('error', (err: Error) => this.emit('error', err))
this._socket.once('close', this._onSocketClose.bind(this))
this._connected = false
this._closed = false
this._disconnectWe = null
this._pingIntervalId = null
this._pingTimeout = options.timeout
this._pingTimeoutId = null
// sub-protocols
this._protocols = []
// send AUTH if outgoing connection
if (this._remoteId !== null) {
this._sendAuth()
}
}
/**
* Send AUTH message
*/
_sendAuth() {
if (this._closed) return
debug(
`Send auth (EIP8: ${this._EIP8}) to ${this._socket.remoteAddress}:${this._socket.remotePort}`
)
if (this._EIP8) {
const authEIP8 = this._eciesSession.createAuthEIP8()
if (!authEIP8) return
this._socket.write(authEIP8)
} else {
const authNonEIP8 = this._eciesSession.createAuthNonEIP8()
if (!authNonEIP8) return
this._socket.write(authNonEIP8)
}
this._state = 'Ack'
this._nextPacketSize = 210
}
/**
* Send ACK message
*/
_sendAck() {
if (this._closed) return
debug(
`Send ack (EIP8: ${this._eciesSession._gotEIP8Auth}) to ${this._socket.remoteAddress}:${this._socket.remotePort}`
)
if (this._eciesSession._gotEIP8Auth) {
const ackEIP8 = this._eciesSession.createAckEIP8()
if (!ackEIP8) return
this._socket.write(ackEIP8)
} else {
const ackOld = this._eciesSession.createAckOld()
if (!ackOld) return
this._socket.write(ackOld)
}
this._state = 'Header'
this._nextPacketSize = 32
this._sendHello()
}
/**
* Create message HEADER and BODY and send to socket
* Also called from SubProtocol context
* @param code
* @param data
*/
_sendMessage(code: number, data: Buffer) {
if (this._closed) return false
const msg = Buffer.concat([rlp.encode(code), data])
const header = this._eciesSession.createHeader(msg.length)
if (!header || this._socket.destroyed) return
this._socket.write(header)
const body = this._eciesSession.createBody(msg)
// this._socket.destroyed added here and above to safeguard against
// occasional "Cannot call write after a stream was destroyed" errors.
// Eventually this can be caught earlier down the line.
if (!body || this._socket.destroyed) return
this._socket.write(body)
return true
}
/**
* Send HELLO message
*/
_sendHello() {
debug(`Send HELLO to ${this._socket.remoteAddress}:${this._socket.remotePort}`)
const payload: HelloMsg = [
int2buffer(BASE_PROTOCOL_VERSION),
this._clientId,
this._capabilities!.map((obj: any) => [Buffer.from(obj.name), int2buffer(obj.version)]),
this._port === null ? Buffer.allocUnsafe(0) : int2buffer(this._port),
this._id,
]
if (!this._closed) {
if (this._sendMessage(PREFIXES.HELLO, rlp.encode(payload as any))) {
this._weHello = payload
}
if (this._hello) {
this.emit('connect')
}
}
}
/**
* Send DISCONNECT message
* @param reason
*/
_sendDisconnect(reason: DISCONNECT_REASONS) {
debug(
`Send DISCONNECT to ${this._socket.remoteAddress}:${
this._socket.remotePort
} (reason: ${this.getDisconnectPrefix(reason)})`
)
const data = rlp.encode(reason)
if (!this._sendMessage(PREFIXES.DISCONNECT, data)) return
this._disconnectReason = reason
this._disconnectWe = true
this._closed = true
setTimeout(() => this._socket.end(), ms('2s'))
}
/**
* Send PING message
*/
_sendPing() {
debug(`Send PING to ${this._socket.remoteAddress}:${this._socket.remotePort}`)
const data = rlp.encode([])
if (!this._sendMessage(PREFIXES.PING, data)) return
clearTimeout(this._pingTimeoutId!)
this._pingTimeoutId = setTimeout(() => {
this.disconnect(DISCONNECT_REASONS.TIMEOUT)
}, this._pingTimeout)
}
/**
* Send PONG message
*/
_sendPong() {
debug(`Send PONG to ${this._socket.remoteAddress}:${this._socket.remotePort}`)
const data = rlp.encode([])
this._sendMessage(PREFIXES.PONG, data)
}
/**
* AUTH message received
*/
_handleAuth() {
const bytesCount = this._nextPacketSize
const parseData = this._socketData.slice(0, bytesCount)
if (!this._eciesSession._gotEIP8Auth) {
if (parseData.slice(0, 1) === Buffer.from('04', 'hex')) {
this._eciesSession.parseAuthPlain(parseData)
} else {
this._eciesSession._gotEIP8Auth = true
this._nextPacketSize = util.buffer2int(this._socketData.slice(0, 2)) + 2
return
}
} else {
this._eciesSession.parseAuthEIP8(parseData)
}
this._state = 'Header'
this._nextPacketSize = 32
process.nextTick(() => this._sendAck())
this._socketData.consume(bytesCount)
}
/**
* ACK message received
*/
_handleAck() {
const bytesCount = this._nextPacketSize
const parseData = this._socketData.slice(0, bytesCount)
if (!this._eciesSession._gotEIP8Ack) {
if (parseData.slice(0, 1) === Buffer.from('04', 'hex')) {
this._eciesSession.parseAckPlain(parseData)
debug(
`Received ack (old format) from ${this._socket.remoteAddress}:${this._socket.remotePort}`
)
} else {
this._eciesSession._gotEIP8Ack = true
this._nextPacketSize = util.buffer2int(this._socketData.slice(0, 2)) + 2
return
}
} else {
this._eciesSession.parseAckEIP8(parseData)
debug(`Received ack (EIP8) from ${this._socket.remoteAddress}:${this._socket.remotePort}`)
}
this._state = 'Header'
this._nextPacketSize = 32
process.nextTick(() => this._sendHello())
this._socketData.consume(bytesCount)
}
/**
* HELLO message received
*/
_handleHello(payload: any) {
this._hello = {
protocolVersion: buffer2int(payload[0]),
clientId: payload[1].toString(),
capabilities: payload[2].map((item: any) => {
return { name: item[0].toString(), version: buffer2int(item[1]) }
}),
port: buffer2int(payload[3]),
id: payload[4],
}
if (this._remoteId === null) {
this._remoteId = Buffer.from(this._hello.id)
} else if (!this._remoteId.equals(this._hello.id)) {
return this.disconnect(DISCONNECT_REASONS.INVALID_IDENTITY)
}
if (this._remoteClientIdFilter) {
for (const filterStr of this._remoteClientIdFilter) {
if (this._hello.clientId.toLowerCase().includes(filterStr.toLowerCase())) {
return this.disconnect(DISCONNECT_REASONS.USELESS_PEER)
}
}
}
const shared: any = {}
for (const item of this._hello.capabilities) {
for (const obj of this._capabilities!) {
if (obj.name !== item.name || obj.version !== item.version) continue
if (shared[obj.name] && shared[obj.name].version > obj.version) continue
shared[obj.name] = obj
}
}
let offset = BASE_PROTOCOL_LENGTH
this._protocols = Object.keys(shared)
.map((key) => shared[key])
.sort((obj1, obj2) => (obj1.name < obj2.name ? -1 : 1))
.map((obj) => {
const _offset = offset
offset += obj.length
const SubProtocol = obj.constructor
const protocol = new SubProtocol(obj.version, this, (code: number, data: Buffer) => {
if (code > obj.length) throw new Error('Code out of range')
this._sendMessage(_offset + code, data)
})
return { protocol, offset: _offset, length: obj.length }
})
if (this._protocols.length === 0) {
return this.disconnect(DISCONNECT_REASONS.USELESS_PEER)
}
this._connected = true
this._pingIntervalId = setInterval(() => this._sendPing(), PING_INTERVAL)
if (this._weHello) {
this.emit('connect')
}
}
/**
* DISCONNECT message received
* @param payload
*/
_handleDisconnect(payload: any) {
this._closed = true
this._disconnectReason = payload[0].length === 0 ? 0 : payload[0][0]
debug(
`DISCONNECT reason: ${DISCONNECT_REASONS[this._disconnectReason as number]} ${
this._socket.remoteAddress
}:${this._socket.remotePort}`
)
this._disconnectWe = false
this._socket.end()
}
/**
* PING message received
*/
_handlePing() {
this._sendPong()
}
/**
* PONG message received
*/
_handlePong() {
clearTimeout(this._pingTimeoutId!)
}
/**
* Message handling, called from a SubProtocol context
* @param code
* @param msg
*/
_handleMessage(code: PREFIXES, msg: Buffer) {
const payload = rlp.decode(msg)
switch (code) {
case PREFIXES.HELLO:
this._handleHello(payload)
break
case PREFIXES.DISCONNECT:
this._handleDisconnect(payload)
break
case PREFIXES.PING:
this._handlePing()
break
case PREFIXES.PONG:
this._handlePong()
break
}
}
/**
* Handle message header
*/
_handleHeader() {
const bytesCount = this._nextPacketSize
const parseData = this._socketData.slice(0, bytesCount)
debug(`Received header ${this._socket.remoteAddress}:${this._socket.remotePort}`)
const size = this._eciesSession.parseHeader(parseData)
if (!size) {
debug('invalid header size!')
return
}
this._state = 'Body'
this._nextPacketSize = size + 16
if (size % 16 > 0) this._nextPacketSize += 16 - (size % 16)
this._socketData.consume(bytesCount)
}
/**
* Handle message body
*/
_handleBody() {
const bytesCount = this._nextPacketSize
const parseData = this._socketData.slice(0, bytesCount)
const body = this._eciesSession.parseBody(parseData)
if (!body) {
debug('empty body!')
return
}
debug(
`Received body ${this._socket.remoteAddress}:${this._socket.remotePort} ${formatLogData(
body.toString('hex'),
verbose
)}`
)
this._state = 'Header'
this._nextPacketSize = 32
// RLP hack
let code = body[0]
if (code === 0x80) code = 0
if (code !== PREFIXES.HELLO && code !== PREFIXES.DISCONNECT && this._hello === null) {
return this.disconnect(DISCONNECT_REASONS.PROTOCOL_ERROR)
}
const obj = this._getProtocol(code)
if (obj === undefined) return this.disconnect(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}`
)
try {
obj.protocol._handleMessage(msgCode, body.slice(1))
} catch (err) {
this.disconnect(DISCONNECT_REASONS.SUBPROTOCOL_ERROR)
debug(`Error on peer subprotocol message handling: ${err}`)
this.emit('error', err)
}
this._socketData.consume(bytesCount)
}
/**
* Process socket data
* @param data
*/
_onSocketData(data: Buffer) {
if (this._closed) return
this._socketData.append(data)
try {
while (this._socketData.length >= this._nextPacketSize) {
switch (this._state) {
case 'Auth':
this._handleAuth()
break
case 'Ack':
this._handleAck()
break
case 'Header':
this._handleHeader()
break
case 'Body':
this._handleBody()
break
}
}
} catch (err) {
this.disconnect(DISCONNECT_REASONS.SUBPROTOCOL_ERROR)
debug(`Error on peer socket data handling: ${err}`)
this.emit('error', err)
}
}
/**
* React to socket being closed
*/
_onSocketClose() {
clearInterval(this._pingIntervalId!)
clearTimeout(this._pingTimeoutId!)
this._closed = true
if (this._connected) this.emit('close', this._disconnectReason, this._disconnectWe)
}
_getProtocol(code: number): ProtocolDescriptor | undefined {
if (code < BASE_PROTOCOL_LENGTH) return { protocol: this, offset: 0 }
for (const obj of this._protocols) {
if (code >= obj.offset && code < obj.offset + obj.length!) return obj
}
}
getId() {
if (this._remoteId === null) return null
return Buffer.from(this._remoteId)
}
getHelloMessage() {
return this._hello
}
getProtocols<T extends ETH | LES>(): T[] {
return this._protocols.map((obj) => obj.protocol)
}
getMsgPrefix(code: PREFIXES): string {
return PREFIXES[code]
}
getDisconnectPrefix(code: DISCONNECT_REASONS): string {
return DISCONNECT_REASONS[code]
}
disconnect(reason: DISCONNECT_REASONS = DISCONNECT_REASONS.DISCONNECT_REQUESTED) {
this._sendDisconnect(reason)
}
}