Class: TCPSocket
- Inherits:
-
Object
show all
- Defined in:
- lib/socksify.rb
Defined Under Namespace
Classes: SOCKSConnectionPeerAddress
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(host = nil, port = 0, local_host = nil, local_port = nil) ⇒ TCPSocket
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/socksify.rb', line 154
def initialize(host=nil, port=0, local_host=nil, local_port=nil)
if host.is_a?(SOCKSConnectionPeerAddress)
socks_peer = host
socks_server = socks_peer.socks_server
socks_port = socks_peer.socks_port
socks_ignores = []
host = socks_peer.peer_host
else
socks_server = self.class.socks_server
socks_port = self.class.socks_port
socks_ignores = self.class.socks_ignores
end
if socks_server and socks_port and not socks_ignores.include?(host)
Socksify::debug_notice "Connecting to SOCKS server #{socks_server}:#{socks_port}"
initialize_tcp socks_server, socks_port
socks_authenticate unless @@socks_version =~ /^4/
if host
socks_connect(host, port)
end
else
Socksify::debug_notice "Connecting directly to #{host}:#{port}"
initialize_tcp host, port, local_host, local_port
Socksify::debug_debug "Connected to #{host}:#{port}"
end
end
|
Class Method Details
.socks_ignores ⇒ Object
127
128
129
|
# File 'lib/socksify.rb', line 127
def self.socks_ignores
@@socks_ignores ||= %w(localhost)
end
|
.socks_ignores=(ignores) ⇒ Object
130
131
132
|
# File 'lib/socksify.rb', line 130
def self.socks_ignores=(ignores)
@@socks_ignores = ignores
end
|
.socks_password ⇒ Object
121
122
123
|
# File 'lib/socksify.rb', line 121
def self.socks_password
@@socks_password ||= nil
end
|
.socks_password=(password) ⇒ Object
124
125
126
|
# File 'lib/socksify.rb', line 124
def self.socks_password=(password)
@@socks_password = password
end
|
.socks_port ⇒ Object
109
110
111
|
# File 'lib/socksify.rb', line 109
def self.socks_port
@@socks_port ||= nil
end
|
.socks_port=(port) ⇒ Object
112
113
114
|
# File 'lib/socksify.rb', line 112
def self.socks_port=(port)
@@socks_port = port
end
|
.socks_server ⇒ Object
103
104
105
|
# File 'lib/socksify.rb', line 103
def self.socks_server
@@socks_server ||= nil
end
|
.socks_server=(host) ⇒ Object
106
107
108
|
# File 'lib/socksify.rb', line 106
def self.socks_server=(host)
@@socks_server = host
end
|
.socks_username ⇒ Object
115
116
117
|
# File 'lib/socksify.rb', line 115
def self.socks_username
@@socks_username ||= nil
end
|
.socks_username=(username) ⇒ Object
118
119
120
|
# File 'lib/socksify.rb', line 118
def self.socks_username=(username)
@@socks_username = username
end
|
.socks_version ⇒ Object
97
98
99
|
# File 'lib/socksify.rb', line 97
def self.socks_version
(@@socks_version == "4a" or @@socks_version == "4") ? "\004" : "\005"
end
|
.socks_version=(version) ⇒ Object
100
101
102
|
# File 'lib/socksify.rb', line 100
def self.socks_version=(version)
@@socks_version = version.to_s
end
|
Instance Method Details
#initialize_tcp ⇒ Object
151
|
# File 'lib/socksify.rb', line 151
alias :initialize_tcp :initialize
|
#socks_authenticate ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/socksify.rb', line 184
def socks_authenticate
if self.class.socks_username || self.class.socks_password
Socksify::debug_debug "Sending username/password authentication"
write "\005\001\002"
else
Socksify::debug_debug "Sending no authentication"
write "\005\001\000"
end
Socksify::debug_debug "Waiting for authentication reply"
auth_reply = recv(2)
if auth_reply.empty?
raise SOCKSError.new("Server doesn't reply authentication")
end
if auth_reply[0..0] != "\004" and auth_reply[0..0] != "\005"
raise SOCKSError.new("SOCKS version #{auth_reply[0..0]} not supported")
end
if self.class.socks_username || self.class.socks_password
if auth_reply[1..1] != "\002"
raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
end
auth = "\001"
auth += self.class.socks_username.to_s.length.chr
auth += self.class.socks_username.to_s
auth += self.class.socks_password.to_s.length.chr
auth += self.class.socks_password.to_s
write auth
auth_reply = recv(2)
if auth_reply[1..1] != "\000"
raise SOCKSError.new("SOCKS authentication failed")
end
else
if auth_reply[1..1] != "\000"
raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
end
end
end
|
#socks_connect(host, port) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/socksify.rb', line 222
def socks_connect(host, port)
port = Socket.getservbyname(port) if port.is_a?(String)
req = String.new
Socksify::debug_debug "Sending destination address"
req << TCPSocket.socks_version
Socksify::debug_debug TCPSocket.socks_version.unpack "H*"
req << "\001"
req << "\000" if @@socks_version == "5"
req << [port].pack('n') if @@socks_version =~ /^4/
if @@socks_version == "4"
host = Resolv::DNS.new.getaddress(host).to_s
end
Socksify::debug_debug host
if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ req << "\001" if @@socks_version == "5"
_ip = [$1.to_i,
$2.to_i,
$3.to_i,
$4.to_i
].pack('CCCC')
req << _ip
elsif host =~ /^[:0-9a-f]+$/ raise "TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor"
req << "\004"
else if @@socks_version == "5"
req << "\003" + [host.size].pack('C') + host
else
req << "\000\000\000\001"
req << "\007\000"
Socksify::debug_notice host
req << host
req << "\000"
end
end
req << [port].pack('n') if @@socks_version == "5"
write req
socks_receive_reply
Socksify::debug_notice "Connected to #{host}:#{port} over SOCKS"
end
|
#socks_receive_reply ⇒ Object
returns [bind_addr: String, bind_port: Fixnum]
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/socksify.rb', line 266
def socks_receive_reply
Socksify::debug_debug "Waiting for SOCKS reply"
if @@socks_version == "5"
connect_reply = recv(4)
if connect_reply.empty?
raise SOCKSError.new("Server doesn't reply")
end
Socksify::debug_debug connect_reply.unpack "H*"
if connect_reply[0..0] != "\005"
raise SOCKSError.new("SOCKS version #{connect_reply[0..0]} is not 5")
end
if connect_reply[1..1] != "\000"
raise SOCKSError.for_response_code(connect_reply.bytes.to_a[1])
end
Socksify::debug_debug "Waiting for bind_addr"
bind_addr_len = case connect_reply[3..3]
when "\001"
4
when "\003"
recv(1).bytes.first
when "\004"
16
else
raise SOCKSError.for_response_code(connect_reply.bytes.to_a[3])
end
bind_addr_s = recv(bind_addr_len)
bind_addr = case connect_reply[3..3]
when "\001"
bind_addr_s.bytes.to_a.join('.')
when "\003"
bind_addr_s
when "\004" i = 0
ip6 = ""
bind_addr_s.each_byte do |b|
if i > 0 and i % 2 == 0
ip6 += ":"
end
i += 1
ip6 += b.to_s(16).rjust(2, '0')
end
end
bind_port = recv(bind_addr_len + 2)
[bind_addr, bind_port.unpack('n')]
else
connect_reply = recv(8)
unless connect_reply[0] == "\000" and connect_reply[1] == "\x5A"
Socksify::debug_debug connect_reply.unpack 'H'
raise SOCKSError.new("Failed while connecting througth socks")
end
end
end
|