-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
222 lines (191 loc) · 7.87 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
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
const { PubSubMessage } = require('./messages')
const sodium = require('sodium-universal')
const PeerExchange = require('./pex')
const EventEmitter = require('events')
const MSG_TYPE_SUBSCRIBE = 1
const MSG_TYPE_UNSUBSCRIBE = 2
const MSG_TYPE_MESSAGE = 3
let debug = msg => {}
class PubSub extends EventEmitter {
constructor(network, opts) {
super()
this.opts = opts || {}
this.subscribers = new Map()
this.network = network
this.topics = new Map()
this.peerExchange = null
this.listeners = new Map()
this.extension = this.network.registerExtension({
name: 'hyperpubsub',
onmessage: (msg, peer) => this._onMessage(msg, peer),
encoding: PubSubMessage,
onerror: err => this.emit('error', err)
})
this.network.on('peer-add', peer => {
debug('peer-add ' + peer.remoteAddress)
for (const topic of this.topics.keys()) {
debug('<- sub ' + topic + ' to ' + peer.remoteAddress)
this.extension.send({ topic, type: MSG_TYPE_SUBSCRIBE, application: this.opts.application }, peer)
}
})
this.network.on('peer-remove', peer => {
debug('peer-remove ' + peer.remoteAddress)
for (const topic of this.subscribers.keys()) {
this._removePeer(peer, topic)
}
})
}
async sub(topic, handler, announce = true) {
this.topics.set(topic, handler)
if (announce) {
await this.join(topic)
}
try {
debug('<- sub ' + topic + ' broadcast')
this.extension.broadcast({ topic, type: MSG_TYPE_SUBSCRIBE, application: this.opts.application })
} catch (err) {
this.emit('error', err)
}
}
pub(topic, message, peer = null) {
const self = this
if (peer) {
send(peer)
} else {
const peers = this.subscribers.get(topic) || []
peers.forEach(p => send(p))
}
function send(to) {
try {
debug('<- msg ' + topic + ' to ' + to.remoteAddress)
self.extension.send({ topic, type: MSG_TYPE_MESSAGE, application: self.opts.application, data: message }, to)
} catch (err) {
this.emit('error', err)
}
}
}
unsub(topic) {
this.topics.delete(topic)
debug('<- unsub ' + topic + ' broadcast')
this.extension.broadcast({ topic, type: MSG_TYPE_UNSUBSCRIBE, application: this.opts.application })
}
close() {
this.extension.destroy()
delete this.topics
delete this.subscribers
}
async join(topic, opts = { lookup: true, announce: true, flush: true, remember: false }) {
const discoveryKey = hash('hyperpubsub.' + topic)
await this.network.configure(discoveryKey, opts)
const discoveryTopic = discoveryKey.toString('hex')
debug(`joined topic "${topic}" (${discoveryTopic})`)
return discoveryTopic
}
_onMessage(msg, peer) {
try {
switch (msg.type) {
case MSG_TYPE_SUBSCRIBE:
debug('-> msg sub ' + msg.topic + ' from ' + peer.remoteAddress)
this._addPeer(peer, msg.topic)
break
case MSG_TYPE_UNSUBSCRIBE:
debug('-> msg unsub ' + msg.topic + ' from ' + peer.remoteAddress)
this._removePeer(peer, msg.topic)
break
case MSG_TYPE_MESSAGE:
const content = msg.data ? msg.data.toString('utf-8') : ''
debug('-> msg data ' + msg.topic + ' from ' + peer.remoteAddress)
const handler = this.topics.get(msg.topic)
if (handler) {
handler(msg.data, msg.application, peer)
} else {
this.emit('error', new Error('no handler found for topic ' + topic))
this.extension.send({ topic, type: MSG_TYPE_UNSUBSCRIBE, application: opts.application }, peer)
}
break
default:
throw new Error('Invalid PubSub message type: ' + msg.type)
}
} catch (err) {
this.emit('error', err)
}
}
_addPeer(peer, topic) {
let peers = []
if (this.subscribers.has(topic)) peers = this.subscribers.get(topic)
else this.subscribers.set(topic, peers)
if (!peers.find(p => p.remoteAddress === peer.remoteAddress)) {
peers.push(peer)
debug('subscriber ' + peer.remoteAddress + ' added to topic ' + topic)
this.emit('subscriber-add', topic, peer)
}
}
_removePeer(peer, topic) {
if (this.subscribers.has(topic)) {
const peers = this.subscribers.get(topic)
const idx = peers.findIndex(p => p.remoteAddress === peer.remoteAddress)
if (idx >= 0) {
peers.splice(idx, 1)
debug('subscriber ' + peer.remoteAddress + ' removed from topic ' + topic)
this.emit('subscriber-remove', topic, peer)
}
}
}
pex(maxSize = 1000, anytopic = false) {
if (!this.peerExchange) {
this.peerExchange = new PeerExchange(this, maxSize, anytopic)
this.peerExchange.on('error', err => this.emit('error', err))
}
return this.peerExchange
}
pubPrivateMsg(recipientPubKey, message, peer = null) {
const ciphertext = privateMessageSeal(message, recipientPubKey)
const topic = hash(recipientPubKey).toString('hex')
this.pub(topic, ciphertext, peer)
}
subPrivateMsg(publicKey, secretKey, handler, announce = true) {
const topic = hash(publicKey).toString('hex')
this.sub(topic, onData, announce)
function onData(data, app, peer) {
try {
const msg = privateMessageOpen(data, publicKey, secretKey)
if(msg) handler(msg, app, peer)
} catch (err) {
this.emit('err', err)
}
}
}
joinPublicKey(publicKey, opts = { lookup: true, announce: true, flush: true, remember: false }) {
const topic = hash(publicKey).toString('hex')
return this.join(topic, opts)
}
}
function hash(txt) {
const buf = typeof txt === 'string' ? Buffer.from(txt, 'utf-8') : Buffer.from(txt)
const digest = Buffer.allocUnsafe(32)
sodium.crypto_generichash(digest, buf)
return digest
}
function privateMessageSeal(message, recipientPubKey) {
if(!Buffer.isBuffer(message)) throw new Error('private message has to be a Buffer')
if(!Buffer.isBuffer(recipientPubKey) || recipientPubKey.length !== sodium.crypto_box_PUBLICKEYBYTES) throw new Error('invalid public key')
const ciphertext = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES)
sodium.crypto_box_seal(ciphertext, message, recipientPubKey)
return ciphertext
}
function privateMessageOpen(ciphertext, publicKey, secretKey) {
if(!Buffer.isBuffer(ciphertext) || ciphertext.length <= sodium.crypto_box_SEALBYTES) throw new Error('invalid ciphertext')
if(!Buffer.isBuffer(publicKey) || publicKey.length !== sodium.crypto_box_PUBLICKEYBYTES) throw new Error('invalid public key')
if(!Buffer.isBuffer(secretKey) || secretKey.length !== sodium.crypto_box_SECRETKEYBYTES) throw new Error('invalid secret key')
const message = Buffer.alloc(ciphertext.length - sodium.crypto_box_SEALBYTES)
if(!sodium.crypto_box_seal_open(message, ciphertext, publicKey, secretKey)) {
throw new Error('failed to open sealed box - corrupted or not intended for this receipient')
}
return message
}
module.exports = {
PubSub,
debug: () => {
debug = msg => console.debug(msg)
return {PubSub}
}}