Sha256: 9428918d6ab41ae5765bf60f357b70c64ad10a1264a8a9cc53b31c30ff4cc30c

Contents?: true

Size: 712 Bytes

Versions: 2

Compression:

Stored size: 712 Bytes

Contents

require 'segment_tree'
require 'irrc'
require 'monitor'
require 'ipaddr'

module Legitbot
  # https://developers.facebook.com/docs/sharing/webmasters/crawler

  class Facebook < BotMatch
    lock = Monitor.new

    AS = 'AS32934'
    ValidIPs = lock.synchronize do
      client = Irrc::Client.new
      client.query :radb, 'AS32934'
      results = client.perform

      Hash[%i(ipv4 ipv6).map { |k|
        [k, SegmentTree.new(results[AS][k][AS].map { |cidr|
            [IPAddr.new(cidr).to_range, true]
          })]
      }]
    end

    def valid?
      ip = IPAddr.new(@ip)
      ValidIPs[ip.ipv4? ? :ipv4 : :ipv6].find(ip)
    end
  end

  rule Legitbot::Facebook, %w(facebookhit facebookexternalhit)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
legitbot-0.2.0 lib/legitbot/facebook.rb
legitbot-0.1.2 lib/legitbot/facebook.rb