Skip to content

Commit 042ece4

Browse files
committed
allow username/password authorization, closes astro#2
1 parent dde1841 commit 042ece4

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

lib/socksify.rb

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ def self.socks_port
111111
def self.socks_port=(port)
112112
@@socks_port = port
113113
end
114+
def self.socks_username
115+
@@socks_username ||= nil
116+
end
117+
def self.socks_username=(username)
118+
@@socks_username = username
119+
end
120+
def self.socks_password
121+
@@socks_password ||= nil
122+
end
123+
def self.socks_password=(password)
124+
@@socks_password = password
125+
end
114126
def self.socks_ignores
115127
@@socks_ignores ||= %w(localhost)
116128
end
@@ -161,18 +173,39 @@ def initialize(host=nil, port=0, local_host="0.0.0.0", local_port=0)
161173
Socksify::debug_debug "Connected to #{host}:#{port}"
162174
end
163175
end
164-
176+
165177
# Authentication
166178
def socks_authenticate
167-
Socksify::debug_debug "Sending no authentication"
168-
write "\005\001\000"
179+
if self.class.socks_username || self.class.socks_password
180+
Socksify::debug_debug "Sending username/password authentication"
181+
write "\005\001\001\002"
182+
else
183+
Socksify::debug_debug "Sending no authentication"
184+
write "\005\001\000"
185+
end
169186
Socksify::debug_debug "Waiting for authentication reply"
170187
auth_reply = recv(2)
171188
if auth_reply[0..0] != "\004" and auth_reply[0..0] != "\005"
172189
raise SOCKSError.new("SOCKS version #{auth_reply[0..0]} not supported")
173190
end
174-
if auth_reply[1..1] != "\000"
175-
raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
191+
if self.class.socks_username || self.class.socks_password
192+
if auth_reply[1..1] != "\002"
193+
raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
194+
end
195+
auth = "\001"
196+
auth += self.class.socks_username.to_s.length.chr
197+
auth += self.class.socks_username.to_s
198+
auth += self.class.socks_password.to_s.length.chr
199+
auth += self.class.socks_password.to_s
200+
write auth
201+
auth_reply = recv(2)
202+
if auth_reply[1..1] != "\000"
203+
raise SOCKSError.new("SOCKS authentication failed")
204+
end
205+
else
206+
if auth_reply[1..1] != "\000"
207+
raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
208+
end
176209
end
177210
end
178211

0 commit comments

Comments
 (0)