Class: Msf::RPC::JSON::Request

Inherits:
Object
  • Object
show all
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

Notification

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

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.

Parameters:

  • uri (URI::HTTP)

    the JSON-RPC service URI

  • api_token (String) (defaults to: nil)

    the API token. Default: nil

  • method (String)

    the JSON-RPC method name.

  • params (Array, Hash) (defaults to: nil)

    the JSON-RPC method parameters. Default: nil

  • namespace (String) (defaults to: nil)

    the namespace for the JSON-RPC method. The namespace will be prepended to the method name with a period separator. Default: nil

  • symbolize_names (Boolean) (defaults to: true)

    If true, symbols are used for the names (keys) when processing JSON objects; otherwise, strings are used. Default: true

  • is_notification (Boolean) (defaults to: false)

    If true, the request is created as a notification; otherwise, a standard request. Default: false

  • private_key_file (String) (defaults to: nil)

    the SSL private key file used for the HTTPS request. Default: nil

  • cert_chain_file (String) (defaults to: nil)

    the SSL cert chain file used for the HTTPS request. Default: nil

  • verify_peer (Boolean) (defaults to: nil)

    indicates whether a server should request a certificate from a peer, to be verified by user code. Default: nil



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)
    ssl_options = {}
    ssl_options[:private_key_file] = private_key_file unless private_key_file.nil?
    ssl_options[:cert_chain_file] = cert_chain_file unless cert_chain_file.nil?
    ssl_options[:verify_peer] = verify_peer if verify_peer.is_a?(TrueClass) || verify_peer.is_a?(FalseClass)
    @request_options[:ssl] = ssl_options
  end
end

Instance Method Details

#sendObject

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