Module: HrrRbSsh::Codable

Includes:
Loggable
Included in:
Algorithm::Publickey::EcdsaSha2::EcdsaSignatureBlob, Algorithm::Publickey::EcdsaSha2::PublicKeyBlob, Algorithm::Publickey::EcdsaSha2::Signature, Algorithm::Publickey::SshDss::PublicKeyBlob, Algorithm::Publickey::SshDss::Signature, Algorithm::Publickey::SshRsa::PublicKeyBlob, Algorithm::Publickey::SshRsa::Signature, Authentication::Method::Publickey::Algorithm::SignatureBlob, Message::SSH_MSG_CHANNEL_CLOSE, Message::SSH_MSG_CHANNEL_DATA, Message::SSH_MSG_CHANNEL_EOF, Message::SSH_MSG_CHANNEL_EXTENDED_DATA, Message::SSH_MSG_CHANNEL_FAILURE, Message::SSH_MSG_CHANNEL_OPEN, Message::SSH_MSG_CHANNEL_OPEN_CONFIRMATION, Message::SSH_MSG_CHANNEL_OPEN_FAILURE, Message::SSH_MSG_CHANNEL_REQUEST, Message::SSH_MSG_CHANNEL_SUCCESS, Message::SSH_MSG_CHANNEL_WINDOW_ADJUST, Message::SSH_MSG_DEBUG, Message::SSH_MSG_DISCONNECT, Message::SSH_MSG_GLOBAL_REQUEST, Message::SSH_MSG_IGNORE, Message::SSH_MSG_KEXDH_INIT, Message::SSH_MSG_KEXDH_REPLY, Message::SSH_MSG_KEXECDH_INIT, Message::SSH_MSG_KEXECDH_REPLY, Message::SSH_MSG_KEXINIT, Message::SSH_MSG_KEX_DH_GEX_GROUP, Message::SSH_MSG_KEX_DH_GEX_INIT, Message::SSH_MSG_KEX_DH_GEX_REPLY, Message::SSH_MSG_KEX_DH_GEX_REQUEST, Message::SSH_MSG_KEX_DH_GEX_REQUEST_OLD, Message::SSH_MSG_NEWKEYS, Message::SSH_MSG_REQUEST_FAILURE, Message::SSH_MSG_REQUEST_SUCCESS, Message::SSH_MSG_SERVICE_ACCEPT, Message::SSH_MSG_SERVICE_REQUEST, Message::SSH_MSG_UNIMPLEMENTED, Message::SSH_MSG_USERAUTH_FAILURE, Message::SSH_MSG_USERAUTH_INFO_REQUEST, Message::SSH_MSG_USERAUTH_INFO_RESPONSE, Message::SSH_MSG_USERAUTH_PK_OK, Message::SSH_MSG_USERAUTH_REQUEST, Message::SSH_MSG_USERAUTH_SUCCESS, Transport::KexAlgorithm::DiffieHellman::H0, Transport::KexAlgorithm::DiffieHellmanGroupExchange::H0, Transport::KexAlgorithm::EllipticCurveDiffieHellman::H0
Defined in:
lib/hrr_rb_ssh/codable.rb

Instance Attribute Summary

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Instance Method Details

#common_definitionObject



14
15
16
# File 'lib/hrr_rb_ssh/codable.rb', line 14

def common_definition
  self.class::DEFINITION
end

#conditional_definition(message) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/hrr_rb_ssh/codable.rb', line 18

def conditional_definition message
  return [] unless self.class.const_defined? :CONDITIONAL_DEFINITION
  message.inject([]){ |a, (k,v)|
    field_name  = k
    field_value = if v.instance_of? ::Proc then v.call else v end
    a + (self.class::CONDITIONAL_DEFINITION.fetch(field_name, {})[field_value] || [])
  }
end

#decode(payload, complementary_message = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/hrr_rb_ssh/codable.rb', line 61

def decode payload, complementary_message={}
  payload_io = StringIO.new payload
  decoded_message = decode_recursively(payload_io).inject(Hash.new){ |h, (k, v)| h.update({k => v}) }
  if complementary_message.any?
    decoded_message.merge! decode_recursively(payload_io, complementary_message.to_a).inject(Hash.new){ |h, (k, v)| h.update({k => v}) }
  end
  log_debug { 'decoded message: ' + decoded_message.inspect }
  decoded_message
end

#decode_recursively(payload_io, message = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hrr_rb_ssh/codable.rb', line 41

def decode_recursively payload_io, message=nil
  if message.class == Array and message.size == 0
    []
  else
    definition = case message
                 when nil
                   common_definition
                 when Array
                   conditional_definition(message)
                 end
    decoded_message = definition.map{ |data_type, field_name|
      [
        field_name,
        data_type.decode(payload_io)
      ]
    }
    decoded_message + decode_recursively(payload_io, decoded_message)
  end
end

#encode(message, complementary_message = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hrr_rb_ssh/codable.rb', line 27

def encode message, complementary_message={}
  log_debug { 'encoding message: ' + message.inspect }
  definition = common_definition + conditional_definition(message.merge complementary_message)
  definition.map{ |data_type, field_name|
    begin
      field_value = if message[field_name].instance_of? ::Proc then message[field_name].call else message[field_name] end
      data_type.encode field_value
    rescue => e
      log_debug { "'field_name', 'field_value': #{field_name.inspect}, #{field_value.inspect}" }
      raise e
    end
  }.join
end

#initialize(logger: nil) ⇒ Object



10
11
12
# File 'lib/hrr_rb_ssh/codable.rb', line 10

def initialize logger: nil
  self.logger = logger
end