Faraday: Faraday and socks proxy with auth supporting

Created on 11 Apr 2018  ·  9Comments  ·  Source: lostisland/faraday

Basic Info

  • Faraday Version: 0.14, adapter Net::HTTP
  • Ruby Version: 2.3.1

    Issue description

Using socks proxy with auth leads to Faraday::ConnectionFailed: end of file reached

Steps to reproduce

Initialize Faraday

    Faraday.new do |connection|
      connection.proxy = "socks://user:pass@host:port"
      connection.adapter Faraday.default_adapter
    end

and make any request

feature v1.0

Most helpful comment

i use gem 'socksify'

require "socksify/http"

class Faraday::Adapter::NetHttp
  def net_http_connection(env)
    if (proxy = env[:request][:proxy])
      proxy_class(proxy)
    else
      Net::HTTP
    end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == "https" ? 443 : 80))
  end

  def proxy_class(proxy)
    if proxy.uri.scheme == "socks"
      TCPSocket.socks_username = proxy[:user] if proxy[:user]
      TCPSocket.socks_password = proxy[:password] if proxy[:password]
      Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port)
    else
      Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:uri].user, proxy[:uri].password)
    end
  end
end

All 9 comments

I was able to fix it by monkey patching Faraday::Adapter::NetHttp, but I am not sure that was the best decision

Hi @yarafan, thanks for reporting.
Can I ask you how you monkey-patched Faraday::Adapter::NetHttp?

i use gem 'socksify'

require "socksify/http"

class Faraday::Adapter::NetHttp
  def net_http_connection(env)
    if (proxy = env[:request][:proxy])
      proxy_class(proxy)
    else
      Net::HTTP
    end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == "https" ? 443 : 80))
  end

  def proxy_class(proxy)
    if proxy.uri.scheme == "socks"
      TCPSocket.socks_username = proxy[:user] if proxy[:user]
      TCPSocket.socks_password = proxy[:password] if proxy[:password]
      Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port)
    else
      Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:uri].user, proxy[:uri].password)
    end
  end
end

@yarafan I see, so basically Net::HTTP doesn't support socks proxy out-of-the-box.
In this case I wouldn't classify this as a bug, but more as a feature request to support socks proxies.
As any other feature request, this should be solved for all adapters in the current 0.x branch, however for the v1.0 branch we're moving all adapters out of faraday and keeping only Net::HTTP in.
That will be a good time to introduce this 👍

I'll update the title and label the Issue, if anyone needs to use socks proxies before then, they can use your monkey-patch

Add authentication support to Net::HTTP.SOCKSProxy
https://github.com/astro/socksify-ruby/pull/24/files

I am looking for doing exactly this

Good job @yarafan

Hi there. What's the status about this feature ? Is it still under development ?
I would be very much interested not to have to use a monkey patch to do this

@gcolson yes @technoweenie did most of the job already 🎉
Unfortunately, however, we're currently waiting for a dependent PR to be merged, see my comment: https://github.com/lostisland/faraday/pull/992#issuecomment-508437342

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JasonBarnabe picture JasonBarnabe  ·  4Comments

mokolabs picture mokolabs  ·  3Comments

Lewiscowles1986 picture Lewiscowles1986  ·  4Comments

mattmill30 picture mattmill30  ·  4Comments

t3hk0d3 picture t3hk0d3  ·  3Comments