forked from loowid/loowid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
188 lines (157 loc) · 4.71 KB
/
proxy.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
/*global unescape: true */
/**
* Local proxy (load balancing for development)
* node proxy [n] -> Run proxy over 443 try to find nodes, from BasePort + 1 to BasePort + n
*/
// This example demonstrates using sticky session
// support
var httpProxy = require('http-proxy');
var fs = require('fs');
var http = require('http');
var https = require('https');
var logger = require('./log.js').getLog('proxy');
var sport = process.env.LOOWID_HTTPS_PORT || 443;
var port = process.env.LOOWID_HTTP_PORT || 80;
var basePort = Number(process.env.LOOWID_BASE_PORT || 8000);
var isRunningTests = function() {
return (process.argv[2] && process.argv[2].indexOf('jasmine_node:')===0) || process.argv[3]==='test';
};
//
//Create the HTTPS proxy server in front of a HTTP server
//
var targets = [];
var parseCookies = function(request) {
var list = {}, rc = request.headers.cookie;
if (rc) {
rc.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
list[parts.shift().trim()] = decodeURI(parts.join('='));
});
}
return list;
};
var findStickyServer = function(req) {
var sid = unescape(parseCookies(req).stickyid);
var id = targets.indexOf(sid);
if (id<0) {
sid = targets[Math.floor(Math.random()*targets.length)];
req.headers.stickyid = sid;
}
logger.debug(sid+req.url);
return sid;
};
var addBackend = function(srv) {
var i = targets.indexOf(srv);
if (i<0) {
logger.info('Add backend '+srv);
targets.push(srv);
}
};
var removeBackend = function(srv) {
var i = targets.indexOf(srv);
if (i>=0) {
logger.info('Remove backend '+srv);
targets.splice(i,1);
}
};
var validateBackend = function(h,p) {
var options = {
host: h,
path: '/img/icons/favicon.ico',
port: p
};
var req = http.get(options,function(response){
addBackend('http://'+h+':'+p);
});
req.on('error',function(err){
removeBackend('http://'+h+':'+p);
});
};
var backends = isNaN(process.argv[2])?Number(process.env.JASMINE_NODES || 2):(process.argv[2]-0);
var checkServers = function() {
for (var j=1; j<=backends; j+=1) {
validateBackend('localhost',basePort+j);
}
// Check new servers every 5 seconds !!
setTimeout(checkServers,5000);
};
var proxy = null;
var httpServer = null;
try {
var credentials = {};
var certificateAvailable = true;
try {
var fs = require('fs');
// Generado con http://www.cert-depot.com/
var privateKey = fs.readFileSync(process.env.PRIVATE_KEY || 'private.pem','utf-8');
var certificate = fs.readFileSync(process.env.PUBLIC_KEY || 'public.pem','utf-8');
var credentials = {
key : privateKey,
cert : certificate
};
} catch (ex) {
logger.warn(ex.message);
certificateAvailable = false;
}
if (!isRunningTests() && certificateAvailable) {
proxy = httpProxy.createProxyServer({
target: 'http://localhost',
ssl: credentials,
ws:true,
secure:true
});
httpServer = https.createServer(credentials, function(req, res){
var tg = findStickyServer(req);
proxy.web(req, res, {target:tg}, function(){
removeBackend(tg);
});
}).listen(sport, '0.0.0.0');
var server = http.createServer(function (req, res) {
// optional, for HSTS
// see https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security
res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');
if (req.headers['x-forwarded-proto'] !== 'https') {
var url = 'https://' + req.headers.host + (sport!==443?':'+sport:'') + '/';
res.writeHead(301, {'location': url});
return res.end('Redirecting to <a href="' + url + '">' + url + '</a>.');
}
});
server.listen(port);
logger.info('Listen for redirect on port ' + port);
} else {
// Test is always running under http
proxy = httpProxy.createProxyServer({
target: 'http://localhost',
ws:true
});
httpServer = http.createServer(function(req, res){
var tg = findStickyServer(req);
proxy.web(req, res, {target:tg}, function(){
removeBackend(tg);
});
}).listen(sport, '0.0.0.0');
}
} catch (ex) {
logger.warn(ex.message);
}
process.on('uncaughtException', function (err) {
if (err.errno === 'ECONNRESET') {
// Backend server fail !!
checkServers();
} else {
logger.error(err);
}
});
httpServer.on('upgrade', function (req, socket, head) {
var tg = findStickyServer(req);
proxy.ws(req, socket, head, {target:tg}, function(){
removeBackend(tg);
});
});
logger.info('Running load balancer in port '+sport);
// Look for backends !!
setTimeout(checkServers,5000);
exports.isReady = function() {
return targets.length===backends;
};