Skip to content

Commit

Permalink
support socksify api introduced in astro/socksify-ruby#33
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 13, 2019
1 parent 347e799 commit c470fca
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,17 @@ def with_net_http_connection(env)
end

def net_http_connection(env)
proxy = env[:request][:proxy]
klass = if proxy.nil?
Net::HTTP
elsif proxy.uri.scheme != 'socks'
http_proxy(proxy)
else
socks_proxy(proxy)
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,
Expand All @@ -168,14 +167,12 @@ def http_proxy(proxy)
end

def socks_proxy(proxy)
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)
rescue NoMethodError => err
if err.to_s =~ /socks/i
if !Net::HTTP.respond_to?(:SOCKSProxy)
raise "SOCKS proxy support requires the socksify gem ~> 1.7.1"
end
raise

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

def configure_ssl(http, ssl)
Expand Down

0 comments on commit c470fca

Please sign in to comment.