Class: Lumberjack::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Create a new Lumberjack server.

  • options is a hash. Valid options are:

  • :port - the port to listen on

  • :address - the host/address to bind to

  • :ssl_certificate - the path to the ssl cert to use

  • :ssl_key - the path to the ssl key to use

  • :ssl_key_passphrase - the key passphrase (optional)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lumberjack/server.rb', line 19

def initialize(options={})
  @options = {
    :port => 0,
    :address => "0.0.0.0",
    :ssl_certificate => nil,
    :ssl_key => nil,
    :ssl_key_passphrase => nil,
  }.merge(options)

  [:ssl_certificate, :ssl_key].each do |k|
    if @options[k].nil?
      raise "You must specify #{k} in Lumberjack::Server.new(...)"
    end
  end

  @tcp_server = TCPServer.new(@options[:port])
  # Query the port in case the port number is '0'
  # TCPServer#addr == [ address_family, port, address, address ]
  @port = @tcp_server.addr[1]
  @ssl = OpenSSL::SSL::SSLContext.new
  @ssl.cert = OpenSSL::X509::Certificate.new(File.read(@options[:ssl_certificate]))
  @ssl.key = OpenSSL::PKey::RSA.new(File.read(@options[:ssl_key]),
                                    @options[:ssl_key_passphrase])
  @ssl_server = OpenSSL::SSL::SSLServer.new(@tcp_server, @ssl)
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/lumberjack/server.rb', line 8

def port
  @port
end

Instance Method Details

#run(&block) ⇒ Object

def initialize



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lumberjack/server.rb', line 45

def run(&block)
  while true
    # NOTE: This means ssl accepting is single-threaded.
    begin
      client = @ssl_server.accept
    rescue EOFError, OpenSSL::SSL::SSLError, IOError
      # ssl handshake failure or other issue, skip it.
      # TODO(sissel): log the error
      # TODO(sissel): try to identify what client was connecting that failed.
      client.close rescue nil
      next
    end

    Thread.new(client) do |fd|
      Connection.new(fd).run(&block)
    end
  end
end