81 lines
2.1 KiB
JavaScript
81 lines
2.1 KiB
JavaScript
|
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);
|
||
|
|