Class: IpDetector
Overview
This utility class will assist with the detection of WarmlyYours IP addresses
Use the constants to define fixed network blocks
Employee ips will be pulled from last login info in the last 15 days.
Constant Summary collapse
- WY_CA_IP_BLOCKS =
[ '172.20.30.0/24', '172.20.41.0/24', '172.20.31.0/24', '69.165.131.68/32', # '173.212.185.65/32', # Richmond Hill Wan 2 (terago) '72.139.108.22/32', '72.138.19.30/32' # Richmond Hill Wan1 (venture) ].map{|subnet| IPAddr.new(subnet) }.freeze
- WY_US_IP_BLOCKS =
[ '172.20.0.0/16', '127.0.0.1/24', '192.168.0.0/16', '50.200.53.120/30' ].map{|subnet| IPAddr.new(subnet) }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#cloudflare_ip_list_items ⇒ Object
Build the Cloudflare IP list payload for $warmlyyours_users.
- #employee_ca_ip_blocks ⇒ Object
- #employee_ip_blocks ⇒ Object
- #warmlyyours_ca_ip_blocks ⇒ Object
- #warmlyyours_canada_ip?(ip) ⇒ Boolean
- #warmlyyours_ip?(ip) ⇒ Boolean
- #warmlyyours_ip_blocks ⇒ Object
Class Method Details
.warmlyyours_canada_ip?(ip) ⇒ Boolean
54 55 56 |
# File 'app/lib/ip_detector.rb', line 54 def self.warmlyyours_canada_ip?(ip) instance.warmlyyours_canada_ip?(ip) end |
.warmlyyours_ip?(ip) ⇒ Boolean
50 51 52 |
# File 'app/lib/ip_detector.rb', line 50 def self.warmlyyours_ip?(ip) instance.warmlyyours_ip?(ip) end |
Instance Method Details
#cloudflare_ip_list_items ⇒ Object
Build the Cloudflare IP list payload for $warmlyyours_users.
Returns an array of { ip:, comment: } hashes ready for the Lists API.
Filters out private/loopback ranges that Cloudflare would reject.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/lib/ip_detector.rb', line 69 def cloudflare_ip_list_items items = [] WY_CA_IP_BLOCKS.each do |block| next if private_range?(block) items << { ip: block.to_range.first.to_s + cidr_suffix(block), comment: 'WY Canada Office' } end WY_US_IP_BLOCKS.each do |block| next if private_range?(block) items << { ip: block.to_range.first.to_s + cidr_suffix(block), comment: 'WY US Office' } end employee_rows = base_scope.distinct.pluck(:current_sign_in_ip, :name, :email) employee_rows.each do |ip_str, name, email| next if ip_str.blank? begin addr = IPAddr.new(ip_str) next if private_range?(addr) rescue IPAddr::InvalidAddressError next end label = [name.presence, email.presence].compact.join(' — ') items << { ip: ip_str, comment: "Employee: #{label}".truncate(500) } end items.uniq { |item| item[:ip] } end |
#employee_ca_ip_blocks ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/lib/ip_detector.rb', line 26 def employee_ca_ip_blocks ips = Rails.cache.fetch([:ip_detector, :employee_ca_ips], expires_in: 1.hour) do base_scope.joins(:employee).where(Employee[:company_id].eq(Company::CAN)).distinct.pluck(:current_sign_in_ip) end ips ||= [] ips.map { |subnet| IPAddr.new subnet } end |
#employee_ip_blocks ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/lib/ip_detector.rb', line 34 def employee_ip_blocks ips = Rails.cache.fetch([:ip_detector, :employee_ips], expires_in: 1.hour) do base_scope.distinct.pluck(:current_sign_in_ip) end ips ||= [] ips.map { |subnet| IPAddr.new subnet } end |
#warmlyyours_ca_ip_blocks ⇒ Object
46 47 48 |
# File 'app/lib/ip_detector.rb', line 46 def warmlyyours_ca_ip_blocks (WY_CA_IP_BLOCKS + employee_ca_ip_blocks).compact.uniq end |
#warmlyyours_canada_ip?(ip) ⇒ Boolean
62 63 64 |
# File 'app/lib/ip_detector.rb', line 62 def warmlyyours_canada_ip?(ip) warmlyyours_ca_ip_blocks.any?{|block| block.include?(ip) } end |
#warmlyyours_ip?(ip) ⇒ Boolean
58 59 60 |
# File 'app/lib/ip_detector.rb', line 58 def warmlyyours_ip?(ip) warmlyyours_ip_blocks.any?{|block| block.include?(ip) } end |
#warmlyyours_ip_blocks ⇒ Object
42 43 44 |
# File 'app/lib/ip_detector.rb', line 42 def warmlyyours_ip_blocks (WY_CA_IP_BLOCKS + WY_US_IP_BLOCKS + employee_ip_blocks).compact.uniq end |