Class: DRb::DRbSSLSocket

Inherits:
DRbTCPSocket show all
Defined in:
lib/drb/ssl.rb

Overview

The protocol for DRb over an SSL socket

The URI for a DRb socket over SSL is: drbssl://<host>:<port>?<option>. The option is optional

Defined Under Namespace

Classes: SSLConfig

Instance Attribute Summary

Attributes inherited from DRbTCPSocket

#uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DRbTCPSocket

#alive?, getservername, open_server_inaddr_any, #peeraddr, #recv_reply, #recv_request, #send_reply, #send_request, #set_sockopt, #shutdown

Constructor Details

#initialize(uri, soc, config, is_established) ⇒ DRbSSLSocket

Create a DRb::DRbSSLSocket instance.

uri is the URI we are connected to. soc is the tcp socket we are bound to. config is our configuration. Either a Hash or SSLConfig is_established is a boolean of whether soc is currently established

This is called automatically based on the DRb protocol.



314
315
316
317
# File 'lib/drb/ssl.rb', line 314

def initialize(uri, soc, config, is_established)
  @ssl = is_established ? soc : nil
  super(uri, soc.to_io, config)
end

Class Method Details

.open(uri, config) ⇒ Object

Return an DRb::DRbSSLSocket instance as a client-side connection, with the SSL connected. This is called from DRb::start_service or while connecting to a remote object:

DRb.start_service 'drbssl://localhost:0', front, config

uri is the URI we are connected to, 'drbssl://localhost:0' above, config is our configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig



259
260
261
262
263
264
265
266
# File 'lib/drb/ssl.rb', line 259

def self.open(uri, config)
  host, port, = parse_uri(uri)
  soc = TCPSocket.open(host, port)
  ssl_conf = SSLConfig::new(config)
  ssl_conf.setup_ssl_context
  ssl = ssl_conf.connect(soc)
  self.new(uri, ssl, ssl_conf, true)
end

.open_server(uri, config) ⇒ Object

Returns a DRb::DRbSSLSocket instance as a server-side connection, with the SSL connected. This is called from DRb::start_service or while connecting to a remote object:

DRb.start_service 'drbssl://localhost:0', front, config

uri is the URI we are connected to, 'drbssl://localhost:0' above, config is our configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/drb/ssl.rb', line 277

def self.open_server(uri, config)
  uri = 'drbssl://:0' unless uri
  host, port, = parse_uri(uri)
  if host.size == 0
    host = getservername
    soc = open_server_inaddr_any(host, port)
  else
    soc = TCPServer.open(host, port)
  end
  port = soc.addr[1] if port == 0
  @uri = "drbssl://#{host}:#{port}"

  ssl_conf = SSLConfig.new(config)
  ssl_conf.setup_certificate
  ssl_conf.setup_ssl_context
  self.new(@uri, soc, ssl_conf, false)
end

.parse_uri(uri) ⇒ Object

Parse the dRuby uri for an SSL connection.

Expects drbssl://…

Raises DRbBadScheme or DRbBadURI if uri is not matching or malformed



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/drb/ssl.rb', line 238

def self.parse_uri(uri) # :nodoc:
  if /\Adrbssl:\/\/(.*?):(\d+)(\?(.*))?\z/ =~ uri
    host = $1
    port = $2.to_i
    option = $4
    [host, port, option]
  else
    raise(DRbBadScheme, uri) unless uri.start_with?('drbssl:')
    raise(DRbBadURI, 'can\'t parse uri:' + uri)
  end
end

.uri_option(uri, config) ⇒ Object

This is a convenience method to parse uri and separate out any additional options appended in the uri.

Returns an option-less uri and the option => [uri,option]

The config is completely unused, so passing nil is sufficient.



301
302
303
304
# File 'lib/drb/ssl.rb', line 301

def self.uri_option(uri, config) # :nodoc:
  host, port, option = parse_uri(uri)
  return "drbssl://#{host}:#{port}", option
end

Instance Method Details

#acceptObject

:nodoc:



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/drb/ssl.rb', line 331

def accept # :nodoc:
  begin
  while true
    soc = accept_or_shutdown
    return nil unless soc
    break if (@acl ? @acl.allow_socket?(soc) : true)
    soc.close
  end
  begin
    ssl = @config.accept(soc)
  rescue Exception
    soc.close
    raise
  end
  self.class.new(uri, ssl, @config, true)
  rescue OpenSSL::SSL::SSLError
    warn("#{$!.message} (#{$!.class})", uplevel: 0) if @config[:verbose]
    retry
  end
end

#closeObject

Closes the SSL stream before closing the dRuby connection.



323
324
325
326
327
328
329
# File 'lib/drb/ssl.rb', line 323

def close # :nodoc:
  if @ssl
    @ssl.close
    @ssl = nil
  end
  super
end

#streamObject

Returns the SSL stream



320
# File 'lib/drb/ssl.rb', line 320

def stream; @ssl; end