Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday and socks proxy with auth supporting #787

Open
yarafan opened this issue Apr 11, 2018 · 9 comments · May be fixed by #992
Open

Faraday and socks proxy with auth supporting #787

yarafan opened this issue Apr 11, 2018 · 9 comments · May be fixed by #992

Comments

@yarafan
Copy link
Contributor

yarafan commented Apr 11, 2018

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

@yarafan
Copy link
Contributor Author

yarafan commented Apr 11, 2018

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

@iMacTia
Copy link
Member

iMacTia commented Apr 11, 2018

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

@yarafan
Copy link
Contributor Author

yarafan commented Apr 12, 2018

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

@iMacTia
Copy link
Member

iMacTia commented Apr 12, 2018

@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

@oshlykov
Copy link

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

@mathieujobin
Copy link

I am looking for doing exactly this

@AriefLuthfi79
Copy link

Good job @yarafan

@gcolson
Copy link

gcolson commented Jul 10, 2020

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

@iMacTia
Copy link
Member

iMacTia commented Jul 10, 2020

@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: #992 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants