class Faraday::RestrictIPAddresses
Constants
- RFC_1918_NETWORKS
- RFC_6890_NETWORKS
- VERSION
Public Class Methods
new(app, options = {})
click to toggle source
Calls superclass method
# File lib/faraday/restrict_ip_addresses.rb, line 33 def initialize(app, options = {}) super(app) @denied_networks = (options[:deny] || []).map { |n| IPAddr.new(n) } @allowed_networks = (options[:allow] || []).map { |n| IPAddr.new(n) } @denied_networks += RFC_1918_NETWORKS if options[:deny_rfc1918] @denied_networks += RFC_6890_NETWORKS if options[:deny_rfc6890] @denied_networks.uniq! @allowed_networks += [IPAddr.new('127.0.0.1')] if options[:allow_localhost] end
Public Instance Methods
addresses(hostname)
click to toggle source
# File lib/faraday/restrict_ip_addresses.rb, line 61 def addresses(hostname) Addrinfo.getaddrinfo(hostname, nil, :UNSPEC, :STREAM).map { |a| IPAddr.new(a.ip_address) } rescue SocketError => e # In case of invalid hostname, return an empty list of addresses [] end
allowed_ip?(address)
click to toggle source
# File lib/faraday/restrict_ip_addresses.rb, line 57 def allowed_ip?(address) @allowed_networks.any? { |net| net.include? address } end
call(env)
click to toggle source
# File lib/faraday/restrict_ip_addresses.rb, line 44 def call(env) raise AddressNotAllowed.new "Address not allowed for #{env[:url]}" if denied?(env) @app.call(env) end
denied?(env)
click to toggle source
# File lib/faraday/restrict_ip_addresses.rb, line 49 def denied?(env) addresses(env[:url].hostname).any? { |a| denied_ip?(a) } end
denied_ip?(address)
click to toggle source
# File lib/faraday/restrict_ip_addresses.rb, line 53 def denied_ip?(address) @denied_networks.any? { |net| net.include?(address) and !allowed_ip?(address) } end