Class: Iodine::SSLConnector

Inherits:
Protocol show all
Defined in:
lib/iodine/ssl_connector.rb

Overview

This is a mini-protocol used only to implement the SSL Handshake in a non-blocking manner, allowing for a hardcoded timeout (which you can monkey patch) of 3 seconds.

Constant Summary collapse

TIMEOUT =

hardcoded SSL/TLS handshake timeout

3

Instance Attribute Summary

Attributes inherited from Protocol

#io, #locker, #options

Instance Method Summary collapse

Methods inherited from Protocol

#close, #closed?, each, #id, #on_message, #on_shutdown, #ping, #read, #set_timeout, #ssl?, #timeout?, #write

Constructor Details

#initialize(io, protocol, options = nil) ⇒ SSLConnector

Returns a new instance of SSLConnector.



6
7
8
9
10
# File 'lib/iodine/ssl_connector.rb', line 6

def initialize io, protocol, options = nil
  @protocol = protocol
  @options = options
  super(io)    
end

Instance Method Details

#callObject

atempt an SSL Handshale



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iodine/ssl_connector.rb', line 19

def call
  return if @locker.locked?
  return unless @locker.try_lock
  begin
    @ssl_socket.accept_nonblock
  rescue ::IO::WaitReadable, ::IO::WaitWritable
    return
  rescue ::OpenSSL::SSL::SSLError
    @e = ::OpenSSL::SSL::SSLError.new "Self-signed Certificate?".freeze
    close
    return
  rescue => e
    ::Iodine.warn "SSL Handshake failed with: #{e.message}".freeze
    @e = e
    close
    return
  ensure
    @locker.unlock
  end
  ( (@ssl_socket.npn_protocol && ::Iodine.ssl_protocols[@ssl_socket.npn_protocol]) || @protocol).new @ssl_socket, @options
end

#on_closeObject



40
41
42
43
44
45
# File 'lib/iodine/ssl_connector.rb', line 40

def on_close
  # inform
  ::Iodine.warn "SSL Handshake #{@e ? "failed with: #{@e.message} (#{@e.class.name})" : 'timed-out.'}".freeze
  # the core @io is already closed, but let's make sure the SSL object is closed as well.
  @ssl_socket.close unless @ssl_socket.closed?
end

#on_openObject



12
13
14
15
16
# File 'lib/iodine/ssl_connector.rb', line 12

def on_open
  set_timeout TIMEOUT
  @ssl_socket = ::OpenSSL::SSL::SSLSocket.new(@io, ::Iodine.ssl_context)
  @ssl_socket.sync_close = true
end