2017-08-28 20:35:20 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2008 Kyle Maxwell, contributors
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation
|
|
|
|
# files (the "Software"), to deal in the Software without
|
|
|
|
# restriction, including without limitation the rights to use,
|
|
|
|
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following
|
|
|
|
# conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
# OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#
|
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
require "open-uri"
|
|
|
|
require "uri"
|
|
|
|
require "timeout"
|
2017-08-28 20:35:20 +00:00
|
|
|
require 'rex/logging/log_dispatcher'
|
2017-08-28 20:20:53 +00:00
|
|
|
|
2017-08-28 20:35:20 +00:00
|
|
|
# https://github.com/fizx/robots
|
2017-08-28 20:20:53 +00:00
|
|
|
class Robots
|
|
|
|
DEFAULT_TIMEOUT = 3
|
2017-08-28 20:35:20 +00:00
|
|
|
|
|
|
|
# Represents a parsed robots.txt file
|
2017-08-28 20:20:53 +00:00
|
|
|
class ParsedRobots
|
|
|
|
def initialize(uri, user_agent)
|
|
|
|
@last_accessed = Time.at(1)
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
io = Robots.get_robots_txt(uri, user_agent)
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:23:27 +00:00
|
|
|
if !io || io.content_type != "text/plain" || io.status.first != "200"
|
2017-08-28 20:20:53 +00:00
|
|
|
io = StringIO.new("User-agent: *\nAllow: /\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
@other = {}
|
|
|
|
@disallows = {}
|
|
|
|
@allows = {}
|
|
|
|
@delays = {} # added delays to make it work
|
|
|
|
agent = /.*/
|
|
|
|
io.each do |line|
|
|
|
|
next if line =~ /^\s*(#.*|$)/
|
|
|
|
arr = line.split(":")
|
2017-08-28 20:22:56 +00:00
|
|
|
key = arr.shift.to_s.downcase
|
2017-08-28 20:20:53 +00:00
|
|
|
value = arr.join(":").strip
|
|
|
|
value.strip!
|
|
|
|
case key
|
2017-08-28 20:22:56 +00:00
|
|
|
when "user-agent"
|
2017-08-28 20:20:53 +00:00
|
|
|
agent = to_regex(value)
|
2017-08-28 20:22:56 +00:00
|
|
|
when "allow"
|
2017-08-28 20:20:53 +00:00
|
|
|
@allows[agent] ||= []
|
|
|
|
@allows[agent] << to_regex(value)
|
2017-08-28 20:22:56 +00:00
|
|
|
when "disallow"
|
2017-08-28 20:20:53 +00:00
|
|
|
@disallows[agent] ||= []
|
|
|
|
@disallows[agent] << to_regex(value)
|
2017-08-28 20:22:56 +00:00
|
|
|
when "crawl-delay"
|
2017-08-28 20:20:53 +00:00
|
|
|
@delays[agent] = value.to_i
|
|
|
|
else
|
|
|
|
@other[key] ||= []
|
|
|
|
@other[key] << value
|
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
@parsed = true
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def allowed?(uri, user_agent)
|
|
|
|
return true unless @parsed
|
|
|
|
allowed = true
|
|
|
|
path = uri.request_uri
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
@disallows.each do |key, value|
|
|
|
|
if user_agent =~ key
|
|
|
|
value.each do |rule|
|
2017-08-28 20:35:20 +00:00
|
|
|
allowed = false if path =~ rule
|
2017-08-28 20:20:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
@allows.each do |key, value|
|
2017-08-28 20:35:20 +00:00
|
|
|
unless allowed
|
2017-08-28 20:20:53 +00:00
|
|
|
if user_agent =~ key
|
|
|
|
value.each do |rule|
|
|
|
|
if path =~ rule
|
|
|
|
allowed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
if allowed && @delays[user_agent]
|
|
|
|
sleep @delays[user_agent] - (Time.now - @last_accessed)
|
|
|
|
@last_accessed = Time.now
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
return allowed
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def other_values
|
|
|
|
@other
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def to_regex(pattern)
|
|
|
|
return /should-not-match-anything-123456789/ if pattern.strip.empty?
|
|
|
|
pattern = Regexp.escape(pattern)
|
|
|
|
pattern.gsub!(Regexp.escape("*"), ".*")
|
|
|
|
Regexp.compile("^#{pattern}")
|
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def self.get_robots_txt(uri, user_agent)
|
|
|
|
begin
|
2017-08-28 20:35:20 +00:00
|
|
|
Timeout.timeout(Robots.timeout) do
|
|
|
|
begin
|
|
|
|
URI.join(uri.to_s, "/robots.txt").open("User-Agent" => user_agent)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:20:53 +00:00
|
|
|
rescue Timeout::Error
|
2017-08-28 20:35:20 +00:00
|
|
|
dlog("robots.txt request timed out")
|
2017-08-28 20:20:53 +00:00
|
|
|
end
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
|
|
|
attr_writer :timeout
|
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def self.timeout
|
|
|
|
@timeout || DEFAULT_TIMEOUT
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def initialize(user_agent)
|
|
|
|
@user_agent = user_agent
|
|
|
|
@parsed = {}
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def allowed?(uri)
|
|
|
|
uri = URI.parse(uri.to_s) unless uri.is_a?(URI)
|
|
|
|
host = uri.host
|
|
|
|
@parsed[host] ||= ParsedRobots.new(uri, @user_agent)
|
|
|
|
@parsed[host].allowed?(uri, @user_agent)
|
|
|
|
end
|
2017-08-28 20:35:20 +00:00
|
|
|
|
2017-08-28 20:20:53 +00:00
|
|
|
def other_values(uri)
|
|
|
|
uri = URI.parse(uri.to_s) unless uri.is_a?(URI)
|
|
|
|
host = uri.host
|
|
|
|
@parsed[host] ||= ParsedRobots.new(uri, @user_agent)
|
|
|
|
@parsed[host].other_values
|
|
|
|
end
|
|
|
|
end
|