Class: RaptorIO::Socket::TCP::SSL

Inherits:
RaptorIO::Socket::TCP show all
Defined in:
lib/raptor-io/socket/tcp/ssl.rb

Overview

TCP client with SSL encryption.

Author:

Constant Summary

Constants inherited from RaptorIO::Socket::TCP

DEFAULT_OPTIONS, DEFAULT_SSL_OPTIONS

Instance Attribute Summary

Attributes inherited from RaptorIO::Socket::TCP

#socket

Attributes inherited from RaptorIO::Socket

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RaptorIO::Socket::TCP

#close, #getpeername, #gets, #read, #read_nonblock, #readpartial, #remote_address, #ssl?, #ssl_version, #to_io, #to_ssl, #ungetc, #write, #write_nonblock

Methods inherited from RaptorIO::Socket

#close, #closed?, getaddrinfo, method_missing, respond_to_missing?, select, #ssl?, #to_io, translate_errors

Constructor Details

#initialize(socket, options = {}) ⇒ SSL

Returns a new instance of SSL.

Parameters:

  • socket (RaptorIO::Socket)
  • options (Hash) (defaults to: {})

    Options

  • ssl_options (Hash)

    a customizable set of options

  • ssl_config (Hash)

    a customizable set of options



45
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
# File 'lib/raptor-io/socket/tcp/ssl.rb', line 45

def initialize( socket, options = {} )
  options = DEFAULT_SSL_OPTIONS.merge( options )
  super

  @context = options[:context] || options[:ssl_context]

  if @context.nil?
    @context = OpenSSL::SSL::SSLContext.new( options[:ssl_version] )
    @context.verify_mode = options[:ssl_verify_mode]
  end

  @socket = OpenSSL::SSL::SSLSocket.new(socket.to_io, @context)
  begin
    #$stderr.puts("#{self.class}#initialize connecting")
    @socket.connect_nonblock
  rescue IO::WaitReadable, IO::WaitWritable => e
    #$stderr.puts("Wait*able #{e}, #{options[:connect_timeout].inspect}")
    if e.kind_of? IO::WaitReadable
      r,w,_ = IO.select([@socket], nil, nil, options[:connect_timeout])
    else
      r,w,_ = IO.select(nil, [@socket], nil, options[:connect_timeout])
    end

    if r.nil? && w.nil?
      #$stderr.puts("timeout")
      raise RaptorIO::Socket::Error::ConnectionTimeout.new(e.to_s)
    end

    retry
  end
end

Class Method Details

.from_openssl(openssl_socket) ⇒ SSL

Create a new RaptorIO::Socket::TCP::SSL from an already-connected ‘OpenSSL::SSL::SSLSocket`.

Examples:

tcp_server = ::TCPServer.new()
ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server)
RaptorIO::Socket::TCP::SSL.from_openssl(ssl_server.accept)

Parameters:

  • openssl_socket (OpenSSL::SSL::SSLSocket)

Returns:

See Also:



18
19
20
21
22
23
24
25
26
# File 'lib/raptor-io/socket/tcp/ssl.rb', line 18

def self.from_openssl(openssl_socket)
  raptor = self.allocate
  raptor.__send__(:socket=, openssl_socket)
  raptor.__send__(:plaintext_socket=, openssl_socket.to_io)
  raptor.options = {}
  raptor.options[:ssl_context] = openssl_socket.context

  raptor
end

Instance Method Details

#ssl_contextOpenSSL::SSL::Context

The SSL context for this encrypted stream.

Returns:

  • (OpenSSL::SSL::Context)


32
# File 'lib/raptor-io/socket/tcp/ssl.rb', line 32

def_delegator :@socket, :ssl_context, :context

#verify_modeFixnum

Returns One of the ‘OpenSSL::SSL::VERIFY_*` constants.

Returns:

  • (Fixnum)

    One of the ‘OpenSSL::SSL::VERIFY_*` constants



36
# File 'lib/raptor-io/socket/tcp/ssl.rb', line 36

def_delegator :@socket, :ssl_verify_mode, :verify_mode

#versionSymbol

Returns SSL version.

Returns:

  • (Symbol)

    SSL version.



40
# File 'lib/raptor-io/socket/tcp/ssl.rb', line 40

def_delegator :@socket, :ssl_version, :version