Created
May 5, 2016 10:27
-
-
Save snipsnipsnip/dc36846892bcd4f749c0b64395a10948 to your computer and use it in GitHub Desktop.
dummy_ssl_server.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'openssl' | |
def wrap(server) | |
key = OpenSSL::PKey::RSA.generate(2048) | |
cert = OpenSSL::X509::Certificate.new | |
cert.issuer = cert.subject = OpenSSL::X509::Name.new([['CN', 'localhost']]) | |
cert.public_key = key.public_key | |
cert.not_before = Time.now | |
cert.not_after = cert.not_before + 60 * 60 | |
cert.serial = cert.not_before.to_i | |
cert.sign(key, "sha256") | |
context = OpenSSL::SSL::SSLContext.new('TLSv1_server') | |
context.set_params({ | |
:verify_mode => OpenSSL::SSL::VERIFY_NONE, | |
:key => key, | |
:cert => cert, | |
}) | |
OpenSSL::SSL::SSLServer.new(gs, context) | |
end | |
TCPServer.open(ARGV[0].to_i) do |gs| | |
puts %_server is on #{gs.addr.join(',')}_ | |
gs = wrap(gs) | |
loop do | |
begin | |
while s = gs.accept | |
addr = s.addr.join(',') | |
puts %_#{addr} connected #{"with #{s.cipher.join(',')}" if defined?(s.cipher)}_ | |
s.each_line {|line| puts "#{addr}: #{line}" } | |
puts %_#{addr} disconnected_ | |
end | |
rescue SystemCallError, OpenSSL::SSL::SSLError => e | |
puts "#{e.class}: #{e}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment