astro / socksify-ruby

Redirect any TCP connection initiated by a Ruby script through a SOCKS5 proxy
http://socksify.rubyforge.org/
Other
163 stars 77 forks source link

rfc3986_parser.rb:130:in `split': bad URI(is not URI?): "127.0.0.1:<my port>" (URI::InvalidURIError) #56

Open erwin opened 1 month ago

erwin commented 1 month ago

Just an FYI, I was getting errors long the lines of:

bin/bundle exec ruby socks.rb
/home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/uri/rfc3986_parser.rb:130:in `split': bad URI(is not URI?): "127.0.0.1:<my port>" (URI::InvalidURIError)
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/uri/rfc3986_parser.rb:135:in `parse'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/uri/common.rb:185:in `parse'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/uri/generic.rb:1563:in `find_proxy'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/net/http.rb:1801:in `proxy_uri'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/net/http.rb:1786:in `proxy?'
        from /home/erwin/dev/awesome/localgems/ruby/3.3.0/bundler/gems/socksify-ruby-94cff4509997/lib/socksify/ruby3net_http_connectable.rb:13:in `connect' from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/net/http.rb:1580:in `do_start'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/net/http.rb:1569:in `start'
        from /home/erwin/.rbenv/versions/3.3.3/lib/ruby/3.3.0/net/http.rb:1029:in `start'
        from socks.rb:7:in `<main>'

When I was running inside rails console I wasn't seeing the whole stack trace, so it took me an embarrassingly long time to get to the bottom of it...

But with the stacktrace in hand, you quickly figure out the issue is the in 'http.rb:1786' inside of 'def proxy?' where it's reading the values of HTTP_PROXY and http_proxy set in the environment.

Some tools can accept socks proxies for these, some can't... If you hit this error, you probably just need to unset HTTP_PROXY and unset http_proxy.

MatzFan commented 1 month ago

Thanks for the report. Can you please provide a minimal reproducible example to replicate this behavior?

erwin commented 1 month ago

You can basically reproduce it with the example from your readme...

# Set the http_proxy to see the error above
ENV['http_proxy'] = '127.0.0.1:9050'
# Clear the http_proxy var and the example runs fine...
# ENV['http_proxy'] = nil
require 'socksify/http'

uri = URI.parse('http://ipecho.net/plain')
Net::HTTP.socks_proxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
  req = Net::HTTP::Get.new uri
  resp = http.request(req)
  puts resp.inspect
  puts resp.body
end

Error:

/usr/lib/ruby/3.0.0/uri/rfc3986_parser.rb:66:in `split': bad URI(is not URI?): "127.0.0.1:9050" (URI::InvalidURIError)
        from /usr/lib/ruby/3.0.0/uri/rfc3986_parser.rb:71:in `parse'
        from /usr/lib/ruby/3.0.0/uri/common.rb:193:in `parse'
        from /usr/lib/ruby/3.0.0/uri/generic.rb:1562:in `find_proxy'
        from /usr/lib/ruby/3.0.0/net/http.rb:1202:in `proxy_uri'
        from /usr/lib/ruby/3.0.0/net/http.rb:1189:in `proxy?'
        from /usr/lib/ruby/3.0.0/net/http.rb:1007:in `connect'
        from /usr/lib/ruby/3.0.0/net/http.rb:995:in `do_start'
        from /usr/lib/ruby/3.0.0/net/http.rb:984:in `start'
        from /usr/lib/ruby/3.0.0/net/http.rb:628:in `start'
        from socks.rb:15:in `<main>'

I don't know if I would really call it a bug...

It's just an inconsistency in the messy way http proxies and socks proxies are handled in kind of unpredictable ways by linux utilities.

But it's a confusing usability issue, and someone that has these variables set is many times more likely to be using socksify in the first place.

My primary purpose for posting is just as documentation for anyone else that run's into this issue.