How to Set a Proxy in Rubys Net/Http

How to set a proxy in rubys net/http?

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|

This will create an http object for you to use in the block. Use that rather than generating new ones each time, here Net::HTTP.get('google.de', '')

proxy_addr = 'proxy'
proxy_port = 8080

Net::HTTP.new('google.de', nil, proxy_addr, proxy_port).start { |http|
# always proxy via your.proxy.addr:8080
http.get('google.de', '')
}

Ruby, Tor and Net::HTTP::Proxy

You are using HTTP proxy class, so you must provide IP of HTTP proxy. Tor Browser has not HTTP proxy bundled.

So you can either install some proxy software e.g. Privoxy and configure it to use Tor's SOCKS:

In config.txt
forward-socks4a / 127.0.0.1:9050 .

then use Privoxy's default listen-address in your script:

proxy = Net::HTTP::Proxy('127.0.0.1',8118)

or use SOCKSify.
According to docs:

require 'socksify/http'
uri = URI.parse('http://rubyforge.org/')
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
http.get(uri.path)
end

No need for additional software..

Third solution is to use SOCKSify as follows:

$ socksify_ruby localhost 9050 script.rb

which redirect all TCP connections of a Ruby script, which means you don't need to use any Proxy code at all.

For clarification you have to understand that 127.0.0.1:9050 is Tor's SOCKS address and 127.0.0.1:8118 is address of Privoxy.

Using Ruby with Charles Proxy via export http_proxy on Mac

The http_proxy environment variable should include the protocol as well. Curl seems to be lazy in that sense.

Try setting the proxy via

export http_proxy="http://localhost:8888"

HTTP2 requests through PROXY, ruby

After all, I finished my idea with an example present in net/http standard library and created a pull request for Net-http2 gem: https://github.com/ostinelli/net-http2/pull/11

The idea was correct, all we have to do is to send proxy "CONNECT" message using any tcp socket, with an address where we want to connect, so it creates a TCP tunnel which bypass all the data in and out, doesn't matter if it is HTTP1.1 or HTTP2, or anything else.

Here is a part of code:

def self.proxy_tcp_socket(uri, options)
proxy_addr = options[:proxy_addr]
proxy_port = options[:proxy_port]
proxy_user = options[:proxy_user]
proxy_pass = options[:proxy_pass]

proxy_uri = URI.parse("#{proxy_addr}:#{proxy_port}")
# create a regular TCP socket (with or w/o SSL, if needed)
proxy_socket = tcp_socket(proxy_uri, options)

# The majority of proxies do not explicitly support HTTP/2 protocol,
# while they successfully create a TCP tunnel
# which can pass through binary data of HTTP/2 connection.
# So we’ll keep HTTP/1.1
http_version = '1.1'

buf = "CONNECT #{uri.host}:#{uri.port} HTTP/#{http_version}\r\n"
buf << "Host: #{uri.host}:#{uri.port}\r\n"
if proxy_user
credential = ["#{proxy_user}:#{proxy_pass}"].pack('m')
credential.delete!("\r\n")
buf << "Proxy-Authorization: Basic #{credential}\r\n"
end
buf << "\r\n"
proxy_socket.write(buf)
validate_proxy_response!(proxy_socket)

proxy_socket
end


Related Topics



Leave a reply



Submit