Module: HrrRbSsh::Codable

Included in:
Algorithm::Publickey::EcdsaSha2::EcdsaSignatureBlob, Algorithm::Publickey::EcdsaSha2::PublicKeyBlob, Algorithm::Publickey::EcdsaSha2::Signature, Algorithm::Publickey::SshDss::PublicKeyBlob, Algorithm::Publickey::SshDss::Signature, Algorithm::Publickey::SshEd25519::OpenSSHPrivateKey, Algorithm::Publickey::SshEd25519::OpenSSHPrivateKeyContent, Algorithm::Publickey::SshEd25519::PublicKeyBlob, Algorithm::Publickey::SshEd25519::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 Method Summary collapse

Instance Method Details

#common_definitionObject



12
13
14
# File 'lib/hrr_rb_ssh/codable.rb', line 12

def common_definition
  self::DEFINITION
end

#conditional_definition(message) ⇒ Object



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

def conditional_definition message
  return [] unless self.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::CONDITIONAL_DEFINITION.fetch(field_name, {})[field_value] || [])
  }
end

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



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

def decode payload, complementary_message={}
  payload_io = StringIO.new payload
  decoded_message = decode_recursively(payload_io).to_h
  if complementary_message.any?
    decoded_message.merge! decode_recursively(payload_io, complementary_message.to_a).to_h
  end
  logger.debug { 'decoded message: ' + decoded_message.inspect }
  decoded_message
end

#decode_recursively(payload_io, message = nil) ⇒ Object



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

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



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

def encode message, complementary_message={}
  logger.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
      logger.debug { "'field_name', 'field_value': #{field_name.inspect}, #{field_value.inspect}" }
      raise e
    end
  }.join
end

#loggerObject



8
9
10
# File 'lib/hrr_rb_ssh/codable.rb', line 8

def logger
  @logger ||= Logger.new self.name
end