-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstorage.js
418 lines (358 loc) · 10.9 KB
/
storage.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
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var pipeline = require('stream-combiner')
var crypto = require('./crypto')
var rsa = require('./crypto/rsa')
var API = require('./api').API
var File = require('./file').File
var mega = require('./mega')
var request = require('request')
var through = require('through')
var util = require('./util')
exports.Storage = Storage
Storage.NODE_TYPE_FILE = 0
Storage.NODE_TYPE_DIR = 1
Storage.NODE_TYPE_DRIVE = 2
Storage.NODE_TYPE_INBOX = 3
Storage.NODE_TYPE_RUBBISH_BIN = 4
function Storage(options, cb) {
if (arguments.length === 1 && typeof options === 'function') {
cb = options
options = {}
}
// Legacy email/password support. Deprecated.
if (typeof options === 'string' && typeof cb === 'string') {
console.log('Warning: new Storage(email, password) is deprecated!')
options = {email: options, password: cb}
if (arguments.length === 3 && typeof arguments[2] === 'function') {
cb = arguments[2]
}
}
if (!cb) {
cb = function(err) {
if (err) throw(err) //Would be nicer to emit error event?
}
}
// Defaults
options.keepalive = options.keepalive === undefined ? true : !!options.keepalive
options.autoload = options.autoload === undefined ? true : !!options.autoload
this.api = new API(options.keepalive)
this.files = {}
var self = this
function ready() {
self.status = 'ready'
cb()
self.emit('ready', self)
}
function loadUser(cb) {
self.api.request({a: 'ug'}, function(err, response) {
if (err) return cb(err)
//console.log('user', response)
self.name = response.name
self.user = response.u
if (options.autoload) {
self.reload(function(err) {
if (err) return cb(err)
ready()
}, true)
}
else ready()
})
}
if (options.email) {
this.email = options.email
var pw = crypto.prepareKey(new Buffer(options.password))
var aes = new crypto.AES(pw)
var uh = aes.stringhash(new Buffer(options.email))
this.api.request({a: 'us', user: options.email, uh: uh}, function(err, response) {
if (err) return cb(err)
//console.log('resp', response)
self.key = crypto.formatKey(response.k)
aes.decryptKey(self.key)
self.aes = new crypto.AES(self.key)
var t = rsa.mpi2b(crypto.formatKey(response.csid).toString('binary'))
var privk = self.aes.decryptKey(crypto.formatKey(response.privk)).toString('binary')
var r = false
var rsa_privk = Array(4);
// decompose private key
for (var i = 0; i < 4; i++)
{
var l = ((privk.charCodeAt(0)*256+privk.charCodeAt(1)+7)>>3)+2;
rsa_privk[i] = rsa.mpi2b(privk.substr(0,l));
if (typeof rsa_privk[i] == 'number') break;
privk = privk.substr(l);
}
var sid = new Buffer(rsa.b2s(rsa.RSAdecrypt(t,rsa_privk[2],rsa_privk[0],rsa_privk[1],rsa_privk[3])).substr(0,43), 'binary')
sid = crypto.e64(sid)
self.api.sid = self.sid = sid
self.RSAPrivateKey = rsa_privk
loadUser(cb)
})
}
else {
this._createUser(function(err) {
if (err) return cb(err)
loadUser(cb)
})
}
this.status = 'connecting'
}
inherits(Storage, EventEmitter)
Storage.prototype._createUser = function(cb) {
var nodeCrypto = require('crypto')
var passwordKey = nodeCrypto.randomBytes(16)
var ssc = nodeCrypto.randomBytes(16)
this.key = nodeCrypto.randomBytes(16)
// crypto-browserify currently returns arrays.
if (!(passwordKey instanceof Buffer)) passwordKey = new Buffer(passwordKey)
if (!(ssc instanceof Buffer)) ssc = new Buffer(ssc)
if (!(this.key instanceof Buffer)) this.key = new Buffer(this.key)
this.aes = new crypto.AES(this.key)
var aes = new crypto.AES(passwordKey)
var k = new Buffer(this.key)
aes.encryptKey(k)
var ssc2 = new Buffer(ssc)
this.aes.encryptKey(ssc2)
var req = {
a: 'up',
k: crypto.e64(k),
ts: crypto.e64(Buffer.concat([ssc, ssc2]))
}
var self = this
this.api.request(req, function(err, user) {
if (err) cb(err)
self.api.request({a: 'us', user: user}, function(err, response) {
if (err) return cb(err)
var t = crypto.d64(response.tsid)
var t_0 = t.slice(0, 16)
var t_1 = t.slice(t.length - 16)
self.aes.encryptKey(t_0)
if (t_0.toString('base64') !== t_1.toString('base64')) {
return cb(new Error('Invalid key on user creation.'))
}
self.api.sid = self.sid = response.tsid
cb(null)
})
})
}
Storage.prototype.reload = function(cb, force) {
if (this.status === 'connecting' && !force) {
return this.once('ready', this.reload.bind(this, cb))
}
var self = this
this.mounts = []
this.api.request({a: 'f', c: 1}, function(err, response) {
//console.log('resp', response)
if (err) return cb(err)
response.f.forEach(self._importFile.bind(self))
cb(null, self.mounts)
})
this.api.on('sc', function(arr) {
var deleted = {}
arr.forEach(function(o) {
if (o.a === 'u') {
var file = self.files[o.n]
if (file) {
file.timestamp = o.ts
file._setAttributes(o.at, function() {})
file.emit('update')
self.emit('update', file)
}
}
else if (o.a === 'd') {
deleted[o.n] = true // Don't know yet if move or delete.
}
else if (o.a === 't') {
o.t.f.forEach(function(f) {
var file
if (file = self.files[f.h]) {
delete deleted[f.h]
var oldparent = file.parent
if (oldparent.nodeId === f.p) return
// todo: move to setParent() to avoid duplicate.
oldparent.children.splice(oldparent.children.indexOf(file), 1)
file.parent = self.files[f.p]
if (!file.parent.children) file.parent.children = []
file.parent.children.push(file)
file.emit('move', oldparent)
self.emit('move', file, oldparent)
}
else {
self.emit('add', self._importFile(f))
}
})
}
})
Object.keys(deleted).forEach(function(n) {
var file = self.files[n]
var parent = file.parent
parent.children.splice(parent.children.indexOf(file), 1)
self.emit('delete', file)
file.emit('delete')
})
})
}
Storage.prototype._importFile = function(f) {
// todo: no support for updates
if (!this.files[f.h]) {
var fo = this.files[f.h] = new File(f, this)
if (f.t === Storage.NODE_TYPE_DRIVE) {
this.root = fo
fo.name = 'Cloud Drive'
}
if (f.t === Storage.NODE_TYPE_RUBBISH_BIN) {
this.trash = fo
fo.name = 'Rubbish Bin'
}
if (f.t === Storage.NODE_TYPE_INBOX) {
this.inbox = fo
fo.name = 'Inbox'
}
if (f.t > 1) {
this.mounts.push(fo)
}
if (f.p) {
var parent = this.files[f.p]
if (!parent.children) parent.children = []
parent.children.push(fo)
fo.parent = parent
}
}
return this.files[f.h]
}
Storage.prototype.mkdir = function(opt, cb) {
if (typeof opt === 'string') {
opt = {name: opt}
}
if (!opt.attributes) opt.attributes = {}
if (opt.name) opt.attributes.n = opt.name
if (!opt.attributes.n) {
return process.nextTick(function() {
cb(new Error('File name is required.'))
})
}
// Wait for ready event.
if (this.status === 'connecting') {
return this.on('ready', this.mkdir.bind(this, opt, cb))
}
if (!opt.target) opt.target = this.root
var key = require('crypto').randomBytes(32)
var at = File.packAttributes(opt.attributes)
var self = this
File.getCipher(key).encryptCBC(at)
this.aes.encryptKey(key)
this.api.request({
a: 'p',
t: opt.target.nodeId ? opt.target.nodeId : opt.target,
n: [{
h: 'xxxxxxxx',
t: 1,
a: crypto.e64(at),
k: crypto.e64(key),
}]}, function(err, response) {
if (err) return returnError(err)
var file = self._importFile(response.f[0])
self.emit('add', file)
if (cb) {
cb(null, file)
}
})
}
Storage.prototype.upload = function(opt, buffer, cb) {
if (arguments.length === 2 && typeof buffer === 'function') {
cb = buffer
buffer = null
}
if (typeof opt === 'string') {
opt = {name: opt}
}
if (!opt.attributes) opt.attributes = {}
if (opt.name) opt.attributes.n = opt.name
if (!opt.attributes.n) {
throw(new Error('File name is required.'))
}
var self = this
var encrypter = mega.encrypt()
var pause = through().pause()
var stream = pipeline(pause, encrypter)
function returnError(e) {
if (cb) cb(e)
else stream.emit('error', e)
}
// Size is needed before upload. Kills the streaming otherwise.
var size = opt.size
if (buffer) {
size = buffer.length
stream.write(buffer)
stream.end()
}
// Wait for ready event.
if (this.status === 'connecting') {
var _upload = upload
upload = function(s) {
self.on('ready', function() { _upload(s) })
}
}
if (size) {
upload(size)
}
else {
stream = pipeline(util.detectSize(upload), stream)
}
function upload(size) {
if (!opt.target) opt.target = self.root
self.api.request({a: 'u', ssl: 0, ms: '-1', s: size, r: 0, e: 0},
function(err, resp) {
if (err) return returnError(err)
var httpreq = request({
url: resp.p,
headers: {'Content-Length': size},
method: 'POST'
})
util.stream2cb(httpreq, function(err, hash) {
if (err) return returnError(err)
var key = encrypter.key
var at = File.packAttributes(opt.attributes)
File.getCipher(key).encryptCBC(at)
self.aes.encryptKey(key)
self.api.request({
a: 'p',
t: opt.target.nodeId ? opt.target.nodeId : opt.target,
n: [{
h: hash.toString(),
t: 0,
a: crypto.e64(at),
k: crypto.e64(key),
}]}, function(err, response) {
if (err) return returnError(err)
var file = self._importFile(response.f[0])
self.emit('add', file)
stream.emit('complete', file)
if (cb) {
cb(null, file)
}
})
//console.log('body', e, hash.toString())
//console.log('target', self.root.nodeId)
})
var size_check = 0
encrypter.on('data', function(d) {
size_check += d.length
stream.emit('progress', {bytesLoaded: size_check, bytesTotal: size})
})
encrypter.on('end', function() {
if (size && size_check != size) {
return stream.emit('error', new Error('Specified data size does not match.'))
}
})
encrypter.pipe(httpreq)
pause.resume()
})
}
return stream
}
Storage.prototype.close = function() {
// does not handle, if still connecting or incomplete streams.
this.status = 'closed'
this.api.close()
}