@@ -40,12 +40,13 @@ def initialize(opts)
4040 @interpreter = opts . fetch ( :interpreter , "ruby" )
4141 @host = opts . fetch ( :host , ::HOST )
4242 @port = opts . fetch ( :port , ::PORT )
43+ @tls = opts . fetch ( :tls , false )
4344 end
4445
4546 def start
4647 return if @serverclass == Object
4748 args = ( File . basename ( @interpreter ) == "jruby" ? "-J-server" : "" )
48- @pipe = IO . popen ( "#{ @interpreter } #{ args } #{ File . dirname ( __FILE__ ) } /server.rb #{ @host } #{ @port } #{ @serverclass . name } " , "r+" )
49+ @pipe = IO . popen ( "#{ @interpreter } #{ args } #{ File . dirname ( __FILE__ ) } /server.rb #{ "-tls" if @tls } #{ @host } #{ @port } #{ @serverclass . name } " , "r+" )
4950 Marshal . load ( @pipe ) # wait until the server has started
5051 sleep 0.4 # give the server time to actually start spawning sockets
5152 end
@@ -75,6 +76,7 @@ def initialize(opts, server)
7576 @interpreter = opts . fetch ( :interpreter , "ruby" )
7677 @server = server
7778 @log_exceptions = opts . fetch ( :log_exceptions , false )
79+ @tls = opts . fetch ( :tls , false )
7880 end
7981
8082 def run
@@ -93,7 +95,7 @@ def run
9395 end
9496
9597 def spawn
96- pipe = IO . popen ( "#{ @interpreter } #{ File . dirname ( __FILE__ ) } /client.rb #{ "-log-exceptions" if @log_exceptions } #{ @host } #{ @port } #{ @clients_per_process } #{ @calls_per_client } " )
98+ pipe = IO . popen ( "#{ @interpreter } #{ File . dirname ( __FILE__ ) } /client.rb #{ "-log-exceptions" if @log_exceptions } #{ "-tls" if @tls } #{ @host } #{ @port } #{ @clients_per_process } #{ @calls_per_client } " )
9799 @pool << pipe
98100 end
99101
@@ -249,18 +251,53 @@ def resolve_const(const)
249251 const and const . split ( '::' ) . inject ( Object ) { |k , c | k . const_get ( c ) }
250252end
251253
254+ def generate_certificate
255+ key = OpenSSL ::PKey ::EC . generate ( "prime256v1" )
256+
257+ cert = OpenSSL ::X509 ::Certificate . new
258+ cert . version = 2
259+ cert . serial = 1
260+ cert . subject = OpenSSL ::X509 ::Name . parse ( "/C=US/O=Benchmark/CN=localhost" )
261+ cert . issuer = cert . subject
262+ cert . public_key = key
263+ cert . not_before = Time . now
264+ cert . not_after = Time . now + 3600
265+
266+ # Add extensions
267+ ef = OpenSSL ::X509 ::ExtensionFactory . new
268+ ef . subject_certificate = cert
269+ ef . issuer_certificate = cert
270+ cert . add_extension ( ef . create_extension ( "basicConstraints" , "CA:TRUE" , true ) )
271+ cert . add_extension ( ef . create_extension ( "subjectAltName" , "DNS:localhost,IP:127.0.0.1" , false ) )
272+
273+ cert . sign ( key , OpenSSL ::Digest . new ( "SHA256" ) )
274+
275+ [ cert , key ]
276+ end
277+
278+ if ENV [ 'THRIFT_TLS' ]
279+ puts "Generating TLS certificate and key..."
280+ require 'openssl'
281+
282+ cert , key = generate_certificate
283+ File . write ( File . expand_path ( "cert.pem" , __dir__ ) , cert . to_pem )
284+ File . write ( File . expand_path ( "key.pem" , __dir__ ) , key . to_pem )
285+ end
286+
252287puts "Starting server..."
253288args = { }
254289args [ :interpreter ] = ENV [ 'THRIFT_SERVER_INTERPRETER' ] || ENV [ 'THRIFT_INTERPRETER' ] || "ruby"
255290args [ :class ] = resolve_const ( ENV [ 'THRIFT_SERVER' ] ) || Thrift ::NonblockingServer
256291args [ :host ] = ENV [ 'THRIFT_HOST' ] || HOST
257292args [ :port ] = ( ENV [ 'THRIFT_PORT' ] || PORT ) . to_i
293+ args [ :tls ] = ENV [ 'THRIFT_TLS' ] == 'true'
258294server = Server . new ( args )
259295server . start
260296
261297args = { }
262298args [ :host ] = ENV [ 'THRIFT_HOST' ] || HOST
263299args [ :port ] = ( ENV [ 'THRIFT_PORT' ] || PORT ) . to_i
300+ args [ :tls ] = ENV [ 'THRIFT_TLS' ] == 'true'
264301args [ :num_processes ] = ( ENV [ 'THRIFT_NUM_PROCESSES' ] || 40 ) . to_i
265302args [ :clients_per_process ] = ( ENV [ 'THRIFT_NUM_CLIENTS' ] || 5 ) . to_i
266303args [ :calls_per_client ] = ( ENV [ 'THRIFT_NUM_CALLS' ] || 50 ) . to_i
0 commit comments