Skip to content

Commit

Permalink
🐎 initialize the list of Facebook valid IPs lazily [closes #7]
Browse files Browse the repository at this point in the history
Also a method to re-load the IPs is provided, see also #5
  • Loading branch information
alaz committed Oct 9, 2018
1 parent 2d03063 commit 42cbc63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion legitbot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 20 additions & 12 deletions lib/legitbot/facebook.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/legitbot/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Legitbot
VERSION = '0.2.0'
VERSION = '0.2.1'
end

0 comments on commit 42cbc63

Please sign in to comment.