Module: Socksify

Defined in:
lib/socksify.rb,
lib/socksify/debug.rb

Defined Under Namespace

Classes: Color

Class Method Summary collapse

Class Method Details

.debug=(enabled) ⇒ Object


28
29
30
# File 'lib/socksify/debug.rb', line 28

def self.debug=(enabled)
  @@debug = enabled
end

.debug_debug(str) ⇒ Object


32
33
34
# File 'lib/socksify/debug.rb', line 32

def self.debug_debug(str)
  debug(Color::Green, str)
end

.debug_error(str) ⇒ Object


40
41
42
# File 'lib/socksify/debug.rb', line 40

def self.debug_error(str)
  debug(Color::Red, str)
end

.debug_notice(str) ⇒ Object


36
37
38
# File 'lib/socksify/debug.rb', line 36

def self.debug_notice(str)
  debug(Color::Yellow, str)
end

.proxy(server, port) ⇒ Object


348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/socksify.rb', line 348

def self.proxy(server, port)
  default_server = TCPSocket::socks_server
  default_port = TCPSocket::socks_port
  begin
    TCPSocket::socks_server = server
    TCPSocket::socks_port = port
    yield
  ensure
    TCPSocket::socks_server = default_server
    TCPSocket::socks_port = default_port
  end
end

.resolve(host) ⇒ Object


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/socksify.rb', line 320

def self.resolve(host)
  s = TCPSocket.new

  begin
    Socksify::debug_debug "Sending hostname to resolve: #{host}"
    s.write "\005"
    if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/  # to IPv4 address
      s.write "\xF1\000\001" + [$1.to_i,
                                $2.to_i,
                                $3.to_i,
                                $4.to_i
                               ].pack('CCCC')
    elsif host =~ /^[:0-9a-f]+$/  # to IPv6 address
      raise "TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor"
      s.write "\004"
    else                          # to hostname
      s.write "\xF0\000\003" + [host.size].pack('C') + host
    end
    s.write [0].pack('n')  # Port
    
    addr, _port = s.socks_receive_reply
    Socksify::debug_notice "Resolved #{host} as #{addr} over SOCKS"
    addr
  ensure
    s.close
  end
end