module PrivateAddressCheck

Constants

CIDR_LIST

www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml

PrivateConnectionAttemptedError
VERSION

Public Instance Methods

only_public_connections() { || ... } click to toggle source
# File lib/private_address_check/tcpsocket_ext.rb, line 6
def only_public_connections
  Thread.current[:private_address_check] = true
  yield
ensure
  Thread.current[:private_address_check] = false
end
private_address?(address) click to toggle source
# File lib/private_address_check.rb, line 41
def private_address?(address)
  CIDR_LIST.any? do |cidr|
    cidr.include?(address)
  end
end
resolves_to_private_address?(hostname) click to toggle source
# File lib/private_address_check.rb, line 47
def resolves_to_private_address?(hostname)
  ips = Socket.getaddrinfo(hostname, nil).map { |info| IPAddr.new(info[3]) }
  return true if ips.empty?

  ips.any? do |ip|
    private_address?(ip)
  end
end