Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net::HTTP::SOCKSProxy(host, port) support #5

Merged
3 commits merged into from Feb 22, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/socksify.rb
Expand Up @@ -17,7 +17,7 @@

require 'socket'
require 'resolv'
require 'socksify_debug'
require 'socksify/debug'

class SOCKSError < RuntimeError
def initialize(msg)
Expand Down Expand Up @@ -118,13 +118,33 @@ def self.socks_ignores=(ignores)
@@socks_ignores = ignores
end

class SOCKSConnectionPeerAddress
attr_reader :socks_server, :socks_port, :peer_host
def initialize(socks_server, socks_port, peer_host)
@socks_server, @socks_port, @peer_host = socks_server, socks_port, peer_host
end

def to_s
"#{@peer_host} (via #{@socks_server}:#{@socks_port}"
end
alias_method :to_str, :to_s
end

alias :initialize_tcp :initialize

# See http://tools.ietf.org/html/rfc1928
def initialize(host=nil, port=0, local_host="0.0.0.0", local_port=0)
socks_server = self.class.socks_server
socks_port = self.class.socks_port
socks_ignores = self.class.socks_ignores
if host.is_a?(SOCKSConnectionPeerAddress)
socks_peer = host
socks_server = socks_peer.socks_server
socks_port = socks_peer.socks_port
socks_ignores = []
host = socks_peer.peer_host
else
socks_server = self.class.socks_server
socks_port = self.class.socks_port
socks_ignores = self.class.socks_ignores
end

if socks_server and socks_port and not socks_ignores.include?(host)
Socksify::debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}"
Expand Down
File renamed without changes.
55 changes: 55 additions & 0 deletions lib/socksify/http.rb
@@ -0,0 +1,55 @@
=begin
Copyright (C) 2007 Stephan Maka <stephan@spaceboyz.net>
Copyright (C) 2011 Musy Bite <musybite@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end

require 'socksify'
require 'net/http'

module Net
class HTTP
def self.SOCKSProxy(p_host, p_port)
delta = SOCKSProxyDelta
proxyclass = Class.new(self)
proxyclass.send(:include, delta)
proxyclass.module_eval {
include delta::InstanceMethods
extend delta::ClassMethods
@socks_server = p_host
@socks_port = p_port
}
proxyclass
end

module SOCKSProxyDelta
module ClassMethods
def socks_server
@socks_server
end

def socks_port
@socks_port
end
end

module InstanceMethods
def conn_address
TCPSocket::SOCKSConnectionPeerAddress.new(self.class.socks_server, self.class.socks_port, address())
end
end
end
end
end
30 changes: 25 additions & 5 deletions test/tc_socksify.rb
Expand Up @@ -6,6 +6,7 @@

$:.unshift "#{File::dirname($0)}/../lib/"
require 'socksify'
require 'socksify/http'


class SocksifyTest < Test::Unit::TestCase
Expand All @@ -22,6 +23,10 @@ def enable_socks
TCPSocket.socks_port = 9050
end

def http_tor_proxy
Net::HTTP::SOCKSProxy("127.0.0.1", 9050)
end

def test_check_tor
[['Hostname', :check_tor],
['IPv4', :check_tor_ip]].each do |f_name, f|
Expand All @@ -39,6 +44,21 @@ def test_check_tor
end
end

def test_check_tor_via_net_http
disable_socks

[['Hostname', :check_tor],
['IPv4', :check_tor_ip]].each do |f_name, f|
tor_direct, ip_direct = send(f)
assert_equal(false, tor_direct)

tor_socks, ip_socks = send(f, http_tor_proxy)
assert_equal(true, tor_socks)

assert(ip_direct != ip_socks)
end
end

def test_ignores
disable_socks

Expand All @@ -54,24 +74,24 @@ def test_ignores
assert(ip_direct == ip_socks_ignored)
end

def check_tor
def check_tor(http_klass = Net::HTTP)
url = URI::parse('http://check.torproject.org/')
parse_check_response(Net::HTTP.start(url.host, url.port) do |http|
parse_check_response(http_klass.start(url.host, url.port) do |http|
http.get('/', "User-Agent"=>"ruby-socksify test").body
end)
end

def check_tor_ip
def check_tor_ip(http_klass = Net::HTTP)
url = URI::parse('http://209.237.247.84/')
parse_check_response(Net::HTTP.start(url.host, url.port) do |http|
parse_check_response(http_klass.start(url.host, url.port) do |http|
http.get('/',
"Host"=>"www.whatismyip.org",
"User-Agent"=>"ruby-socksify test").body
end)
end

def parse_check_response(body)
if body.include? 'You are using Tor.'
if body.include? 'Your browser is configured to use Tor.'
is_tor = true
elsif body.include? 'You are not using Tor.'
is_tor = false
Expand Down