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(proxy_addr, proxy_port) ⇒ ProxyValidator

Initialize new ProxyValidator instance

Parameters:

  • proxy_addr (String)

    proxy address or IP

  • proxy_port (String, Integer)

    proxy port



30
31
32
33
34
# File 'lib/proxy_fetcher/utils/proxy_validator.rb', line 30

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

Class Method Details

.connectable?(proxy_addr, proxy_port) ⇒ Boolean

Short variant to validate proxy.

Parameters:

  • proxy_addr (String)

    proxy address or IP

  • proxy_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?(proxy_addr, proxy_port)
  new(proxy_addr, proxy_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



42
43
44
45
46
47
48
49
# File 'lib/proxy_fetcher/utils/proxy_validator.rb', line 42

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