Skip to content

Commit c0586f9

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
sending requests via proxy + request lib added
1 parent a22f0bd commit c0586f9

File tree

7 files changed

+58
-49
lines changed

7 files changed

+58
-49
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
npm-debug.log
2+
npm-debug.log
3+
proxy.js
4+
proxy2.js

config/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
'PROTOCOL' : 'https',
23
'HOST': 'dev.stackify.com',
34
'PORT': 443,
45
'IDENTIFY_PATH': '/API/Metrics/IdentifyApp',

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = {
1010

1111
start: function(options) {
1212
api.methods.identifyApp(options);
13-
exc.exc();
14-
},
13+
/* exc.exc();
14+
*/ },
1515

1616
log: logger.methods.log,
1717
trace: logger.methods.trace,

lib/api.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var os = require('os'),
2+
url = require('url'),
23
util = require('util'),
34
send = require('./sender'),
45
logger = require('./logger'),
@@ -7,33 +8,36 @@ var os = require('os'),
78

89
appname = module.exports.name,
910

10-
options = {
11-
hostname: CONFIG.HOST,
12-
port: CONFIG.PORT,
13-
method: 'POST',
14-
secureProtocol: 'SSLv3_method',
15-
headers: {
16-
'Content-Type': 'application/json',
17-
'X-Stackify-Key': CONFIG.APIKEY,
18-
'X-Stackify-PV': 'V1'
11+
options = function options(path, body) {
12+
return {
13+
url: CONFIG.PROTOCOL + '://' + CONFIG.HOST + path,
14+
method: 'POST',
15+
json: true,
16+
headers: {
17+
'Content-Type': 'application/json',
18+
'X-Stackify-Key': CONFIG.APIKEY,
19+
'X-Stackify-PV': 'V1'
20+
},
21+
body: body
1922
}
2023
};
2124

2225
module.exports.methods = {
2326

2427
identifyApp: function identifyApp(settings) {
25-
var opt = options,
26-
fail_counter = 0,
27-
data = {
28+
var body = {
2829
DeviceName: os.hostname(),
2930
AppName: appname,
3031
ConfiguredAppName: appname,
3132
AppLocaton: process.env.PWD
3233
},
34+
opt = options(CONFIG.IDENTIFY_PATH, body),
35+
fail_counter = 0,
3336
callback = function (data) {
3437
console.log('successfully identified');
3538

36-
CONFIG.APP_DETAILS = JSON.parse(data);
39+
CONFIG.APP_DETAILS = data;
40+
console.log(util.inspect(data));
3741
CONFIG.APP_DETAILS.ENV = settings.env;
3842
CONFIG.APIKEY = settings.apiKey;
3943

@@ -44,28 +48,29 @@ module.exports.methods = {
4448
fail_counter += 1;
4549
if (fail_counter <= CONFIG.REQUEST_ATTEMPTS) {
4650
setTimeout(function () {
47-
send(opt, data, callback, fail);
51+
send(opt, callback, fail);
4852
}, CONFIG.REQUEST_TIMER);
4953
}
5054
};
5155

52-
opt.path = CONFIG.IDENTIFY_PATH;
53-
5456
console.log('Identifying...');
5557

58+
if (settings.proxy) {
59+
CONFIG.PROXY = opt.proxy = settings.proxy
60+
};
61+
5662
if (typeof (settings.apiKey) === 'string' && typeof (settings.env) === 'string') {
5763
opt.headers['X-Stackify-Key'] = settings.apiKey;
5864
CONFIG.APPNAME = appname;
59-
send(opt, data, callback, fail);
65+
send(opt, callback, fail);
6066
} else {
6167
throw new TypeError('You have to pass apiKey and env parameters');
6268
}
6369

6470
},
6571

6672
postLogs: function postLogs(messages, cb, fail) {
67-
var opt = options,
68-
data = {
73+
var body = {
6974
CDID: CONFIG.APP_DETAILS.DeviceID,
7075
CDAppID: CONFIG.APP_DETAILS.DeviceAppID,
7176
AppNameID: CONFIG.APP_DETAILS.AppNameID,
@@ -78,9 +83,14 @@ module.exports.methods = {
7883
Logger: 'Node.js Stackify Logger ver. 1.0',
7984
Platform: 'Node.js',
8085
Msgs : messages
86+
},
87+
opt = options(CONFIG.LOG_SAVE_PATH, body);
88+
89+
if (CONFIG.PROXY) {
90+
opt.proxy = CONFIG.PROXY;
8191
};
8292

83-
opt.path = CONFIG.LOG_SAVE_PATH;
84-
send(options, data, cb, fail);
93+
94+
send(opt, cb, fail);
8595
}
8696
};

lib/sender.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,32 @@
55
###### @fail {Function} **Optional** function to be executed if request wasn't succesful
66
Low level function for sending http/https requests
77
*/
8-
var https = require('https'),
9-
util = require('util');
8+
var http = require('http'),
9+
util = require('util'),
10+
request = require('request');
1011

11-
module.exports = function sender(options, data, cb, fail) {
12-
var callback = function (response) {
13-
var str = '';
14-
15-
//another chunk of data has been recieved, so append it to `str`
16-
response.on('data', function (chunk) {
17-
str += chunk;
18-
});
19-
20-
//the whole response has been recieved
21-
response.on('end', function () {
12+
module.exports = function sender(options, cb, fail) {
13+
console.log('here');
14+
console.log(util.inspect(options));
15+
var callback = function (error, response, body) {
16+
if (!error) {
2217
if (response.statusCode === 200) {
2318
if (cb) {
24-
cb(str);
19+
cb(body);
20+
console.log('success');
2521
}
2622
} else {
2723
if (fail) {
2824
fail();
29-
console.log('fail');
25+
console.log('fail:', body);
3026
}
3127
}
32-
});
33-
},
34-
req = https.request(options, callback);
28+
} else {
29+
console.log(error);
30+
}
31+
};
32+
33+
request(options, callback);
3534

36-
req.on('error', function (e) {
37-
console.log('err: ' + e);
38-
});
3935

40-
req.write(JSON.stringify(data));
41-
req.end();
4236
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
"author": "Stackify",
1414
"license": "ISC",
1515
"dependencies": {
16+
"https-proxy-agent": "^0.3.5",
1617
"on-finished": "^2.1.1",
1718
"pkginfo": "^0.3.0",
19+
"request": "^2.48.0",
1820
"stack-trace": "0.0.9"
1921
}
2022
}

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var http = require('http');
22
var util = require('util');
3-
var stackify = require('./index')({apiKey: '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr', env: 'dev'});
3+
var stackify = require('./index');
4+
stackify.start({apiKey: '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr', env: 'dev'/*, proxy: 'http://74.50.126.249:8089'*/});
45
var stack = require('stack-trace');
56
var pkginfo = require('pkginfo')(module, 'name');
67
var express = require("express");
@@ -14,7 +15,6 @@ var foo = function foo() {
1415
stackify.error('dfg');
1516
};
1617
foo();
17-
fg;
1818

1919
setInterval(function () {
2020
stackify.info('test');

0 commit comments

Comments
 (0)