Skip to content

Socks proxy support for net/http #992

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
'Make sure openssl is installed if you want ssl support'
require 'net/http'
end

begin
require 'socksify/http'
rescue LoadError # rubocop:disable Lint/HandleExceptions
# socksify is optional. don't @ me
end

require 'zlib'

module Faraday
Expand Down Expand Up @@ -139,16 +146,37 @@ def with_net_http_connection(env)
end

def net_http_connection(env)
klass = if (proxy = env[:request][:proxy])
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
proxy[:user], proxy[:password])
else
Net::HTTP
end
klass = proxy_class(env[:request][:proxy])
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
klass.new(env[:url].hostname, port)
end

def proxy_class(proxy)
return Net::HTTP if proxy.nil?

return http_proxy(proxy) unless proxy.uri.scheme == 'socks'

socks_proxy(proxy)
end

def http_proxy(proxy)
Net::HTTP::Proxy(
proxy[:uri].host,
proxy[:uri].port,
proxy[:uri].user,
proxy[:uri].password
)
end

def socks_proxy(proxy)
unless Net::HTTP.respond_to?(:SOCKSProxy)
raise 'SOCKS proxy support requires the socksify gem ~> 1.7.1'
end

Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port,
proxy[:user], proxy[:password])
end

def configure_ssl(http, ssl)
http.use_ssl = true
http.verify_mode = ssl_verify_mode(ssl)
Expand Down