Class: ServerHALEC

Inherits:
HALEC
  • Object
show all
Defined in:
lib/rack/tctp/halec.rb

Overview

The Server end of an HALEC

Instance Attribute Summary

Attributes inherited from HALEC

#certificate, #ctx, #private_key, #socket_here, #socket_there, #ssl_socket, #url

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ServerHALEC

Returns a new instance of ServerHALEC.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rack/tctp/halec.rb', line 58

def initialize(options = {})
  super(options)

  if(options[:private_key] && options[:certificate])
    @private_key = options[:private_key]
    @certificate = options[:certificate]
  else
    @private_key = ServerHALEC.default_key
    @certificate = ServerHALEC.default_self_signed_certificate
  end

  @ctx.cert = @certificate
  @ctx.key = @private_key

  @ssl_socket = OpenSSL::SSL::SSLSocket.new(@socket_here, @ctx)
  Thread.new {
    begin
      s = @ssl_socket.accept
    rescue Exception => e
      puts e
    end
  }
end

Class Method Details

.default_keyObject



93
94
95
# File 'lib/rack/tctp/halec.rb', line 93

def default_key
  @default_key ||= OpenSSL::PKey::RSA.new 2048
end

.default_self_signed_certificateObject



97
98
99
# File 'lib/rack/tctp/halec.rb', line 97

def default_self_signed_certificate
  @default_self_signed_certificate ||= generate_self_signed_certificate
end

.generate_self_signed_certificateObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rack/tctp/halec.rb', line 101

def generate_self_signed_certificate
  name = OpenSSL::X509::Name.parse 'CN=tctp-server/DC=tctp'

  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = 0
  cert.not_before = Time.now
  cert.not_after = Time.now + 3600

  cert.public_key = @default_key.public_key
  cert.subject = name

  cert
end

.initializeObject



86
87
88
89
90
91
# File 'lib/rack/tctp/halec.rb', line 86

def initialize
  default_key
  default_self_signed_certificate

  self
end