限制可以拯救多少错误?

时间:2016-04-02 02:56:19

标签: ruby exception-handling timeout

我有一个程序,我正在使用它作为一个测试工具,我正在发现网站是否是SQL易受攻击并遇到Timeout::Error现在我试图挽救错误,但也有还有一些其他需要拯救的错误。所以我的问题是,救援区内可以挽救多少错误是否有限制?如果没有,为什么这个Timeout没有被救出?

来源:

def get_urls
  info("Searching for possible SQL vulnerable sites.")
  @agent = Mechanize.new
  page = @agent.get('http://www.google.com/')
  google_form = page.form('f')
  google_form.q = "#{SEARCH}"
  url = @agent.submit(google_form, google_form.buttons.first)
  url.links.each do |link|
    if link.href.to_s =~ /url.q/
      str = link.href.to_s
      str_list = str.split(%r{=|&})
      urls = str_list[1]
      next if str_list[1].split('/')[2] == "webcache.googleusercontent.com"
      urls_to_log = urls.gsub("%3F", '?').gsub("%3D", '=')
      success("Site found: #{urls_to_log}")
      File.open("#{PATH}/temp/SQL_sites_to_check.txt", "a+") {|s| s.puts("#{urls_to_log}'")}
    end
  end
  info("Possible vulnerable sites dumped into #{PATH}/temp/SQL_sites.txt")
end

def check_if_vulnerable
  info("Checking if sites are vulnerable.")
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
    Timeout::timeout(5) do
      begin
        @parsing = Nokogiri::HTML(RestClient.get("#{parse.chomp}")) 
      rescue Timeout::Error, RestClient::ResourceNotFound, RestClient::SSLCertificateNotVerified
        if RestClient::ResourceNotFound
          warn("URL: #{parse.chomp} returned 404 error, URL dumped into 404 bin")
          File.open("#{PATH}/lib/404_bin.txt", "a+"){|s| s.puts(parse)}
        elsif RestClient::SSLCertificateNotVerified
          err("URL: #{parse.chomp} requires SSL cert, url dumped into SSL bin")
          File.open("#{PATH}/lib/SSL_bin.txt", "a+"){|s| s.puts(parse)}
        elsif Timeout::Error
          warn("URL: #{parse.chomp} failed to load resulting in time out after 10 seconds. URL dumped into TIMEOUT bin")
          File.open("#{PATH}/lib/TIMEOUT_bin.txt", "a+"){|s| s.puts(parse)}
        end
      end
    end
  end
end

错误:

C:/Ruby22/lib/ruby/2.2.0/net/http.rb:892:in `new': execution expired (Timeout::E
rror)
        from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:892:in `connect'
        from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
        from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:852:in `start'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0-x86-mingw32/li
b/restclient/request.rb:413:in `transmit'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0-x86-mingw32/li
b/restclient/request.rb:176:in `execute'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0-x86-mingw32/li
b/restclient/request.rb:41:in `execute'
        from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0-x86-mingw32/li
b/restclient.rb:65:in `get'
        from whitewidow.rb:94:in `block (2 levels) in check_if_vulnerable'
        from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
        from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:32:in `block in catch'
        from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:32:in `catch'
        from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:32:in `catch'
        from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:103:in `timeout'
        from whitewidow.rb:92:in `block in check_if_vulnerable'
        from whitewidow.rb:91:in `each_line'
        from whitewidow.rb:91:in `check_if_vulnerable'
        from whitewidow.rb:113:in `<main>'

正如您在check_vulns方法中所看到的那样,Timeout::Error获救了。那么是什么导致这个超时而不转移到下一个URL?我已经尝试添加next来救援,但它仍然无效,请帮忙吗?

1 个答案:

答案 0 :(得分:1)

只需移动超时我就可以解决错误

def check_if_vulnerable
  info("Checking if sites are vulnerable.")
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
      begin
        Timeout::timeout(5) do
          @parsing = Nokogiri::HTML(RestClient.get("#{parse.chomp}")) 
        end
      rescue Timeout::Error, RestClient::ResourceNotFound, RestClient::SSLCertificateNotVerified
        if RestClient::ResourceNotFound
          warn("URL: #{parse.chomp} returned 404 error, URL dumped into 404 bin")
          File.open("#{PATH}/lib/404_bin.txt", "a+"){|s| s.puts(parse)}
        elsif RestClient::SSLCertificateNotVerified
          err("URL: #{parse.chomp} requires SSL cert, url dumped into SSL bin")
          File.open("#{PATH}/lib/SSL_bin.txt", "a+"){|s| s.puts(parse)}
        elsif Timeout::Error
          warn("URL: #{parse.chomp} failed to load resulting in time out after 10 seconds. URL dumped into TIMEOUT bin")
          File.open("#{PATH}/lib/TIMEOUT_bin.txt", "a+"){|s| s.puts(parse)}
        end
      end
    end
  end
end