From 4593259e5ef3c2039df952d78f56d0bdf217cffb Mon Sep 17 00:00:00 2001 From: jusax23 Date: Wed, 3 Jul 2024 17:22:22 +0200 Subject: [PATCH] initial --- .gitignore | 1 + main.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 23 ++++++++++++++ package.json | 6 ++++ 4 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 main.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..870a7a9 --- /dev/null +++ b/main.js @@ -0,0 +1,80 @@ +var http = require('http'); +const https = require('https'); +const zlib = require("zlib"); + + +const TARGET_HOST = 'prod.tafmobile.de' + +const resetAnsi = "\u001B[0m"; +function setColor(r, g, b) { + return "\x1b[38;2;" + r + ";" + g + ";" + b + "m"; +} + +//create a server object: +http.createServer(function (req, res) { + let headers = { ...req.headers, host: TARGET_HOST }; + const options = { + hostname: TARGET_HOST, + path: req.url, + headers, + method: req.method ?? "POST", + } + + + let request = https.request(options, (response) => { + const resp_headers = { ...response.headers, }; + res.writeHead(response.statusCode, resp_headers); + + const isGzip = resp_headers["content-encoding"] === "gzip"; + + let body_response = []; + const gunzip = zlib.createGunzip(); + + function finish() { + body = Buffer.concat(body).toString(); + body_response = Buffer.concat(body_response).toString(); + console.log( + `${setColor(255, 255, 255)}Request on (${req.method}): ${req.url} +- ${setColor(100, 0, 0)}headers: ${Object.entries(headers).map(([a, b]) => a + "=" + b).join(", ")} +- ${setColor(200, 0, 0)}request: ${body} +- ${setColor(0, 100, 0)}response headers: ${Object.entries(resp_headers).map(([a, b]) => a + "=" + b).join(", ")} +- ${setColor(0, 200, 0)}recieved: ${body_response} +${resetAnsi}`); + } + + response.on('data', function (chunk) { + if (isGzip) gunzip.write(chunk); + else body_response.push(chunk); + res.write(chunk); + }); + + gunzip.on('data', function (chunk) { + body_response.push(chunk); + }); + gunzip.on("end", finish); + + response.on('end', function () { + if (isGzip) gunzip.end(); + else { + finish(); + } + res.end(); + }); + + }); + + + let body = []; + req + .on('error', err => { + console.error(err); + }) + .on('data', chunk => { + request.write(chunk); + body.push(chunk); + }) + .on('end', () => { + request.end(); + }); +}).listen(10920); + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e6c2508 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,23 @@ +{ + "name": "node-proxy", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "http": "^0.0.1-security", + "https": "^1.0.0" + } + }, + "node_modules/http": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz", + "integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g==" + }, + "node_modules/https": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz", + "integrity": "sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f367f3a --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "http": "^0.0.1-security", + "https": "^1.0.0" + } +}