Skip to content

Commit

Permalink
"interval_tree" instead of "segment_tree" (#11) [closes #10]
Browse files Browse the repository at this point in the history
  • Loading branch information
alaz authored Apr 12, 2019
1 parent d1d8ef1 commit eb2d53c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion legitbot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.3.0'
spec.add_dependency "irrc", ">= 0.2.1"
spec.add_dependency "segment_tree"
spec.add_dependency "augmented_interval_tree", ">= 0.1.1"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"

Expand Down
23 changes: 16 additions & 7 deletions lib/legitbot/facebook.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'ipaddr'
require 'irrc'
require 'segment_tree'
require 'interval_tree'

module Legitbot
# https://developers.facebook.com/docs/sharing/webmasters/crawler
Expand All @@ -10,7 +10,7 @@ class Facebook < BotMatch

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
Expand All @@ -24,15 +24,24 @@ def self.reload!
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

Expand Down
15 changes: 15 additions & 0 deletions test/facebook_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
require 'minitest/autorun'
require 'legitbot'

module Legitbot
class Facebook
def self.whois
{
ipv4: ["69.63.176.0/20", "66.220.144.0/20", "66.220.144.0/21", "69.63.184.0/21", "69.63.176.0/21", "74.119.76.0/22", "69.171.255.0/24", "173.252.64.0/18", "69.171.224.0/19", "69.171.224.0/20", "103.4.96.0/22", "69.63.176.0/24", "173.252.64.0/19", "173.252.70.0/24", "31.13.64.0/18", "31.13.24.0/21", "66.220.152.0/21", "66.220.159.0/24", "69.171.239.0/24", "69.171.240.0/20", "31.13.64.0/19", "31.13.64.0/24", "31.13.65.0/24", "31.13.67.0/24", "31.13.68.0/24", "31.13.69.0/24", "31.13.70.0/24", "31.13.71.0/24", "31.13.72.0/24", "31.13.73.0/24", "31.13.74.0/24", "31.13.75.0/24", "31.13.76.0/24", "31.13.77.0/24", "31.13.96.0/19", "31.13.66.0/24", "173.252.96.0/19", "69.63.178.0/24", "31.13.78.0/24", "31.13.79.0/24", "31.13.80.0/24", "31.13.82.0/24", "31.13.83.0/24", "31.13.84.0/24", "31.13.85.0/24", "31.13.86.0/24", "31.13.87.0/24", "31.13.88.0/24", "31.13.89.0/24", "31.13.90.0/24", "31.13.91.0/24", "31.13.92.0/24", "31.13.93.0/24", "31.13.94.0/24", "31.13.95.0/24", "69.171.253.0/24", "69.63.186.0/24", "31.13.81.0/24", "179.60.192.0/22", "179.60.192.0/24", "179.60.193.0/24", "179.60.194.0/24", "179.60.195.0/24", "185.60.216.0/22", "45.64.40.0/22", "185.60.216.0/24", "185.60.217.0/24", "185.60.218.0/24", "185.60.219.0/24", "129.134.0.0/16", "157.240.0.0/16", "157.240.8.0/24", "157.240.0.0/24", "157.240.1.0/24", "157.240.2.0/24", "157.240.3.0/24", "157.240.4.0/24", "157.240.5.0/24", "157.240.6.0/24", "157.240.7.0/24", "157.240.9.0/24", "157.240.10.0/24", "157.240.16.0/24", "157.240.19.0/24", "157.240.11.0/24", "157.240.12.0/24", "157.240.13.0/24", "157.240.14.0/24", "157.240.15.0/24", "157.240.17.0/24", "157.240.18.0/24", "157.240.20.0/24", "157.240.21.0/24", "157.240.22.0/24", "157.240.23.0/24", "157.240.0.0/17", "69.171.250.0/24", "157.240.24.0/24", "157.240.25.0/24", "199.201.64.0/24", "199.201.65.0/24", "199.201.64.0/22", "204.15.20.0/22", "157.240.192.0/24", "129.134.0.0/17", "157.240.198.0/24"],
ipv6: []
}
end
end
end

class FacebookTest < Minitest::Test
def test_valid_ip
ip = "69.63.186.89"
match = Legitbot::Facebook.new(ip)
assert match.valid?, msg: "#{ip} is a valid Facebook IP"

ip = '69.171.251.1'
match = Legitbot::Facebook.new(ip)
assert match.valid?, msg: "#{ip} is a valid Facebook IP"
end

def test_invalid_ip
Expand Down

0 comments on commit eb2d53c

Please sign in to comment.