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
Instance Method Summary collapse
- #common_definition ⇒ Object
- #conditional_definition(message) ⇒ Object
- #decode(payload, complementary_message = {}) ⇒ Object
- #decode_recursively(payload_io, message = nil) ⇒ Object
- #encode(message, complementary_message = {}) ⇒ Object
- #initialize(logger: nil) ⇒ Object
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn
Instance Method Details
#common_definition ⇒ Object
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 return [] unless self.class.const_defined? :CONDITIONAL_DEFINITION .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, ={} payload_io = StringIO.new payload = decode_recursively(payload_io).inject(Hash.new){ |h, (k, v)| h.update({k => v}) } if .any? .merge! decode_recursively(payload_io, .to_a).inject(Hash.new){ |h, (k, v)| h.update({k => v}) } end log_debug { 'decoded message: ' + .inspect } 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, =nil if .class == Array and .size == 0 [] else definition = case when nil common_definition when Array conditional_definition() end = definition.map{ |data_type, field_name| [ field_name, data_type.decode(payload_io) ] } + decode_recursively(payload_io, ) 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 , ={} log_debug { 'encoding message: ' + .inspect } definition = common_definition + conditional_definition(.merge ) definition.map{ |data_type, field_name| begin field_value = if [field_name].instance_of? ::Proc then [field_name].call else [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 |