This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passer.js
103 lines (80 loc) · 1.95 KB
/
passer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
if (process.env['AIRBRAKE_KEY']) {
var airbrake = require('airbrake');
airbrake = airbrake.createClient(process.env['AIRBRAKE_KEY']);
airbrake.handleExceptions();
}
var Router = require('alice-proxy')
, Http = require('http')
, Net = require('net')
;
var router
, port
, host
, alice_host
, alice_port
;
host = process.env['PASSER_HOST'] || 'localhost';
port = process.argv[2] || process.env['PASSER_PORT'] || '5200';
port = parseInt(port, 10);
alice_host = process.env['ALICE_HOST'] || 'localhost';
alice_port = process.env['ALICE_PORT'] || '5000';
alice_port = parseInt(alice_port, 10);
router = Router.create('passer', function(env){
var backend
;
backend = env.headers['x-pluto-backend-port'];
delete env.headers['x-pluto-backend-port'];
if (!backend) {
// return 503
env.respond(503);
return;
}
backend = parseInt(backend, 10);
if (env.headers['x-alice-probe'] === 'true') {
var c
;
c = Net.createConnection(port, host);
c.setTimeout(1000);
c.on('connect', function(){
c.destroy();
env.respond(404);
});
c.on('timeout', function(){
c.destroy();
env.respond(503);
});
c.on('error', function(){
c.destroy();
env.respond(503);
});
} else {
env.forward(host, backend);
}
});
router.listen(port);
console.log('listening on port '+port);
var _ping = function(){
var body
, req
;
body = JSON.stringify([{'type': 'passer', 'machine': host, 'port': port}]);
req = Http.request({
host: alice_host,
port: alice_port,
path: '/api_v1/register.json',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accepts': 'application/json',
'Content-Length': body.length
}
}, function(res){
});
req.on('error', function(){
console.log('Failed to ping!');
});
req.write(body);
req.end();
};
setInterval(_ping, 600000); // every 10 minutes
setTimeout(_ping, 30000);