From 4248af36c5c51184fbe1db90ce29295b9c583b66 Mon Sep 17 00:00:00 2001 From: Karol Sarnacki Date: Wed, 13 Feb 2013 11:05:04 +0100 Subject: [PATCH 1/4] Update lib/proxifier/proxy.rb Fix wrong parameter name inside constructor. --- lib/proxifier/proxy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proxifier/proxy.rb b/lib/proxifier/proxy.rb index 0bbb1b6..4b75fab 100644 --- a/lib/proxifier/proxy.rb +++ b/lib/proxifier/proxy.rb @@ -16,7 +16,7 @@ def proxify?(host, no_proxy = nil) attr_reader :url, :options def initialize(url, options = {}) - url = URI.parse(uri) unless url.is_a?(URI::Generic) + url = URI.parse(url) unless url.is_a?(URI::Generic) @url, @options = url, options end From a3ec983f81cd0165cd9c9de8ef7862c1033409e7 Mon Sep 17 00:00:00 2001 From: Karol Sarnacki Date: Wed, 13 Feb 2013 11:43:46 +0100 Subject: [PATCH 2/4] Add tests for Proxy creation from String and URI::Generic --- Rakefile | 11 ++++++++++- proxifier.gemspec | 2 ++ spec/proxifier/proxy_spec.rb | 28 ++++++++++++++++++++++++++++ spec/proxifier_spec.rb | 20 ++++++++++++++++++++ spec/spec_helper.rb | 8 ++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 spec/proxifier/proxy_spec.rb create mode 100644 spec/proxifier_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Rakefile b/Rakefile index 2995527..54e2043 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,10 @@ -require "bundler/gem_tasks" +require 'bundler/gem_tasks' +require 'rake/testtask' + +task default: :test + +Rake::TestTask.new do |task| + task.libs << 'lib' << 'spec' + task.pattern = 'spec/**/*_spec.rb' + task.verbose = true +end diff --git a/proxifier.gemspec b/proxifier.gemspec index 30923bb..b74570f 100644 --- a/proxifier.gemspec +++ b/proxifier.gemspec @@ -13,4 +13,6 @@ Gem::Specification.new do |s| s.files = Dir["bin/*", "lib/**/*"] + ["LICENSE", "README.md"] s.executables = ["pirb", "pruby"] + + s.add_development_dependency 'minitest', '>= 4.6.0' end diff --git a/spec/proxifier/proxy_spec.rb b/spec/proxifier/proxy_spec.rb new file mode 100644 index 0000000..e85be42 --- /dev/null +++ b/spec/proxifier/proxy_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' +require 'uri' +require 'proxifier/proxy' + +describe Proxifier::Proxy do + + it 'should create proxy from URL String' do + proxy = Proxifier::Proxy.new('socks://joe:sekret@myproxy:60123') + + proxy.url.scheme.must_equal 'socks' + proxy.user.must_equal 'joe' + proxy.password.must_equal 'sekret' + proxy.host.must_equal 'myproxy' + proxy.port.must_equal 60123 + end + + it 'should create proxy from generic URI' do + uri = URI::Generic.new('socks', 'joe:sekret', 'myproxy', 60123, nil, nil, nil, nil, nil) + proxy = Proxifier::Proxy.new(uri) + + proxy.url.scheme.must_equal 'socks' + proxy.user.must_equal 'joe' + proxy.password.must_equal 'sekret' + proxy.host.must_equal 'myproxy' + proxy.port.must_equal 60123 + end + +end diff --git a/spec/proxifier_spec.rb b/spec/proxifier_spec.rb new file mode 100644 index 0000000..7bc4b8f --- /dev/null +++ b/spec/proxifier_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' +require 'proxifier/proxies/socks' + +describe Proxifier do + + it 'should have a version number' do + Proxifier::VERSION.wont_be_nil + end + + it 'should create Proxy from URL String' do + proxy = Proxifier::Proxy('socks://joe:sekret@myproxy:60123') + + proxy.must_be_instance_of Proxifier::SOCKSProxy + proxy.user.must_equal 'joe' + proxy.password.must_equal 'sekret' + proxy.host.must_equal 'myproxy' + proxy.port.must_equal 60123 + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..51a5143 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,8 @@ +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) +require 'rubygems' + +gem 'minitest' # ensure we are using the gem version +require 'minitest/spec' +require 'minitest/autorun' + +require 'proxifier' From 12f2f63cec40b8f1b427455f9912bdf7a61a82c6 Mon Sep 17 00:00:00 2001 From: Karol Sarnacki Date: Wed, 13 Feb 2013 14:21:05 +0100 Subject: [PATCH 3/4] Fix SOCKS5 authentication to comply with RFC 1928 and 1929 --- lib/proxifier/proxies/socks.rb | 13 ++++++------ spec/proxifier/proxies/socks_spec.rb | 30 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 spec/proxifier/proxies/socks_spec.rb diff --git a/lib/proxifier/proxies/socks.rb b/lib/proxifier/proxies/socks.rb index 21fdc74..f7ffdfb 100644 --- a/lib/proxifier/proxies/socks.rb +++ b/lib/proxifier/proxies/socks.rb @@ -3,7 +3,8 @@ module Proxifier class SOCKSProxy < Proxy - VERSION = 0x05 + VERSION = 0x05 + SUBNEGOTIATION_VERSION = 0x01 def do_proxify(socket, host, port) authenticaton_method = greet(socket) @@ -26,12 +27,12 @@ def authenticate(socket, method) case method when 0x00 # NO AUTHENTICATION REQUIRED when 0x02 # USERNAME/PASSWORD - user &&= user[0, 0xFF] - password &&= password[0, 0xFF] + _user = user ? user[0, 0xFF] : '' + _password = password ? password[0, 0xFF] : '' - socket << [user.size, user, password.size, password].pack("CA#{user.size}CA#{password.size}") - version, status = socket.read(2).unpack("CC") - check_version(version) + socket << [SUBNEGOTIATION_VERSION, _user.size, _user, _password.size, _password].pack("CCA#{_user.size}CA#{_password.size}") + version, status = socket.read(2).unpack('CC') + check_version(version, SUBNEGOTIATION_VERSION) case status when 0x00 # SUCCESS diff --git a/spec/proxifier/proxies/socks_spec.rb b/spec/proxifier/proxies/socks_spec.rb new file mode 100644 index 0000000..75ea231 --- /dev/null +++ b/spec/proxifier/proxies/socks_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' +require 'uri' +require 'proxifier/proxies/socks' + +describe Proxifier::SOCKSProxy do + + before do + @socket = MiniTest::Mock.new + end + + it 'should comply with SOCKS5 authentication specification' do + proxy = Proxifier::Proxy('socks://joe:sekret@myproxy:60123') + + proxy.must_be_instance_of Proxifier::SOCKSProxy + + TCPSocket.stub :new, @socket do + @socket.expect :<<, nil, ["\x05\x02\x00\x02"] + @socket.expect :read, "\x05\x02", [2] + @socket.expect :<<, nil, ["\x01\x03joe\x06sekret"] + @socket.expect :read, "\x01\x00", [2] + @socket.expect :<<, nil, ["\x05\x01\x00\x03\tlocalhost\x048"] + @socket.expect :read, "\x05\x00\x00\x01", [4] + @socket.expect :read, "\x7F\x00\x00\x01", [4] + @socket.expect :read, "\x08", [2] + + proxy.open('localhost', 1080) + end + end + +end From 5d91c25aedf85aac4a3e383e67f152416d79a597 Mon Sep 17 00:00:00 2001 From: Karol Sarnacki Date: Thu, 14 Feb 2013 15:30:18 +0100 Subject: [PATCH 4/4] Bump version number --- lib/proxifier/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proxifier/version.rb b/lib/proxifier/version.rb index fc7de0f..98f583e 100644 --- a/lib/proxifier/version.rb +++ b/lib/proxifier/version.rb @@ -1,3 +1,3 @@ module Proxifier - VERSION = "1.0.3" + VERSION = '1.0.4' end