Sha256: ee47b4cd9bc68db65f8aec489fd2fbc73587208895f8e207e3c8174f7d6c0e9f

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'openssl'
require 'socket'
require 'thread'

module Grocer
  class SSLServer
    attr_accessor :port

    def initialize(options = {})
      options = defaults.merge(options)
      options.each { |k, v| send("#{k}=", v) }
    end

    def defaults
      {
        port: 2195
      }
    end

    def accept
      while socket = ssl_socket.accept
        yield socket if block_given?
      end
    end

    def close
      if @ssl_socket
        @ssl_socket.shutdown
        @ssl_socket.close
        @ssl_socket = nil
        @socket = nil
      end
    end

    private

    def ssl_socket
      @ssl_socket ||= OpenSSL::SSL::SSLServer.new(socket, context)
    end

    def socket
      @socket ||= TCPServer.new('127.0.0.1', port)
    end

    def context
      @context ||= OpenSSL::SSL::SSLContext.new.tap do |c|
        c.cert = OpenSSL::X509::Certificate.new(File.read(crt_path))
        c.key  = OpenSSL::PKey::RSA.new(File.read(key_path))
      end
    end

    def crt_path
      File.join(File.dirname(__FILE__), "test", "server.crt")
    end

    def key_path
      File.join(File.dirname(__FILE__), "test", "server.key")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grocer-0.0.10 lib/grocer/ssl_server.rb