-
-
Save omarramos/d0f2c0d6a6c08b0ade2f01b0d39954b4 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment