Class: IO::Endpoint::SSLEndpoint
- Defined in:
- lib/io/endpoint/ssl_endpoint.rb
Overview
Represents an SSL/TLS endpoint that wraps another endpoint.
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
- #The underlying endpoint.(underlyingendpoint.) ⇒ Object readonly
Instance Method Summary collapse
-
#address ⇒ Object
Get the address from the underlying endpoint.
-
#bind(*arguments, **options, &block) ⇒ Object
Connect to the underlying endpoint and establish a SSL connection.
-
#build_context(context = ::OpenSSL::SSL::SSLContext.new) ⇒ Object
Build an SSL context with configured parameters.
-
#connect(&block) ⇒ Object
Connect to the underlying endpoint and establish a SSL connection.
-
#context ⇒ Object
Get or build the SSL context.
-
#each ⇒ Object
Enumerate all endpoints by wrapping each underlying endpoint with SSL.
-
#hostname ⇒ Object
Get the hostname for SSL verification.
-
#initialize(endpoint, **options) ⇒ SSLEndpoint
constructor
Initialize a new SSL endpoint.
-
#inspect ⇒ Object
Get a detailed string representation of the SSL endpoint.
-
#make_server(io) ⇒ Object
Create an SSL server socket from an IO object.
-
#make_socket(io) ⇒ Object
Create an SSL client socket from an IO object.
-
#params ⇒ Object
Get SSL parameters from options.
- #The options hash.=(optionshash. = (value)) ⇒ Object
-
#to_s ⇒ Object
Get a string representation of the SSL endpoint.
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.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/io/endpoint/ssl_endpoint.rb', line 106 def initialize(endpoint, **) super(**) @endpoint = endpoint if ssl_context = [:ssl_context] @context = build_context(ssl_context) else @context = nil end end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
143 144 145 |
# File 'lib/io/endpoint/ssl_endpoint.rb', line 143 def endpoint @endpoint end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
145 146 147 |
# File 'lib/io/endpoint/ssl_endpoint.rb', line 145 def @options end |
#The underlying endpoint.(underlyingendpoint.) ⇒ Object (readonly)
143 |
# File 'lib/io/endpoint/ssl_endpoint.rb', line 143 attr :endpoint |
Instance Method Details
#address ⇒ Object
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, **, &block) if block_given? @endpoint.bind(*arguments, **) do |server| yield self.make_server(server) end else @endpoint.bind(*arguments, **).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 |
#context ⇒ Object
Get or build the SSL context.
169 170 171 |
# File 'lib/io/endpoint/ssl_endpoint.rb', line 169 def context @context ||= build_context end |
#each ⇒ Object
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 |
#hostname ⇒ Object
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 |
#inspect ⇒ Object
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 |
#params ⇒ Object
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_s ⇒ Object
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 |