class ShadowsocksRuby::Protocols::IvCipherProtocol

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

Attributes

next_protocol[RW]

Public Class Methods

new(params = {}) click to toggle source

@param [Hash] configuration parameters @option params [Cipher::OpenSSL, Cipher::RbNaCl, Cipher::RC4_MD5] :cipher a cipher object with IV and a key, required

# 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

Public Instance Methods

send_first_packet_process(data) click to toggle source
# 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) click to toggle source
# 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(n)
tcp_receive_from_localbackend_first_packet(n) click to toggle source
# 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) click to toggle source
# 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(n)
tcp_receive_from_remoteserver_first_packet(n) click to toggle source
# 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) click to toggle source
# 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(data)
tcp_send_to_localbackend_first_packet(data) click to toggle source
# 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
Also aliased as: tcp_send_to_localbackend
tcp_send_to_remoteserver(data)
tcp_send_to_remoteserver_first_packet(data) click to toggle source
# 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
Also aliased as: tcp_send_to_remoteserver