简体   繁体   中英

How to capture a raw request in Node.js

I was able to capture raw request in Node.js using source below.

var http = require('http');
var express = require('express');

var remote = express();
remote.use(function(req, res, next) {
  req.socket.once('data', function(data) {
    console.log(data.toString());
  });
  next();
});

remote.use(function(req, res, next) {
  res.end('end');
});

http.createServer(remote).listen(8080);

But this source could capture raw request after(including) the second request because the first request was consumed before binding event handler. If a client do not use keep alive, I cannot capture any request.

How can I capture raw request including first request?

I found a way using 'connection' event on http.Server .

var http = require('http');
var express = require('express');

var remote = express();
remote.use(function(req, res, next) {
  res.end('end');
});

var server = http.createServer(remote);
server.on('connection', function(socket) {
  socket.on('data', function(chunk) {
    console.log(chunk.toString());
  });
});
server.listen(8080);

Hey not sure if you're still having the problem. I recently had to solve this, and I played around with a lot of things and this is what I ended up with:

https://github.com/vincenzorm117/http-capture/blob/master/index.js

Here is the code just in case the link has any issues:

var PORT = process.env.PORT || 3000

const net = require('net')
const fs = require('fs')

if( !fs.existsSync('./payloads') ) {
    fs.mkdirSync('./payloads');
}

var server = net.createServer((sock) => {
    console.log(`Connected: ${sock.remoteAddress}`)
    let filename = FileName(sock);
    let wstream = fs.createWriteStream(`./payloads/${filename}`);


    sock.on('data', wstream.write.bind(wstream));
    sock.on('end', () => {
        wstream.end();
        delete wstream;
        console.log(`Disconnected: ${sock.remoteAddress}`)
    });

    setTimeout(() => {
        if( !sock.destroyed ) {
            sock.write('HTTP/1.1 200 OK\r\n');
            sock.end();
        }
    }, 3000);
});

server.listen(PORT, 'localhost');



function FileName() {
    var d = new Date(),
    year = d.getFullYear(),
    month = d.getMonth(),
    date = d.getDate();
    hour = d.getHours();
    minutes = d.getMinutes();
    seconds = d.getSeconds();
    if( month < 10 ) month = '0' + month;
    if( date < 10 ) date = '0' + date;
    if( hour < 10 ) hour = '0' + hour;
    if( minutes < 10 ) minutes = '0' + minutes;
    if( seconds < 10 ) seconds = '0' + seconds;
    return `request__${year}-${month}-${date}__${hour}-${minutes}-${seconds}.http`;
}



I was a bit lazy and set the server to kill the connection after 3 seconds instead of parsing the HTTP request. You can update it to 1 second and it should be fine though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM