【问题标题】:Thin with SSL support and ruby-debug带有 SSL 支持和 ruby​​-debug 的瘦身
【发布时间】:2012-01-06 04:57:48
【问题描述】:

有人知道使用 Thin 同时运行 ruby​​ 调试器和 SSL 的方法吗?

我已经在 Rails 3.0.10 中成功使用 Thin。

我使用rails server --debugger 启动它,我可以调试我的代码。

最近,我还需要为我的应用程序添加 SSL 支持,我希望能够使用自签名证书在本地对其进行测试。

不幸的是,在使用 rails server 时,我还没有找到一种方法来启动 Thin 并支持 SSL。

我可以使用以下方法成功启动具有 SSL 支持的 Thin:

thin start --ssl --ssl-verify --ssl-key-file ssllocal/server.key
    --ssl-cert-file ssllocal/server.crt

但是,我还没有找到使用thin start 激活调试器的方法。

看来我可以选择运行调试器 (rails server) 或 SSL (thin start),但不能同时运行。

似乎可以通过修改 rails/script 文件 (see here) 让 Webrick 使用 rails server 运行 SSL。我尝试了这种方法,但没有成功。这是其中一种尝试:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3
# gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)


# THIS IS NEW:
require "rails/commands/server"
require 'rack'
require 'thin'
module Rails
  class Server
    def default_options
      super.merge({
        :Port        => 3000,
        :environment => (ENV['RAILS_ENV'] || "development").dup,
        :daemonize   => false,
        :debugger    => false,
        :pid         => File.expand_path("tmp/pids/server.pid"),
        :config      => File.expand_path("config.ru"),
        :SSLEnable   => true
        :ssl => true,
        "ssl-verify" => true,
        "ssl-key-file" => File.expand_path("ssllocal/server.key"),
        "ssl-cert-file" => File.expand_path("ssllocal/server.crt")       
      })
    end
  end
end


require 'rails/commands'

注意:对于那些可能想知道的人,我在根应用程序目录之外创建了一个“ssllocal”目录,这是我存储 ssl 密钥和证书的地方。

【问题讨论】:

    标签: ruby-on-rails ssl thin ruby-debug


    【解决方案1】:

    您可以尝试在自己的开发环境中只需要调试器。

    在您的 Gemfile 中:

    if RUBY_VERSION =~ /^1.9/
      gem "ruby-debug19", :group => :development
    else
      gem "ruby-debug", :group => :development
    end
    

    在 config/environments/development.rb 的配置块中:

    require 'ruby-debug'
    Debugger.start
    

    这允许您将调试器语句放置在代码中的任何位置。

    【讨论】:

      【解决方案2】:

      这是我的解决方案 - 我破解了 Thin TcpServer 以加载我的自签名 SSL 证书,但仅在开发环境中。我的script/rails 看起来像这样:

      #!/usr/bin/env ruby
      # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
      
      APP_PATH = File.expand_path('../../config/application',  __FILE__)
      require File.expand_path('../../config/boot',  __FILE__)
      
      # Hack our SSL certs into Thin TcpServer, only in development environment
      require 'thin'
      module Thin
        module Backends
          TcpServer.class_eval do
            def initialize_with_SSL(host, port)
              if Rails.env.development?
                Rails.logger.info "Loading SSL certs from ./ssl_dev..."
                @ssl = true
                @ssl_options = {
                  :private_key_file => File.expand_path("../../ssl_dev/server.key", __FILE__),
                  :cert_chain_file  => File.expand_path("../../ssl_dev/server.crt", __FILE__),
                  :verify_peer => nil
                }
              end
      
              initialize_without_SSL(host, port)
            end
      
            alias_method :initialize_without_SSL, :initialize
            alias_method :initialize, :initialize_with_SSL      
          end
        end
      end
      
      # Must load 'rails/commands' after Thin SSL hack
      require 'rails/commands'
      

      【讨论】:

        【解决方案3】:

        这是我最终使用 Thin 进行生产的方法:

        rvmsudo thin start -p 443 --ssl --ssl-key-file ssl/server.key --ssl-cert-file ssl/server.crt
        

        如果您的 KEY 文件有问题,请确保使用以下网站验证 CSR:

        https://ssl-tools.verisign.com
        

        如果您的 CSR 失败,那么您从签名机构收到的证书也将失败。 我的网站会拒绝加载 SSL 证书,只是发现我在创建私钥时将州名缩写为“TX”而不是“Texas”。这就是它一直不起作用的原因! SSL 证书很麻烦!

        【讨论】:

          【解决方案4】:

          使用 nathan 建议的解决方案,我能够成功地在启用 ssl 的情况下进行调试。尽管在调用 initialize_without_ssl(原始 TcpServer 的初始化的别名方法)之后,我不得不对 @ssl 的初始化进行一个小的更改

          require 'thin'
          module Thin
            module Backends
              TcpServer.class_eval do
                def initialize_with_SSL(host, port)
                  if Rails.env.development?
                    Rails.logger.info "Loading SSL certs from ./ssl_dev..."
                    @ssl_options = {
                      :private_key_file => File.expand_path("../../ssl_dev/server.key", __FILE__),
                      :cert_chain_file  => File.expand_path("../../ssl_dev/server.crt", __FILE__),
                      :verify_peer => nil
                    }
                  end
          
                  initialize_without_SSL(host, port)
                  # @ssl initialized after calling the original initialize of TcpServer
                  @ssl = true if Rails.env.development? 
          
                end
          
                alias_method :initialize_without_SSL, :initialize
                alias_method :initialize, :initialize_with_SSL      
              end
            end
          end
          
            alias_method :initialize_without_SSL, :initialize
            alias_method :initialize, :initialize_with_SSL      
          end
          

          在上面的代码sn-pt中,@ssl在调用Thin::Backend::TcpServer的原始初始化调用后被设置为true。我必须这样做,因为 TcpServer 调用其父级的初始化 (Thin::Backend:Base) 将 @ssl 设置为 nil

            #Base initialize method. Thin gem version 1.5.0
            def initialize
              @connections                    = []
              @timeout                        = Server::DEFAULT_TIMEOUT
              @persistent_connection_count    = 0
              @maximum_connections            = Server::DEFAULT_MAXIMUM_CONNECTIONS
              @maximum_persistent_connections = Server::DEFAULT_MAXIMUM_PERSISTENT_CONNECTIONS
              @no_epoll                       = false
              @ssl                            = nil
              @threaded                       = nil
            end
          

          正如 nathan 的代码块中所指出的,整个解决方案似乎是一个 hack。在我看来,我对 sn-p 很好,考虑到代码是在 env.development 的上下文中完成的,最重要的是它允许在启用 ssl 的情况下进行调试。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-16
            • 2013-09-24
            • 2014-05-10
            • 1970-01-01
            • 2015-10-23
            • 1970-01-01
            • 2011-06-05
            相关资源
            最近更新 更多