Class: ShadowsocksRuby::Protocols::IvCipherProtocol

Inherits:
Object
  • Object
show all
Includes:
BufferHelper, DummyHelper
Defined in:
lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb

Overview

To be used with any cipher methods with an IV, like Cipher::OpenSSL, Cipher::RbNaCl and Cipher::RC4_MD5.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BufferHelper

#tcp_receive_from_localbackend_in_buffer, #tcp_receive_from_localbackend_other_packet_helper, #tcp_receive_from_remoteserver_in_buffer, #tcp_receive_from_remoteserver_other_packet_helper

Methods included from DummyHelper

#async_recv, #raise_me, #send_data

Constructor Details

#initialize(params = {}) ⇒ IvCipherProtocol

Returns a new instance of IvCipherProtocol.

Parameters:

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

    Configuration parameters

Options Hash (params):


14
15
16
17
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 14

def initialize params = {}
  @cipher = params[:cipher] or raise ProtocolError, "params[:cipher] is required"
  @buffer =""
end

Instance Attribute Details

#next_protocolObject

Returns the value of attribute next_protocol


10
11
12
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 10

def next_protocol
  @next_protocol
end

Instance Method Details

#send_first_packet_process(data) ⇒ Object


70
71
72
73
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 70

def send_first_packet_process data
  @send_iv = @cipher.random_iv
  send_data @send_iv + @cipher.encrypt(data, @send_iv)
end

#send_other_packet(data) ⇒ Object


75
76
77
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 75

def send_other_packet data
  send_data @cipher.encrypt(data, @send_iv)
end

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


44
45
46
47
48
49
50
51
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 44

def tcp_receive_from_localbackend_first_packet n
  iv_len = @cipher.iv_len
  @recv_iv = async_recv iv_len
  class << self
    alias tcp_receive_from_localbackend tcp_receive_from_localbackend_other_packet
  end
  tcp_receive_from_localbackend_other_packet n
end

#tcp_receive_from_localbackend_other_packet(n) ⇒ Object


55
56
57
58
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 55

def tcp_receive_from_localbackend_other_packet n
  @buffer << @cipher.decrypt(async_recv(-1), @recv_iv)
  tcp_receive_from_localbackend_other_packet_helper n
end

#tcp_receive_from_remoteserver_first_packet(n) ⇒ Object Also known as: tcp_receive_from_remoteserver


19
20
21
22
23
24
25
26
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 19

def tcp_receive_from_remoteserver_first_packet n
  iv_len = @cipher.iv_len
  @recv_iv = async_recv iv_len
  class << self
    alias tcp_receive_from_remoteserver tcp_receive_from_remoteserver_other_packet
  end
  tcp_receive_from_remoteserver_other_packet n
end

#tcp_receive_from_remoteserver_other_packet(n) ⇒ Object


30
31
32
33
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 30

def tcp_receive_from_remoteserver_other_packet n
  @buffer << @cipher.decrypt(async_recv(-1), @recv_iv)
  tcp_receive_from_remoteserver_other_packet_helper n
end

#tcp_send_to_localbackend_first_packet(data) ⇒ Object Also known as: tcp_send_to_localbackend


60
61
62
63
64
65
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 60

def tcp_send_to_localbackend_first_packet data
  send_first_packet_process data
  class << self
    alias tcp_send_to_localbackend send_other_packet
  end
end

#tcp_send_to_remoteserver_first_packet(data) ⇒ Object Also known as: tcp_send_to_remoteserver


35
36
37
38
39
40
# File 'lib/shadowsocks_ruby/protocols/cipher/iv_cipher.rb', line 35

def tcp_send_to_remoteserver_first_packet data
  send_first_packet_process data
  class << self
    alias tcp_send_to_remoteserver send_other_packet
  end
end