From 42cbc63b95a78b8e87fc4e7898e179872eba28c4 Mon Sep 17 00:00:00 2001 From: Alexander Azarov <azarov@osinka.ru> Date: Sun, 7 Oct 2018 12:00:00 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=8E=20initialize=20the=20list=20of=20F?= =?UTF-8?q?acebook=20valid=20IPs=20lazily=20[closes=20#7]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also a method to re-load the IPs is provided, see also #5 --- legitbot.gemspec | 2 +- lib/legitbot/facebook.rb | 32 ++++++++++++++++++++------------ lib/legitbot/version.rb | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/legitbot.gemspec b/legitbot.gemspec index eaa43c9..290dc08 100644 --- a/legitbot.gemspec +++ b/legitbot.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| "made by a real search engine, not a fake" spec.required_ruby_version = '>= 2.0.0' - spec.add_dependency "irrc" + spec.add_dependency "irrc", ">= 0.2.1" spec.add_dependency "segment_tree" spec.add_development_dependency "rake" spec.add_development_dependency "minitest" diff --git a/lib/legitbot/facebook.rb b/lib/legitbot/facebook.rb index a18c7a8..48e74e7 100644 --- a/lib/legitbot/facebook.rb +++ b/lib/legitbot/facebook.rb @@ -1,18 +1,31 @@ -require 'segment_tree' -require 'irrc' -require 'monitor' require 'ipaddr' +require 'irrc' +require 'segment_tree' module Legitbot # https://developers.facebook.com/docs/sharing/webmasters/crawler class Facebook < BotMatch - lock = Monitor.new - AS = 'AS32934' - ValidIPs = lock.synchronize do + + def valid? + ip = IPAddr.new(@ip) + Facebook.valid_ips[ip.ipv4? ? :ipv4 : :ipv6].find(ip) + end + + @mutex = Mutex.new + + def self.valid_ips + @mutex.synchronize { @ips ||= load_ips } + end + + def self.reload! + @mutex.synchronize { @ips = load_ips } + end + + def self.load_ips client = Irrc::Client.new - client.query :radb, 'AS32934' + client.query :radb, AS results = client.perform Hash[%i(ipv4 ipv6).map { |k| @@ -21,11 +34,6 @@ class Facebook < BotMatch })] }] end - - def valid? - ip = IPAddr.new(@ip) - ValidIPs[ip.ipv4? ? :ipv4 : :ipv6].find(ip) - end end rule Legitbot::Facebook, %w(facebookhit facebookexternalhit) diff --git a/lib/legitbot/version.rb b/lib/legitbot/version.rb index cd91305..bf21a74 100644 --- a/lib/legitbot/version.rb +++ b/lib/legitbot/version.rb @@ -1,3 +1,3 @@ module Legitbot - VERSION = '0.2.0' + VERSION = '0.2.1' end