Skip to content

Commit

Permalink
Facebook bot
Browse files Browse the repository at this point in the history
  • Loading branch information
alaz committed Dec 20, 2016
1 parent 639e691 commit 7759274
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ end
## Issues, problems, plans

* Rails middleware
* Facebook: https://developers.facebook.com/docs/sharing/webmasters/crawler
* More testing for Facebook
* Review for thread safety
* Make it possible to reload Facebook IP ranges

## License

Expand Down
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ Rake::TestTask.new do |t|
t.verbose = true
end

desc 'Start a console'
task :console do
require 'irb'
ARGV.clear
IRB.start
end

task default: %w[test]
3 changes: 3 additions & 0 deletions legitbot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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 "segment_tree"
spec.add_dependency "concurrent-ruby"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"

Expand Down
8 changes: 5 additions & 3 deletions lib/legitbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

require_relative 'legitbot/legitbot'
require_relative 'legitbot/botmatch'
require_relative 'legitbot/google'
require_relative 'legitbot/yandex'
require_relative 'legitbot/bing'

require_relative 'legitbot/baidu'
require_relative 'legitbot/bing'
require_relative 'legitbot/duckduckgo'
require_relative 'legitbot/facebook'
require_relative 'legitbot/google'
require_relative 'legitbot/yandex'
37 changes: 37 additions & 0 deletions lib/legitbot/facebook.rb
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
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.0.1'
VERSION = '0.1.0'
end
22 changes: 22 additions & 0 deletions test/facebook_test.rb
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

0 comments on commit 7759274

Please sign in to comment.