-
Notifications
You must be signed in to change notification settings - Fork 1
/
transfer.js
69 lines (63 loc) · 2.44 KB
/
transfer.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
// Modified copy of https://github.com/roccomuso/transfer-sh/
var fs = require('fs')
var os = require('os')
var path = require('path')
var got = require('got')
var concat = require('concat-stream')
var pump = require('pump')
var crypto = require('crypto')
var through2 = require('through2')
var base64 = require('base64-stream')
var Batch = require('batched-stream')
var eos = require('end-of-stream')
var PassThroughStream = require('stream').PassThrough
var isReadable = require('is-stream').readable
var isWritable = require('is-stream').writable
var domain = 'https://t.pwnsquad.net'
function Transfer (fileInput, opts) {
if (!fileInput) throw Error('File input required')
var algorithm = 'aes-256-cbc'
this.fileInput = fileInput
this.opts = opts || {}
this.httpOptions = {}
this.inputStream = isReadable(fileInput) ? fileInput : fs.createReadStream(fileInput)
this.encryptedStream = null
this.sEncrypt = this.opts.password ? crypto.createCipher(algorithm, this.opts.password) : new PassThroughStream()
this.sDecrypt = this.opts.password ? crypto.createDecipher(algorithm, this.opts.password) : new PassThroughStream()
}
Transfer.prototype.upload = function () {
var self = this
if (this.opts.password) this._crypt()
return new Promise(function (resolve, reject) {
var fileName = isReadable(self.fileInput) ? self.opts.name || 'noname' : path.basename(self.fileInput)
pump(self.encryptedStream || self.inputStream,
got.stream.put(domain + '/' + fileName, self.httpOptions),
concat(function (link) { resolve(link.toString()) }),
reject)
})
}
Transfer.prototype._crypt = function () {
this.encryptedStream = this.inputStream.pipe(this.sEncrypt)
.pipe(base64.encode())
.pipe(new Batch({size: 76, strictMode: false}))
.pipe(through2(function (chunk, enc, next) {
this.push(chunk + os.EOL) // new line every 76 chars
next()
}))
}
Transfer.prototype.decrypt = function (destination) {
if (!destination) throw Error('destination required for the decrypt method')
var self = this
return new Promise(function (resolve, reject) {
var wStream = isWritable(destination) ? destination : fs.createWriteStream(destination)
eos(wStream, function (err) {
if (err) return reject(new Error('stream had an error or closed early'))
resolve(this)
})
/* start decrypt */
self.inputStream.pipe(base64.decode())
.pipe(self.sDecrypt)
.pipe(wStream)
})
}
module.exports = Transfer