From 9443d8e740d00f6f2a06a6d80d7c66137cbe425d Mon Sep 17 00:00:00 2001 From: liz johnson Date: Thu, 8 Aug 2024 17:47:37 -0500 Subject: [PATCH 1/3] allow for tcp socket with connect timeout --- lib/socksify/tcpsocket.rb | 8 ++++---- test/test_tcp_socket.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/test_tcp_socket.rb diff --git a/lib/socksify/tcpsocket.rb b/lib/socksify/tcpsocket.rb index 24efc6c..8ff20cf 100644 --- a/lib/socksify/tcpsocket.rb +++ b/lib/socksify/tcpsocket.rb @@ -10,7 +10,7 @@ class TCPSocket # See http://tools.ietf.org/html/rfc1928 # rubocop:disable Metrics/ParameterLists - def initialize(host = nil, port = nil, local_host = nil, local_port = nil) + def initialize(host = nil, port = nil, local_host = nil, local_port = nil, **kwargs) socks_peer = host if host.is_a?(SOCKSConnectionPeerAddress) socks_server = set_socks_server(socks_peer) socks_port = set_socks_port(socks_peer) @@ -19,7 +19,7 @@ def initialize(host = nil, port = nil, local_host = nil, local_port = nil) if socks_server && socks_port && !socks_ignores.include?(host) make_socks_connection(host, port, socks_server, socks_port) else - make_direct_connection(host, port, local_host, local_port) + make_direct_connection(host, port, local_host, local_port, **kwargs) end end # rubocop:enable Metrics/ParameterLists @@ -64,9 +64,9 @@ def make_socks_connection(host, port, socks_server, socks_port) socks_connect(host, port) if host end - def make_direct_connection(host, port, local_host, local_port) + def make_direct_connection(host, port, local_host, local_port, **kwargs) Socksify.debug_notice "Connecting directly to #{host}:#{port}" - initialize_tcp host, port, local_host, local_port + initialize_tcp host, port, local_host, local_port, **kwargs Socksify.debug_debug "Connected to #{host}:#{port}" end end diff --git a/test/test_tcp_socket.rb b/test/test_tcp_socket.rb new file mode 100644 index 0000000..43989b8 --- /dev/null +++ b/test/test_tcp_socket.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require_relative 'test_helper' + +class TCPSocketTest < Minitest::Test + include HelperMethods + + if RUBY_VERSION.to_f >= 3.0 + def test_tcp_socket_direct_connection_with_connection_timeout + socket = TCPSocket.new('127.0.0.1', 9050, connect_timeout: 0.1) + + assert !socket.closed? + end + end +end From 49aa7d0e9434aec100d78a3e1a8458ac785d00a1 Mon Sep 17 00:00:00 2001 From: liz johnson Date: Thu, 8 Aug 2024 17:52:47 -0500 Subject: [PATCH 2/3] add test to make sure it works for ruby 2 --- test/{test_tcp_socket.rb => test_tcpsocket.rb} | 6 ++++++ 1 file changed, 6 insertions(+) rename test/{test_tcp_socket.rb => test_tcpsocket.rb} (70%) diff --git a/test/test_tcp_socket.rb b/test/test_tcpsocket.rb similarity index 70% rename from test/test_tcp_socket.rb rename to test/test_tcpsocket.rb index 43989b8..2ce740c 100644 --- a/test/test_tcp_socket.rb +++ b/test/test_tcpsocket.rb @@ -12,4 +12,10 @@ def test_tcp_socket_direct_connection_with_connection_timeout assert !socket.closed? end end + + def test_tcp_socket_direct_connection_with_connection_timeout + socket = TCPSocket.new('127.0.0.1', 9050) + + assert !socket.closed? + end end From c1179c8e680c2f3601e3249bb5a6b38a1d2c82f2 Mon Sep 17 00:00:00 2001 From: liz johnson Date: Thu, 8 Aug 2024 18:30:54 -0500 Subject: [PATCH 3/3] handle socks connection --- lib/socksify/tcpsocket.rb | 6 +++--- test/test_tcpsocket.rb | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/socksify/tcpsocket.rb b/lib/socksify/tcpsocket.rb index 8ff20cf..a2160e1 100644 --- a/lib/socksify/tcpsocket.rb +++ b/lib/socksify/tcpsocket.rb @@ -17,7 +17,7 @@ def initialize(host = nil, port = nil, local_host = nil, local_port = nil, **kwa socks_ignores = set_socks_ignores(socks_peer) host = socks_peer.peer_host if socks_peer if socks_server && socks_port && !socks_ignores.include?(host) - make_socks_connection(host, port, socks_server, socks_port) + make_socks_connection(host, port, socks_server, socks_port, **kwargs) else make_direct_connection(host, port, local_host, local_port, **kwargs) end @@ -57,9 +57,9 @@ def set_socks_ignores(socks_peer = nil) socks_peer ? [] : self.class.socks_ignores end - def make_socks_connection(host, port, socks_server, socks_port) + def make_socks_connection(host, port, socks_server, socks_port, **kwargs) Socksify.debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}" - initialize_tcp socks_server, socks_port + initialize_tcp socks_server, socks_port, **kwargs socks_authenticate unless @socks_version =~ /^4/ socks_connect(host, port) if host end diff --git a/test/test_tcpsocket.rb b/test/test_tcpsocket.rb index 2ce740c..66c5b15 100644 --- a/test/test_tcpsocket.rb +++ b/test/test_tcpsocket.rb @@ -7,13 +7,26 @@ class TCPSocketTest < Minitest::Test if RUBY_VERSION.to_f >= 3.0 def test_tcp_socket_direct_connection_with_connection_timeout + disable_socks + socket = TCPSocket.new('127.0.0.1', 9050, connect_timeout: 0.1) assert !socket.closed? end + + def test_tcp_socket_socks_connection_with_connection_timeout + enable_socks + + # leave off the host because we don't need to worry about connecting to socks + socket = TCPSocket.new(connect_timeout: 0.1) + + assert !socket.closed? + end end - def test_tcp_socket_direct_connection_with_connection_timeout + def test_tcp_socket_direct_connection_with_connection_timeout_no_kwargs + disable_socks + socket = TCPSocket.new('127.0.0.1', 9050) assert !socket.closed?