迁移Sinatra Webrick RACK:基于SSLEnforcer的HTTPS到Thin

时间:2015-01-12 20:33:05

标签: ruby ssl https sinatra thin

我在开发环境中使用Rack :: SSLenforcer在Webrick和SSL上运行Sinatra很长一段时间没有任何问题(基于https://github.com/tobmatth/rack-ssl-enforcer#readme),我试图迁移到Thin以添加websockets支持但在使用Thin和SSL运行我的当前应用程序(没有websockets)时遇到问题。

我目前在websockets上的基本代码如下:

begin
  pkey = OpenSSL::PKey::RSA.new(File.open("private_key.pem").read)
  cert = OpenSSL::X509::Certificate.new(File.open("certificate.pem").read)
end

webrick_options = {
        :Port               => 8447,
        :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
        :DocumentRoot       => "/ruby/htdocs",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => cert,
        :SSLPrivateKey      => pkey,
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ],
        :app                  => MyWebRTCServer
}

Rack::Server.start webrick_options

然后在我的应用程序中,我有以下内容:

configure do
    # require SSL - https://github.com/tobmatth/rack-ssl-enforcer#readme
    use Rack::SslEnforcer
    set :session_secret, 'asdfa2342923422f1adc05c837fa234230e3594b93824b00e930ab0fb94b'

    use Rack::Session::Cookie, :key => '_rack_session',
                           :path => '/',
                           :expire_after => 2592000, # In seconds
                           :secret => session_secret

    # load password file - 
    begin
      @@config = YAML.load_file(File.join(Dir.pwd, 'config', 'users.yml'))
    rescue ArgumentError => e
      puts "Could not parse YAML: #{e.message}"
    end

    # puts "config: " + @@config.to_s
    use Rack::Auth::Basic, "Restricted Area" do |u, p|
      $LOG.info "Use Rack::Auth::Basic"

      if (!@@config[:users][u])
        puts "Bad username"
        false
      else
        # initialize the BCrypt with the password
        tPassword = BCrypt::Password.new(@@config[:users][u][:password].to_s)
        # puts "From BCrypt: " + tPassword
        if (tPassword == p)
          # puts "Validated password"
          # check whether the user is already logged in or not
          if (!@@user_table_cache[u.to_sym])
            # puts "User already logged in or session has not expired"
            userHash = Hash.new
            userHash[:name] = u
            userHash[:privilege] = @@config[:users][u][:privilege]

            # add the user hash to the cache
            @@user_table_cache[u.to_sym] = userHash
          end

        end
          true
        end
      end
    end

所有这些都适用于与Sinatra的webrick。我在Thin上尝试了以下内容(基于Can I enable SSL in Sinatra with Thin?

class MyApp < Sinatra::Base
  # ...
  get '/' do
    puts "got request"
  end

end

MyApp.run! do |server|

  ssl_options = {
    :cert_chain_file => './certificate.pem',
    :private_key_file => './private_key.pem',
    :verify_peer => false
  }
  server.ssl = true
  server.ssl_options = ssl_options
end

但是,当我尝试从浏览器访问它时,我收到以下错误。

C:\Software\Ruby Projects\Utils\sandbox\thintest>thistest
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Th
in
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
terminate called after throwing an instance of 'std::runtime_error'
  what():  Encryption not available on this event-machine

此应用程序已请求Runtime以不寻常的方式终止它。 请联系应用程序的支持团队以获取更多信息。

任何想法都会非常感激。

0 个答案:

没有答案