Skip to content

Instantly share code, notes, and snippets.

@sscarduzio
Created July 7, 2020 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sscarduzio/e56dabc2baa8b138b470a5473cae2ad8 to your computer and use it in GitHub Desktop.
Save sscarduzio/e56dabc2baa8b138b470a5473cae2ad8 to your computer and use it in GitHub Desktop.
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