1 | const QueryString = require('querystring');
|
2 | const Https = require('https');
|
3 | const Fs = require('fs');
|
4 | const Tmp = require('tmp');
|
5 | const FormData = require('form-data')
|
6 |
|
7 | const Files = require('./file.js')
|
8 |
|
9 |
|
10 | module.exports = {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | createForm: function(token, raw_data){
|
18 | var tmp_obj = Tmp.fileSync();
|
19 | Files.saveResult(tmp_obj.name, raw_data);
|
20 | var form = new FormData();
|
21 | form.append('token', token);
|
22 | form.append('my_buffer', new Buffer(10));
|
23 | form.append('fileformat', 'json');
|
24 | form.append('reportfile', Fs.createReadStream(tmp_obj.name));
|
25 |
|
26 | return form;
|
27 | },
|
28 |
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | createUrl: function(host, port){
|
38 | const end = '/r1_report_upload_endpoint/';
|
39 | var start = (port == 443) ? 'https://' : 'http://';
|
40 | var mid = (host == null) ? 'reshift.softwaresecured.com' : host;
|
41 | return start + mid + ":" + port + end;
|
42 | },
|
43 |
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | sendResult: function(token, raw_data, host, port) {
|
54 |
|
55 | var form = this.createForm(token, raw_data);
|
56 | var reshift_url = this.createUrl(host, port);
|
57 |
|
58 | form.submit(reshift_url, function(error, response, body){
|
59 | if (!error && response.statusCode == 200) {
|
60 | console.log("INFO - Successfully upload the report.");
|
61 | } else {
|
62 | let status = (response == null) ? null : response.statusCode
|
63 | let msg = (response == null) ? null : response.statusMessage
|
64 | console.log("INFO - Failed to upload the report, status: " + status);
|
65 | console.log("INFO - Return message: " + msg);
|
66 | }
|
67 | });
|
68 | }
|
69 | };
|