4

I’m using Rails 4.2.7. How do I set the “use_ssl” parameter when sending a web request through a proxy? I have this

    res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port).start(uri.host, uri.port) do |http|
      http.use_ssl = (uri.scheme == "https")
      resp = http.get(uri, initheader = headers)
      status = resp.code.to_i
      if status == 302 || status == 301
        redirect = resp['location']
      end
      content = resp.body
      content_type = resp['content-type']
      content_encoding = resp['content-encoding']
    end

But when I get to this line — “http.use_ssl = (uri.scheme == "https”)” I get the exception

IOError: use_ssl value changed, but session already started
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:758:in `use_ssl='
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:96:in `block in get_content'
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:853:in `start'
    from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:584:in `start'
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:94:in `get_content'
    from /Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:33:in `get_url'
    from (irb):1
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

2

according to this test sample, line 109: https://github.com/astro/socksify-ruby/blob/master/test/tc_socksify.rb#L109

I'd try something like this:

res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port).start(
  uri.host, uri.port, :use_ssl => (uri.scheme == "https")) do |http|

  resp = http.get(uri, initheader = headers)
  status = resp.code.to_i
  if status == 302 || status == 301
    redirect = resp['location']
  end
  content = resp.body
  content_type = resp['content-type']
  content_encoding = resp['content-encoding']
end

if you don't have a valid SSL cert, you can disable SSL validation like this (not recommended for production!)

https://github.com/astro/socksify-ruby/blob/master/test/tc_socksify.rb#L110

Zoran Majstorovic
  • 1,549
  • 16
  • 17
  • This is a proper approach. Documentation states: 1. SSL flag has to be set _before_ the call to [Net::HTTP#start](https://ruby-doc.org/stdlib-2.3.2/libdoc/net/http/rdoc/Net/HTTP.html#method-i-start) is made, and session is started 2. setting SSL [in an option](https://ruby-doc.org/stdlib-2.3.2/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS) to Net::HTTP#start implies this is a correct way to set it – dariodaic Nov 22 '16 at 02:46
0

Have you tried using the use_ssl before start like shown below

res1 = Net::HTTP.SOCKSProxy(TCPSocket::socks_server, TCPSocket::socks_port)
res1.use_ssl = (uri.scheme == "https")
res1.start(uri.host, uri.port) do |http|
  resp = http.get(uri, initheader = headers)
  status = resp.code.to_i
  if status == 302 || status == 301
    redirect = resp['location']
  end
  content = resp.body
  content_type = resp['content-type']
  content_encoding = resp['content-encoding']
end
logesh
  • 2,572
  • 4
  • 33
  • 60
  • THis doesn't work. This line -- "res1.use_ssl = (uri.scheme == "https")" throws the error "Error during processing: undefined method `use_ssl=' for #" – Dave Nov 15 '16 at 21:42
  • @logesh #use_ssl= is an **instance method** defined on Net::HTTP. Call to **::SOCKSProxy** returns an _anonymous class #_ which is a subclass of NET::HTTP. Therefore, you get _NoMethodError_ under the hood. – dariodaic Nov 22 '16 at 02:24