Class: ShadowsocksRuby::Protocols::Socks5Protocol::TCP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shadowsocks_ruby/protocols/packet/socks5.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • params (Hash) (defaults to: {})

    Configuration parameters


26
27
28
# File 'lib/shadowsocks_ruby/protocols/packet/socks5.rb', line 26

def initialize params = {}
  @params = {}.merge(params)
end

Instance Attribute Details

#next_protocolObject

Returns the value of attribute next_protocol


23
24
25
# File 'lib/shadowsocks_ruby/protocols/packet/socks5.rb', line 23

def next_protocol
  @next_protocol
end

Instance Method Details

#async_recv_first_packet(n) ⇒ Object Also known as: async_recv


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
# File 'lib/shadowsocks_ruby/protocols/packet/socks5.rb', line 30

def async_recv_first_packet n
  puts "go hereeeeee"
  x = @next_protocol  # check version

  version = x.async_recv(1).unpack("C").first
  if version != SOCKS5
    raise PharseError, "SOCKS version not supported: #{version.inspect}"
  end

  # client handshake v5
  nmethods = x.async_recv(1).unpack("C").first
  *methods = x.async_recv(nmethods).unpack("C*")
  if methods.include?(METHOD_NO_AUTH)
    packet = [SOCKS5, METHOD_NO_AUTH].pack("C*")
    x.send_data packet
  else
    raise PharseError, 'Unsupported authentication method. Only "No Authentication" is supported'
  end

  version, command, reserved = x.async_recv(3).unpack("C3")
  buf =''
  buf << (s = x.async_recv(1))
  address_type = s.unpack("C").first
  case address_type
  when ATYP_IPV4
    buf << x.async_recv(4)
  when ATYP_IPV6
    buf << x.async_recv(16)
  when ATYP_DOMAIN
    buf << (s = x.async_recv(1))
    domain_len = s.unpack("C").first
    buf << x.async_recv(domain_len)
    buf << x.async_recv(2) # port
  else
    raise PharseError, "unknown address_type: #{address_type}"
  end 

  packet = ([SOCKS5, REP_SUCCESS, RESERVED, 1, 0, 0, 0, 0, 0]).pack("C8n")
  x.send_data packet
  class << self
    alias async_recv async_recv_other_packet
  end

  # first packet is special:
  # ATYP + Destination Address + Destination Port
  buf        
end

#async_recv_other_packet(n) ⇒ Object


80
81
82
83
# File 'lib/shadowsocks_ruby/protocols/packet/socks5.rb', line 80

def async_recv_other_packet n
  x = @next_protocol
  x.async_recv(n)
end

#send_data(data) ⇒ Object


85
86
87
88
# File 'lib/shadowsocks_ruby/protocols/packet/socks5.rb', line 85

def send_data data
  x = @next_protocol
  x.send_data data
end