Sha256: 2555cce55f6c96d61fe2bfc218fdc82a29c84bdd6b3c2daf9368c4f1a5b7d830

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  # 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?).
  class ProxyValidator
    # Default URL that will be used to check if proxy can be used.
    URL_TO_CHECK = 'https://google.com'.freeze

    # Short variant to validate proxy.
    #
    # @param proxy_addr [String] proxy address or IP
    # @param proxy_port [String, Integer] proxy port
    #
    # @return [Boolean]
    #   true if connection to the server using proxy established, otherwise false
    #
    def self.connectable?(proxy_addr, proxy_port)
      new(proxy_addr, proxy_port).connectable?
    end

    # Initialize new ProxyValidator instance
    #
    # @param proxy_addr [String] proxy address or IP
    # @param proxy_port [String, Integer] proxy port
    #
    # @return [ProxyValidator]
    #
    def initialize(proxy_addr, proxy_port)
      timeout = ProxyFetcher.config.proxy_validation_timeout

      @http = HTTP.follow.via(proxy_addr, proxy_port.to_i).timeout(connect: timeout, read: timeout)
    end

    # 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
    #
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
proxy_fetcher-0.10.2 lib/proxy_fetcher/utils/proxy_validator.rb
proxy_fetcher-0.10.1 lib/proxy_fetcher/utils/proxy_validator.rb
proxy_fetcher-0.10.0 lib/proxy_fetcher/utils/proxy_validator.rb
proxy_fetcher-0.9.0 lib/proxy_fetcher/utils/proxy_validator.rb