Created
July 7, 2020 20:51
-
-
Save sscarduzio/d24970294ca20388ff5efbd62e8a8bc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
const httpProxy = require('http-proxy') | |
const l = require('chalk') | |
// ---- Kibana | |
function filterHeaders(prefix, o) { | |
var filtered = [] | |
for (k of Object.keys(o)) { | |
if (k.indexOf(prefix) >= 0) { | |
var partialObj = {} | |
partialObj[k] = o[k] | |
filtered.push(partialObj) | |
} | |
} | |
return filtered | |
} | |
function log(prefix, req, proxyRes) { | |
let code = proxyRes.statusCode | |
let logger = l.green | |
if(code >= 500){ | |
logger = l.bold.red | |
} | |
else if (code >= 400) { | |
logger = l.bold.keyword('orange') | |
} | |
console.log(logger(prefix + '\t' + code + ` ${req.method} ${req.url} req hdrs: ${JSON.stringify( filterHeaders("uthoriz", req.headers) )}response hdrs: ${JSON.stringify( filterHeaders("uthoriz", proxyRes.headers) )}`)) | |
} | |
var kibanaProxy = httpProxy.createProxyServer({ secure: false }) | |
kibanaProxy.on('proxyRes', function (proxyRes, req, res) { | |
log('KBN', req, proxyRes) | |
}) | |
var s = http.createServer(function (req, res) { | |
req.headers['x-forwarded-user'] = 'proxy_user_test' | |
let pres = kibanaProxy.web(req, res, { target: 'http://127.0.0.1:5601' }) | |
}) | |
console.log("listening kibana on 6601") | |
s.listen(6601) | |
// ---------- ES | |
var esProxy = httpProxy.createProxyServer({ secure: false }) | |
esProxy.on('proxyRes', function (proxyRes, req, res) { | |
log('ES', req, proxyRes) | |
}) | |
s = http.createServer(function (req, res) { | |
let pres = esProxy.web(req, res, { target: 'https://127.0.0.1:9200' }) | |
}) | |
console.log("listening ES on 10200") | |
s.listen(10200) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment