/usr/lib/ruby/vendor_ruby/em-socksify/socks5.rb is in ruby-em-socksify 0.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | module EventMachine
module Socksify
module SOCKS5
def socks_send_handshake
# Method Negotiation as described on
# http://www.faqs.org/rfcs/rfc1928.html Section 3
@socks_state = :method_negotiation
socks_methods.tap do |methods|
send_data [5, methods.size].pack('CC') + methods.pack('C*')
end
end
def socks_send_connect_request
@socks_state = :connecting
send_data [5, 1, 0].pack('CCC')
if matches = @socks_target_host.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
send_data "\x01" + matches.to_a[1 .. -1].map { |s| s.to_i }.pack('CCCC')
elsif @socks_target_host =~ /^[:0-9a-f]+$/
raise SOCKSError, 'TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor)'
else
send_data [3, @socks_target_host.size, @socks_target_host].pack('CCA*')
end
send_data [@socks_target_port].pack('n')
end
def socks_send_authentication
@socks_state = :authenticating
send_data [5,
@socks_username.length, @socks_username,
@socks_password.length, @socks_password
].pack('CCA*CA*')
end
private
# parses socks 5 server responses as specified
# on http://www.faqs.org/rfcs/rfc1928.html
def socks_parse_response
case @socks_state
when :method_negotiation
return unless @socks_data.size >= 2
_, method = @socks_data.slice!(0, 2).unpack('CC')
if socks_methods.include?(method)
case method
when 0 then socks_send_connect_request
when 2 then socks_send_authentication
end
else
raise SOCKSError, 'proxy did not accept method'
end
when :authenticating
return unless @socks_data.size >= 2
socks_version, status_code = @socks_data.slice!(0, 2).unpack('CC')
raise SOCKSError, "SOCKS version 5 not supported" unless socks_version == 5
raise SOCKSError, 'access denied by proxy' unless status_code == 0
send_socks_connect_request
when :connecting
return unless @socks_data.size >= 2
socks_version, status_code = @socks_data.slice(0, 2).unpack('CC')
raise SOCKSError, "SOCKS version #{socks_version} is not 5" unless socks_version == 5
raise SOCKSError.for_response_code(status_code) unless status_code == 0
min_size = @socks_data[3].ord == 3 ? 5 : 4
return unless @socks_data.size >= min_size
size = case @socks_data[3].ord
when 1 then 4
when 3 then @socks_data[4].ord
when 4 then 16
else raise SOCKSError.for_response_code(@socks_data[3])
end
return unless @socks_data.size >= (min_size + size)
bind_addr = @socks_data.slice(min_size, size)
ip = case @socks_data[3].ord
when 1 then bind_addr.bytes.to_a.join('.')
when 3 then bind_addr
when 4 then # TODO: ???
end
socks_unhook(ip)
end
rescue Exception => e
@socks_deferrable.fail(e)
end
def socks_methods
methods = []
methods << 2 if !@socks_username.nil? # 2 => Username/Password Authentication
methods << 0 # 0 => No Authentication Required
methods
end
end
end
end
|