Class: BinProxy::ProxyMessage

Inherits:
ProxyBaseItem show all
Defined in:
lib/binproxy/proxy_message.rb

Overview

This class represents a message being proxied, including the raw bits, the BinData parsed representation, and metadata such as the asssociated session, which direction it’s going, and whether it’s been forwarded. [some of the above still TODO!]

Instance Attribute Summary collapse

Attributes inherited from ProxyBaseItem

#dest, #disposition, #id, #session, #src, #time

Instance Method Summary collapse

Methods included from Logger

log

Constructor Details

#initialize(raw_bytes, parsed_message) ⇒ ProxyMessage

Returns a new instance of ProxyMessage.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/binproxy/proxy_message.rb', line 89

def initialize(raw_bytes, parsed_message)
  super()
  @raw = raw_bytes
  @message = parsed_message
  @message_class = @message.class
  @modified = false
  @force_reserialize # XXX ???

  if @raw != @message.to_binary_s
    log.warn "WARNING, inconsistent binary representation:\n[[ORIGINAL]]\n#{@raw.hexdump}\n[[RESERIALIZED]]\n#{@message.to_binary_s.hexdump}"
    log.warn "... @raw encoding is #{@raw.encoding}; to_binary_s is #{@message.to_binary_s.encoding}"
  end
end

Instance Attribute Details

#force_reserializeObject

Returns the value of attribute force_reserialize.



85
86
87
# File 'lib/binproxy/proxy_message.rb', line 85

def force_reserialize
  @force_reserialize
end

#messageObject

Returns the value of attribute message.



85
86
87
# File 'lib/binproxy/proxy_message.rb', line 85

def message
  @message
end

#message_classObject

Returns the value of attribute message_class.



85
86
87
# File 'lib/binproxy/proxy_message.rb', line 85

def message_class
  @message_class
end

#modifiedObject (readonly)

Returns the value of attribute modified.



86
87
88
# File 'lib/binproxy/proxy_message.rb', line 86

def modified
  @modified
end

Instance Method Details

#drop!(reason) ⇒ Object



145
146
147
# File 'lib/binproxy/proxy_message.rb', line 145

def drop!(reason)
  self.disposition = "Dropped #{reason}"
end

#forward!(reason) ⇒ Object



140
141
142
143
# File 'lib/binproxy/proxy_message.rb', line 140

def forward!(reason)
  @session.send_message(self)
  self.disposition = "Sent #{reason}"
end

#headersObject

The next two methods are the last stop before JSON encoding, so all strings in the returned hash must be UTF-8 compatible.



106
107
108
109
110
111
112
113
114
115
# File 'lib/binproxy/proxy_message.rb', line 106

def headers
  super.merge({
    size: @raw.length,
    # HACK - this will prevent errors, but will mangle anything that isn't
    # actually utf8. We should try to handle this upstream where we might
    # know what the actual encoding is.
    summary: @message.summary.force_encoding('UTF-8').scrub,
    message_class: @message_class.to_s,
  })
end

#inspectObject

standard inspect pulls in junk from sesssion



149
150
151
# File 'lib/binproxy/proxy_message.rb', line 149

def inspect #standard inspect pulls in junk from sesssion
  "#<#{self.class.to_s} #{self.to_hash}>"
end

#to_binary_sObject



127
128
129
130
131
132
133
# File 'lib/binproxy/proxy_message.rb', line 127

def to_binary_s
  if @modified or @force_reserialize
    @message.to_binary_s
  else
    @raw
  end
end

#to_hashObject



117
118
119
120
121
122
123
124
125
# File 'lib/binproxy/proxy_message.rb', line 117

def to_hash
  {
    head: headers,
    body: {
      snapshot: @message.annotated_snapshot,
      raw: Base64.encode64(@raw)
    }
  }
end

#update!(snapshot) ⇒ Object



135
136
137
138
# File 'lib/binproxy/proxy_message.rb', line 135

def update!(snapshot)
  @modified = true
  @message.assign( deannotate_snapshot(snapshot) )
end