class IORequest::SSLSockets::Server

SSL socket server.

Public Class Methods

new( port: 8000, authorizer: Authorizer.empty, certificate: nil, key: nil, &requests_handler ) click to toggle source

Initalize new server. @param port [Integer] port of server. @param authorizer [Authorizer] @param certificate [String] @param key [String]

# File lib/io_request/connection/ssl_sockets.rb, line 20
def initialize(
  port: 8000,
  authorizer: Authorizer.empty,
  certificate: nil,
  key: nil,
  &requests_handler
)
  @port = port
  @authorizer = authorizer
  @requests_handler = requests_handler

  initialize_ssl_context(certificate, key)
end

Public Instance Methods

clients() click to toggle source

@return [Array<IORequest::Client>]

# File lib/io_request/connection/ssl_sockets.rb, line 35
def clients
  @clients_data.keys
end
data(client) click to toggle source

@param client [IORequest::Client] @return [Hash, nil] you are free to store anything you want in hash.

Only field you will find in it is `auth` with authenticator data.
# File lib/io_request/connection/ssl_sockets.rb, line 42
def data(client)
  @clients_data[client]
end
start() click to toggle source

Start server.

# File lib/io_request/connection/ssl_sockets.rb, line 47
def start
  @clients_data = {}

  @server = TCPServer.new(@port)

  @accept_thread = in_thread(name: 'accept_thr') { accept_loop }
end
stop() click to toggle source

Fully stop server.

# File lib/io_request/connection/ssl_sockets.rb, line 56
def stop
  clients.each(&:close)
  @clients_data.clear

  @server.close
  @server = nil

  @accept_thread&.kill
  @accept_thread = nil
end

Private Instance Methods

accept_loop() click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 76
def accept_loop
  while (socket = @server.accept)
    handle_socket(socket)
  end
rescue StandardError
  stop
end
handle_client(ssl_socket, client) click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 93
def handle_client(ssl_socket, client)
  auth_data = client.open read_write: ssl_socket
  client.on_request { |data| @requests_handler.call(data, client) }
  @clients_data[client] = { auth: auth_data }
  client.on_close do
    @clients_data.select! { |c, _d| c.open? }
  end
rescue StandardError => e
  IORequest.logger.debug "Failed to open client: #{e}"
  ssl_socket.close
end
handle_socket(socket) click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 84
def handle_socket(socket)
  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, @ctx)
  ssl_socket.accept

  handle_client(ssl_socket, IORequest::Client.new(authorizer: @authorizer))
rescue StandardError => e
  IORequest.logger.warn "Unknown error while handling sockets: #{e}"
end
initialize_ssl_context(certificate, key) click to toggle source
# File lib/io_request/connection/ssl_sockets.rb, line 69
def initialize_ssl_context(certificate, key)
  @ctx = OpenSSL::SSL::SSLContext.new
  @ctx.cert = OpenSSL::X509::Certificate.new certificate
  @ctx.key = OpenSSL::PKey::RSA.new key
  @ctx.ssl_version = :TLSv1_2
end