class ProxyFetcher::ProxyValidator

Default ProxyFetcher proxy validator that checks either proxy connectable or not. It tries to send HEAD request to default URL to check if proxy can be used (aka connectable?).

Constants

URL_TO_CHECK

Default URL that will be used to check if proxy can be used.

Public Class Methods

connectable?(address, port) click to toggle source

Short variant to validate proxy.

@param address [String] proxy address or IP @param port [String, Integer] proxy port

@return [Boolean]

true if connection to the server using proxy established, otherwise false
# File lib/proxy_fetcher/utils/proxy_validator.rb, line 19
def self.connectable?(address, port)
  new(address, port).connectable?
end
new(address, port, options: {}) click to toggle source

Initialize new ProxyValidator instance

@param address [String] Proxy address or IP @param port [String, Integer] Proxy port @param options [Hash] proxy options

@option username [String] Proxy authentication username
@option password [String] Proxy authentication password
@option headers [Hash] Proxy headers

@return [ProxyValidator]

# File lib/proxy_fetcher/utils/proxy_validator.rb, line 34
def initialize(address, port, options: {})
  timeout = ProxyFetcher.config.proxy_validation_timeout
  proxy = [address, port.to_i]

  if options[:username] && options[:password]
    proxy << options[:username]
    proxy << options[:password]
  end

  proxy << options[:headers].to_h if options[:headers]

  @http = HTTP.follow.via(*proxy).timeout(connect: timeout, read: timeout)
end

Public Instance Methods

connectable?() click to toggle source

Checks if proxy is connectable (can be used to connect resources via proxy server).

@return [Boolean]

true if connection to the server using proxy established, otherwise false
# File lib/proxy_fetcher/utils/proxy_validator.rb, line 54
def connectable?
  ssl_context = OpenSSL::SSL::SSLContext.new
  ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE

  @http.head(URL_TO_CHECK, ssl_context: ssl_context).status.success?
rescue StandardError
  false
end