-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
752 lines (605 loc) · 20.2 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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
const fs = require('fs')
const EventEmitter = require('events')
const getDirName = require('path').dirname
const path = require('path')
const Database = require('./lib/database')
const Hyperbee = require('hyperbee')
const Hypercore = require('./lib/core')
const pump = require('pump')
const Crypto = require('./lib/crypto')
const Swarm = require('./lib/swarm')
const stream = require('stream')
const blake = require('blakejs')
const Hyperswarm = require('hyperswarm')
const MemoryStream = require('memorystream')
const { v4: uuidv4 } = require('uuid')
const FixedChunker = require('./util/fixedChunker.js')
const RequestChunker = require('./util/requestChunker.js')
const WorkerKeyPairs = require('./util/workerKeyPairs.js')
const isOnline = require('is-online');
const HASH_OUTPUT_LENGTH = 32 // bytes
const MAX_PLAINTEXT_BLOCK_SIZE = 65536
const MAX_ENCRYPTED_BLOCK_SIZE = 65553
const FILE_TIMEOUT = 10000 // How long to wait for the on data event when downloading a file from a remote drive.
const FILE_RETRY_ATTEMPTS = 2 // Fail to fetch file after 3 attempts
const FILE_BATCH_SIZE = 10 // How many parallel requests are made in each file request batch
class Drive extends EventEmitter {
constructor(drivePath, peerPubKey, { storage, keyPair, writable, swarmOpts, encryptionKey, fileTimeout, fileRetryAttempts, checkNetworkStatus, joinSwarm }) {
super()
this.storage = storage
this.encryptionKey = encryptionKey
this.database = null;
this.db = null;
this.drivePath = drivePath
this.swarmOpts = swarmOpts
this.publicKey = null
this.peerPubKey = peerPubKey // Key used to clone and seed drive. Should only be shared with trusted sources
this.keyPair = keyPair // ed25519 keypair to listen on
this.writable = writable
this.fileTimeout = fileTimeout || FILE_TIMEOUT
this.fileRetryAttempts = fileRetryAttempts-1 || FILE_RETRY_ATTEMPTS-1
this.requestQueue = new RequestChunker(null, FILE_BATCH_SIZE)
this.checkNetworkStatus = checkNetworkStatus
this.joinSwarm = typeof joinSwarm === 'boolean' ? joinSwarm : true
this.network = {
internet: false,
drive: false
}
// When using custom storage, transform drive path into beginning of the storage namespace
this.storageName = drivePath.slice(drivePath.lastIndexOf('/') + 1, drivePath.length)
this._localCore = new Hypercore(this.storage || path.join(drivePath, `./LocalCore`), { storageNamespace: `${this.storageName}:local-core` })
this._swarm = null
this._workerKeyPairs = new WorkerKeyPairs(FILE_BATCH_SIZE)
this._collections = {}
this._filesDir = path.join(drivePath, `./Files`)
this._localHB = null // Local Key value datastore only. This db does not sync with remote drives.
this._lastSeq = null
this._checkInternetInt = null
this._checkInternetInProgress = false
if (!fs.existsSync(drivePath)) {
fs.mkdirSync(drivePath)
}
if (!fs.existsSync(this._filesDir)) {
fs.mkdirSync(this._filesDir)
}
this.requestQueue.on('process-queue', async files => {
this.requestQueue.reset()
await this.fetchFileBatch(files, (stream, file) => {
return new Promise((resolve, reject) => {
fs.mkdirSync(getDirName(this._filesDir + file.path), { recursive: true })
const writeStream = fs.createWriteStream(this._filesDir + file.path)
pump(stream, writeStream, (err) => {
if (err) reject(err)
setTimeout(() => {
this.emit('file-sync', file)
})
resolve()
})
})
})
})
process.on('uncaughtException', err => {
// gracefully catch uncaught exceptions
})
// Periodically check this drive is connected to the internet.
// When internet is down, emit a network status updated event.
if(this.checkNetworkStatus) {
this._checkInternetInt = setInterval(async () => {
if(!this._checkInternetInProgress) {
this._checkInternetInProgress = true
await this._checkInternet();
this._checkInternetInProgress = false
}
}, 1500)
}
}
async ready() {
await this._bootstrap()
this.publicKey = this.database.localMetaCore.key.toString('hex')
if (this.peerPubKey) {
this.discoveryKey = createTopicHash(this.peerPubKey).toString('hex')
} else {
this.discoveryKey = createTopicHash(this.publicKey).toString('hex')
}
// Data here can only be read by peer drives
// that are sharing the same drive secret
this._collections.files = await this.database.collection('file')
if (this.keyPair && this.joinSwarm) {
await this.connect()
}
this._lastSeq = await this._localHB.get('lastSeq')
const stream = this.database.metaBase.createReadStream({ live: true })
stream.on('data', async data => {
const node = {
...JSON.parse(data.value.toString()),
seq: data.seq
}
if (
node.key !== '__peers' && !this._lastSeq ||
node.key !== '__peers' && this._lastSeq && data.seq > this._lastSeq.seq
) {
await this._update(node)
}
})
// This stopped streaming async updates after migrating to autobase
// const hs = this.metadb.createHistoryStream({ live: true, gte: this._lastSeq ? -1 : 1 })
// hs.on('data', async data => {
// this.emit('sync', data)
// if (data.key !== '__peers') {
// data.value = JSON.parse(data.value).value
// await this._update(data)
// }
// })
// hs.on('error', err => {
// // catch get out of bounds errors
// })
this.opened = true
}
// Connect to the Hyperswarm network
async connect() {
if (this._swarm) {
await this._swarm.close()
}
this._swarm = new Swarm({
keyPair: this.keyPair,
workerKeyPairs: this._workerKeyPairs.keyPairs,
topic: this.discoveryKey,
publicKey: this.peerPubKey || this.publicKey,
isServer: this.swarmOpts.server,
isClient: this.swarmOpts.client,
acl: this.swarmOpts.acl
})
if(this.checkNetworkStatus) {
this._swarm.on('disconnected', () => {
if(this.network.drive) {
this.network.drive = false
this.emit('network-updated', { drive: this.network.drive })
}
})
this._swarm.on('connected', () => {
if(!this.network.drive) {
this.network.drive = true
this.emit('network-updated', { drive: this.network.drive })
}
})
}
this._swarm.on('message', (peerPubKey, data) => {
this.emit('message', peerPubKey, data)
})
this._swarm.on('file-requested', socket => {
socket.once('data', async data => {
const fileHash = data.toString('utf-8')
const file = await this.metadb.get(fileHash)
if (!file || file.value.deleted) {
let err = new Error()
err.message = 'Requested file was not found on drive'
socket.destroy(err)
} else {
const readStream = fs.createReadStream(path.join(this.drivePath, `./Files${file.value.path}`))
pump(readStream, socket, (err) => {
// handle done
})
}
})
socket.on('error', (err) => {
// handle errors
})
})
await this._swarm.ready()
}
async addPeer(peerKey) {
const remotePeers = await this._localHB.get('remotePeers')
const peers = [...remotePeers.value, peerKey]
await this._localHB.put('remotePeers', peers)
await this.database.addInput(peerKey)
}
// Remove Peer
async removePeer(peerKey) {
await this.database.removeInput(peerKey)
}
async writeFile(path, readStream, opts = {}) {
let filePath = path
let dest
const uuid = uuidv4()
if (filePath[0] === '/') {
filePath = filePath.slice(1, filePath.length)
}
if (opts.encrypted) {
dest = `${this._filesDir}/${uuid}`
} else {
fs.mkdirSync(getDirName(this._filesDir + path), { recursive: true })
dest = this._filesDir + path
}
return new Promise(async (resolve, reject) => {
const pathSeg = filePath.split('/')
let fullFile = pathSeg[pathSeg.length - 1]
let fileName
let fileExt
if (fullFile.indexOf('.') > -1) {
fileName = fullFile.split('.')[0]
fileExt = fullFile.split('.')[1]
}
const writeStream = fs.createWriteStream(dest)
if (opts.encrypted && !opts.skipEncryption) {
const fixedChunker = new FixedChunker(readStream, MAX_PLAINTEXT_BLOCK_SIZE)
const { key, header, file } = await Crypto.encryptStream(fixedChunker, writeStream)
await this.metadb.put(file.hash, {
uuid,
size: file.size,
hash: file.hash,
path: `/${uuid}`,
peer_key: this.keyPair.publicKey.toString('hex'),
discovery_key: this.discoveryKey
})
const fileMeta = {
uuid,
name: fileName,
size: file.size,
mimetype: fileExt,
encrypted: true,
key: key.toString('hex'),
header: header.toString('hex'),
hash: file.hash,
path: filePath,
peer_key: this.keyPair.publicKey.toString('hex'),
discovery_key: this.discoveryKey
}
await this._collections.files.put(filePath, fileMeta)
this.emit('file-add', fileMeta)
resolve({
key: key.toString('hex'),
header: header.toString('hex'),
...fileMeta
})
} else {
let bytes = ''
const hash = blake.blake2bInit(HASH_OUTPUT_LENGTH, null)
const calcHash = new stream.Transform({
transform
})
function transform(chunk, encoding, callback) {
bytes += chunk.byteLength
blake.blake2bUpdate(hash, chunk)
callback(null, chunk)
}
pump(readStream, calcHash, writeStream, async () => {
setTimeout(async () => {
const _hash = Buffer.from(blake.blake2bFinal(hash)).toString('hex')
if (bytes > 0) {
await this.metadb.put(_hash, {
uuid,
size: bytes,
hash: _hash,
path,
peer_key: this.keyPair.publicKey.toString('hex'),
discovery_key: this.discoveryKey
})
const fileMeta = {
uuid,
name: fileName,
size: bytes,
mimetype: fileExt,
hash: _hash,
path: filePath,
peer_key: this.keyPair.publicKey.toString('hex'),
discovery_key: this.discoveryKey
}
await this._collections.files.put(filePath, fileMeta)
this.emit('file-add', fileMeta)
resolve(fileMeta)
} else {
reject('No bytes were written.')
}
})
})
}
})
}
async readFile(path) {
let file
let filePath = path
if (filePath[0] === '/') {
filePath = filePath.slice(1, filePath.length)
}
try {
file = await this._collections.files.get(filePath)
file = file.value
const stream = fs.createReadStream(`${this._filesDir}/${file.uuid}`)
// If key then decipher file
if (file.encrypted && file.key && file.header) {
const fixedChunker = new FixedChunker(stream, MAX_ENCRYPTED_BLOCK_SIZE)
return Crypto.decryptStream(fixedChunker, file.key, file.header)
} else {
return stream
}
} catch (err) {
throw err
}
}
decryptFileStream(stream, key, header) {
const fixedChunker = new FixedChunker(stream, MAX_ENCRYPTED_BLOCK_SIZE)
return Crypto.decryptStream(fixedChunker, key, header)
}
// TODO: Implement this
fetchFileByHash(fileHash) {
}
async fetchFileByDriveHash(discoveryKey, fileHash, opts = {}) {
const keyPair = opts.keyPair || this.keyPair
const memStream = new MemoryStream()
const topic = blake.blake2bHex(discoveryKey, null, HASH_OUTPUT_LENGTH)
if (!fileHash || typeof fileHash !== 'string') {
return reject('File hash is required before making a request.')
}
if (!discoveryKey || typeof discoveryKey !== 'string') {
return reject('Discovery key cannot be null and must be a string.')
}
try {
await this._initFileSwarm(memStream, topic, fileHash, 0, { keyPair })
} catch(e) {
setTimeout(() => {
memStream.destroy(e)
})
return memStream
}
if (opts.key && opts.header) {
return this.decryptFileStream(memStream, opts.key, opts.header)
}
return memStream
}
async fetchFileBatch(files, cb) {
const batches = new RequestChunker(files, FILE_BATCH_SIZE)
for (let batch of batches) {
const requests = []
for (let file of batch) {
requests.push(new Promise(async (resolve, reject) => {
if (file.discovery_key) {
const keyPair = this._workerKeyPairs.getKeyPair()
const stream = await this.fetchFileByDriveHash(file.discovery_key, file.hash, { key: file.key, header: file.header, keyPair })
await cb(stream, file)
resolve()
} else {
// TODO: Fetch files by hash
}
}))
}
await Promise.all(requests)
this.requestQueue.queue = []
}
}
async _initFileSwarm(stream, topic, fileHash, attempts, { keyPair }) {
return new Promise((resolve, reject) => {
if (attempts > this.fileRetryAttempts) {
const err = new Error('Unable to make a connection or receive data within the allotted time.')
err.fileHash = fileHash
this._workerKeyPairs.release(keyPair.publicKey.toString('hex'))
stream.destroy(err)
return reject(err)
}
const swarm = new Hyperswarm({ keyPair })
let connected = false
let receivedData = false
let streamError = false
swarm.join(Buffer.from(topic, 'hex'), { server: false, client: true })
swarm.on('connection', async (socket, info) => {
receivedData = false
if (!connected) {
connected = true
// Tell the host drive which file we want
socket.write(fileHash)
socket.on('data', (data) => {
resolve()
stream.write(data)
receivedData = true
})
socket.once('end', () => {
if (receivedData) {
this._workerKeyPairs.release(keyPair.publicKey.toString('hex'))
stream.end()
swarm.destroy()
}
})
socket.once('error', (err) => {
stream.destroy(err)
streamError = true
reject(err)
})
}
})
setTimeout(async () => {
if (!connected || streamError || !receivedData) {
attempts += 1
await swarm.leave(topic)
await swarm.destroy()
try {
await this._initFileSwarm(stream, topic, fileHash, attempts, { keyPair })
resolve()
} catch(e) {
reject(e)
}
}
}, this.fileTimeout)
})
}
async _checkInternet() {
return new Promise((resolve, reject) => {
isOnline().then((isOnline) => {
if(!isOnline && this.network.internet) {
this.network.internet = false
this.emit('network-updated', { internet: this.network.internet })
}
if(isOnline && !this.network.internet) {
this.network.internet = true
this.emit('network-updated', { internet: this.network.internet })
}
resolve()
})
})
}
async unlink(filePath) {
let fp = filePath
if (fp[0] === '/') {
fp = filePath.slice(1, fp.length)
}
try {
let file = await this._collections.files.get(fp)
if (!file) return
file = await this.metadb.get(file.value.hash)
if(!file) return
fs.unlinkSync(path.join(this._filesDir, file.value.path))
await this._collections.files.put(fp, {
uuid: file.value.uuid,
deleted: true
})
await this.metadb.put(file.value.hash, {
uuid: file.value.uuid,
discovery_key: file.value.discovery_key,
deleted: true
})
this.emit('file-unlink', file.value)
} catch (err) {
throw err
}
}
async destroyHyperfile(path) {
const filePath = await this.bee.get(path)
const file = await this.bee.get(filePath.value.hash)
await this._clearStorage(file.value)
}
async _bootstrap() {
// Init local core
this._localHB = new Hyperbee(this._localCore, {
keyEncoding: 'utf-8',
valueEncoding: 'json'
})
this.database = new Database(this.storage || this.drivePath, {
keyPair: this.keyPair,
storageName: this.storageName,
encryptionKey: this.encryptionKey,
peerPubKey: this.peerPubKey,
acl: this.swarmOpts.acl,
joinSwarm: this.joinSwarm
})
if(this.checkNetworkStatus) {
this.database.on('disconnected', () => {
if(this.network.drive) {
this.network.drive = false
this.emit('network-updated', { drive: this.network.drive })
}
})
this.database.on('connected', () => {
if(!this.network.drive) {
this.network.drive = true
this.emit('network-updated', { drive: this.network.drive })
}
})
}
await this.database.ready()
this.db = this.database
this.metadb = this.database.metadb
}
async _update(data) {
let lastSeq
lastSeq = await this._localHB.get(`lastSeq`)
if (!lastSeq) lastSeq = { value: { seq: null } }
if (
data.type === 'put' &&
!data.value.deleted &&
data.value.peer_key !== this.keyPair.publicKey.toString('hex') &&
lastSeq.value.seq !== data.seq
) {
this.emit('sync')
if (data.value.hash) {
try {
await this._localHB.put(`lastSeq`, { seq: data.seq })
this.requestQueue.addFile(data.value)
} catch (err) {
throw err
}
}
}
if (
data.type === 'put' &&
data.value.deleted &&
data.value.peer_key !== this.keyPair.publicKey.toString('hex')
) {
try {
const filePath = path.join(this._filesDir, `/${data.value.uuid}`)
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath)
setTimeout(() => {
this.emit('file-unlink', data.value)
})
}
} catch (err) {
throw err
}
}
}
info() {
const bytes = getTotalSize(this.drivePath)
return {
size: bytes
}
}
/**
* Close drive and disconnect from all Hyperswarm topics
*/
async close() {
if(this.joinSwarm) {
await this._swarm.close()
}
await this.database.close()
await this._localCore.close()
if(this.checkNetworkStatus) {
clearInterval(this._checkInternetInt)
this.network = {
internet: false,
drive: false
}
this.emit('network-updated', this.network)
}
this.openend = false
}
}
function createTopicHash(topic) {
const crypto = require('crypto')
return crypto.createHash('sha256')
.update(topic)
.digest()
}
async function auditFile(stream, remoteHash) {
return new Promise((resolve, reject) => {
let hash = blake.blake2bInit(HASH_OUTPUT_LENGTH, null)
stream.on('error', err => reject(err))
stream.on('data', chunk => {
blake.blake2bUpdate(hash, chunk)
})
stream.on('end', () => {
const localHash = Buffer.from(blake.blake2bFinal(hash)).toString('hex')
if (localHash === remoteHash)
return resolve()
reject('Hashes do not match')
})
})
}
const getAllFiles = function (dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(dirPath, file))
}
})
return arrayOfFiles
}
const getTotalSize = function (directoryPath) {
const arrayOfFiles = getAllFiles(directoryPath)
let totalSize = 0
arrayOfFiles.forEach(function (filePath) {
totalSize += fs.statSync(filePath).size
})
return totalSize
}
module.exports = Drive