Class: Rex::Post::HWBridge::Extensions::CustomMethods::CustomMethods
- Inherits:
-
Rex::Post::HWBridge::Extension
- Object
- Rex::Post::HWBridge::Extension
- Rex::Post::HWBridge::Extensions::CustomMethods::CustomMethods
- Defined in:
- lib/rex/post/hwbridge/extensions/custom_methods/custom_methods.rb
Overview
Custom Methods extension - set of commands provided by the HW itself
Instance Attribute Summary
Attributes inherited from Rex::Post::HWBridge::Extension
Class Method Summary collapse
-
.command_name(path) ⇒ String?
The console command, or nil if the route is unsafe.
Instance Method Summary collapse
-
#convert_value(type, value) ⇒ Object
protected
Converts a value returned by the hardware to the type the method declared.
-
#initialize(client) ⇒ CustomMethods
constructor
A new instance of CustomMethods.
-
#send_request(cmd, args, methods) ⇒ Hash
Converts a command and its arguments to a request to the hardware device.
Constructor Details
#initialize(client) ⇒ CustomMethods
Returns a new instance of CustomMethods.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rex/post/hwbridge/extensions/custom_methods/custom_methods.rb', line 32 def initialize(client) super(client, 'automotive') # Alias the following things on the client object so that they # can be directly referenced client.register_extension_aliases( [ { 'name' => 'custom_methods', 'ext' => self } ]) end |
Class Method Details
.command_name(path) ⇒ String?
Returns The console command, or nil if the route is unsafe.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rex/post/hwbridge/extensions/custom_methods/custom_methods.rb', line 20 def self.command_name(path) return unless path.is_a?(String) && path.ascii_only? return unless path.match?(%r{\A/?(?:[a-zA-Z0-9_~.-]+/)*[a-zA-Z_][a-zA-Z0-9_]*\z}) # Dot segments can escape the configured bridge path. segments = path.split('/') return if segments.any? { |segment| segment == '.' || segment == '..' } segments.last end |
Instance Method Details
#convert_value(type, value) ⇒ Object (protected)
Converts a value returned by the hardware to the type the method declared. A value that cannot be converted to that type is returned unchanged.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rex/post/hwbridge/extensions/custom_methods/custom_methods.rb', line 106 def convert_value(type, value) case type when "int" value.respond_to?(:to_i) ? value.to_i : value when "hex" value.is_a?(Integer) ? "0x" + value.to_s(16) : value when "boolean" value == "true" when "float" value.respond_to?(:to_f) ? value.to_f : value else value end end |
#send_request(cmd, args, methods) ⇒ Hash
Converts a command and its arguments to a request to the hardware device.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rex/post/hwbridge/extensions/custom_methods/custom_methods.rb', line 54 def send_request(cmd, args, methods) return { 'success' => false } unless cmd.is_a?(String) && methods.is_a?(Array) matches = methods.count { |meth| meth.is_a?(Hash) && self.class.command_name(meth['method_name']) == cmd } return { 'success' => false } if matches.zero? return { 'success' => false, 'status' => 'Ambiguous custom method name' } if matches > 1 arguments = "" if args.size > 0 arguments = "?" first = true args.each do |arg| arguments += "&" if not first arguments += URI.encode_www_form([arg.split('=', 2)]) first = false end end resp = { "success" => false } methods.each do |meth| if meth.is_a?(Hash) && self.class.command_name(meth['method_name']) == cmd resp = client.send_request("#{meth["method_name"]}#{arguments}") # The reply is whatever the hardware sent back: it can be missing # (nil) or valid JSON that is not an object. unless resp.is_a?(Hash) resp = { "success" => false, "status" => "Unexpected or missing reply from the hardware bridge" } next end if resp.has_key? "value" and meth.has_key? "return" case meth["return"] when "nil" print_warning("A return was given when none was expected") else resp["value"] = convert_value(meth["return"], resp["value"]) end end end end resp end |