2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 12:06:41 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/jsonrpc.hpp"
|
|
|
|
#include "base/netstring.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2017-09-07 14:37:02 +02:00
|
|
|
#include "base/console.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2019-02-18 15:23:41 +01:00
|
|
|
#include "base/tlsstream.hpp"
|
2017-10-02 13:49:42 +02:00
|
|
|
#include <iostream>
|
2019-02-18 15:23:41 +01:00
|
|
|
#include <memory>
|
2019-02-19 11:29:45 +01:00
|
|
|
#include <utility>
|
2019-02-18 15:23:41 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2017-09-07 14:37:02 +02:00
|
|
|
#ifdef I2_DEBUG
|
2019-05-23 17:25:56 +02:00
|
|
|
/**
|
|
|
|
* Determine whether the developer wants to see raw JSON messages.
|
|
|
|
*
|
|
|
|
* @return Internal.DebugJsonRpc boolean
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
static bool GetDebugJsonRpcCached()
|
2017-09-07 14:37:02 +02:00
|
|
|
{
|
|
|
|
static int debugJsonRpc = -1;
|
|
|
|
|
|
|
|
if (debugJsonRpc != -1)
|
|
|
|
return debugJsonRpc;
|
|
|
|
|
|
|
|
debugJsonRpc = false;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
Namespace::Ptr internal = ScriptGlobal::Get("Internal", &Empty);
|
2017-09-07 14:37:02 +02:00
|
|
|
|
|
|
|
if (!internal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value vdebug;
|
|
|
|
|
|
|
|
if (!internal->Get("DebugJsonRpc", &vdebug))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
debugJsonRpc = Convert::ToLong(vdebug);
|
|
|
|
|
|
|
|
return debugJsonRpc;
|
|
|
|
}
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2019-02-25 18:15:47 +01:00
|
|
|
/**
|
|
|
|
* Sends a message to the connected peer and returns the bytes sent.
|
|
|
|
*
|
|
|
|
* @param message The message.
|
|
|
|
*
|
|
|
|
* @return The amount of bytes sent.
|
|
|
|
*/
|
2019-07-25 14:34:29 +02:00
|
|
|
size_t JsonRpc::SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message)
|
2019-02-25 18:15:47 +01:00
|
|
|
{
|
|
|
|
String json = JsonEncode(message);
|
|
|
|
|
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (GetDebugJsonRpcCached())
|
|
|
|
std::cerr << ConsoleColorTag(Console_ForegroundBlue) << ">> " << json << ConsoleColorTag(Console_Normal) << "\n";
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
|
|
|
return NetString::WriteStringToStream(stream, json);
|
|
|
|
}
|
|
|
|
|
2019-02-18 15:23:41 +01:00
|
|
|
/**
|
|
|
|
* Sends a message to the connected peer and returns the bytes sent.
|
|
|
|
*
|
|
|
|
* @param message The message.
|
|
|
|
*
|
|
|
|
* @return The amount of bytes sent.
|
|
|
|
*/
|
2019-07-25 14:34:29 +02:00
|
|
|
size_t JsonRpc::SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc)
|
2019-02-18 15:23:41 +01:00
|
|
|
{
|
2019-02-26 11:13:34 +01:00
|
|
|
return JsonRpc::SendRawMessage(stream, JsonEncode(message), yc);
|
|
|
|
}
|
2019-02-18 15:23:41 +01:00
|
|
|
|
2019-05-23 17:25:56 +02:00
|
|
|
/**
|
|
|
|
* Sends a raw message to the connected peer.
|
|
|
|
*
|
|
|
|
* @param stream ASIO TLS Stream
|
|
|
|
* @param json message
|
|
|
|
* @param yc Yield context required for ASIO
|
|
|
|
*
|
|
|
|
* @return bytes sent
|
|
|
|
*/
|
2019-07-25 14:34:29 +02:00
|
|
|
size_t JsonRpc::SendRawMessage(const Shared<AsioTlsStream>::Ptr& stream, const String& json, boost::asio::yield_context yc)
|
2019-02-26 11:13:34 +01:00
|
|
|
{
|
2019-02-18 15:23:41 +01:00
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (GetDebugJsonRpcCached())
|
|
|
|
std::cerr << ConsoleColorTag(Console_ForegroundBlue) << ">> " << json << ConsoleColorTag(Console_Normal) << "\n";
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
|
|
|
return NetString::WriteStringToStream(stream, json, yc);
|
|
|
|
}
|
|
|
|
|
2019-05-23 17:25:56 +02:00
|
|
|
/**
|
|
|
|
* Reads a message from the connected peer.
|
|
|
|
*
|
|
|
|
* @param stream ASIO TLS Stream
|
|
|
|
* @param maxMessageLength maximum size of bytes read.
|
|
|
|
*
|
|
|
|
* @return A JSON string
|
|
|
|
*/
|
2016-01-27 16:43:23 +01:00
|
|
|
|
2019-07-25 14:34:29 +02:00
|
|
|
String JsonRpc::ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, ssize_t maxMessageLength)
|
2019-02-25 18:15:47 +01:00
|
|
|
{
|
|
|
|
String jsonString = NetString::ReadStringFromStream(stream, maxMessageLength);
|
|
|
|
|
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (GetDebugJsonRpcCached())
|
|
|
|
std::cerr << ConsoleColorTag(Console_ForegroundBlue) << "<< " << jsonString << ConsoleColorTag(Console_Normal) << "\n";
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2022-02-22 17:47:54 +01:00
|
|
|
return jsonString;
|
2019-02-25 18:15:47 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:25:56 +02:00
|
|
|
/**
|
|
|
|
* Reads a message from the connected peer.
|
|
|
|
*
|
|
|
|
* @param stream ASIO TLS Stream
|
|
|
|
* @param yc Yield Context for ASIO
|
|
|
|
* @param maxMessageLength maximum size of bytes read.
|
|
|
|
*
|
|
|
|
* @return A JSON string
|
|
|
|
*/
|
2019-07-25 14:34:29 +02:00
|
|
|
String JsonRpc::ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, boost::asio::yield_context yc, ssize_t maxMessageLength)
|
2019-02-19 11:29:45 +01:00
|
|
|
{
|
|
|
|
String jsonString = NetString::ReadStringFromStream(stream, yc, maxMessageLength);
|
|
|
|
|
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (GetDebugJsonRpcCached())
|
|
|
|
std::cerr << ConsoleColorTag(Console_ForegroundBlue) << "<< " << jsonString << ConsoleColorTag(Console_Normal) << "\n";
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2022-02-22 17:47:54 +01:00
|
|
|
return jsonString;
|
2019-02-19 11:29:45 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:25:56 +02:00
|
|
|
/**
|
|
|
|
* Decode message, enforce a Dictionary
|
|
|
|
*
|
|
|
|
* @param message JSON string
|
|
|
|
*
|
|
|
|
* @return Dictionary ptr
|
|
|
|
*/
|
2016-01-27 16:43:23 +01:00
|
|
|
Dictionary::Ptr JsonRpc::DecodeMessage(const String& message)
|
|
|
|
{
|
|
|
|
Value value = JsonDecode(message);
|
2012-07-24 10:50:53 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (!value.IsObjectType<Dictionary>()) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("JSON-RPC"
|
2017-12-19 15:50:05 +01:00
|
|
|
" message must be a dictionary."));
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2016-01-27 16:43:23 +01:00
|
|
|
return value;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|