Class: IO::Endpoint::SSLEndpoint

Inherits:
Generic
  • Object
show all
Defined in:
lib/io/endpoint/ssl_endpoint.rb

Overview

Represents an SSL/TLS endpoint that wraps another endpoint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#accept, #bound, #connected, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper

Constructor Details

#initialize(endpoint, **options) ⇒ SSLEndpoint

Initialize a new SSL endpoint.

Parameters:

  • ssl_context (Hash)

    a customizable set of options



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/io/endpoint/ssl_endpoint.rb', line 106

def initialize(endpoint, **options)
	super(**options)
	
	@endpoint = endpoint
	
	if ssl_context = options[:ssl_context]
		@context = build_context(ssl_context)
	else
		@context = nil
	end
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



143
144
145
# File 'lib/io/endpoint/ssl_endpoint.rb', line 143

def endpoint
  @endpoint
end

#optionsObject (readonly)

Returns the value of attribute options.



145
146
147
# File 'lib/io/endpoint/ssl_endpoint.rb', line 145

def options
  @options
end

#The underlying endpoint.(underlyingendpoint.) ⇒ Object (readonly)



143
# File 'lib/io/endpoint/ssl_endpoint.rb', line 143

attr :endpoint

Instance Method Details

#addressObject

Get the address from the underlying endpoint.



132
133
134
# File 'lib/io/endpoint/ssl_endpoint.rb', line 132

def address
	@endpoint.address
end

#bind(*arguments, **options, &block) ⇒ Object

Connect to the underlying endpoint and establish a SSL connection.



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/io/endpoint/ssl_endpoint.rb', line 196

def bind(*arguments, **options, &block)
	if block_given?
		@endpoint.bind(*arguments, **options) do |server|
			yield self.make_server(server)
		end
	else
		@endpoint.bind(*arguments, **options).map do |server|
			self.make_server(server)
		end
	end
end

#build_context(context = ::OpenSSL::SSL::SSLContext.new) ⇒ Object

Build an SSL context with configured parameters.



156
157
158
159
160
161
162
163
164
165
# File 'lib/io/endpoint/ssl_endpoint.rb', line 156

def build_context(context = ::OpenSSL::SSL::SSLContext.new)
	if params = self.params
		context.set_params(params)
	end
	
	# context.setup
	# context.freeze
	
	return context
end

#connect(&block) ⇒ Object

Connect to the underlying endpoint and establish a SSL connection.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/io/endpoint/ssl_endpoint.rb', line 212

def connect(&block)
	socket = self.make_socket(@endpoint.connect)
	
	if hostname = self.hostname
		socket.hostname = hostname
	end
	
	begin
		socket.connect
	rescue
		socket.close
		raise
	end
	
	return socket unless block_given?
	
	begin
		yield socket
	ensure
		socket.close
	end
end

#contextObject

Get or build the SSL context.



169
170
171
# File 'lib/io/endpoint/ssl_endpoint.rb', line 169

def context
	@context ||= build_context
end

#eachObject

Enumerate all endpoints by wrapping each underlying endpoint with SSL.



238
239
240
241
242
243
244
# File 'lib/io/endpoint/ssl_endpoint.rb', line 238

def each
	return to_enum unless block_given?
	
	@endpoint.each do |endpoint|
		yield self.class.new(endpoint, **@options)
	end
end

#hostnameObject

Get the hostname for SSL verification.



138
139
140
# File 'lib/io/endpoint/ssl_endpoint.rb', line 138

def hostname
	@options[:hostname] || @endpoint.hostname
end

#inspectObject

Get a detailed string representation of the SSL endpoint.



126
127
128
# File 'lib/io/endpoint/ssl_endpoint.rb', line 126

def inspect
	"\#<#{self.class} endpoint=#{@endpoint.inspect}>"
end

#make_server(io) ⇒ Object

Create an SSL server socket from an IO object.



176
177
178
179
180
# File 'lib/io/endpoint/ssl_endpoint.rb', line 176

def make_server(io)
	::OpenSSL::SSL::SSLServer.new(io, self.context).tap do |server|
		server.start_immediately = false
	end
end

#make_socket(io) ⇒ Object

Create an SSL client socket from an IO object.



185
186
187
188
189
190
# File 'lib/io/endpoint/ssl_endpoint.rb', line 185

def make_socket(io)
	::OpenSSL::SSL::SSLSocket.new(io, self.context).tap do |socket|
		# We consider the underlying IO is owned by the SSL socket:
		socket.sync_close = true
	end
end

#paramsObject

Get SSL parameters from options.



149
150
151
# File 'lib/io/endpoint/ssl_endpoint.rb', line 149

def params
	@options[:ssl_params]
end

#The options hash.=(optionshash. = (value)) ⇒ Object



145
# File 'lib/io/endpoint/ssl_endpoint.rb', line 145

attr :options

#to_sObject

Get a string representation of the SSL endpoint.



120
121
122
# File 'lib/io/endpoint/ssl_endpoint.rb', line 120

def to_s
	"ssl:#{@endpoint}"
end