5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/MLserver/server.rb', line 5
def self.start
settings = MLserver.settings
host = settings.host
port = settings.port
ssl_host = settings.ssl_host
ssl_port = settings.ssl_port
handler = settings.handler
logger = settings.logger
@@use_http_versions = @@valid_http_versions - (@@valid_http_versions - settings.use_http_versions)
logger.log "MLserver #{MLserver.version}"
http_server = TCPServer.new(host, port)
logger.log "Listening on #{logger.format_ip_address host}:#{port}"
if settings.ssl
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.cert = OpenSSL::X509::Certificate.new(File.open(settings.ssl_cert).read)
ssl_context.key = OpenSSL::PKey::RSA.new(File.open(settings.ssl_key).read)
store = OpenSSL::X509::Store.new
settings.ssl_additional_certs.each do |c|
store.add_cert(OpenSSL::X509::Certificate.new(File.open(c).read))
end
ssl_context.cert_store = store
tcp_server = TCPServer.new(ssl_host, ssl_port)
ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ssl_context)
logger.log "Listening with SSL on #{logger.format_ip_address ssl_host}:#{ssl_port}"
end
if settings.ssl
Thread.new do
loop do
begin
client = ssl_server.accept
rescue OpenSSL::SSL::SSLError => e
logger.log "SSL error occured: #{e}", :error
client.close if client
next
rescue Errno::ECONNRESET => e
logger.log "SSL Connection reset: #{e}", :error
client.close if client
next
end
Thread.start(client) do |client|
self.handle_client client
end
end
end
end
loop do
begin
client = http_server.accept
rescue OpenSSL::SSL::SSLError => e
logger.log "SSL error occured: #{e}", :error
client.close if client
next
end
Thread.start(client) do |client|
self.handle_client client
end
end
end
|