Search code examples
ruby-on-railsrubyfaraday

Faraday Proxy - Connection Failed - End of File Reached


I am working with OAuth2 gem and am trying to configure it to make calls through a Proxy. I am encountering this error Faraday::ConnectionFailed: end of file reached and am not sure what I am doing wrong. I thought it was the certificate but I managed to get Curl::Easy to make a connection and post data.

I highly appreciate any effort you make to help me resolve this issue. Thanks in advance.

The Faraday Example

I used to rake tasks to play around with Faraday.

namespace :vgs do
  task faraday: :environment do
    Faraday.default_adapter = :net_http
    ssl_cert_path = VGS.configuration.cert_path # "/path/to/sandbox.pem"

    options = {
      headers: { 'Content-Type' => 'application/json' },
      ssl: { ca_file: ssl_cert_path },
      proxy: VGS.proxy_uri, # => "https://<USERNAME>:<PASSWORD>@<VAULT>.sandbox.verygoodproxy.com:8443"
    }

    pp options

    conn = Faraday.new(
      'https://eoftqqzi7ii7mmn.m.pipedream.net',
      options
    )

    data = {
      card_number: 'tok_sandbox_sS27yztTZjuznHD1hmtgCS',
      card_cvc: 'tok_sandbox_241N1r8hxoYDyqrUSmZ4ih',
      card_expiration_date: '2212',
      transaction_amount: 84.0
    }

    response = conn.post('/') do |req|
      req.body = data.to_json
    end

    pp response
    pp response.body
  end
end

Running this task results the following error:

Faraday::ConnectionFailed: end of file reached
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:146:in `request_via_request_method'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:131:in `request_with_wrapped_block'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:122:in `perform_request'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:66:in `block in call'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-1.10.0/lib/faraday/adapter.rb:50:in `connection'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:64:in `call'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-1.10.0/lib/faraday/request/url_encoded.rb:25:in `call'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/faraday-1.10.0/lib/faraday/connection.rb:281:in `post'
/Volumes/Dev/Work/Edge/edge/lib/tasks/vgs.rake:60:in `block (2 levels) in <main>'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
/Users/jdeen/.rvm/gems/ruby-3.1.1/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:48:in `load'
<internal:/Users/jdeen/.rvm/rubies/ruby-3.1.1/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/Users/jdeen/.rvm/rubies/ruby-3.1.1/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
-e:1:in `<main>'

I would appreciate if you could point what I might be doing wrong.

Extra

The Proxy documentation: https://www.verygoodsecurity.com/docs/guides/outbound-connection

This is the Curl::Easy that worked. I used Curl::Easy variation to make sure it wasn't perhaps the cert.

namespace :vgs do
  task curl: :environment do
    proxy = VGS.proxy_uri
    uri = 'https://eoftqqzi7ii7mmn.m.pipedream.net'

    options = {
      card_number: 'tok_sandbox_sS27yztTZjuznHD1hmtgCS',
      card_cvc: 'tok_sandbox_241N1r8hxoYDyqrUSmZ4ih',
      card_expiration_date: '2212',
      transaction_amount: 84.0
    }

    c = Curl::Easy.new(uri) do |http|
      http.headers['Content-Type'] = 'application/json'
      http.cacert = VGS.configuration.cert_path # "/path/to/sandbox.pem"
      http.proxy_url = proxy
      http.follow_location = true
      http.ssl_verify_peer = true
      http.post_body = options.to_json
      http.post
    end

    puts "Response #{c.status}: #{c.body}"
  end
end

Solution

  • I had the same issue with vgs, this helped to solve this:

    require 'faraday'
    require 'typhoeus'
    require 'typhoeus/adapters/faraday'
    
    conn = Faraday.new do |faraday|
      faraday.adapter :typhoeus, http_version: :httpv2_0
    end
    

    enforcing Faraday adapter :typhoeus to use HTTP/2 for requests

    Also I had issue with making the request on my M1 mac, solved with exporting the ENV variable in my ~/.zshrc file (project's .env did not work for me)

    export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
    

    https://github.com/rails/rails/issues/38560