The essence of this script is to get the data in the buffer, make a replacement and give it back. The biggest problem for me was the encoding. Here is a working example:
"use strict"; const http = require('http'), url = require('url'), // для установки этого модуля выполнить: npm i -S iconv Iconv = require('iconv').Iconv ; var server = http.createServer(function(request, response) { var ph = url.parse(request.url); var options = { port: ph.port ? parseInt(ph.port) : 80, hostname: ph.hostname, method: request.method, path: ph.path, headers: request.headers } var proxyRequest = http.request(options) proxyRequest.on('response', function(proxyResponse) { // буффер с телом ответа var buf = Buffer.alloc(0, 'binary'); proxyResponse.on('data', function(chunk) { // собираем тело ответа по частям (без заголовоков, только тело) buf = Buffer.concat([buf, chunk]); }); proxyResponse.on('end', function() { // по-умолчанию будем считать кодировку буфера текста latin-1, она же binary var headCharset = 'latin1'; // если в заголовках указана кодировка, используем ее var _m = proxyResponse.headers['content-type'].match(/charset=(.+)/i); if(_m) { headCharset = _m[1].toLowerCase(); } // превращаем бинарный буфер в строку utf-8 (внутренняя кодировка JavaScript) согласно определенной кодировки var iconv = new Iconv(headCharset, 'utf-8'); buf = iconv.convert(buf); var body = buf.toString('utf-8'); // Производим замену // Правильное определение кодировки очень важно, иначе символы, не входящие в latin-1, не будут заменены body = body.replace(/hello/gi, '-'); body = body.replace(/привет/gi, '-'); // Конвертируем назад в ту кодировку, которая была iconv = new Iconv('utf-8', headCharset); buf = Buffer.from(body, 'utf-8'); buf = iconv.convert(buf); // Меняем длину 'content-length', иначе клиент будет ругаться: // curl: (18) transfer closed with 12 bytes remaining to read proxyResponse.headers['content-length'] = buf.length; // отправляем заголовки response.writeHead(proxyResponse.statusCode, proxyResponse.headers); // отправляем тело response.write(buf, 'binary'); // конец передачи response.end(); }); }); request.on('data', function(chunk) { proxyRequest.write(chunk, 'binary') }); request.on('end', function() { proxyRequest.end() }); }).listen(8080);
How to check out bash:
export http_proxy=http://127.0.0.1:8080 curl http://site.ru/cp1251/ curl http://site.ru/utf-8/