X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fhttp-https-proxy%2Fhttp_proxy.js;h=e90dfcadcc3292b00835bc4c691df1f5fa311a63;hb=refs%2Fchanges%2F62%2F8262%2F1;hp=714548a2f6d7fa7c2e0331fffc1e4e5f7ad841cc;hpb=a0139337d35be172ae132bbf836859e41132860f;p=nonrtric.git diff --git a/test/http-https-proxy/http_proxy.js b/test/http-https-proxy/http_proxy.js index 714548a2..e90dfcad 100644 --- a/test/http-https-proxy/http_proxy.js +++ b/test/http-https-proxy/http_proxy.js @@ -38,6 +38,8 @@ const aliveporthttps = 8434; // Default https destination port const defaulthttpsport = "443"; +var debug = false; + // Certs etc for https const httpsoptions = { key: fs.readFileSync('cert/key.crt'), @@ -56,13 +58,34 @@ const stats = { function httpclientrequest(clientrequest, clientresponse) { stats['http-requests-initiated']++; - if (clientrequest.url == "/" ) { - console.log("Catch bad url in http request: "+clientrequest.url) + // Extract destination information + var crurl=clientrequest.url; + var crhost=clientrequest.headers['host']; + var crproto=clientrequest.headers['x-forwarded-proto']; + + if (debug) { + console.log("crurl: "+crurl) + console.log("crhost: "+crhost) + console.log("crproto: "+crproto) + } + + // If this server is running behind a proxy (like istio envoy proxy) then the 'clientrequest.url' + // only contains the path component (i.e /test ). The host name and port is included in the + // 'host' header and the protocol (http/https) is in the header 'x-forwarded-proto'. + // In case of istio - https to a pod over mTLS does not seem to work. Only http. + // Othewise, if no front proxy, the full url is included in the 'clientrequest.url' + if (crproto != undefined) { + crurl=crproto+"://"+crhost+crurl + if (debug) { + console.log(" Constructed url: "+crurl) + } + } else if (crurl.startsWith('/')) { + console.log("Catched bad url in http request: "+crurl) clientresponse.end(); return; } - // Extract destination information - const clientrequesturl = new URL(clientrequest.url); + + const clientrequesturl = new URL(crurl); var proxyrequestoptions = { 'host': clientrequesturl.hostname, @@ -105,55 +128,15 @@ function httpclientrequest(clientrequest, clientresponse) { }); } -function main() { - - // -------------------- Alive server ---------------------------------- - // Responde with '200' and statistics for any path on the alive address - const alivelistener = function (req, res) { - console.log(stats) - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.write(JSON.stringify(stats)) - res.end(); - }; - - // The alive server - for healthckeck - const aliveserver = http.createServer(alivelistener); - - // The alive server - for healthckeck - const aliveserverhttps = https.createServer(httpsoptions, alivelistener); - - //Handle heatlhcheck requests - aliveserver.listen(aliveport, () => { - console.log('alive server on: '+aliveport); - console.log(' example: curl localhost: '+aliveport) - }); - - //Handle heatlhcheck requests - aliveserverhttps.listen(aliveporthttps, () => { - console.log('alive server on: '+aliveporthttps); - console.log(' example: curl -k https://localhost: '+aliveporthttps) - }); - - // -------------------- Proxy server --------------------------------- - - // The proxy server - const proxyserver = http.createServer(httpclientrequest).listen(proxyport); - console.log('http/https proxy for http proxy calls on port ' + proxyport); - console.log(' example: curl --proxy localhost:8080 http://pms:1234') - console.log(' example: curl -k --proxy localhost:8080 https://pms:5678') - - const proxyserverhttps = https.createServer(httpsoptions, httpclientrequest).listen(proxyporthttps); - console.log('http/https proxy for https proxy calls on port ' + proxyporthttps); - console.log(' example: curl --proxy-insecure localhost:8433 http://pms:1234') - console.log(' example: curl --proxy-insecure localhost:8433 https://pms:5678') - console.log(' note: proxy shall not specify https') - - // handle a http proxy request - https listener - proxyserver.addListener( +// Function to add a 'connect' message listener to a http server +function addhttpsconnect(httpserver) { + httpserver.addListener( 'connect', function (request, socketrequest, bodyhead) { - + if (debug) { + console.log("Received 'connect' for: "+request['url']) + } stats['https-requests-initiated']++; // Extract destination information var res = request['url'].split(":") @@ -205,6 +188,76 @@ function main() { ); } +function main() { + + // -------------------- Alive server ---------------------------------- + // Responde with '200' and statistics for any path (except for GET|PUT|DELETE on /debug) on the alive address + const alivelistener = function (req, res) { + if (req.url == "/debug") { + if (req.method == "GET") { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write(""+debug) + res.end() + return + } else if (req.method == "PUT") { + debug=true + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write("OK") + res.end() + return + } else if (req.method == "DELETE") { + debug=false + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write("OK") + res.end() + return + } + } + console.log(stats) + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.write(JSON.stringify(stats)) + res.end(); + }; + + // The alive server - for healthckeck + const aliveserver = http.createServer(alivelistener); + + // The alive server - for healthckeck + const aliveserverhttps = https.createServer(httpsoptions, alivelistener); + + //Handle heatlhcheck requests + aliveserver.listen(aliveport, () => { + console.log('alive server on: '+aliveport); + console.log(' example: curl localhost:'+aliveport) + }); + + //Handle heatlhcheck requests + aliveserverhttps.listen(aliveporthttps, () => { + console.log('alive server on: '+aliveporthttps); + console.log(' example: curl -k https://localhost:'+aliveporthttps) + }); + + // -------------------- Proxy server --------------------------------- + + // The proxy server + const proxyserver = http.createServer(httpclientrequest).listen(proxyport); + console.log('http/https proxy for http proxy calls on port ' + proxyport); + console.log(' example: curl --proxy http://localhost:8080 http://100.110.120.130:1234') + console.log(' example: curl -k --proxy http//localhost:8080 https://100.110.120.130:5678') + + // handle a http proxy request - https listener + addhttpsconnect(proxyserver); + + const proxyserverhttps = https.createServer(httpsoptions, httpclientrequest).listen(proxyporthttps); + console.log('http/https proxy for https proxy calls on port ' + proxyporthttps); + console.log(' example: curl --proxy-insecure --proxy https://localhost:8433 http://100.110.120.130:1234') + console.log(' example: curl -k --proxy-insecure --proxy https://localhost:8433 https://100.110.120.130:5678') + + // handle a https proxy request - https listener + addhttpsconnect(proxyserverhttps); + +} + //Handle ctrl c when running in interactive mode process.on('SIGINT', () => { console.info("Interrupted")