Class: ShadowsocksRuby::Protocols::ShadowsocksProtocol

Inherits:
Object
  • Object
show all
Includes:
DummyHelper
Defined in:
lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb

Overview

Shadowsocks packet protocol for both origin shadowsocks protocol and OTA shadowsocks protocol.

specification:

This is a packet protocol, so no need to implement @buffer

Constant Summary collapse

ATYP_IPV4 =
1
ATYP_DOMAIN =
3
ATYP_IPV6 =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DummyHelper

#async_recv, #raise_me, #send_data

Constructor Details

#initialize(params = {}) ⇒ ShadowsocksProtocol

Returns a new instance of ShadowsocksProtocol.

Parameters:

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

    Configuration parameters



20
21
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 20

def initialize params = {}
end

Instance Attribute Details

#next_protocolObject

Returns the value of attribute next_protocol.



13
14
15
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 13

def next_protocol
  @next_protocol
end

Instance Method Details

#tcp_receive_from_localbackend_first_packet(n) ⇒ Object Also known as: tcp_receive_from_localbackend



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

def tcp_receive_from_localbackend_first_packet n
  buf = ""
  s = async_recv(1)
  buf << s
  address_type = s.unpack("C").first
  case address_type
  when ATYP_IPV4
    buf << async_recv(4)
  when ATYP_IPV6
    buf << async_recv(16)
  when ATYP_DOMAIN
    buf << (s = async_recv(1))
    domain_len = s.unpack("C").first
    buf << async_recv(domain_len)
    buf << async_recv(2) # port
  else
    raise PharseError, "unknown address_type: #{address_type}"
  end 

  class << self
    alias tcp_receive_from_localbackend tcp_receive_from_localbackend_other_packet
  end
  # first packet is special:
  # ATYP + Destination Address + Destination Port
  buf
end

#tcp_receive_from_localbackend_other_packet(n) ⇒ Object



52
53
54
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 52

def tcp_receive_from_localbackend_other_packet n
  async_recv(n)
end

#tcp_receive_from_remoteserver(n) ⇒ Object



60
61
62
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 60

def tcp_receive_from_remoteserver n
  async_recv(n)
end

#tcp_send_to_localbackend(data) ⇒ Object



56
57
58
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 56

def tcp_send_to_localbackend data
  send_data data
end

#tcp_send_to_remoteserver(data) ⇒ Object



64
65
66
# File 'lib/shadowsocks_ruby/protocols/packet/shadowsocks.rb', line 64

def tcp_send_to_remoteserver data
  send_data data
end