2004-05-26 14:14:27 -04:00
|
|
|
require "socket"
|
|
|
|
require "thread"
|
|
|
|
require "openssl"
|
|
|
|
require File.join(File.dirname(__FILE__), "utils.rb")
|
|
|
|
|
|
|
|
def get_pem(io=$stdin)
|
|
|
|
buf = ""
|
|
|
|
while line = io.gets
|
|
|
|
if /^-----BEGIN / =~ line
|
|
|
|
buf << line
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
while line = io.gets
|
|
|
|
buf << line
|
|
|
|
if /^-----END / =~ line
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_key(pem)
|
|
|
|
begin
|
|
|
|
return OpenSSL::PKey::RSA.new(pem)
|
|
|
|
rescue
|
|
|
|
return OpenSSL::PKey::DSA.new(pem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ca_cert = OpenSSL::X509::Certificate.new(get_pem)
|
|
|
|
ssl_cert = OpenSSL::X509::Certificate.new(get_pem)
|
|
|
|
ssl_key = make_key(get_pem)
|
|
|
|
port = Integer(ARGV.shift)
|
|
|
|
verify_mode = Integer(ARGV.shift)
|
|
|
|
start_immediately = (/yes/ =~ ARGV.shift)
|
|
|
|
|
|
|
|
store = OpenSSL::X509::Store.new
|
|
|
|
store.add_cert(ca_cert)
|
|
|
|
store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.cert_store = store
|
|
|
|
#ctx.extra_chain_cert = [ ca_cert ]
|
|
|
|
ctx.cert = ssl_cert
|
|
|
|
ctx.key = ssl_key
|
|
|
|
ctx.verify_mode = verify_mode
|
|
|
|
|
|
|
|
Socket.do_not_reverse_lookup = true
|
2004-11-17 03:04:19 -05:00
|
|
|
tcps = nil
|
|
|
|
100.times{|i|
|
|
|
|
begin
|
|
|
|
tcps = TCPServer.new("0.0.0.0", port+i)
|
|
|
|
port = port + i
|
|
|
|
break
|
|
|
|
rescue Errno::EADDRINUSE
|
2009-03-05 22:56:38 -05:00
|
|
|
next
|
2004-11-17 03:04:19 -05:00
|
|
|
end
|
|
|
|
}
|
2004-05-26 14:14:27 -04:00
|
|
|
ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
|
|
|
|
ssls.start_immediately = start_immediately
|
|
|
|
|
|
|
|
$stdout.sync = true
|
|
|
|
$stdout.puts Process.pid
|
2004-11-17 03:04:19 -05:00
|
|
|
$stdout.puts port
|
2004-05-26 14:14:27 -04:00
|
|
|
|
|
|
|
loop do
|
2005-03-09 05:45:42 -05:00
|
|
|
ssl = ssls.accept rescue next
|
|
|
|
Thread.start{
|
2004-05-26 14:14:27 -04:00
|
|
|
q = Queue.new
|
2004-06-20 11:57:40 -04:00
|
|
|
th = Thread.start{ ssl.write(q.shift) while true }
|
|
|
|
while line = ssl.gets
|
|
|
|
if line =~ /^STARTTLS$/
|
|
|
|
ssl.accept
|
|
|
|
next
|
|
|
|
end
|
2004-05-26 14:14:27 -04:00
|
|
|
q.push(line)
|
|
|
|
end
|
2004-06-20 11:57:40 -04:00
|
|
|
th.kill if q.empty?
|
|
|
|
ssl.close
|
2004-05-26 14:14:27 -04:00
|
|
|
}
|
|
|
|
end
|