1
0

private_address_check.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. module PrivateAddressCheck
  3. module_function
  4. CIDR_LIST = [
  5. IPAddr.new('0.0.0.0/8'), # Current network (only valid as source address)
  6. IPAddr.new('100.64.0.0/10'), # Shared Address Space
  7. IPAddr.new('172.16.0.0/12'), # Private network
  8. IPAddr.new('192.0.0.0/24'), # IETF Protocol Assignments
  9. IPAddr.new('192.0.2.0/24'), # TEST-NET-1, documentation and examples
  10. IPAddr.new('192.88.99.0/24'), # IPv6 to IPv4 relay (includes 2002::/16)
  11. IPAddr.new('198.18.0.0/15'), # Network benchmark tests
  12. IPAddr.new('198.51.100.0/24'), # TEST-NET-2, documentation and examples
  13. IPAddr.new('203.0.113.0/24'), # TEST-NET-3, documentation and examples
  14. IPAddr.new('224.0.0.0/4'), # IP multicast (former Class D network)
  15. IPAddr.new('240.0.0.0/4'), # Reserved (former Class E network)
  16. IPAddr.new('255.255.255.255'), # Broadcast
  17. IPAddr.new('64:ff9b::/96'), # IPv4/IPv6 translation (RFC 6052)
  18. IPAddr.new('100::/64'), # Discard prefix (RFC 6666)
  19. IPAddr.new('2001::/32'), # Teredo tunneling
  20. IPAddr.new('2001:10::/28'), # Deprecated (previously ORCHID)
  21. IPAddr.new('2001:20::/28'), # ORCHIDv2
  22. IPAddr.new('2001:db8::/32'), # Addresses used in documentation and example source code
  23. IPAddr.new('2002::/16'), # 6to4
  24. IPAddr.new('fc00::/7'), # Unique local address
  25. IPAddr.new('ff00::/8'), # Multicast
  26. ].freeze
  27. def private_address?(address)
  28. address.private? || address.loopback? || address.link_local? || CIDR_LIST.any? { |cidr| cidr.include?(address) }
  29. end
  30. end