Created
May 5, 2020 12:31
-
-
Save ArmandoAssuncao/008fb07faddc383f7703cf36cefe7e5e to your computer and use it in GitHub Desktop.
validates ip by format and blacklists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ip_valid?(ip_str) | |
begin | |
ip = IPAddr.new(ip_str).to_i | |
rescue | |
return false | |
end | |
blacklist_ranges = [ | |
['0.0.0.0', '0.255.255.255'], | |
['10.0.0.0', '10.255.255.255'], | |
['127.0.0.1', '127.255.255.255'], | |
['172.16.0.0', '172.31.255.255'], | |
['192.168.0.0', '192.168.255.255'], | |
].freeze | |
blacklist_ranges.each do |ip_start, ip_end| | |
low = IPAddr.new(ip_start).to_i | |
high = IPAddr.new(ip_end).to_i | |
return false if (low..high) === ip | |
end | |
true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment