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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/**
* Module dependencies.
*/
var net = require('net');
var url = require('url');
var http = require('http');
var assert = require('assert');
var debug = require('debug')('proxy');
// log levels
debug.request = require('debug')('proxy ← ← ←');
debug.response = require('debug')('proxy → → →');
debug.proxyRequest = require('debug')('proxy ↑ ↑ ↑');
debug.proxyResponse = require('debug')('proxy ↓ ↓ ↓');
// hostname
var hostname = require('os').hostname();
// proxy server version
var version = require('./package.json').version;
/**
* Module exports.
*/
module.exports = setup;
/**
* Sets up an `http.Server` or `https.Server` instance with the necessary
* "request" and "connect" event listeners in order to make the server act as an
* HTTP proxy.
*
* @param {http.Server|https.Server} server
* @param {Object} options
* @api public
*/
function setup(server, options) {
if (!server) server = http.createServer();
server.on('request', onrequest);
server.on('connect', onconnect);
return server;
}
/**
* 13.5.1 End-to-end and Hop-by-hop Headers
*
* Hop-by-hop headers must be removed by the proxy before passing it on to the
* next endpoint. Per-request basis hop-by-hop headers MUST be listed in a
* Connection header, (section 14.10) to be introduced into HTTP/1.1 (or later).
*/
var hopByHopHeaders = [
'Connection',
'Keep-Alive',
'Proxy-Authenticate',
'Proxy-Authorization',
'TE',
'Trailers',
'Transfer-Encoding',
'Upgrade'
];
// create a case-insensitive RegExp to match "hop by hop" headers
var isHopByHop = new RegExp('^(' + hopByHopHeaders.join('|') + ')$', 'i');
/**
* Iterator function for the request/response's "headers".
* Invokes `fn` for "each" header entry in the request.
*
* @api private
*/
function eachHeader(obj, fn) {
if (Array.isArray(obj.rawHeaders)) {
// ideal scenario... >= node v0.11.x
// every even entry is a "key", every odd entry is a "value"
var key = null;
obj.rawHeaders.forEach(function(v) {
if (key === null) {
key = v;
} else {
fn(key, v);
key = null;
}
});
} else {
// otherwise we can *only* proxy the header names as lowercase'd
var headers = obj.headers;
if (!headers) return;
Object.keys(headers).forEach(function(key) {
var value = headers[key];
if (Array.isArray(value)) {
// set-cookie
value.forEach(function(val) {
fn(key, val);
});
} else {
fn(key, value);
}
});
}
}
/**
* HTTP GET/POST/DELETE/PUT, etc. proxy requests.
*/
function onrequest(req, res) {
debug.request('%s %s HTTP/%s ', req.method, req.url, req.httpVersion);
var server = this;
var socket = req.socket;
// pause the socket during authentication so no data is lost
socket.pause();
authenticate(server, req, function(err, auth) {
socket.resume();
if (err) {
// an error occured during login!
res.writeHead(500);
res.end((err.stack || err.message || err) + '\n');
return;
}
if (!auth) return requestAuthorization(req, res);
var parsed = url.parse(req.url);
// proxy the request HTTP method
parsed.method = req.method;
// setup outbound proxy request HTTP headers
var headers = {};
var hasXForwardedFor = false;
var hasVia = false;
var via = '1.1 ' + hostname + ' (proxy/' + version + ')';
parsed.headers = headers;
eachHeader(req, function(key, value) {
debug.request('Request Header: "%s: %s"', key, value);
var keyLower = key.toLowerCase();
if (!hasXForwardedFor && 'x-forwarded-for' === keyLower) {
// append to existing "X-Forwarded-For" header
// http://en.wikipedia.org/wiki/X-Forwarded-For
hasXForwardedFor = true;
value += ', ' + socket.remoteAddress;
debug.proxyRequest(
'appending to existing "%s" header: "%s"',
key,
value
);
}
if (!hasVia && 'via' === keyLower) {
// append to existing "Via" header
hasVia = true;
value += ', ' + via;
debug.proxyRequest(
'appending to existing "%s" header: "%s"',
key,
value
);
}
if (isHopByHop.test(key)) {
debug.proxyRequest('ignoring hop-by-hop header "%s"', key);
} else {
var v = headers[key];
if (Array.isArray(v)) {
v.push(value);
} else if (null != v) {
headers[key] = [v, value];
} else {
headers[key] = value;
}
}
});
// add "X-Forwarded-For" header if it's still not here by now
// http://en.wikipedia.org/wiki/X-Forwarded-For
if (!hasXForwardedFor) {
headers['X-Forwarded-For'] = socket.remoteAddress;
debug.proxyRequest(
'adding new "X-Forwarded-For" header: "%s"',
headers['X-Forwarded-For']
);
}
// add "Via" header if still not set by now
if (!hasVia) {
headers.Via = via;
debug.proxyRequest('adding new "Via" header: "%s"', headers.Via);
}
// custom `http.Agent` support, set `server.agent`
var agent = server.agent;
if (null != agent) {
debug.proxyRequest(
'setting custom `http.Agent` option for proxy request: %s',
agent
);
parsed.agent = agent;
agent = null;
}
if (null == parsed.port) {
// default the port number if not specified, for >= node v0.11.6...
// https://github.com/joyent/node/issues/6199
parsed.port = 80;
}
if ('http:' != parsed.protocol) {
// only "http://" is supported, "https://" should use CONNECT method
res.writeHead(400);
res.end('Only "http:" protocol prefix is supported\n');
return;
}
if (server.localAddress) {
parsed.localAddress = server.localAddress;
}
var gotResponse = false;
var proxyReq = http.request(parsed);
debug.proxyRequest('%s %s HTTP/1.1 ', proxyReq.method, proxyReq.path);
proxyReq.on('response', function(proxyRes) {
debug.proxyResponse('HTTP/1.1 %s', proxyRes.statusCode);
gotResponse = true;
var headers = {};
eachHeader(proxyRes, function(key, value) {
debug.proxyResponse(
'Proxy Response Header: "%s: %s"',
key,
value
);
if (isHopByHop.test(key)) {
debug.response('ignoring hop-by-hop header "%s"', key);
} else {
var v = headers[key];
if (Array.isArray(v)) {
v.push(value);
} else if (null != v) {
headers[key] = [v, value];
} else {
headers[key] = value;
}
}
});
debug.response('HTTP/1.1 %s', proxyRes.statusCode);
res.writeHead(proxyRes.statusCode, headers);
proxyRes.pipe(res);
res.on('finish', onfinish);
});
proxyReq.on('error', function(err) {
debug.proxyResponse(
'proxy HTTP request "error" event\n%s',
err.stack || err
);
cleanup();
if (gotResponse) {
debug.response(
'already sent a response, just destroying the socket...'
);
socket.destroy();
} else if ('ENOTFOUND' == err.code) {
debug.response('HTTP/1.1 404 Not Found');
res.writeHead(404);
res.end();
} else {
debug.response('HTTP/1.1 500 Internal Server Error');
res.writeHead(500);
res.end();
}
});
// if the client closes the connection prematurely,
// then close the upstream socket
function onclose() {
debug.request(
'client socket "close" event, aborting HTTP request to "%s"',
req.url
);
proxyReq.abort();
cleanup();
}
socket.on('close', onclose);
function onfinish() {
debug.response('"finish" event');
cleanup();
}
function cleanup() {
debug.response('cleanup');
socket.removeListener('close', onclose);
res.removeListener('finish', onfinish);
}
req.pipe(proxyReq);
});
}
/**
* HTTP CONNECT proxy requests.
*/
function onconnect(req, socket, head) {
debug.request('%s %s HTTP/%s ', req.method, req.url, req.httpVersion);
assert(
!head || 0 == head.length,
'"head" should be empty for proxy requests'
);
var res;
var target;
var gotResponse = false;
// define request socket event listeners
socket.on('close', function onclientclose() {
debug.request('HTTP request %s socket "close" event', req.url);
});
socket.on('end', function onclientend() {
debug.request('HTTP request %s socket "end" event', req.url);
});
socket.on('error', function onclienterror(err) {
debug.request(
'HTTP request %s socket "error" event:\n%s',
req.url,
err.stack || err
);
});
// define target socket event listeners
function ontargetclose() {
debug.proxyResponse('proxy target %s "close" event', req.url);
socket.destroy();
}
function ontargetend() {
debug.proxyResponse('proxy target %s "end" event', req.url);
}
function ontargeterror(err) {
debug.proxyResponse(
'proxy target %s "error" event:\n%s',
req.url,
err.stack || err
);
if (gotResponse) {
debug.response(
'already sent a response, just destroying the socket...'
);
socket.destroy();
} else if ('ENOTFOUND' == err.code) {
debug.response('HTTP/1.1 404 Not Found');
res.writeHead(404);
res.end();
} else {
debug.response('HTTP/1.1 500 Internal Server Error');
res.writeHead(500);
res.end();
}
}
function ontargetconnect() {
debug.proxyResponse('proxy target %s "connect" event', req.url);
debug.response('HTTP/1.1 200 Connection established');
gotResponse = true;
res.removeListener('finish', onfinish);
res.writeHead(200, 'Connection established');
res.flushHeaders();
// relinquish control of the `socket` from the ServerResponse instance
res.detachSocket(socket);
// nullify the ServerResponse object, so that it can be cleaned
// up before this socket proxying is completed
res = null;
socket.pipe(target);
target.pipe(socket);
}
// create the `res` instance for this request since Node.js
// doesn't provide us with one :(
// XXX: this is undocumented API, so it will break some day (ノಠ益ಠ)ノ彡┻━┻
res = new http.ServerResponse(req);
res.shouldKeepAlive = false;
res.chunkedEncoding = false;
res.useChunkedEncodingByDefault = false;
res.assignSocket(socket);
// called for the ServerResponse's "finish" event
// XXX: normally, node's "http" module has a "finish" event listener that would
// take care of closing the socket once the HTTP response has completed, but
// since we're making this ServerResponse instance manually, that event handler
// never gets hooked up, so we must manually close the socket...
function onfinish() {
debug.response('response "finish" event');
res.detachSocket(socket);
socket.end();
}
res.once('finish', onfinish);
// pause the socket during authentication so no data is lost
socket.pause();
authenticate(this, req, function(err, auth) {
socket.resume();
if (err) {
// an error occured during login!
res.writeHead(500);
res.end((err.stack || err.message || err) + '\n');
return;
}
if (!auth) return requestAuthorization(req, res);
var parts = req.url.split(':');
var host = parts[0];
var port = +parts[1];
var opts = { host: host, port: port };
debug.proxyRequest('connecting to proxy target %j', opts);
target = net.connect(opts);
target.on('connect', ontargetconnect);
target.on('close', ontargetclose);
target.on('error', ontargeterror);
target.on('end', ontargetend);
});
}
/**
* Checks `Proxy-Authorization` request headers. Same logic applied to CONNECT
* requests as well as regular HTTP requests.
*
* @param {http.Server} server
* @param {http.ServerRequest} req
* @param {Function} fn callback function
* @api private
*/
function authenticate(server, req, fn) {
var hasAuthenticate = 'function' == typeof server.authenticate;
if (hasAuthenticate) {
debug.request('authenticating request "%s %s"', req.method, req.url);
server.authenticate(req, fn);
} else {
// no `server.authenticate()` function, so just allow the request
fn(null, true);
}
}
/**
* Sends a "407 Proxy Authentication Required" HTTP response to the `socket`.
*
* @api private
*/
function requestAuthorization(req, res) {
// request Basic proxy authorization
debug.response(
'requesting proxy authorization for "%s %s"',
req.method,
req.url
);
// TODO: make "realm" and "type" (Basic) be configurable...
var realm = 'proxy';
var headers = {
'Proxy-Authenticate': 'Basic realm="' + realm + '"'
};
res.writeHead(407, headers);
res.end();
}
|