DadiBit
DadiBit

Reputation: 811

Express intentional timeout

express.js allows me to "destroy" a response with:

blacklist = ['1.1.1.1']

app.use((req, res, next) => {
  if (blacklist.includes(req.ip))
    res.destroy(null);
  else
    next();
})

This throws ERR_EMPTY_RESPONSE on google chrome.

I can simply end it with:

blacklist = ['1.1.1.1']

app.use((req, res, next) => {
  if (blacklist.includes(req.ip))
    res.end();
  else
    next();
})

This instead returns 200 OK with an empty page.

Not sending a response hangs the page without time outing it:

blacklist = ['1.1.1.1']

app.use((req, res, next) => {
  if (!blacklist.includes(req.ip))
    next();
})

I know about the timeout module for express, but I can't make it throw a client-side timeout error with a simple timeout(0).

What I actually want is to "make it look like" the server "crashed" for the blacklisted IPs by throwing ERR_CONNECTION_TIMED_OUT. I know that port 80 will still be open (and can be seen), but that's not what I'm aiming for. Is it possible to stop listening to certain IPs and therefor throw an ERR_CONNECTION_TIMED_OUT error?

Upvotes: 0

Views: 128

Answers (1)

Reed Schalo
Reed Schalo

Reputation: 11

According to

res.end([data] [, encoding]) 

from: http://expressjs.com/en/4x/api.html#res.end

It looks like you may be able to use:

res.status(404).end()

to respond to the request with a 404 status and then terminate the response process.

Have you tried this? If it works could you use:

res.status(522).end()

to respond with a connection timed out error?

Upvotes: 1

Related Questions