OpenSSL在Ruby 1.9.3中出现问题

10

我在Ubuntu 12.04上使用OpenSSL 1.0.1和Ruby 1.9.3遇到了半严重的问题。

所有的Rubies都是通过rvm安装的。

require 'uri'
require 'net/http'
require 'net/https'

endpoint = "https://secure.mmoagateway.com/api/transact.php"
RUBY_184_POST_HEADERS = { "Content-Type" => "application/x-www-form-urlencoded" }
body = "orderid=ae5dd847d9f31209cbffeeea076ed966&orderdescription=Active+Merchant+Remote+Test+Purchase&ccnumber=4111111111111111&ccexp=0913&cvv=123&company=Widgets+Inc&address1=1234+My+Street&address2=Apt+1&city=Ottawa&state=ON&zip=K1C2N6&country=CA&phone=%28555%29555-5555&firstname=&lastname=&email=&amount=1.00&type=auth&username=demo&password=password"
headers = {}

endpoint     = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)

http = Net::HTTP.new(endpoint.host, endpoint.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.set_debug_output(STDOUT)

result = http.post(endpoint.request_uri, body, RUBY_184_POST_HEADERS.merge(headers))
puts(result)

在Ubuntu 12.04 + Ruby 1.9.3 + Openss 1.0.1上,我得到以下输出:

% ruby test.rb 
opening connection to secure.mmoagateway.com...
opened
Conn close because of connect error Connection reset by peer - SSL_connect
/usr/lib/ruby/1.9.1/net/http.rb:799:in `connect': Connection reset by peer - SSL_connect (Errno::ECONNRESET)
        from /usr/lib/ruby/1.9.1/net/http.rb:799:in `block in connect'
        from /usr/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
        from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
        from /usr/lib/ruby/1.9.1/net/http.rb:799:in `connect'
        from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
        from /usr/lib/ruby/1.9.1/net/http.rb:744:in `start'
        from /usr/lib/ruby/1.9.1/net/http.rb:1284:in `request'
        from /usr/lib/ruby/1.9.1/net/http.rb:1307:in `send_entity'
        from /usr/lib/ruby/1.9.1/net/http.rb:1096:in `post'
        from test.rb:17:in `<main>'

在 Ruby 1.8.7 中,我得到了正确的输出:

$ ruby test.rb
opening connection to secure.mmoagateway.com...
opened
<- "POST /api/transact.php HTTP/1.1\r\nAccept: */*\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nContent-Length: 347\r\nHost: secure.mmoagateway.com\r\n\r\n"
<- "orderid=ae5dd847d9f31209cbffeeea076ed966&orderdescription=Active+Merchant+Remote+Test+Purchase&ccnumber=4111111111111111&ccexp=0913&cvv=123&company=Widgets+Inc&address1=1234+My+Street&address2=Apt+1&city=Ottawa&state=ON&zip=K1C2N6&country=CA&phone=%28555%29555-5555&firstname=&lastname=&email=&amount=1.00&type=auth&username=demo&password=password"
-> "HTTP/1.1 200 OK\r\n"
-> "Date: Wed, 04 Jul 2012 01:26:35 GMT\r\n"
-> "Server: Apache\r\n"
-> "Content-Length: 240\r\n"
-> "Connection: close\r\n"
-> "Content-Type: text/html\r\n"
-> "\r\n"
reading 240 bytes...
-> "response=1&responsetext=SUCCESS&authcode=123456&transactionid=1648894346&avsresponse=N&cvvresponse=N&orderid=ae5dd847d9f31209cbffeeea076ed966&type=auth&response_code=100&merchant_defined_field_6=&merchant_defined_field_7=&customer_vault_id="
read 240 bytes
Conn close
#<Net::HTTPOK:0xb74175c8>
response=1&responsetext=SUCCESS&authcode=123456&transactionid=1648894346&avsresponse=N&cvvresponse=N&orderid=ae5dd847d9f31209cbffeeea076ed966&type=auth&response_code=100&merchant_defined_field_6=&merchant_defined_field_7=&customer_vault_id=

我在 arch 系统中使用 1.9.3 和 1.0.1 遇到了同样的问题。

如果我从 Oneiric 安装 1.0.0e 到我的 12.04 系统中,也可与 ruby 1.9.3 很好地配合使用。

我认为这可能与 ubuntu bug 有关,链接如下: https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/965371

虽然我已经下载了 Debian 的软件包,他们说它已经修复了该问题,但是运行时并无改善。

是否有其他人遇到过类似的问题?


我没有解决方案,只有一些数据点:问题在我的机器上复现(Debian测试版,Ruby 1.8.7或1.9.3,openssl 1.0.1b-1)。如果我尝试连接到不同的https主机,则问题不会再现。哦,而且我不确定那个错误报告中的症状是否与您的症状非常匹配。 - Wayne Conrad
2个回答

16

我曾经遇到连接授权网关的问题。最终我通过强制使用 SSLv3 连接成功了。

http = Net::HTTP.new(uri.host, uri.port)

http.use_ssl = true if @is_https
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @is_https
http.ssl_version = :SSLv3

我有一段时间没有查看这篇文章了。是的!这对我有效。似乎它应该能够自己协商,但这将解决我的问题。 - Jeffery Utter
这里有一个解决方案,可以修复此问题,而不必禁用证书验证。 - Pavel Nikolov

-1
我也遇到了同样的问题...在这里有一些信息,说rvm pkg install opensslrvm reinstall 1.9.3-p194 --with-openssl-dir=~/.rvm/usr可以解决这个问题,但对我没有帮助。

是的,在我进行了一次大版本的OSX升级以及与brew/macports/...相关的问题之后,这才对我起作用。在这种情况下,重新安装Ruby可能会有所帮助。也许他在编译Ruby 1.9之后更新了OpenSSL。 - Marc Seeger
仍然对我无效。我在基本的Linux系统上安装了最新的1.0.1版本,同时rvm openssl也完全相同,我尝试了所有可能的解决方案,但对我来说都没有解决...有什么想法吗? - CLod
1
“--with-openssl-dir = ...”不是1.9.3的有效配置选项:“configure:WARNING:unrecognized options: --with-openssl-dir” - user246672

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接