This commit is contained in:
jusax23 2024-07-03 17:23:44 +02:00
commit adb9d33971
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
4 changed files with 110 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

80
main.js Normal file
View file

@ -0,0 +1,80 @@
var http = require('http');
const https = require('https');
const zlib = require("zlib");
const TARGET_HOST = 'example.org';
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);

23
package-lock.json generated Normal file
View file

@ -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=="
}
}
}

6
package.json Normal file
View file

@ -0,0 +1,6 @@
{
"dependencies": {
"http": "^0.0.1-security",
"https": "^1.0.0"
}
}