7

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

我在 Rails 3.0.10 中成功使用了 Thin。

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

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

不幸的是,在使用rails server.

我可以使用以下方法成功启动具有 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 server通过修改 rails/script 文件来让 Webrick 运行 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 密钥和证书的地方。

4

4 回答 4

5

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

在您的 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

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

于 2012-01-23T14:12:38.733 回答
3

这是我的解决方案——我破解了 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'
于 2012-07-26T00:57:58.157 回答
1

以下是我最终使用 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 证书很麻烦!

于 2013-08-06T18:02:59.397 回答
0

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

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

在上面的代码片段中,@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 的代码块中所指出的,整个解决方案似乎是一种破解。在我看来,考虑到代码是在 env.development 的上下文中完成的,并且最重要的是它允许在启用 ssl 的情况下进行调试,我对代码片段感到满意。

于 2013-05-04T18:53:56.397 回答