-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'segment_tree' | ||
require 'irrc' | ||
require 'concurrent' | ||
|
||
module Legitbot | ||
# https://developers.facebook.com/docs/sharing/webmasters/crawler | ||
|
||
class Facebook < BotMatch | ||
AS = 'AS32934' | ||
ValidIPs = Concurrent::Delay.new do | ||
client = Irrc::Client.new | ||
client.query :radb, 'AS32934' | ||
results = client.perform | ||
Hash[ | ||
:ipv4 => SegmentTree.new( | ||
results[AS][:ipv4][AS].map { |cidr| | ||
[IPAddr.new(cidr).to_range, true] | ||
}), | ||
:ipv6 => SegmentTree.new( | ||
results[AS][:ipv6][AS].map { |cidr| | ||
[IPAddr.new(cidr).to_range, true] | ||
}) | ||
] | ||
end | ||
|
||
def valid? | ||
ip = IPAddr.new(@ip) | ||
if ip.ipv4? | ||
ValidIPs.value[:ipv4].find(ip) | ||
else | ||
ValidIPs.value[:ipv6].find(ip) | ||
end | ||
end | ||
end | ||
|
||
rule Legitbot::Facebook, %w(facebookhit facebookexternalhit) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Legitbot | ||
VERSION = '0.0.1' | ||
VERSION = '0.1.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'minitest/autorun' | ||
require 'legitbot' | ||
|
||
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" | ||
end | ||
|
||
def test_invalid_ip | ||
ip = "127.0.0.1" | ||
match = Legitbot::Facebook.new(ip) | ||
assert match.fake?, msg: "#{ip} is a fake Facebook IP" | ||
end | ||
|
||
def test_user_agent | ||
bot = Legitbot.bot("facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)", "31.13.76.56") | ||
assert_equal "Facebook", bot.detected_as | ||
assert bot.valid?, msg: "A valid Facebook User-agent and IP" | ||
end | ||
end |