diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c213a24..de66a0e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,18 +15,17 @@ jobs: runs-on: ubuntu-latest services: - services: - socks5: - image: serjs/go-socks5-proxy - env: - PROXY_USER: user - PROXY_PASSWORD: password - ports: - - 1080:1080 + socks5: + image: serjs/go-socks5-proxy + env: + PROXY_USER: user + PROXY_PASSWORD: password + ports: + - 1080:1080 strategy: matrix: - ruby: ['3.1', '3.2', '3.3', head] + ruby: ['3.2', '3.3', '3.4', head] steps: - uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9ada38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/pkg/ +.ruby-version diff --git a/.rubocop.yml b/.rubocop.yml index 9a36435..bb838fb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,5 @@ --- -require: +plugins: - rubocop-minitest - rubocop-performance - rubocop-rake diff --git a/ChangeLog b/ChangeLog index d5fef26..908cc39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -81,5 +81,9 @@ SOCKSify Ruby 1.7.3 =================== * add Rakefile * fix missing :timeout kwarg in TCPSocket class (thanks @lizzypy) -* Authentication support added to Net::HTTP.SOCKSProxy + +unreleased +SOCKSify Ruby 1.8.0 +=================== +* Authentication support added to Net::HTTP.socks_proxy (thanks to @ojab and @anton-smagin) diff --git a/Gemfile b/Gemfile index 2bae2b5..01e2bff 100644 --- a/Gemfile +++ b/Gemfile @@ -1,13 +1,13 @@ source 'https://rubygems.org' group :test do - gem 'minitest', '~> 5.16' + gem 'minitest', '~> 5.25' end group :development, :test do - gem 'rake', '~> 13.2' - gem 'rubocop', '~> 1.31' - gem 'rubocop-minitest', '~> 0.20' - gem 'rubocop-performance', '~> 1.21' - gem 'rubocop-rake', '~> 0.6' + gem 'rake', '~> 13.3' + gem 'rubocop', '~> 1.78' + gem 'rubocop-minitest', '~> 0.38' + gem 'rubocop-performance', '~> 1.25' + gem 'rubocop-rake', '~> 0.7' end diff --git a/Gemfile.lock b/Gemfile.lock index 91d5f77..599155b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,56 +1,61 @@ GEM remote: https://rubygems.org/ specs: - ast (2.4.2) - json (2.7.2) - language_server-protocol (3.17.0.3) - minitest (5.24.1) - parallel (1.26.1) - parser (3.3.4.2) + ast (2.4.3) + json (2.12.2) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) + minitest (5.25.5) + parallel (1.27.0) + parser (3.3.8.0) ast (~> 2.4.1) racc + prism (1.4.0) racc (1.8.1) rainbow (3.1.1) - rake (13.2.1) - regexp_parser (2.9.2) - rexml (3.3.4) - strscan - rubocop (1.65.1) + rake (13.3.0) + regexp_parser (2.10.0) + rubocop (1.78.0) json (~> 2.3) - language_server-protocol (>= 3.17.0) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 2.4, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.45.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.32.0) - parser (>= 3.3.1.0) - rubocop-minitest (0.35.1) - rubocop (>= 1.61, < 2.0) - rubocop-ast (>= 1.31.1, < 2.0) - rubocop-performance (1.21.1) - rubocop (>= 1.48.1, < 2.0) - rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rake (0.6.0) - rubocop (~> 1.0) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.45.1) + parser (>= 3.3.7.2) + prism (~> 1.4) + rubocop-minitest (0.38.1) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-performance (1.25.0) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-rake (0.7.1) + lint_roller (~> 1.1) + rubocop (>= 1.72.1) ruby-progressbar (1.13.0) - strscan (3.1.0) - unicode-display_width (2.5.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) PLATFORMS ruby x86_64-linux DEPENDENCIES - minitest (~> 5.16) - rake (~> 13.2) - rubocop (~> 1.31) - rubocop-minitest (~> 0.20) - rubocop-performance (~> 1.21) - rubocop-rake (~> 0.6) + minitest (~> 5.25) + rake (~> 13.3) + rubocop (~> 1.78) + rubocop-minitest (~> 0.38) + rubocop-performance (~> 1.25) + rubocop-rake (~> 0.7) BUNDLED WITH - 2.5.17 + 2.6.9 diff --git a/README.md b/README.md index 1181edc..2ed0fc8 100644 --- a/README.md +++ b/README.md @@ -63,17 +63,32 @@ end ``` Note that `Net::HTTP.socks_proxy` never relies on `TCPSocket.socks_server`/`socks_port`. You should either set `socks_proxy` arguments explicitly or use `Net::HTTP` directly. +### Authentication +```rb +require 'socksify/http' + +uri = URI.parse('http://ipecho.net/plain') +Net::HTTP.socks_proxy('127.0.0.1', 1080, username: 'my_username', password: 'my_pwd').start(uri.host, uri.port) do |http| + req = Net::HTTP::Get.new uri + resp = http.request(req) + puts resp.inspect + puts resp.body +end +``` + ### Resolve addresses via SOCKS ```rb Socksify.resolve("spaceboyz.net") # => "87.106.131.203" ``` + ### Testing and Debugging A tor proxy and socks5 proxy with auth is required before running the tests. -* Install tor from your usual package manager, check it is running with `pidof tor` then run the tests with: +* Install tor from your usual package manager, check it is running with `pidof tor` * Start a SOCKS5 proxy using Docker `docker run -d --name socks5 -p 1080:1080 -e PROXY_USER=user -e PROXY_PASSWORD=password serjs/go-socks5-proxy` +Then run the tests with: `bundle exec rake` Colorful diagnostic messages are enabled by default via: diff --git a/lib/socksify/http.rb b/lib/socksify/http.rb index 46749ad..1373a49 100644 --- a/lib/socksify/http.rb +++ b/lib/socksify/http.rb @@ -21,7 +21,7 @@ module Net # patched class class HTTP - def self.socks_proxy(p_host, p_port, p_username = nil, p_password = nil) + def self.socks_proxy(p_host, p_port, username: nil, password: nil) proxyclass.module_eval do include Ruby3NetHTTPConnectable if RUBY_VERSION.to_f > 3.0 # patch #connect method include SOCKSProxyDelta::InstanceMethods @@ -29,8 +29,8 @@ def self.socks_proxy(p_host, p_port, p_username = nil, p_password = nil) @socks_server = p_host @socks_port = p_port - @socks_username = p_username - @socks_password = p_password + @socks_username = username + @socks_password = password end proxyclass diff --git a/lib/socksify/socksproxyable.rb b/lib/socksify/socksproxyable.rb index e97a096..f0e83c2 100644 --- a/lib/socksify/socksproxyable.rb +++ b/lib/socksify/socksproxyable.rb @@ -4,6 +4,8 @@ module Socksproxyable # class methods module ClassMethods + SOCKS4_VERSIONS = %w[4 4a].freeze + attr_accessor :socks_server, :socks_port, :socks_username, :socks_password def socks_version @@ -19,7 +21,7 @@ def socks_ignores=(*hosts) end def socks_version_hex - socks_version == '4a' || socks_version == '4' ? "\004" : "\005" + SOCKS4_VERSIONS.include?(socks_version) ? "\004" : "\005" end end diff --git a/lib/socksify/version.rb b/lib/socksify/version.rb new file mode 100644 index 0000000..647c1c1 --- /dev/null +++ b/lib/socksify/version.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# namespace +module Socksify + VERSION = '1.8.0' +end diff --git a/socksify.gemspec b/socksify.gemspec index b53540e..5a2af51 100644 --- a/socksify.gemspec +++ b/socksify.gemspec @@ -1,10 +1,10 @@ # frozen_string_literal: true -require 'rubygems' +require_relative 'lib/socksify/version' Gem::Specification.new do |s| s.name = 'socksify' - s.version = '1.7.3' + s.version = Socksify::VERSION s.summary = 'Redirect all TCPSockets through a SOCKS5 proxy' s.authors = ['Stephan Maka', 'Andrey Kouznetsov', 'Christopher Thorpe', 'Musy Bite', 'Yuichi Tateno', 'David Dollar'] s.licenses = ['Ruby', 'GPL-3.0'] diff --git a/test/test_helper.rb b/test/test_helper.rb index 7e99aab..046585f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,7 +27,7 @@ def http_tor_proxy end def http_tor_proxy_with_auth(username, password) - Net::HTTP.socks_proxy('127.0.0.1', 1080, username, password) + Net::HTTP.socks_proxy('127.0.0.1', 1080, username: username, password: password) end def get_http(http_klass, url, host_header = nil)