Skip to content

Commit 2fd0fc3

Browse files
musybiteastro
authored andcommitted
Add authentication support to Net::HTTP.SOCKSProxy
Inspired by #24. This version does not require thread-local variables, everything kept inside instance and local variables. Thanks @ojab!
1 parent d486263 commit 2fd0fc3

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ SOCKSify Ruby 1.7.3
8181
===================
8282
* add Rakefile
8383
* fix missing :timeout kwarg in TCPSocket class (thanks @lizzypy)
84+
* Authentication support added to Net::HTTP.SOCKSProxy
85+
(thanks to @ojab)

doc/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ <h3>Use Net::HTTP explicitly via SOCKS</h3>
105105
explicitly or use <code>Net::HTTP</code> directly.
106106
</p>
107107

108+
<p>
109+
<code>Net::HTTP.SOCKSProxy</code> also supports SOCKS authentication:
110+
</p>
111+
<pre>
112+
Net::HTTP.SOCKSProxy('127.0.0.1', 9050, 'username', 'p4ssw0rd')
113+
</pre>
114+
115+
108116
<h3>Resolve addresses via SOCKS</h3>
109117
<pre>Socksify::resolve("spaceboyz.net")
110118
# => "87.106.131.203"</pre>

lib/socksify/http.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
module Net
2222
# patched class
2323
class HTTP
24-
def self.socks_proxy(p_host, p_port)
24+
def self.socks_proxy(p_host, p_port, p_username = nil, p_password = nil)
25+
delta = SOCKSProxyDelta
2526
proxyclass = Class.new(self)
2627
proxyclass.send(:include, SOCKSProxyDelta)
2728
proxyclass.module_eval do
@@ -30,6 +31,8 @@ def self.socks_proxy(p_host, p_port)
3031
extend SOCKSProxyDelta::ClassMethods
3132
@socks_server = p_host
3233
@socks_port = p_port
34+
@socks_username = p_username
35+
@socks_password = p_password
3336
end
3437
proxyclass
3538
end
@@ -41,13 +44,13 @@ class << self
4144
module SOCKSProxyDelta
4245
# class methods
4346
module ClassMethods
44-
attr_reader :socks_server, :socks_port
47+
attr_reader :socks_server, :socks_port, :socks_username, :socks_password
4548
end
4649

4750
# instance methods - no long supports Ruby < 2
4851
module InstanceMethods
4952
def address
50-
TCPSocket::SOCKSConnectionPeerAddress.new(self.class.socks_server, self.class.socks_port, @address)
53+
TCPSocket::SOCKSConnectionPeerAddress.new(self.class.socks_server, self.class.socks_port, @address, self.class.socks_username, self.class.socks_password)
5154
end
5255
end
5356
end

lib/socksify/tcpsocket.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ def initialize(host = nil, port = nil, local_host = nil, local_port = nil, **kwa
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)
17+
socks_username = set_socks_username(socks_peer)
18+
socks_password = set_socks_password(socks_peer)
1719
socks_ignores = set_socks_ignores(socks_peer)
1820
host = socks_peer.peer_host if socks_peer
1921
if socks_server && socks_port && !socks_ignores.include?(host)
20-
make_socks_connection(host, port, socks_server, socks_port, **kwargs)
22+
make_socks_connection(host, port, socks_server, socks_port, socks_username, socks_password, **kwargs)
2123
else
2224
make_direct_connection(host, port, local_host, local_port, **kwargs)
2325
end
@@ -26,11 +28,13 @@ def initialize(host = nil, port = nil, local_host = nil, local_port = nil, **kwa
2628

2729
# string representation of the peer host address
2830
class SOCKSConnectionPeerAddress < String
29-
attr_reader :socks_server, :socks_port
31+
attr_reader :socks_server, :socks_port, :socks_username, :socks_password
3032

31-
def initialize(socks_server, socks_port, peer_host)
33+
def initialize(socks_server, socks_port, peer_host, socks_username = nil, socks_password = nil)
3234
@socks_server = socks_server
3335
@socks_port = socks_port
36+
@socks_username = socks_username
37+
@socks_password = socks_password
3438
super(peer_host)
3539
end
3640

@@ -53,14 +57,22 @@ def set_socks_port(socks_peer = nil)
5357
socks_peer ? socks_peer.socks_port : self.class.socks_port
5458
end
5559

60+
def set_socks_username(socks_peer = nil)
61+
socks_peer ? socks_peer.socks_username : self.class.socks_username
62+
end
63+
64+
def set_socks_password(socks_peer = nil)
65+
socks_peer ? socks_peer.socks_password : self.class.socks_password
66+
end
67+
5668
def set_socks_ignores(socks_peer = nil)
5769
socks_peer ? [] : self.class.socks_ignores
5870
end
5971

60-
def make_socks_connection(host, port, socks_server, socks_port, **kwargs)
72+
def make_socks_connection(host, port, socks_server, socks_port, socks_username, socks_password, **kwargs)
6173
Socksify.debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}"
6274
initialize_tcp socks_server, socks_port, **kwargs
63-
socks_authenticate unless @socks_version =~ /^4/
75+
socks_authenticate(socks_username, socks_password) unless @socks_version =~ /^4/
6476
socks_connect(host, port) if host
6577
end
6678

0 commit comments

Comments
 (0)