lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.74k stars 976 forks source link

Faraday and socks proxy with auth supporting #787

Open yarafan opened 6 years ago

yarafan commented 6 years ago

Basic Info

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 commented 6 years ago

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

iMacTia commented 6 years ago

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

yarafan commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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

mathieujobin commented 5 years ago

I am looking for doing exactly this

AriefLuthfi79 commented 4 years ago

Good job @yarafan

gcolson commented 4 years ago

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 commented 4 years ago

@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