Sha256: 5f8c24114f6c73079c6eadfbbdbb82cf9ea1af48199c49a51e4a0a7e0fdf3f3b

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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)
    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
      @reverse_ip ||= @dns.getaddress(reverse_name)
      @reverse_ip.to_s
    end

    def reverse_resolves?
      reversed_ip == @ip
    end

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

    def detected_as
      self.class.name.split('::').last
    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

1 entries across 1 versions & 1 rubygems

Version Path
legitbot-0.1.2 lib/legitbot/botmatch.rb