I'm using Rails 4.2.7 with Ruby 2.3.0. I have this code for getting a web page through a SOCKS Proxy
require "resolv-replace.rb"
require 'open-uri'
require 'uri'
require "socksify"
require 'socksify/http'
...
def get_content_via_socks(socks_server, socks_port, url, headers)
TCPSocket::socks_server = socks_server
TCPSocket::socks_port = socks_port
uri = URI(url)
res1 = Net::HTTP.SOCKSProxy(nil, nil).start(uri.host, uri.port, :use_ssl => (uri.scheme == "https")) do |http|
http.read_timeout = 500
puts "launching #{uri} on #{socks_server}:#{socks_port}"
resp = http.get(uri, initheader = headers)
end
end
The only problem with this is even though I have set
TCPSocket::socks_server = socks_server
TCPSocket::socks_port = socks_port
within my local method, it seems to affect the entire application. That is when I call
http = Net::HTTP.new(uri.host, uri.port)
from another method, it fails unless the above socks_server and socks_port are running. How do I set the socks_server and socks_port for that method only and not affect the rest of the application? Please don't say set the parameters here
res1 = Net::HTTP.SOCKSProxy(nil, nil)
These parameters are meaningless. You can set them to "abc" and "def" and they will behave exactly the same as if you set them to valid values.
Edit: Including my Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 5.0.1'
# Use SCSS for stylesheets
gem 'sass-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
#gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
gem 'addressable'
gem 'pg'
gem 'pundit'
gem 'omniauth-oauth2', '~> 1.3.1'
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'omniauth-linkedin-oauth2'
gem 'jquery-ui-rails'
#gem 'jquery-cookie-rails'
gem 'will_paginate'
gem 'bootstrap-sass'
gem 'autoprefixer-rails'
gem 'compass-rails'
gem 'pdf-reader'
gem 'jquery-turbolinks'
gem 'activerecord-import'
gem 'w3c_validators'
gem 'whenever', :require => false
gem 'roo-xls'
gem 'socksify'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :production do
gem 'unicorn'
gem 'puma'
end
Notes
Sorry, but I'll have to say that you should set those parameters. :)
I couldn't get your example to work when setting
TCPSocket::socks_server
, but it worked just fine when usingNet::HTTP.SOCKSProxy(socks_server, socks_port)
.I used :
Setup
On a linux client, I connect to two linux servers with those commands, in 2 separate terminals at the same time :
I now have 2 Socks proxies on my client, running on port 1084 and 1085.
Test
The methods connect to [
http://whatismyip.akamai.com
][1] to check if the SOCKS proxy is being used.It outputs :
The
Net::HTTP.SOCKSProxy(socks_server, socks_port)
version works just fine!If I try to use
Net::HTTP.SOCKSProxy("bogus", "bogus")
, the method raises an exception:Multiple threads
Setting
TCPSocket::socks_server
at the beginning of the method wouldn't be thread-safe.Using a
Net::HTTP.SOCKSProxy
with a block should prevent race-conditions :It outputs :
Test with your Gemfile
With
and your Gemfile on Linux Mint 17, the script above worked just fine when launched with
I created an empty Gemset and only added your gems. The resulting
Gemfile.lock
was :