Class: ProxyFetcher::ProxyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_fetcher/utils/proxy_validator.rb

Overview

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?).

Constant Summary collapse

URL_TO_CHECK =

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

"https://google.com"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port, options: {}) ⇒ ProxyValidator

Initialize new ProxyValidator instance

Parameters:

  • address (String)

    Proxy address or IP

  • port (String, Integer)

    Proxy port

  • options (Hash) (defaults to: {})

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



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Class Method Details

.connectable?(address, port) ⇒ Boolean

Short variant to validate proxy.

Parameters:

  • address (String)

    proxy address or IP

  • port (String, Integer)

    proxy port

Returns:

  • (Boolean)

    true if connection to the server using proxy established, otherwise false



19
20
21
# File 'lib/proxy_fetcher/utils/proxy_validator.rb', line 19

def self.connectable?(address, port)
  new(address, port).connectable?
end

Instance Method Details

#connectable?Boolean

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

Returns:

  • (Boolean)

    true if connection to the server using proxy established, otherwise false



54
55
56
57
58
59
60
61
# 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