2012-01-06 30 views
7

Có ai biết cách chạy trình gỡ lỗi ruby ​​và SSL cùng lúc với Thin không?Mỏng với hỗ trợ SSL và gỡ lỗi ruby ​​

Tôi đã sử dụng Thin thành công với Rails 3.0.10.

Tôi bắt đầu sử dụng rails server --debugger và tôi có thể gỡ lỗi mã của mình.

Gần đây, tôi cũng cần thêm hỗ trợ SSL vào ứng dụng của mình và tôi muốn có thể thử nghiệm cục bộ bằng chứng chỉ tự ký.

Thật không may, tôi chưa tìm thấy cách khởi động Thin với hỗ trợ SSL khi sử dụng rails server.

tôi thành công có thể bắt đầu mỏng với sự hỗ trợ SSL bằng cách sử dụng:

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

Tuy nhiên, tôi đã không tìm thấy một cách để kích hoạt các chương trình gỡ rối bằng thin start.

Vì vậy, có vẻ như tôi có lựa chọn chạy trình gỡ lỗi (rails server) hoặc SSL (thin start), nhưng không phải cả hai.

Có vẻ như có thể để Webrick chạy SSL bằng cách sử dụng rails server bằng cách sửa đổi tệp đường ray/tập lệnh (see here). Tôi đã thử nghiệm với cách tiếp cận này, nhưng tôi đã không thành công. Dưới đây là một trong những nỗ lực:

#!/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' 

Lưu ý: đối với những người có thể tự hỏi, tôi đã tạo ra một thư mục 'ssllocal' tắt thư mục ứng dụng gốc của tôi, và đó là nơi tôi lưu trữ các phím ssl và Certs.

Trả lời

4

Bạn có thể thử chỉ yêu cầu trình gỡ lỗi trong môi trường phát triển của mình.

Trong Gemfile của bạn:

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

Và trong khối cấu hình của cấu hình của bạn/môi trường/development.rb:

require 'ruby-debug' 
Debugger.start 

này cho phép bạn đặt câu lệnh debugger bất cứ nơi nào trong mã của bạn.

3

Đây là giải pháp của tôi - Tôi đã tấn công Máy chủ TcpS mỏng để tải chứng chỉ SSL tự ký của mình, chỉ trong môi trường phát triển. My script/rails trông giống như sau:

#!/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' 
0

Tôi đã có thể thành công khi gỡ lỗi hoạt động với ssl được kích hoạt mỏng, sử dụng giải pháp được đề xuất bởi nathan. Mặc dù tôi đã phải làm một sự thay đổi nhỏ của trì hoãn khởi của @ssl sau khi cuộc gọi của initialize_without_ssl (một phương pháp bí danh cho initialize bản gốc TcpServer của)

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 

Trong mã snippett trên, @ssl được thiết lập là true sau khi gọi cuộc gọi khởi tạo ban đầu của Thin :: Backend :: TcpServer.Tôi phải làm điều này kể từ khi TcpServer gọi initialize mẹ của (Thin :: Backend: Base) mà đặt @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 

Như đã nêu trong khối mã nathan của, toàn bộ giải pháp dường như là một hack xung quanh . Theo tôi, tôi tốt với đoạn mã xem xét mã được thực hiện trong bối cảnh của env.development và quan trọng nhất là nó cho phép gỡ lỗi với ssl được kích hoạt.

0

Đây là cách tôi đã nhận nó để cuối cùng làm việc trên sản xuất sử dụng Thin:

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

Nếu bạn đang gặp vấn đề với tập tin KEY của bạn, hãy chắc chắn bạn xác CSR bằng cách sử dụng một trang web như:

https://ssl-tools.verisign.com 

Nếu CSR của bạn không thành công, khi đó chứng chỉ bạn nhận được từ cơ quan đăng ký của bạn cũng sẽ không thành công. Trang web của tôi sẽ từ chối tải bằng chứng chỉ SSL, chỉ để tìm ra rằng tôi viết tắt tên Tiểu bang của tôi thành "TX" thay vì "Texas" trong khi tạo khóa riêng của tôi. Đó là lý do nó không hoạt động cùng! SSL certs là một nỗi đau trong ass!

Các vấn đề liên quan