Class: Legitbot::Facebook

Inherits:
BotMatch show all
Defined in:
lib/legitbot/facebook.rb

Overview

Constant Summary collapse

AS =
'AS32934'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BotMatch

#detected_as, #fake?, fake?, #initialize, #reverse_domain, #reverse_name, #reverse_resolves?, #reversed_ip, #subdomain_of?, valid?

Constructor Details

This class inherits a constructor from Legitbot::BotMatch

Class Method Details

.load_ipsObject



26
27
28
29
30
31
32
33
34
# File 'lib/legitbot/facebook.rb', line 26

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

.reload!Object



22
23
24
# File 'lib/legitbot/facebook.rb', line 22

def self.reload!
  @mutex.synchronize { @ips = load_ips }
end

.valid_ipsObject



18
19
20
# File 'lib/legitbot/facebook.rb', line 18

def self.valid_ips
  @mutex.synchronize { @ips ||= load_ips }
end

.whoisObject



36
37
38
39
40
41
42
43
44
# File 'lib/legitbot/facebook.rb', line 36

def self.whois
  client = Irrc::Client.new
  client.query :radb, AS
  results = client.perform

  %i(ipv4 ipv6).map do |family|
    [family, results[AS][family][AS]]
  end.to_h
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/legitbot/facebook.rb', line 11

def valid?
  ip = IPAddr.new(@ip)
  Facebook.valid_ips[ip.ipv4? ? :ipv4 : :ipv6].search(ip.to_i).size > 0
end