This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biton-ext.js
200 lines (174 loc) · 6.29 KB
/
biton-ext.js
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
'use strict'
const debug = require('debug')('biton:ext')
const { EventEmitter } = require('events')
const bencode = require('bencode')
const bitonCrypto = require('./crypto')
const Noise = require('./noise')
const extName = 'biton'
const BITFIELD_GROW = 1E3
const METADATA_LENGTH = 1 << 14 // 16 KiB
module.exports = (challengeSeed, localPeerId, identity, identityKeypair, initiator) => {
class bitonExtension extends EventEmitter {
constructor (wire) {
debug('load biton extension on wire with peerId %s', wire.peerId)
super()
this._wire = wire
this.abort = false
this._passedChallenge = false
this._challengeSeed = challengeSeed
this._localPeerId = localPeerId
this._identity = identity
this._keypair = identityKeypair
this.initiator = initiator
this._noise = new Noise(initiator)
this.once('noiseReady', this._onNoiseReady)
}
onHandshake (infoHash, peerId, extensions) {
if (this.abort) return
this._infoHash = infoHash
this._remotePeerId = peerId
}
onExtendedHandshake (handshake) {
if (this.abort) return
// TODO Do not expose biton extension on handshake messages
if (!handshake.m || !handshake.m.biton) {
return this.emit('warning', new Error('Peer does not support biton'))
}
debug('peer %s computing challenges', this._remotePeerId)
// TODO tie challenges to the IP:PORT of local and remote peers (e.g. as in the DHT "announce" challenge )
const localChallenge = bitonCrypto.blake2b_256(Uint8Array.from([this._challengeSeed, this._localPeerId,
this._remotePeerId].join('&')))
const remoteChallenge = bitonCrypto.blake2b_256(Uint8Array.from([this._challengeSeed, this._remotePeerId,
this._localPeerId].join('&')))
this._localChallenge = Buffer.from(localChallenge).toString('base64')
this._remoteChallenge = Buffer.from(remoteChallenge).toString('base64')
if (this.initiator) {
// Initiate Noise XX handshake
let A
try {
A = this._noise._noiseXXMsgA()
} catch (err) {
debug('peer %s could not create Noise XX message A, abort: %s', this._remotePeerId, err)
this.abort = true
this._noise._destroy()
return
}
const dictSend = { cmd: 'noiseXX', challenge: this._remoteChallenge, A: Buffer.from(A).toString('base64') }
this._send(dictSend)
}
}
_send(dict)
{
const buf = bencode.encode(dict, 'utf-8')
this._wire.extended(extName, buf)
}
/**
* biton-extension messages are bencoded dictionaries.
* First message is a challenge to the other party
*
* @param {Buffer} buf bencoded PEX dictionary
*/
onMessage (buf) {
if (this.abort) {
debug('peer %s sent a message to biton-ext after abort; dropping message')
return
}
let dictRecv
try {
dictRecv = bencode.decode(buf, 'utf-8')
debug('peer %s sent message: %s', this._remotePeerId, JSON.stringify(dictRecv))
} catch (err) {
// Drop invalid messages
debug('peer %s sent invalid message: %s', this._remotePeerId, err)
return
}
if (dictRecv.cmd) {
switch (dictRecv) {
case 'ping':
debug('peer %s sent ping', this._remotePeerId)
this.emit('ping')
break
case 'noiseXX':
this._handleNoiseHandshakeMsg(dictRecv)
return
}
}
if (dictRecv.noiseMsg && this._noise.isSplit) {
try {
const decNoiseMsg = this._noise._recvDec(Buffer.from(dictRecv.noiseMsg, 'utf-8'))
console.log('peer %s sent noise message: %s', this._remotePeerId, Buffer.from(decNoiseMsg).toString())
this.emit('recvMsg')
} catch (err) {
debug('peer %s sent invalid noise message %s', this._remotePeerId, err)
}
}
}
_handleNoiseHandshakeMsg(dictRecv)
{
if (!this._passedChallenge) {
// Noise_XX Handshake A and B messages
if (!dictRecv.challenge || dictRecv.challenge !== this._localChallenge) {
debug('peer %s sent invalid challenge message during handshake, abort', this._remotePeerId)
this.abort = true
this._noise._destroy()
return
}
debug('peer %s succeeded in our challenge', this._remotePeerId)
if (!this.initiator) {
// received message A
let B
try {
B = this._noise._noiseXXMsgB(Buffer.from(dictRecv.A, 'base64'))
} catch (err) {
debug('peer %s could not create Noise_XX message A, abort: %s', this._remotePeerId, err)
this.abort = true
this._noise._destroy()
return
}
// Send challenge and message B [e, ee, s, es]
const dictSend = { cmd: 'noiseXX', challenge: this._remoteChallenge, B: Buffer.from(B).toString('base64') }
this._send(dictSend)
} else {
// received message B
let C
try {
C = this._noise._noiseXXMsgC(Buffer.from(dictRecv.B, 'base64'))
} catch (err) {
debug('peer %s sent invalid Noise_XX message B, abort: %s', this._remotePeerId, err)
this.abort = true
this._noise._destroy()
return
}
const dictSend = { cmd: 'noiseXX', C: Buffer.from(C).toString('base64') }
this._send(dictSend)
this.emit('noiseReady')
}
this._passedChallenge = true
} else {
// received message C
try {
this._noise._noiseXXMsgD(Buffer.from(dictRecv.C, 'base64'))
this.emit('noiseReady')
} catch (err) {
debug('peer %s sent invalid Noise_XX message C, abort: %s', this._remotePeerId, err)
this.abort = true
this._noise._destroy()
return
}
}
}
_onNoiseReady () {
debug('peer %s noise ready', this._remotePeerId)
}
sendPing () {
this._send({ cmd: 'ping' })
}
sendEnc (buf) {
const noiseMsg = this._noise._sendEnc(buf)
this._send({ noiseMsg: Buffer.from(noiseMsg).toString('utf-8') })
}
}
// Name of the bittorrent-protocol extension
bitonExtension.prototype.name = extName
return bitonExtension
}