lib/legitbot/facebook.rb in legitbot-0.2.6 vs lib/legitbot/facebook.rb in legitbot-0.2.7

- old
+ new

@@ -1,18 +1,18 @@ require 'ipaddr' require 'irrc' -require 'segment_tree' +require 'interval_tree' module Legitbot # https://developers.facebook.com/docs/sharing/webmasters/crawler class Facebook < BotMatch AS = 'AS32934' def valid? ip = IPAddr.new(@ip) - Facebook.valid_ips[ip.ipv4? ? :ipv4 : :ipv6].find(ip) + Facebook.valid_ips[ip.ipv4? ? :ipv4 : :ipv6].search(ip.to_i).size > 0 end @mutex = Mutex.new def self.valid_ips @@ -22,18 +22,27 @@ def self.reload! @mutex.synchronize { @ips = load_ips } end def self.load_ips + whois.map do |(family, records)| + ranges = records.map do |cidr| + range = IPAddr.new(cidr).to_range + (range.begin.to_i..range.end.to_i) + end + [family, IntervalTree::Tree.new(ranges)] + end.to_h + end + + def self.whois client = Irrc::Client.new client.query :radb, AS results = client.perform - Hash[%i(ipv4 ipv6).map { |k| - [k, SegmentTree.new(results[AS][k][AS].map { |cidr| - [IPAddr.new(cidr).to_range, true] - })] - }] + %i(ipv4 ipv6).map do |family| + [family, results[AS][family][AS]] + end.to_h + # { ipv4: results[AS][:ipv4][AS], ipv6: results[AS][:ipv6][AS] } end end rule Legitbot::Facebook, %w(Facebot facebookexternalhit/1.1) end