Created
February 17, 2012 18:27
-
-
Save andrey-kazakov/1854726 to your computer and use it in GitHub Desktop.
HTTP over SOCKS support monkey patch for Mechanize, Faraday and it's based clients (OAuth2 like)
This file contains hidden or 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
# requires socksify gem | |
require "socksify" | |
require 'socksify/http' | |
# use w/ OAuth2 like OAuth2::Client.new(id, secret, connection_opts: { proxy: 'socks://127.0.0.1:9050' }) | |
class Faraday::Adapter::NetHttp | |
def net_http_class(env) | |
if proxy = env[:request][:proxy] | |
if proxy[:uri].scheme == 'socks' | |
Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port) | |
else | |
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password]) | |
end | |
else | |
Net::HTTP | |
end | |
end | |
end |
This file contains hidden or 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
# requires socksify gem | |
require "socksify" | |
require 'socksify/http' | |
# Mechanize: call @agent.set_socks(addr, port) before using | |
# any of it's methods; it might be working in other cases, | |
# but I just didn't tried :) | |
class Mechanize::HTTP::Agent | |
public | |
def set_socks addr, port | |
set_http unless @http | |
class << @http | |
attr_accessor :socks_addr, :socks_port | |
def http_class | |
Net::HTTP.SOCKSProxy(socks_addr, socks_port) | |
end | |
end | |
@http.socks_addr = addr | |
@http.socks_port = port | |
end | |
end |
Hi, I'm new to Rails, I used your script for mechanize gem, but it gives no method error 'set_socks', can you please guide me where to put this file. I'm using rails 4.2.0 and ruby 2.1.2. Also I'm using version 2.7.3 of mechanize and the scrapping code is written in worker class for background processing.
I posted this gist quite long ago, and it probably won't work with modern versions of Faraday or Mechanize.
anyway, ensure that you're calling set_socks
method of right class (agent.class.name should be equal to "Mechanize::HTTP::Agent" because I monkeypatch exactly this class by adding set_socks
method).
here's an example from one of my gist's forks:
agent = Mechanize.new
agent.agent.set_socks('localhost', 9050) #Use Tor as proxy
@kuroineko, thanks for this update
@brauliobo did it work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks :)