Class: Msf::RPC::JSON::Request
- Inherits:
-
Object
- Object
- Msf::RPC::JSON::Request
- Includes:
- EM::Deferrable
- Defined in:
- lib/msf/core/rpc/json/request.rb
Overview
Represents a JSON-RPC request. This is an EM::Deferrable class and instances respond to #callback and #errback to store callback actions.
Direct Known Subclasses
Constant Summary collapse
- JSON_MEDIA_TYPE =
'application/json'
- JSON_RPC_VERSION =
'2.0'
- JSON_RPC_RESPONSE_REQUIRED_MEMBERS =
%i(jsonrpc id)
- JSON_RPC_RESPONSE_MEMBER_TYPES =
{ # A String specifying the version of the JSON-RPC protocol. jsonrpc: [String], # An identifier established by the Client that MUST contain a String, # Number, or NULL value if included. If it is not included it is assumed # to be a notification. The value SHOULD normally not be Null [1] and # Numbers SHOULD NOT contain fractional parts [2] id: [Integer, String, NilClass], }
- JSON_RPC_ERROR_RESPONSE_REQUIRED_MEMBERS =
%i(code message)
- JSON_RPC_ERROR_RESPONSE_MEMBER_TYPES =
{ # A Number that indicates the error type that occurred. # This MUST be an integer. code: [Integer], # A String providing a short description of the error. # The message SHOULD be limited to a concise single sentence. message: [String] }
Instance Method Summary collapse
-
#initialize(uri, api_token: nil, method:, params: nil, namespace: nil, symbolize_names: true, is_notification: false, private_key_file: nil, cert_chain_file: nil, verify_peer: nil) ⇒ Request
constructor
Instantiate a Request.
-
#send ⇒ Object
Sends the JSON-RPC request using an EM::HttpRequest object, then validates and processes the JSON-RPC response.
Constructor Details
#initialize(uri, api_token: nil, method:, params: nil, namespace: nil, symbolize_names: true, is_notification: false, private_key_file: nil, cert_chain_file: nil, verify_peer: nil) ⇒ Request
Instantiate a Request.
49 50 51 52 53 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 |
# File 'lib/msf/core/rpc/json/request.rb', line 49 def initialize(uri, api_token: nil, method:, params: nil, namespace: nil, symbolize_names: true, is_notification: false, private_key_file: nil, cert_chain_file: nil, verify_peer: nil) @uri = uri @api_token = api_token @namespace = namespace @symbolize_names = symbolize_names @is_notification = is_notification @headers = { Accept: JSON_MEDIA_TYPE, 'Content-Type': JSON_MEDIA_TYPE, Authorization: "Bearer #{@api_token}" } absolute_method_name = @namespace.nil? ? method : "#{@namespace}.#{method}" request_msg = { jsonrpc: JSON_RPC_VERSION, method: absolute_method_name } request_msg[:id] = Request.generate_id unless is_notification request_msg[:params] = params unless params.nil? @request_options = { head: @headers, body: request_msg.to_json } # add SSL options if specified if !private_key_file.nil? || !cert_chain_file.nil? || verify_peer.is_a?(TrueClass) || verify_peer.is_a?(FalseClass) = {} [:private_key_file] = private_key_file unless private_key_file.nil? [:cert_chain_file] = cert_chain_file unless cert_chain_file.nil? [:verify_peer] = verify_peer if verify_peer.is_a?(TrueClass) || verify_peer.is_a?(FalseClass) @request_options[:ssl] = end end |
Instance Method Details
#send ⇒ Object
Sends the JSON-RPC request using an EM::HttpRequest object, then validates and processes the JSON-RPC response.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/msf/core/rpc/json/request.rb', line 89 def send http = EM::HttpRequest.new(@uri).post(@request_options) http.callback do process(http.response) end http.errback do fail(http.error) end end |