Skip to content
Browse files

WebshellCall, POST calls

  • Loading branch information...
1 parent 6a5e3ca commit 2f015fbaa3a66ba4c77c26de6be5be6fa528c87c @bumpmann bumpmann committed Mar 25, 2013
Showing with 136 additions and 117 deletions.
  1. +136 −117 lib/webshell-node.js
View
253 lib/webshell-node.js
@@ -12,7 +12,15 @@ function Webshell() {
this._domain = '';
}
+var callnb = 1;
+function WebshellCall(opts) {
+ events.EventEmitter.call(this);
+ this._opts = opts;
+ this._callid = callnb++;
+}
+
util.inherits(Webshell, events.EventEmitter);
+util.inherits(WebshellCall, events.EventEmitter);
Webshell.prototype.init = function(options) {
this._apiKey = options.key || this._apiKey;
@@ -25,127 +33,138 @@ Webshell.prototype.init = function(options) {
Webshell.prototype.exec = function(options) {
var self = this;
- if ( ! self._apiKey) {
- self.emit('error', 'You must set parameter "key"');
- return;
- }
-
- if ( typeof options == 'string') {
- if (options[0] == '#')
- options = { 'hash': options.substr(1) };
- else
- options = { 'code': options };
- }
-
- if ( ! options.code && ! options.hash) {
- self.emit('error', 'You must set parameter "code" or "hash"');
- return;
- }
-
- // guess client session id
- var csid = options.csid || self._csid || options.session && options.session._wsh_csid || '';
-
- // construct request
- params = {};
-
- params.domain = options.domain || self._domain;
- if ( ! params.domain) {
- self.emit('error', 'You must set parameter "domain"');
- return;
- }
-
- if (options.hash)
- params.hash = options.hash;
- else if (options.code)
- {
- if (typeof options.code == 'string' && options.code.match(/^\#\![ \t]+[a-zA-Z_]+[ \t]*[\r\n]/))
- params.code = options.code
- else
- {
- if (typeof options.code == 'string')
- params.code = options.closure ? '(function() {' + options.code.trim() + "\n})();" : options.code.trim();
- else if (typeof options.code == 'function')
- params.code = '(' + options.code.toString().trim() + ')();';
- else
- {
- self.emit('error', 'Bad type for parameter "code"');
- return false;
- }
- }
- }
- else
- {
- self.emit('error', 'You must provide code parameter');
- return false;
- }
- params.key = options.key || self._apiKey || undefined;
- params.secret = options.secret || self._secretKey || undefined;
- if (csid)
- params.csid = csid;
- if (options.version)
- params.version = options.version;
- if (options.args)
- params.args = JSON.stringify(options.args);
- params.here = options.here || (params.code && params.code.indexOf('here()') >= 0);
- params.min = 'false';
-
- // execute request
- var reqoptions = {
- host: self._baseUrl,
- path: '/?' + querystring.stringify(params),
- headers: { 'Referer': 'http://' + params.domain }
- };
-
- var req = http.request(reqoptions, function(res) {
- content = '';
- res.setEncoding('utf8');
- res.on('data', function(chunk) { content += chunk });
- res.on('error', function(err) {
- self.emit('error', err)
- });
- res.on('end', function() {
- if ( ! content)
- return;
- try {
- data = JSON.parse(content);
- }
- catch (e) {
- self.emit('error', e.message);
- return false;
- }
- var res, meta;
- if ( ! Array.isArray(data))
- {
- self.emit('error', 'Bad response from server');
- return false;
- }
- var resultval = data.shift();
- for (var i in data) {
- var value = data[i];
- if (typeof value == 'object' && value != null && value._meta) {
- res = value.data;
- meta = value._meta;
- } else {
- res = value;
- meta = null;
+ var wshcall = new WebshellCall(options);
+ process.nextTick(function() {
+ if ( ! self._apiKey && ! options.key) {
+ wshcall.emit('error', 'You must set parameter "key"');
+ return;
+ }
+
+ if ( typeof options == 'string') {
+ if (options[0] == '#')
+ options = { 'hash': options.substr(1) };
+ else
+ options = { 'code': options };
+ }
+
+ if ( ! options.code && ! options.hash) {
+ wshcall.emit('error', 'You must set parameter "code" or "hash"');
+ return;
+ }
+
+ // guess client session id
+ var csid = options.csid || self._csid || options.session && options.session._wsh_csid || '';
+
+ // construct request
+ params = {};
+ bodyparams = {};
+
+ params.domain = options.domain || self._domain;
+ if ( ! params.domain) {
+ wshcall.emit('error', 'You must set parameter "domain"');
+ return;
+ }
+
+ if (options.hash)
+ params.hash = options.hash;
+ else if (options.code)
+ {
+ if (typeof options.code == 'string' && options.code.match(/^\#\![ \t]+[a-zA-Z_]+[ \t]*[\r\n]/))
+ bodyparams.code = options.code
+ else
+ {
+ if (typeof options.code == 'string')
+ bodyparams.code = options.closure ? '(function() {' + options.code.trim() + "\n})();" : options.code.trim();
+ else if (typeof options.code == 'function')
+ bodyparams.code = '(' + options.code.toString().trim() + ')();';
+ else
+ {
+ wshcall.emit('error', 'Bad type for parameter "code"');
+ return;
+ }
+ }
+ }
+ else
+ {
+ wshcall.emit('error', 'You must provide code parameter');
+ return;
+ }
+ params.key = options.key || self._apiKey || undefined;
+ bodyparams.secret = options.secret || self._secretKey || undefined;
+ if (csid)
+ bodyparams.csid = csid;
+ if (options.version)
+ params.version = options.version;
+ if (options.args)
+ bodyparams.args = JSON.stringify(options.args);
+ params.here = options.here || (bodyparams.code && bodyparams.code.indexOf('here()') >= 0);
+ params.min = 'false';
+
+ bodyparamsstr = JSON.stringify(bodyparams);
+
+ // execute request
+ var reqoptions = {
+ host: self._baseUrl,
+ method: 'POST',
+ path: '/?' + querystring.stringify(params),
+ headers: { 'Content-Type':'application/json', 'Content-Length':bodyparamsstr.length, 'Referer': 'http://' + params.domain }
+ };
+
+ var req = http.request(reqoptions, function(res) {
+ content = '';
+ res.setEncoding('utf8');
+ res.on('data', function(chunk) { content += chunk });
+ res.on('error', function(err) {
+ wshcall.emit('error', err)
+ });
+ res.on('end', function() {
+ if ( ! content)
+ return;
+ try {
+ data = JSON.parse(content);
+ }
+ catch (e) {
+ wshcall.emit('error', e.message);
+ return;
}
+ var res, meta;
+ if ( ! Array.isArray(data))
+ {
+ if (typeof data == "object" && data != null && typeof data.data == "object" && data.data != null && data.data.error)
+ wshcall.emit('error', data._meta.view);
+ else
+ wshcall.emit('error', 'Bad response from server');
+ return;
+ }
+
+ var resultval = data.shift();
+ for (var i in data) {
+ var value = data[i];
+ if (typeof value == 'object' && value != null && value._meta) {
+ res = value.data;
+ meta = value._meta;
+ } else {
+ res = value;
+ meta = null;
+ }
- if (meta && meta.cookie_add) {
- self.emit('setSession', meta.cookie_add.sid);
- if (options.session)
- options.session._wsh_csid = meta.cookie_add.sid;
- } else {
- self.emit('process', res, meta);
+ if (meta && meta.cookie_add) {
+ wshcall.emit('setSession', meta.cookie_add.sid);
+ if (options.session)
+ options.session._wsh_csid = meta.cookie_add.sid;
+ } else {
+ if (typeof res == 'object' && res != null && res.error && typeof meta == 'object' && meta != null)
+ wshcall.emit('error', meta.view);
+ else
+ wshcall.emit('process', res, meta);
+ }
}
- }
- self.emit('success', resultval);
+ wshcall.emit('success', resultval);
+ });
});
+ req.end(bodyparamsstr);
});
- req.on('error', function(err) {
- self.emit('error', err);
- });
- req.end();
- return this;
+ return wshcall;
}
-module.exports = new Webshell();
+module.exports = new Webshell();

0 comments on commit 2f015fb

Please sign in to comment.
Something went wrong with that request. Please try again.