0

I am wondering if it is possible to achieve the following ? I have an Array which stores the IP address and a DateTime , the datetime format is flexible so i could use timestamp or actual datetime whatever is better. What i want to be able to do is query the Array and check if a certain IP has been added lets say 5 times in the last minute. The reason behind this is, that i want to catch users who try to scan my express servers for open holes and add them automatically to my blacklist. Currently i add all 404 not found requests to the array. When doing so i want to check how many records are in array for the timeframe.

MisterniceGuy
  • 1,646
  • 2
  • 18
  • 41

1 Answers1

0

Yep. You can filter only the entries from the last minute with Array.filter, then count the number of each IP with Array.reduce, and finally filter only the IPs that appear more than 5 times with another Array.filter:

const counts = data
  .filter(entry => (Date.now() - entry.dateTime) < 60)
  .reduce((result, entry) => {
      result[entry.IP] = (result[entry.IP] || 0) + 1;
      return result;
    }, {});
const flagged = Object.keys(counts)
  .filter(IP => counts[IP] >= 5);

Then flagged will have a list of the IPs which are in the data array more than 5 times in the last minute.

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26