class ProxyTester::Actions::FetchUrls

Attributes

concurrent[R]
count[R]
output[R]
proxy[R]
reporter[R]
result[R]
timeout[R]
urls[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/proxy_tester/actions/fetch_urls.rb, line 12
def initialize(options = {})
  @urls   = options.fetch(:urls).collect { |u| Addressable::URI.heuristic_parse(u).to_s }
  @proxy = options.fetch(:proxy)
  @timeout  = options.fetch(:timeout).to_i
  @count = options.fetch(:count).to_i
  @concurrent = options.fetch(:concurrent)
  
  ProxyTester.ui_logger.debug "Options: " + options.to_s

  @output   =  options.fetch(:output)
  @reporter = options.fetch(:reporter, Reporters::FetchUrls.new(output))

  @result = []
end

Public Instance Methods

run() click to toggle source
# File lib/proxy_tester/actions/fetch_urls.rb, line 27
def run
  threads = []

  reporter.header

  urls.each do |u|
    count.times do |n|
      threads << Thread.new do
        ProxyTester.ui_logger.info "Starting #{n + 1}-nth thread to fetch \"#{u}\" via \"#{proxy.to_string}\"."

        begin 
          fetch_url(u)
        end while concurrent
      end
    end
  end


  sleep_time = 0
  while sleep_time < timeout and threads.any? { |t| t.alive? }
    sleep 1
    sleep_time += 1
  end

  if sleep_time < timeout
    ProxyTester.ui_logger.info "All threads finished before waiting time \"#{timeout}\" was over."
  else
    ProxyTester.ui_logger.info "Waiting time #{timeout} is over. Killing all active threads..."
  end

  threads.each do |t|
    Thread.kill(t)
  end

end

Private Instance Methods

fetch_url(url) click to toggle source
# File lib/proxy_tester/actions/fetch_urls.rb, line 65
def fetch_url(url)
  begin
    response = Excon.get(url, 
                         proxy: proxy.to_string(cleartext: true),
                         middlewares: Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower],
                         read_timeout: timeout,
                         write_timeout: timeout,
                         headers: {
                           'User-Agent' => 'ProxyTester',
                         }
                        )
  rescue Excon::Errors::SocketError
    handler = ErrorHandler.find(Exceptions::ProxyNotReachable)
    handler.use(host: proxy.host, port: proxy.port)

    ProxyTester.ui_logger.fatal handler.details

    raise Exceptions::ProxyNotReachable, JSON.dump(host: proxy.port, port: proxy.port)
  end

  reporter.data(
    headers: response.headers,
    status: response.status,
    url: u,
    proxy: proxy.to_string,
  )
end