socksify-ruby icon indicating copy to clipboard operation
socksify-ruby copied to clipboard

Connecting to existing SOCKS5 tunnel

Open ruehsn opened this issue 3 years ago • 1 comments

I am having difficulties trying to make FTPS connection to SOCKS5 tunnel I've open via Putty.

I am able to get it work using FileZilla settings: image

so I know the network connection is working.

But I am unclear how to set it up for my Ruby scripts. I've tried setting the "SOCKS_SERVER" environment variable and connecting directly without 'socksify' gem and few of the sample code with from the socksify main page out any luck.

The error message leads me to believe that script it trying to connect directly to FTP host and not go through the host: ruby/3.1.0/socket.rb:64:in `connect': A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. - connect(2) for 10.1.4.20:2021 (Errno::ETIMEDOUT)

@host = '10.1.4.20'

def connection
  @connection ||= Net::FTP.new(@host, connection_params)
end

def connection_params
  {
    port:     '2021',
    ssl:      { verify_mode: OpenSSL::SSL::VERIFY_NONE},
    passive:  true,
    debug_mode:  true,
    username: $user_name,
    password: $hog_password
  }
end

ENV["SOCKS_SERVER"] = "127.0.0.1:9090"
# set SOCKS_SERVER="127.0.0.1:9090"
# set http_proxy="127.0.0.1:9090"

def test_ftps()
  c = connection
  c.chdir('..')
end

puts "Your proxy is #{ENV["SOCKS_SERVER"]}"
test_ftps

ruehsn avatar Oct 05 '22 15:10 ruehsn

To resolve your issue, wrap your FTPS connection code with a Socksify::Proxy block and pass the SOCKS5 server address to Socksify::Proxy.new(). This ensures your script connects through the specified SOCKS5 proxy instead of connecting directly to the FTP host.

require 'net/ftp'
require 'socksify'
require 'openssl'

@host = '10.1.4.20'

def connection
  @connection ||= Net::FTP.new(@host, connection_params)
end

def connection_params
  {
    port:     '2021',
    ssl:      { verify_mode: OpenSSL::SSL::VERIFY_NONE},
    passive:  true,
    debug_mode:  true,
    username: $user_name,
    password: $hog_password
  }
end

# Set the SOCKS5 server address
ENV["SOCKS_SERVER"] = "127.0.0.1:9090"

def test_ftps()
  c = connection
  c.chdir('..')
end

puts "Your proxy is #{ENV["SOCKS_SERVER"]}"

# Create a new Socksify::Proxy instance with the SOCKS5 server address
proxy = Socksify::Proxy.new(ENV["SOCKS_SERVER"])

# Wrap the test_ftps method with the proxy instance to ensure the connection goes through the proxy
proxy.wrap { test_ftps }

kornate avatar May 03 '23 19:05 kornate