Skip to content

Commit

Permalink
Merge pull request #57 from lizzypy/fix-tcp-socket-with-connect-timeout
Browse files Browse the repository at this point in the history
Fix tcp socket with connect timeout
  • Loading branch information
MatzFan authored Aug 9, 2024
2 parents 41dd2a2 + c1179c8 commit 5ee3fe0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/socksify/tcpsocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ 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)
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)
make_direct_connection(host, port, local_host, local_port, **kwargs)
end
end
# rubocop:enable Metrics/ParameterLists
Expand Down Expand Up @@ -57,16 +57,16 @@ 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

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
34 changes: 34 additions & 0 deletions test/test_tcpsocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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
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_no_kwargs
disable_socks

socket = TCPSocket.new('127.0.0.1', 9050)

assert !socket.closed?
end
end

0 comments on commit 5ee3fe0

Please sign in to comment.