HTTP subsystem refactoring.
This commit is contained in:
parent
5486300db7
commit
cad5fef1ea
25 changed files with 474 additions and 216 deletions
|
@ -30,20 +30,14 @@
|
|||
#include "base/io/json/JsonRequest.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/kernel/interfaces/IClientListener.h"
|
||||
#include "base/net/http/HttpClient.h"
|
||||
#include "base/net/http/Fetch.h"
|
||||
#include "base/net/http/HttpData.h"
|
||||
#include "base/net/stratum/SubmitResult.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
#include "base/tools/Timer.h"
|
||||
#include "net/JobResult.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/error/en.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_TLS
|
||||
# include "base/net/http/HttpsClient.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
|
@ -65,8 +59,7 @@ static const size_t BlobReserveSize = 8;
|
|||
|
||||
|
||||
xmrig::DaemonClient::DaemonClient(int id, IClientListener *listener) :
|
||||
BaseClient(id, listener),
|
||||
m_apiVersion(API_MONERO)
|
||||
BaseClient(id, listener)
|
||||
{
|
||||
m_httpListener = std::make_shared<HttpListener>(this);
|
||||
m_timer = new Timer(this);
|
||||
|
@ -133,9 +126,7 @@ int64_t xmrig::DaemonClient::submit(const JobResult &result)
|
|||
m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff(), 0, result.backend);
|
||||
# endif
|
||||
|
||||
send(HTTP_POST, kJsonRPC, doc);
|
||||
|
||||
return m_sequence++;
|
||||
return rpcSend(doc);
|
||||
}
|
||||
|
||||
|
||||
|
@ -163,15 +154,11 @@ void xmrig::DaemonClient::onHttpData(const HttpData &data)
|
|||
return retry();
|
||||
}
|
||||
|
||||
LOG_DEBUG("[%s:%d] received (%d bytes): \"%.*s\"", m_pool.host().data(), m_pool.port(), static_cast<int>(data.body.size()), static_cast<int>(data.body.size()), data.body.c_str());
|
||||
|
||||
m_ip = static_cast<const HttpContext &>(data).ip().c_str();
|
||||
m_ip = data.ip().c_str();
|
||||
|
||||
# ifdef XMRIG_FEATURE_TLS
|
||||
if (isTLS()) {
|
||||
m_tlsVersion = static_cast<const HttpsClient &>(data).version();
|
||||
m_tlsFingerprint = static_cast<const HttpsClient &>(data).fingerprint();
|
||||
}
|
||||
m_tlsVersion = data.tlsVersion();
|
||||
m_tlsFingerprint = data.tlsFingerprint();
|
||||
# endif
|
||||
|
||||
rapidjson::Document doc;
|
||||
|
@ -188,7 +175,7 @@ void xmrig::DaemonClient::onHttpData(const HttpData &data)
|
|||
if (!doc.HasMember(kHash)) {
|
||||
m_apiVersion = API_CRYPTONOTE_DEFAULT;
|
||||
|
||||
return send(HTTP_GET, kGetInfo);
|
||||
return send(kGetInfo);
|
||||
}
|
||||
|
||||
if (isOutdated(Json::getUint64(doc, kHeight), Json::getString(doc, kHash))) {
|
||||
|
@ -215,19 +202,10 @@ void xmrig::DaemonClient::onTimer(const Timer *)
|
|||
}
|
||||
else if (m_state == ConnectedState) {
|
||||
if (m_apiVersion == API_DERO) {
|
||||
using namespace rapidjson;
|
||||
Document doc(kObjectType);
|
||||
auto& allocator = doc.GetAllocator();
|
||||
|
||||
doc.AddMember("id", m_sequence, allocator);
|
||||
doc.AddMember("jsonrpc", "2.0", allocator);
|
||||
doc.AddMember("method", "get_info", allocator);
|
||||
|
||||
send(HTTP_POST, kJsonRPC, doc);
|
||||
++m_sequence;
|
||||
rpcSend(JsonRequest::create(m_sequence, "get_info"));
|
||||
}
|
||||
else {
|
||||
send(HTTP_GET, (m_apiVersion == API_MONERO) ? kGetHeight : kGetInfo);
|
||||
send((m_apiVersion == API_MONERO) ? kGetHeight : kGetInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -344,7 +322,14 @@ int64_t xmrig::DaemonClient::getBlockTemplate()
|
|||
|
||||
JsonRequest::create(doc, m_sequence, "getblocktemplate", params);
|
||||
|
||||
send(HTTP_POST, kJsonRPC, doc);
|
||||
return rpcSend(doc);
|
||||
}
|
||||
|
||||
|
||||
int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc)
|
||||
{
|
||||
FetchRequest req(HTTP_POST, m_pool, kJsonRPC, doc, isQuiet());
|
||||
fetch(std::move(req), m_httpListener);
|
||||
|
||||
return m_sequence++;
|
||||
}
|
||||
|
@ -368,46 +353,10 @@ void xmrig::DaemonClient::retry()
|
|||
}
|
||||
|
||||
|
||||
void xmrig::DaemonClient::send(int method, const char *url, const char *data, size_t size)
|
||||
void xmrig::DaemonClient::send(const char *path)
|
||||
{
|
||||
LOG_DEBUG("[%s:%d] " MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD_S " send (%zu bytes): \"%.*s\"",
|
||||
m_pool.host().data(),
|
||||
m_pool.port(),
|
||||
http_method_str(static_cast<http_method>(method)),
|
||||
url,
|
||||
size,
|
||||
static_cast<int>(size),
|
||||
data);
|
||||
|
||||
HttpClient *client;
|
||||
# ifdef XMRIG_FEATURE_TLS
|
||||
if (m_pool.isTLS()) {
|
||||
client = new HttpsClient(method, url, m_httpListener, data, size, m_pool.fingerprint());
|
||||
}
|
||||
else
|
||||
# endif
|
||||
{
|
||||
client = new HttpClient(method, url, m_httpListener, data, size);
|
||||
}
|
||||
|
||||
client->setQuiet(isQuiet());
|
||||
client->connect(m_pool.host(), m_pool.port());
|
||||
|
||||
if (method != HTTP_GET) {
|
||||
client->headers.insert({ "Content-Type", "application/json" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DaemonClient::send(int method, const char *url, const rapidjson::Document &doc)
|
||||
{
|
||||
using namespace rapidjson;
|
||||
|
||||
StringBuffer buffer(nullptr, 512);
|
||||
Writer<StringBuffer> writer(buffer);
|
||||
doc.Accept(writer);
|
||||
|
||||
send(method, url, buffer.GetString(), buffer.GetSize());
|
||||
FetchRequest req(HTTP_GET, m_pool, path, isQuiet());
|
||||
fetch(std::move(req), m_httpListener);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -71,16 +71,16 @@ private:
|
|||
bool parseJob(const rapidjson::Value ¶ms, int *code);
|
||||
bool parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error);
|
||||
int64_t getBlockTemplate();
|
||||
int64_t rpcSend(const rapidjson::Document &doc);
|
||||
void retry();
|
||||
void send(int method, const char *url, const char *data = nullptr, size_t size = 0);
|
||||
void send(int method, const char *url, const rapidjson::Document &doc);
|
||||
void send(const char *path);
|
||||
void setState(SocketState state);
|
||||
|
||||
enum {
|
||||
API_CRYPTONOTE_DEFAULT,
|
||||
API_MONERO,
|
||||
API_DERO,
|
||||
} m_apiVersion;
|
||||
} m_apiVersion = API_MONERO;
|
||||
|
||||
std::shared_ptr<IHttpListener> m_httpListener;
|
||||
String m_blocktemplate;
|
||||
|
|
|
@ -29,17 +29,11 @@
|
|||
#include "base/io/json/Json.h"
|
||||
#include "base/io/json/JsonRequest.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/net/http/HttpClient.h"
|
||||
#include "base/net/http/Fetch.h"
|
||||
#include "base/net/http/HttpData.h"
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/error/en.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_TLS
|
||||
# include "base/net/http/HttpsClient.h"
|
||||
#endif
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
@ -159,7 +153,8 @@ void xmrig::SelfSelectClient::getBlockTemplate()
|
|||
|
||||
JsonRequest::create(doc, m_sequence++, "getblocktemplate", params);
|
||||
|
||||
send(HTTP_POST, "/json_rpc", doc);
|
||||
FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet());
|
||||
fetch(std::move(req), m_httpListener);
|
||||
}
|
||||
|
||||
|
||||
|
@ -169,44 +164,6 @@ void xmrig::SelfSelectClient::retry()
|
|||
}
|
||||
|
||||
|
||||
void xmrig::SelfSelectClient::send(int method, const char *url, const char *data, size_t size)
|
||||
{
|
||||
LOG_DEBUG("[%s] " MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD_S " send (%zu bytes): \"%.*s\"",
|
||||
pool().daemon().url().data(),
|
||||
http_method_str(static_cast<http_method>(method)),
|
||||
url,
|
||||
size,
|
||||
static_cast<int>(size),
|
||||
data);
|
||||
|
||||
HttpClient *client;
|
||||
# ifdef XMRIG_FEATURE_TLS
|
||||
if (pool().daemon().isTLS()) {
|
||||
client = new HttpsClient(method, url, m_httpListener, data, size, String());
|
||||
}
|
||||
else
|
||||
# endif
|
||||
{
|
||||
client = new HttpClient(method, url, m_httpListener, data, size);
|
||||
}
|
||||
|
||||
client->setQuiet(isQuiet());
|
||||
client->connect(pool().daemon().host(), pool().daemon().port());
|
||||
}
|
||||
|
||||
|
||||
void xmrig::SelfSelectClient::send(int method, const char *url, const rapidjson::Document &doc)
|
||||
{
|
||||
using namespace rapidjson;
|
||||
|
||||
StringBuffer buffer(nullptr, 512);
|
||||
Writer<StringBuffer> writer(buffer);
|
||||
doc.Accept(writer);
|
||||
|
||||
send(method, url, buffer.GetString(), buffer.GetSize());
|
||||
}
|
||||
|
||||
|
||||
void xmrig::SelfSelectClient::setState(State state)
|
||||
{
|
||||
if (m_state == state) {
|
||||
|
@ -256,7 +213,7 @@ void xmrig::SelfSelectClient::submitBlockTemplate(rapidjson::Value &result)
|
|||
|
||||
JsonRequest::create(doc, sequence(), "block_template", params);
|
||||
|
||||
send(doc, [this](const rapidjson::Value &result, bool success, uint64_t elapsed) {
|
||||
send(doc, [this](const rapidjson::Value &result, bool success, uint64_t) {
|
||||
if (!success) {
|
||||
if (!isQuiet()) {
|
||||
LOG_ERR("[%s] error: " RED_BOLD("\"%s\"") RED_S ", code: %d", pool().daemon().url().data(), Json::getString(result, "message"), Json::getInt(result, "code"));
|
||||
|
@ -285,8 +242,6 @@ void xmrig::SelfSelectClient::onHttpData(const HttpData &data)
|
|||
return retry();
|
||||
}
|
||||
|
||||
LOG_DEBUG("[%s] received (%d bytes): \"%.*s\"", pool().daemon().url().data(), static_cast<int>(data.body.size()), static_cast<int>(data.body.size()), data.body.c_str());
|
||||
|
||||
rapidjson::Document doc;
|
||||
if (doc.Parse(data.body.c_str()).HasParseError()) {
|
||||
if (!isQuiet()) {
|
||||
|
|
|
@ -102,8 +102,6 @@ private:
|
|||
bool parseResponse(int64_t id, rapidjson::Value &result, const rapidjson::Value &error);
|
||||
void getBlockTemplate();
|
||||
void retry();
|
||||
void send(int method, const char *url, const char *data = nullptr, size_t size = 0);
|
||||
void send(int method, const char *url, const rapidjson::Document &doc);
|
||||
void setState(State state);
|
||||
void submitBlockTemplate(rapidjson::Value &result);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue