legitbot 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43121c17f4de712a7c1b33797c0075b31762ff31a1461a871efa486461acf0e2
4
- data.tar.gz: b6d015e3889a421b3dbce8189489239894bbdf87f8a5677e0d612ece4655f973
3
+ metadata.gz: d526ed7ca64658503c66d7c291c8a24f5a9f8fdecb47899eca06d4135ccf4165
4
+ data.tar.gz: 9ba9bc7cebe5137e5a018366ccc63f869864f264b8a4988e4f971b0399d6bf39
5
5
  SHA512:
6
- metadata.gz: 44dec432e5ba97bb28cefd59e6f800d363342360d10ae72cf69419124836ffcc115b780882e797ea2879e41217402c53b6bd8472a449343814f007d1ea9b6996
7
- data.tar.gz: '091b35b6eccc08b4e4b083e8498084e3726534a7560e8626b47e0e9dbcaa5a469568b477429804b47a3c16848d506c856823e8809ee88fc70df0d10723507a9d'
6
+ metadata.gz: dc2814147aa8a02dc14ca39831b62ce5dab4d6506e4c7868d9b1938747858c09dd303e3ff955183dff853fdbb4d2f5e7469e6b433af71191ffb519c9cce403a9
7
+ data.tar.gz: 9b523df0e7d980cc4002197e63d9b76c95b7ed06504a431b2a7dcc3a61a0d126a786996e7e4031727aec92fbba726ff7e507d4d99e8e121780ce822c12706d99
data/.travis.yml CHANGED
@@ -2,4 +2,4 @@ sudo: false
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.2
5
+ - 2.3
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Legitbot [![Build Status](https://secure.travis-ci.org/alaz/legitbot.png?branch=master)](http://travis-ci.org/alaz/legitbot) [![Gem Version](https://badge.fury.io/rb/legitbot.svg)](https://badge.fury.io/rb/legitbot)
2
2
 
3
- Ruby gem to check if an IP really belongs to some bot, typically a search
4
- engine. This can of much help if one wants to protect his/her web site from
5
- malicious scanners who pretend to be e.g. a Googlebot.
3
+ Ruby gem to check that an IP belongs to a bot, typically a search
4
+ engine. This can be of help in protecting a web site from fake search
5
+ engines.
6
6
 
7
7
  ## Usage
8
8
 
@@ -14,7 +14,7 @@ bot = Legitbot.bot(userAgent, ip)
14
14
  ```
15
15
 
16
16
  `bot` will be `nil` if no bot signature was found in the `User-Agent`. Otherwise,
17
- it will be an instance with methods
17
+ it will be an object with methods
18
18
 
19
19
  ```ruby
20
20
  bot.detected_as # => :google
@@ -22,7 +22,7 @@ bot.valid? # => true
22
22
  bot.fake? # => false
23
23
  ```
24
24
 
25
- Sometimes you already know what search engine to expect. For example, you may
25
+ Sometimes you already know what search engine to expect. For example, you might
26
26
  be using [rack-attack](https://github.com/kickstarter/rack-attack):
27
27
 
28
28
  ```ruby
@@ -31,6 +31,16 @@ Rack::Attack.blocklist("fake Googlebot") do |req|
31
31
  end
32
32
  ```
33
33
 
34
+ Or if you do not like all these nasty crawlers stealing your content or
35
+ maybe evaluating it and getting ready to invade your site with spammers,
36
+ then block them all:
37
+
38
+ ```ruby
39
+ Rack::Attack.blocklist 'fake search engines' do |request|
40
+ Legitbot.bot(request.user_agent, request.ip)&.fake?
41
+ end
42
+ ```
43
+
34
44
  ## Supported
35
45
 
36
46
  * [Applebot](https://support.apple.com/en-us/HT204683)
@@ -48,9 +58,7 @@ Apache 2.0
48
58
 
49
59
  ## References
50
60
 
51
- * I have initially created Play Framework version in Scala: [play-legitbot](https://github.com/osinka/play-legitbot)
61
+ * Play Framework variant in Scala: [play-legitbot](https://github.com/osinka/play-legitbot)
52
62
  * Article [When (Fake) Googlebots Attack Your Rails App](http://jessewolgamott.com/blog/2015/11/17/when-fake-googlebots-attack-your-rails-app/)
53
- * [Voight-Kampff](https://github.com/biola/Voight-Kampff) is a Ruby gem which
63
+ * [Voight-Kampff](https://github.com/biola/Voight-Kampff) is a Ruby gem that
54
64
  detects bots by `User-Agent`
55
- * [browser](https://github.com/fnando/browser) is a Ruby gem which may tell
56
- you if the request comes from a search engine.
@@ -17,26 +17,32 @@ module Legitbot
17
17
  # the reverse name
18
18
  def reverse_domain
19
19
  @reverse_domain ||= @dns.getname(@ip)
20
+ rescue Resolv::ResolvError
21
+ @reverse_domain ||= nil
20
22
  end
21
23
 
22
24
  ##
23
25
  # Returns a String with the reverse name
24
26
  def reverse_name
25
- reverse_domain.to_s
27
+ reverse_domain&.to_s
26
28
  end
27
29
 
28
30
  ##
29
31
  # Returns a String with IP created from the reverse name
30
32
  def reversed_ip
33
+ return nil if reverse_name.nil?
34
+
31
35
  @reverse_ip ||= @dns.getaddress(reverse_name)
32
36
  @reverse_ip.to_s
33
37
  end
34
38
 
35
39
  def reverse_resolves?
36
- reversed_ip == @ip
40
+ @ip == reversed_ip
37
41
  end
38
42
 
39
43
  def subdomain_of?(*domains)
44
+ return false if reverse_name.nil?
45
+
40
46
  domains.any? { |d|
41
47
  reverse_domain.subdomain_of? Resolv::DNS::Name.create(d)
42
48
  }
@@ -18,13 +18,13 @@ module Legitbot
18
18
  rule[:class].new(ip, resolver_config)
19
19
  }
20
20
 
21
- bot = bots.select { |bot| bot.valid? }.first if bots.size > 1
22
- bot = bots.first if bot.nil?
21
+ selected = bots.select { |b| b.valid? }.first if bots.size > 1
22
+ selected = bots.first if selected.nil?
23
23
 
24
- if bot && block_given?
25
- yield bot
24
+ if selected && block_given?
25
+ yield selected
26
26
  else
27
- bot
27
+ selected
28
28
  end
29
29
  end
30
30
 
@@ -9,5 +9,5 @@ module Legitbot
9
9
  end
10
10
  end
11
11
 
12
- rule Legitbot::Pinterest, %w(Pinterestbot Pinterest)
12
+ rule Legitbot::Pinterest, %w(Pinterestbot Pinterest/0.2)
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module Legitbot
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -17,6 +17,11 @@ class BotMatchTest < Minitest::Test
17
17
  assert_equal true, match.reverse_resolves?
18
18
  end
19
19
 
20
+ def test_reverse_doesnt_resolve
21
+ match = Legitbot::BotMatch.new "5.140.70.64"
22
+ assert !match.reverse_resolves?
23
+ end
24
+
20
25
  def test_valid_class_syntax
21
26
  assert Legitbot::Google.valid?("66.249.64.141"), msg: "Valid Googlebot"
22
27
  assert Legitbot::Google.fake?("149.210.164.47"), msg: "Fake Googlebot"
@@ -4,6 +4,7 @@ require 'legitbot'
4
4
  class LegitbotTest < Minitest::Test
5
5
  def test_rules
6
6
  assert !Legitbot.bot("Firefox", "127.0.0.1"), msg: "Not a bot"
7
+ assert Legitbot.bot("Googlebot", "5.140.70.64"), msg: "No reverse resolve, bot"
7
8
 
8
9
  Legitbot.bot("Firefox", "127.0.0.1") do |bot|
9
10
  flunk "No bot Firefox is possible"
@@ -30,6 +30,11 @@ class PinterestTest < Minitest::Test
30
30
  assert bot.valid?, msg: "Valid Pinterest"
31
31
  end
32
32
 
33
+ def test_android_not_bot
34
+ bot = Legitbot.bot("Mozilla/5.0 (Linux; Android 8.0.0; SM-G965F Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.64 Mobile Safari/537.36 [Pinterest/Android]", "85.117.106.133")
35
+ assert_nil bot
36
+ end
37
+
33
38
  def test_engine_name
34
39
  bot = Legitbot.bot("Mozilla/5.0 (compatible; Pinterestbot/1.0; +https://www.pinterest.com/bot.html)", "54.236.1.11")
35
40
  assert_equal :pinterest, bot.detected_as
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legitbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Azarov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-09 00:00:00.000000000 Z
11
+ date: 2018-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: irrc