2014-02-20 16:07:02 -05:00
|
|
|
module CrawlerDetection
|
2017-09-28 22:31:50 -04:00
|
|
|
|
2018-01-16 00:28:11 -05:00
|
|
|
def self.to_matcher(string, type: nil)
|
2017-09-28 22:31:50 -04:00
|
|
|
escaped = string.split('|').map { |agent| Regexp.escape(agent) }.join('|')
|
2018-01-16 00:28:11 -05:00
|
|
|
|
|
|
|
if type == :real && Rails.env == "test"
|
|
|
|
# we need this bypass so we properly render views
|
|
|
|
escaped << "|Rails Testing"
|
|
|
|
end
|
|
|
|
|
2018-01-15 23:41:13 -05:00
|
|
|
Regexp.new(escaped, Regexp::IGNORECASE)
|
2017-09-28 22:31:50 -04:00
|
|
|
end
|
2015-02-14 09:24:51 -05:00
|
|
|
|
2014-02-20 16:07:02 -05:00
|
|
|
def self.crawler?(user_agent)
|
2018-01-16 00:28:11 -05:00
|
|
|
return true if user_agent.nil?
|
|
|
|
|
2017-09-28 22:31:50 -04:00
|
|
|
# this is done to avoid regenerating regexes
|
2018-01-15 23:41:13 -05:00
|
|
|
@non_crawler_matchers ||= {}
|
2017-09-28 22:31:50 -04:00
|
|
|
@matchers ||= {}
|
2018-01-15 23:41:13 -05:00
|
|
|
|
2018-01-16 00:28:11 -05:00
|
|
|
possibly_real = (@non_crawler_matchers[SiteSetting.non_crawler_user_agents] ||= to_matcher(SiteSetting.non_crawler_user_agents, type: :real))
|
2018-01-15 23:41:13 -05:00
|
|
|
|
|
|
|
if user_agent.match?(possibly_real)
|
|
|
|
known_bots = (@matchers[SiteSetting.crawler_user_agents] ||= to_matcher(SiteSetting.crawler_user_agents))
|
|
|
|
user_agent.match?(known_bots)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-02-14 17:10:08 -05:00
|
|
|
end
|
2018-03-15 17:10:45 -04:00
|
|
|
|
|
|
|
# Given a user_agent that returns true from crawler?, should its request be allowed?
|
|
|
|
def self.allow_crawler?(user_agent)
|
|
|
|
return true if SiteSetting.whitelisted_crawler_user_agents.blank? &&
|
|
|
|
SiteSetting.blacklisted_crawler_user_agents.blank?
|
|
|
|
|
|
|
|
@whitelisted_matchers ||= {}
|
|
|
|
@blacklisted_matchers ||= {}
|
|
|
|
|
|
|
|
if SiteSetting.whitelisted_crawler_user_agents.present?
|
|
|
|
whitelisted = @whitelisted_matchers[SiteSetting.whitelisted_crawler_user_agents] ||= to_matcher(SiteSetting.whitelisted_crawler_user_agents)
|
|
|
|
!user_agent.nil? && user_agent.match?(whitelisted)
|
|
|
|
else
|
|
|
|
blacklisted = @blacklisted_matchers[SiteSetting.blacklisted_crawler_user_agents] ||= to_matcher(SiteSetting.blacklisted_crawler_user_agents)
|
|
|
|
user_agent.nil? || !user_agent.match?(blacklisted)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_blocked_crawler?(user_agent)
|
|
|
|
crawler?(user_agent) && !allow_crawler?(user_agent)
|
|
|
|
end
|
2014-02-14 17:10:08 -05:00
|
|
|
end
|