Sha256: 41e210515c231d644da140d84a69d185365a81b76986bcee1a1b8963a389746d

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

require 'resolv'
require 'ipaddr'

module Legitbot
  ##
  # Represents a bot instance match. Typical methods are
  # +valid?+, +fake?+ and +detected_as+
  #
  class BotMatch
    def initialize(ip, resolver_config = nil)
      @dns = Resolv::DNS.new(resolver_config)
      @ip = ip
    end

    ##
    # Returns a Resolv::DNS::Name instance with
    # the reverse name
    def reverse_domain
      @reverse_domain ||= @dns.getname(@ip)
    rescue Resolv::ResolvError
      @reverse_domain ||= nil
    end

    ##
    # Returns a String with the reverse name
    def reverse_name
      reverse_domain&.to_s
    end

    ##
    # Returns a String with IP created from the reverse name
    def reversed_ip
      return nil if reverse_name.nil?

      @reverse_ip ||= @dns.getaddress(reverse_name)
      @reverse_ip.to_s
    end

    def reverse_resolves?
      @ip == reversed_ip
    end

    def subdomain_of?(*domains)
      return false if reverse_name.nil?

      domains.any? { |d|
        reverse_domain.subdomain_of? Resolv::DNS::Name.create(d)
      }
    end

    def detected_as
      self.class.name.split('::').last.downcase.to_sym
    end

    def fake?
      !valid?
    end

    def self.valid?(ip, resolver_config = nil)
      self.new(ip, resolver_config).valid?
    end

    def self.fake?(ip, resolver_config = nil)
      self.new(ip, resolver_config).fake?
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
legitbot-0.3.2 lib/legitbot/botmatch.rb
legitbot-0.3.1 lib/legitbot/botmatch.rb
legitbot-0.3.0 lib/legitbot/botmatch.rb
legitbot-0.2.7 lib/legitbot/botmatch.rb
legitbot-0.2.6 lib/legitbot/botmatch.rb
legitbot-0.2.4 lib/legitbot/botmatch.rb
legitbot-0.2.3 lib/legitbot/botmatch.rb
legitbot-0.2.2 lib/legitbot/botmatch.rb