Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically adding proxy to all HTTP connections in ruby

Tags:

http

ruby

proxy

I have an application that initiates multiple HTTP connections and I would like to add a proxy to all connections.

The application is using net/HTTP, TCP sockets and open-uri so ideally I would like to be able to patch all connections initiated from those libraries instead of adding it manually to each and every location in the code that initiates a connection.

Is there a way to accomplish that (on Ruby 1.9.2)?

like image 890
Ori Avatar asked Jul 29 '11 04:07

Ori


2 Answers

Open URI uses the HTTP_PROXY environment variable

Here is an article on how to use it on both windows and unix variants.

http://kaamka.blogspot.com/2009/06/httpproxy-environment-variable.html

you can also set it directly in ruby using the ENV hash

ENV['HTTP_PROXY'] = 'http://username:password@hostname:port'

the net/http documentation says not to rely on the environment and set it each time

require 'net/http'
require 'uri'

proxy_host = 'your.proxy.host'
proxy_port = 8080
uri = URI.parse(ENV['http_proxy'])
proxy_user, proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
Net::HTTP::Proxy(proxy_host, proxy_port,
                 proxy_user, proxy_pass).start('www.example.com') {|http|
  # always connect to your.proxy.addr:8080 using specified username and password
        :
}

from http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

like image 80
Richard Blackman Avatar answered Oct 21 '22 18:10

Richard Blackman


Yes and mechanize does too (this is for the 1.0.0 verison)

require 'mechanize'
url = 'http://www.example.com'

agent = Mechanize.new
agent.user_agent_alias = 'Mac Safari'
agent.set_proxy('127.0.0.1', '3128')
@page = agent.get(:url => url)
like image 42
Niels Kristian Avatar answered Oct 21 '22 17:10

Niels Kristian