Module: Socksify

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

Overview

namespace

Defined Under Namespace

Classes: Color

Constant Summary collapse

VERSION =
'1.8.1'

Class Method Summary collapse

Class Method Details

.debug(color, str) ⇒ Object



61
62
63
# File 'lib/socksify/debug.rb', line 61

def self.debug(color, str)
  puts "#{color}#{now_s}#{Color::Reset} #{str}" if defined?(@debug) && @debug
end

.debug=(enabled) ⇒ Object



45
46
47
# File 'lib/socksify/debug.rb', line 45

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

.debug_debug(str) ⇒ Object



49
50
51
# File 'lib/socksify/debug.rb', line 49

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

.debug_error(str) ⇒ Object



57
58
59
# File 'lib/socksify/debug.rb', line 57

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

.debug_notice(str) ⇒ Object



53
54
55
# File 'lib/socksify/debug.rb', line 53

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

.now_sObject



65
66
67
# File 'lib/socksify/debug.rb', line 65

def self.now_s
  Time.now.strftime('%H:%M:%S')
end

.proxy(server, port) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/socksify.rb', line 109

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 # failback
    TCPSocket.socks_server = default_server
    TCPSocket.socks_port = default_port
  end
end

.request(host) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/socksify.rb', line 122

def self.request(host)
  req = (+'') << "\005"
  case host
  when /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ # to IPv4 address
    req << "\xF1\000\001#{(1..4).map { |i| Regexp.last_match(i).to_i }.pack('CCCC')}"
  when /^[:0-9a-f]+$/ # to IPv6 address
    raise 'TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor)'
  #   # req << "\004" # UNREACHABLE
  else # to hostname
    req << "\xF0\000\003#{[host.size].pack('C')}#{host}"
  end
  req << [0].pack('n') # Port
end

.resolve(host) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/socksify.rb', line 97

def self.resolve(host)
  socket = TCPSocket.new # no args?
  Socksify.debug_debug "Sending hostname to resolve: #{host}"
  req = request(host)
  socket.write req
  addr, _port = socket.socks_receive_reply
  Socksify.debug_notice "Resolved #{host} as #{addr} over SOCKS"
  addr
ensure
  socket.close
end