|
| 1 | +var config = require('../../config.json'); |
1 | 2 | var debug = require('debug')('cipherlayer:service'); |
2 | 3 | var request = require('request'); |
| 4 | +var httpProxy = require('http-proxy'); |
| 5 | +var _ = require('lodash'); |
| 6 | + |
| 7 | +var proxy = httpProxy.createProxyServer({}); |
| 8 | + |
| 9 | +proxy.on('proxyReq', function(proxyReq, req, res, options) { |
| 10 | + debug('> http-proxy request received'); |
| 11 | +}); |
| 12 | + |
| 13 | +proxy.on('proxyRes', function() { |
| 14 | + debug('< http-proxy response received'); |
| 15 | +}); |
| 16 | + |
| 17 | +proxy.on('error', function (err, req, res) { |
| 18 | + debug('http-proxy error occurred', err); |
| 19 | + res.send(500, {err:' auth_proxy_error', des: 'there was an internal error when redirecting the call to protected service'}); |
| 20 | +}); |
| 21 | + |
| 22 | + |
| 23 | + |
3 | 24 |
|
4 | 25 | function propagateRequest(req, res, next){ |
5 | 26 | var start = Date.now(); |
6 | 27 |
|
7 | | - request(req.options, function(err, private_res, body) { |
8 | | - var timing = Date.now() - start; |
9 | | - if(err) { |
10 | | - debug('<= ' + private_res.statusCode + ' ' + err + ' ' + timing + 'ms'); |
11 | | - res.send(500, {err:' auth_proxy_error', des: 'there was an internal error when redirecting the call to protected service'}); |
| 28 | + var useDirectProxy = _.some(config.directProxyUrls, function(pattern) { |
| 29 | + return req.url.match(new RegExp(pattern, 'g')); |
| 30 | + }); |
| 31 | + |
| 32 | + // if url is a direct proxy request, use http-proxy |
| 33 | + if (useDirectProxy) { |
| 34 | + |
| 35 | + proxy.web(req, res, { |
| 36 | + target: 'http://'+ config.private_host + ':' + config.private_port |
| 37 | + }); |
| 38 | + return; |
| 39 | + |
12 | 40 | } else { |
13 | | - try{ |
14 | | - body = JSON.parse(body); |
15 | | - debug('<= ' + private_res.statusCode + ' json body' + ' ' + timing + 'ms'); |
16 | | - } catch(ex) { |
17 | | - debug('<= ' + private_res.statusCode + ' no json body' + ' ' + timing + 'ms'); |
18 | | - } |
19 | | - if(private_res.statusCode === 302){ |
20 | | - res.header('Location', private_res.headers.location); |
21 | | - res.send(302); |
22 | | - } else { |
23 | | - res.send(Number(private_res.statusCode), body); |
24 | | - } |
25 | | - } |
26 | 41 |
|
27 | | - return next(); |
28 | | - }); |
| 42 | + // This are the normal requests |
| 43 | + |
| 44 | + request(req.options, function(err, private_res, body) { |
| 45 | + var timing = Date.now() - start; |
| 46 | + if(err) { |
| 47 | + debug('<= ' + private_res.statusCode + ' ' + err + ' ' + timing + 'ms'); |
| 48 | + res.send(500, {err:' auth_proxy_error', des: 'there was an internal error when redirecting the call to protected service'}); |
| 49 | + } else { |
| 50 | + try{ |
| 51 | + body = JSON.parse(body); |
| 52 | + debug('<= ' + private_res.statusCode + ' json body' + ' ' + timing + 'ms'); |
| 53 | + } catch(ex) { |
| 54 | + debug('<= ' + private_res.statusCode + ' no json body' + ' ' + timing + 'ms'); |
| 55 | + } |
| 56 | + if(private_res.statusCode === 302){ |
| 57 | + res.header('Location', private_res.headers.location); |
| 58 | + res.send(302); |
| 59 | + } else { |
| 60 | + res.send(Number(private_res.statusCode), body); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return next(); |
| 65 | + }); |
| 66 | + } |
29 | 67 | } |
30 | 68 |
|
31 | 69 | module.exports = propagateRequest; |
0 commit comments