简体   繁体   中英

Node - Parse AWS IP ranges and block

I see that AWS posts a json file with all their IP ranges here (Actual JSON HERE )

I was thinking of using this json file to check against every incoming connection in my node app but firstly I was wondering if it would be far too much overhead to loop through it for every request?

Secondly, I wasn't sure exactly how to go about this, as many IP ranges are formatted differently eg.

43.250.192.0/24
46.51.128.0/18
27.0.0.0/22

I'm not too sure what them suffix's mean.

Has anyone don something similar?

Your first concern is correct - it's a lot of overhead to loop through Amazon's IPs for each request. This should be handled at the firewall.

Nevertheless, the ip_prefix field Amazon is providing can be used to ensure valid IP addresses exist within that subnet. The node-ip module can help with this. It has a cidrSubnet function that can be used to test a prefix against a user's IP. See the below coffeescript.

ip = require 'node-ip'
amazonIPs = require 'amazonIPs.json'
someUsersIP = '192.168.1.190'

for prefix in amazonIPs.prefix
  if ip.cidrSubnet(prefix).contains(someUsersIP)
    console.log "#{someUsersIP} is within the #{prefix} range"

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