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

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) ⇒ SSLConnector

Returns a new instance of SSLConnector.



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

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

Instance Method Details

#callObject

atempt an SSL Handshale



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

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
end

#on_closeObject



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

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



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

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