Class: Coppertone::Utils::IPInDomainChecker
- Inherits:
-
Object
- Object
- Coppertone::Utils::IPInDomainChecker
- Defined in:
- lib/coppertone/utils/ip_in_domain_checker.rb
Overview
Checks the IP address from the request against an A or AAAA record for a domain. Takes optional CIDR arguments so the match can check subnets
Instance Method Summary collapse
- #check(domain_name, ip_v4_cidr_length = 32, ip_v6_cidr_length = 128) ⇒ Object
- #dns_client ⇒ Object
- #filtered_records(recs, cidr_length) ⇒ Object
-
#initialize(macro_context, request_context) ⇒ IPInDomainChecker
constructor
A new instance of IPInDomainChecker.
- #ip ⇒ Object
- #ip_networks(domain_name, cidr_length) ⇒ Object
- #ip_v6? ⇒ Boolean
Constructor Details
#initialize(macro_context, request_context) ⇒ IPInDomainChecker
Returns a new instance of IPInDomainChecker.
7 8 9 10 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 7 def initialize(macro_context, request_context) @macro_context = macro_context @request_context = request_context end |
Instance Method Details
#check(domain_name, ip_v4_cidr_length = 32, ip_v6_cidr_length = 128) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 12 def check(domain_name, ip_v4_cidr_length = 32, ip_v6_cidr_length = 128) cidr_length = ip_v6? ? ip_v6_cidr_length : ip_v4_cidr_length networks = ip_networks(domain_name, cidr_length) @request_context.register_void_dns_result if networks.empty? matching_network = networks.find do |network| network.include?(ip) end !matching_network.nil? end |
#dns_client ⇒ Object
44 45 46 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 44 def dns_client @request_context.dns_client end |
#filtered_records(recs, cidr_length) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 48 def filtered_records(recs, cidr_length) ips = recs.map do |r| IPAddr.new(r[:address]).mask(cidr_length.to_i) end ips.reject(&:nil?) end |
#ip ⇒ Object
30 31 32 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 30 def ip ip_v6? ? @macro_context.ip_v6 : @macro_context.ip_v4 end |
#ip_networks(domain_name, cidr_length) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 34 def ip_networks(domain_name, cidr_length) ip_records = if ip_v6? dns_client.fetch_aaaa_records(domain_name) else dns_client.fetch_a_records(domain_name) end filtered_records(ip_records, cidr_length) end |
#ip_v6? ⇒ Boolean
26 27 28 |
# File 'lib/coppertone/utils/ip_in_domain_checker.rb', line 26 def ip_v6? @macro_context.original_ipv6? end |