-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (124 loc) · 3.68 KB
/
index.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
const assert = require('nanoassert')
const secretstream = require('secretstream-stream')
const bint = require('bint8array')
const { Duplex } = require('streamx')
const pump = require('pump')
const { Encode, Decode } = require('./encoder')
module.exports = class HandshakePeer extends Duplex {
constructor (localInfo, remoteInfo, transport, opts = {}) {
super()
this.localInfo = localInfo
this.remoteInfo = remoteInfo
this.recv = new Decode()
this.send = new Encode()
this.transport = transport
this.keys = new Keys()
this.encrypter = null
this.decrypter = null
this.handshake = opts.handshake
this.handshakeState = null
this._destroyed = false
}
_open (cb) {
cb = once(cb)
const self = this
const handshake = this.handshake
let step = 0
const initData = handshake[step++](self)
if (initData) this.send.write(initData)
this.recv.once('data', doHandshake(handshake[step++]))
pump(this.send, this.transport, this.recv, err => {
if (this._destroyed) return
cb(err)
})
function doHandshake (fn) {
return (data) => {
self.recv.pause()
let ret
try {
ret = fn(data, self)
if (ret) self.send.write(ret)
} catch (e) {
self.send.error(e.message)
return cb(e)
}
// proceed to next step, if there is one
if (!self.keys.empty() && self.encrypter == null) {
const header = new Uint8Array(secretstream.HEADERBYTES)
self.encrypter = secretstream.encrypt(header, self.keys.local)
self.send.write(header)
}
// proceed to next step, if there is one
if (step !== handshake.length) {
self.recv.once('data', doHandshake(handshake[step++]))
return
}
// wipe handshake state
self.handshakeState._sanitize()
self.handshakeState = null
self.recv.once('data', onheader)
}
}
function onheader (header) {
self.recv.pause()
self.decrypter = secretstream.decrypt(header, self.keys.remote)
self.recv.on('data', ondata)
self.recv.resume()
cb()
}
function ondata (data) {
try {
const plaintext = self.decrypter.decrypt(data)
self.push(plaintext)
} catch (e) {
self.send.end(e)
return cb(e)
}
if (!bint.compare(self.decrypter.decrypt.tag, secretstream.TAG_FINAL)) {
self.push(null)
}
}
}
_write (data, cb) {
const ciphertext = this.encrypter.encrypt(secretstream.TAG_MESSAGE, data)
this.send.write(ciphertext)
cb()
}
_final (cb) {
const finalMessage = this.encrypter.encrypt(secretstream.TAG_FINAL, new Uint8Array(0))
this.send.end(finalMessage)
cb()
}
_predestroy () {
this._destroyed = true
}
}
class Keys {
constructor () {
this._remote = null
this._local = null
}
get local () { return this._local }
get remote () { return this._remote }
set local (buf) {
assert(buf instanceof Uint8Array, 'key should be a Buffer or Uint8Array')
assert(buf.byteLength === secretstream.KEYBYTES, 'key should be secretstream.KEYBYTES [' + secretstream.KEYBYTES + '] bytes.')
this._local = buf
}
set remote (buf) {
assert(buf instanceof Uint8Array, 'key should be a Buffer or Uint8Array')
assert(buf.byteLength === secretstream.KEYBYTES, 'key should be secretstream.KEYBYTES [' + secretstream.KEYBYTES + '] bytes.')
this._remote = buf
}
empty () {
return this._local == null || this._remote == null
}
}
function once (fn) {
let called = false
return (err) => {
if (called) return
called = true
return fn(err)
}
}