Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions lib/msfrpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
*/
const URL = require('url');
const decamelize = require('decamelize');
const request = require('request');
const fetch = require('node-fetch');
const MsgPack5 = require('msgpack5');
const Promise = require('bluebird');

/**
* Instance MsgPack5.
Expand Down Expand Up @@ -106,41 +105,30 @@ module.exports = class MsfRpc {
// Encode method and arguments as msgpack buffer.
const buffer = msgpack.encode([method, ...args]);

return new Promise((resolve) => {
// Send post request to the MsfRpc server.
request({
method: 'POST',
uri: `${this.ssl ? 'https' : 'http'}://${this.host}:${this.port}/api/1.0`,
body: buffer,
headers: {
'content-type': 'binary/message-pack'
},
// This translates to encode body as binary data.
encoding: null
}, (error, response, body) => {
if(error) {
throw error;
} else {
// Parse body.
body = this._parseBody(body)
// Send post request to the MsfRpc server.
return fetch(`${this.ssl ? 'https' : 'http'}://${this.host}:${this.port}/api/1.0`, {
method: 'POST',
body: buffer,
headers: {
'content-type': 'binary/message-pack'
}
}).then(result => result.buffer()).then(body => {
// Parse body.
body = this._parseBody(body)

// If server responded with an exception, build and throw error.
if(body.error) {
const errorLines = [];
errorLines.push(`${body.error_message}`);
errorLines.push(` Backtrace:`);
body.error_backtrace.forEach((trace) => {
errorLines.push(` ${trace}`);
});
errorLines.push('');
throw new Error(errorLines.join('\n'));
} else {
// Else, resolve with parsed body.
resolve(body);
}
// If server responded with an exception, build and throw error.
if(body.error) {
const errorLines = [];
errorLines.push(`${body.error_message}`);
errorLines.push(` Backtrace:`);
body.error_backtrace.forEach((trace) => {
errorLines.push(` ${trace}`);
});
errorLines.push('');
throw new Error(errorLines.join('\n'));
}
});
});
return body
})
}

/**
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"bluebird": "^3.5.0",
"decamelize": "^1.2.0",
"msgpack5": "^3.5.0",
"request": "^2.81.0"
},
"devDependencies": {}
"node-fetch": "^2.6.1"
}
}