Class: SSLCheck::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sslcheck/client.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

@@timeout_seconds =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



42
43
44
# File 'lib/sslcheck/client.rb', line 42

def initialize
  @response = Response.new
end

Class Method Details

.timeout_secondsObject



10
11
12
# File 'lib/sslcheck/client.rb', line 10

def self.timeout_seconds
  @@timeout_seconds
end

.timeout_seconds=(seconds) ⇒ Object



14
15
16
# File 'lib/sslcheck/client.rb', line 14

def self.timeout_seconds=(seconds)
  @@timeout_seconds = seconds
end

Instance Method Details

#get(url) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sslcheck/client.rb', line 46

def get(url)
  begin
    Timeout::timeout(Client.timeout_seconds) {
      uri = determine_uri(url)

      sock = TCPSocket.new(uri.host, 443)
      ctx = OpenSSL::SSL::SSLContext.new
      ctx.set_params(
        :verify_mode => OpenSSL::SSL::VERIFY_PEER,
        :timeout     => Client.timeout_seconds,
        :ssl_timeout => Client.timeout_seconds,
      )

      ctx.timeout = Client.timeout_seconds
      ctx.ssl_timeout = Client.timeout_seconds

      @socket = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket|
        socket.sync_close = true
        socket.connect
        @response.host_name = uri.host
        @response.raw_peer_cert = OpenSSL::X509::Certificate.new(socket.peer_cert)
        @response.raw_peer_cert_chain = socket.peer_cert_chain
      end

      @socket.sysclose

    }
  rescue Timeout::Error, Errno::ETIMEDOUT
    @response.errors << SSLCheck::Errors::Connection::Timeout.new({:name => "Timeout Error", :type => :timeout_error, :message => "The connection to #{url} took too long."})
  rescue SocketError
    @response.errors << SSLCheck::Errors::Connection::SocketError.new({:name => "Connection Error", :type => :socket_error, :message => "The connection to #{url} failed."})
  rescue URI::InvalidURIError
    @response.errors << SSLCheck::Errors::Connection::InvalidURI.new({:name => "Invalid URI Error", :type => :invalid_uri, :message => "The URI, #{url}, is not a valid URI."})
  rescue OpenSSL::SSL::SSLError
    @response.errors << SSLCheck::Errors::Connection::SSLVerify.new({:name => "OpenSSL Verification Error", :type => :openssl_error, :message => "There was a peer verification error."})
  end

  @response
end