-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (64 loc) · 2.05 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require('pmx').init({
http: true,
network: true
});
var http = require('http');
var url = require('url');
var request = require('request');
var config = require('config');
var probe = require('pmx').probe();
var StatsD = require('node-statsd'),
client = new StatsD();
http.createServer(onRequest).listen(80);
var counter = 0;
var statsd = 0;
var ips = config.get('ips');
console.log('Loading on IPs: ' + ips);
function onRequest(req, res) {
var queryData = url.parse(req.url, true).query;
var ip = ips[Math.floor(Math.random() * ips.length)];
//console.log('Proxying on ip: ' + ip + ' -- ' + decodeURIComponent(req.url));
if (queryData.url) {
counter++;
statsd++;
var headers = Object.assign({}, config.get('headers')[0]);
//console.log('Proxying on ip: ' + ip + ' -- ' + queryData.url);
if (req.headers && req.headers.authorization) {
headers.authorization = req.headers.authorization;
}
if (req.headers['x-api-key']) {
headers['x-api-key'] = req.headers['x-api-key'];
}
var internalRequest = request({
url: queryData.url,
family: 6,
localAddress: ip,
headers: headers,
time: true
});
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
internalRequest
.on('error', function(e) {
console.log('error: ' + e);
res.statusCode = 500;
res.end('error 500: ' + e);
})
.pipe(res);
} else {
//res.end('No url found');
res.end('loaderio-1e4ac91768b2952d177a51207bc4e3ac');
}
}
var metric = probe.metric({
name: 'Requests',
value: function() {
return counter;
}
});
var last = 0;
var timer = setInterval(function() {
client.increment('proxy_request', statsd - last);
last = statsd;
}, 1000);