Skip to content

Commit 5ee3fe0

Browse files
authored
Merge pull request #57 from lizzypy/fix-tcp-socket-with-connect-timeout
Fix tcp socket with connect timeout
2 parents 41dd2a2 + c1179c8 commit 5ee3fe0

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/socksify/tcpsocket.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ class TCPSocket
1010

1111
# See http://tools.ietf.org/html/rfc1928
1212
# rubocop:disable Metrics/ParameterLists
13-
def initialize(host = nil, port = nil, local_host = nil, local_port = nil)
13+
def initialize(host = nil, port = nil, local_host = nil, local_port = nil, **kwargs)
1414
socks_peer = host if host.is_a?(SOCKSConnectionPeerAddress)
1515
socks_server = set_socks_server(socks_peer)
1616
socks_port = set_socks_port(socks_peer)
1717
socks_ignores = set_socks_ignores(socks_peer)
1818
host = socks_peer.peer_host if socks_peer
1919
if socks_server && socks_port && !socks_ignores.include?(host)
20-
make_socks_connection(host, port, socks_server, socks_port)
20+
make_socks_connection(host, port, socks_server, socks_port, **kwargs)
2121
else
22-
make_direct_connection(host, port, local_host, local_port)
22+
make_direct_connection(host, port, local_host, local_port, **kwargs)
2323
end
2424
end
2525
# rubocop:enable Metrics/ParameterLists
@@ -57,16 +57,16 @@ def set_socks_ignores(socks_peer = nil)
5757
socks_peer ? [] : self.class.socks_ignores
5858
end
5959

60-
def make_socks_connection(host, port, socks_server, socks_port)
60+
def make_socks_connection(host, port, socks_server, socks_port, **kwargs)
6161
Socksify.debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}"
62-
initialize_tcp socks_server, socks_port
62+
initialize_tcp socks_server, socks_port, **kwargs
6363
socks_authenticate unless @socks_version =~ /^4/
6464
socks_connect(host, port) if host
6565
end
6666

67-
def make_direct_connection(host, port, local_host, local_port)
67+
def make_direct_connection(host, port, local_host, local_port, **kwargs)
6868
Socksify.debug_notice "Connecting directly to #{host}:#{port}"
69-
initialize_tcp host, port, local_host, local_port
69+
initialize_tcp host, port, local_host, local_port, **kwargs
7070
Socksify.debug_debug "Connected to #{host}:#{port}"
7171
end
7272
end

test/test_tcpsocket.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'test_helper'
4+
5+
class TCPSocketTest < Minitest::Test
6+
include HelperMethods
7+
8+
if RUBY_VERSION.to_f >= 3.0
9+
def test_tcp_socket_direct_connection_with_connection_timeout
10+
disable_socks
11+
12+
socket = TCPSocket.new('127.0.0.1', 9050, connect_timeout: 0.1)
13+
14+
assert !socket.closed?
15+
end
16+
17+
def test_tcp_socket_socks_connection_with_connection_timeout
18+
enable_socks
19+
20+
# leave off the host because we don't need to worry about connecting to socks
21+
socket = TCPSocket.new(connect_timeout: 0.1)
22+
23+
assert !socket.closed?
24+
end
25+
end
26+
27+
def test_tcp_socket_direct_connection_with_connection_timeout_no_kwargs
28+
disable_socks
29+
30+
socket = TCPSocket.new('127.0.0.1', 9050)
31+
32+
assert !socket.closed?
33+
end
34+
end

0 commit comments

Comments
 (0)