Last active
May 7, 2021 20:52
-
-
Save shannonwells/a7be8752093cd74bc590 to your computer and use it in GitHub Desktop.
Faraday + SOCKS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'faraday' | |
require 'socksify' | |
require 'socksify/http' | |
require 'awesome_print' | |
# This is a SOCKS monkey patch for Faraday, with example use/unit test. | |
# Notes: | |
# * It is altered to work with SOCKS5 authentication. | |
# * net_http_class must return a Faraday::Adapter::NetHttp instance. | |
# * Verified as of 2020-02-24 with: | |
# - SOCKS5 ss5 server | |
# - gem faraday, '1.0' | |
# - gem socksify,'1.7.1', git: 'git://github.com/pirateradio/socksify-ruby.git' | |
# - ruby 2.6 | |
Socksify::debug = true | |
class Faraday::Adapter::NetHttp | |
def net_http_connection(env) | |
if proxy = env[:request][:proxy] | |
ap here: "PROXY" | |
if proxy.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 | |
else | |
Net::HTTP | |
end.new(env[:url].host, env[:url].port) | |
end | |
end | |
# Replace with your server values, obviously | |
uri = URI.parse('socks://186.225.157.22:8080') | |
# DO NOT DO THIS IN PRODUCTION, this is just for testing | |
SSL_OPTS = {verify: false} | |
connection = Faraday.new(url: "https://www.google.com", | |
ssl: SSL_OPTS, | |
proxy: uri) do |c| | |
c.headers[:user_agent] = "Just Some Engineer" | |
c.adapter :net_http | |
c.response :logger | |
end | |
response = connection.get | |
ap body: response.body |
Thanks it's useful 👍
Don't set SSL_VERIFY=false except as a test! This leaves you vulnerable to Man-in-the-middle attacks.
Now at faraday1.7 version, proxy[:socks] will throw an error, instead, use this code:
proxy.scheme == 'sock5'
Now at faraday1.7 version, proxy[:socks] will throw an error, instead, use this code:
proxy.scheme == 'sock5'
NB:
faraday is at 1.0, it's socksify that's 1.7. I have updated and verified the gist. That said, there is an approved PR in Faraday to support socks [Edit May 2021, which is still blocked due to a linter error??]:
lostisland/faraday#992
if proxy.scheme == 'socks5' || proxy.scheme == 'socks4'
works due https://github.com/pirateradio/socksify-ruby/blob/master/lib/socksify.rb#L97
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output should be something like the following: