HTTP subsystem refactoring.

This commit is contained in:
XMRig 2020-03-06 12:38:09 +07:00
parent 5486300db7
commit cad5fef1ea
No known key found for this signature in database
GPG key ID: 446A53638BE94409
25 changed files with 474 additions and 216 deletions

View file

@ -38,7 +38,6 @@
namespace xmrig {
static const char *kAuthorization = "authorization";
static const char *kContentType = "content-type";
#ifdef _WIN32
static const char *favicon = nullptr;
@ -138,7 +137,7 @@ void xmrig::Httpd::onHttpData(const HttpData &data)
# ifdef _WIN32
if (favicon != nullptr) {
HttpResponse response(data.id());
response.setHeader("Content-Type", "image/x-icon");
response.setHeader(HttpData::kContentType, "image/x-icon");
return response.end(favicon, faviconSize);
}
@ -161,7 +160,7 @@ void xmrig::Httpd::onHttpData(const HttpData &data)
return HttpApiResponse(data.id(), HTTP_STATUS_FORBIDDEN).end();
}
if (!data.headers.count(kContentType) || data.headers.at(kContentType) != "application/json") {
if (!data.headers.count(HttpData::kContentTypeL) || data.headers.at(HttpData::kContentTypeL) != HttpData::kApplicationJson) {
return HttpApiResponse(data.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end();
}
}

View file

@ -34,6 +34,7 @@ set(HEADERS_BASE
src/base/kernel/Signals.h
src/base/net/dns/Dns.h
src/base/net/dns/DnsRecord.h
src/base/net/http/Fetch.h
src/base/net/http/Http.h
src/base/net/http/HttpListener.h
src/base/net/stratum/BaseClient.h
@ -80,7 +81,10 @@ set(SOURCES_BASE
src/base/kernel/Signals.cpp
src/base/net/dns/Dns.cpp
src/base/net/dns/DnsRecord.cpp
src/base/net/http/Fetch.cpp
src/base/net/http/Http.cpp
src/base/net/http/HttpData.cpp
src/base/net/http/HttpListener.cpp
src/base/net/stratum/BaseClient.cpp
src/base/net/stratum/Client.cpp
src/base/net/stratum/Job.cpp

View file

@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,12 +27,40 @@
#include "rapidjson/document.h"
void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params)
namespace xmrig {
static const char *k2_0 = "2.0";
static const char *kId = "id";
static const char *kJsonRPC = "jsonrpc";
static const char *kMethod = "method";
const char *JsonRequest::kParams = "params";
} // namespace xmrig
rapidjson::Document xmrig::JsonRequest::create(int64_t id, const char *method)
{
using namespace rapidjson;
Document doc(kObjectType);
auto &allocator = doc.GetAllocator();
doc.AddMember("id", id, allocator);
doc.AddMember("jsonrpc", "2.0", allocator);
doc.AddMember("method", rapidjson::StringRef(method), allocator);
doc.AddMember("params", params, allocator);
doc.AddMember(StringRef(kId), id, allocator);
doc.AddMember(StringRef(kJsonRPC), StringRef(k2_0), allocator);
doc.AddMember(StringRef(kMethod), StringRef(method), allocator);
return doc;
}
void xmrig::JsonRequest::create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params)
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
doc.AddMember(StringRef(kId), id, allocator);
doc.AddMember(StringRef(kJsonRPC), StringRef(k2_0), allocator);
doc.AddMember(StringRef(kMethod), StringRef(method), allocator);
doc.AddMember(StringRef(kParams), params, allocator);
}

View file

@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -35,6 +35,9 @@ namespace xmrig {
class JsonRequest
{
public:
static const char *kParams;
static rapidjson::Document create(int64_t id, const char *method);
static void create(rapidjson::Document &doc, int64_t id, const char *method, rapidjson::Value &params);
};

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -189,7 +189,7 @@ private:
}
char m_buf[4096]{};
char m_buf[Log::kMaxBufferSize]{};
std::mutex m_mutex;
std::vector<ILogBackend*> m_backends;
};

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2019 Spudz76 <https://github.com/Spudz76>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -52,6 +52,8 @@ public:
DEBUG, // debug-level messages
};
constexpr static size_t kMaxBufferSize = 16384;
static void add(ILogBackend *backend);
static void destroy();
static void print(const char *fmt, ...);

155
src/base/net/http/Fetch.cpp Normal file
View file

@ -0,0 +1,155 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/http/Fetch.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpClient.h"
#include "base/net/stratum/Pool.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#ifdef XMRIG_FEATURE_TLS
# include "base/net/http/HttpsClient.h"
#endif
xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16_t port, const String &path, bool tls, bool quiet, const char *data, size_t size, const char *contentType) :
quiet(quiet),
tls(tls),
method(method),
host(host),
path(path),
port(port)
{
assert(port > 0);
setBody(data, size, contentType);
}
xmrig::FetchRequest::FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Document &doc, bool tls, bool quiet) :
quiet(quiet),
tls(tls),
method(method),
host(host),
path(path),
port(port)
{
assert(port > 0);
setBody(doc);
}
xmrig::FetchRequest::FetchRequest(int method, const Pool &pool, const String &path, bool quiet, const char *data, size_t size, const char *contentType) :
quiet(quiet),
tls(pool.isTLS()),
method(static_cast<http_method>(method)),
fingerprint(pool.fingerprint()),
host(pool.host()),
path(path),
port(pool.port())
{
assert(pool.isValid());
setBody(data, size, contentType);
}
xmrig::FetchRequest::FetchRequest(int method, const Pool &pool, const String &path, const rapidjson::Document &doc, bool quiet) :
quiet(quiet),
tls(pool.isTLS()),
method(static_cast<http_method>(method)),
fingerprint(pool.fingerprint()),
host(pool.host()),
path(path),
port(pool.port())
{
assert(pool.isValid());
setBody(doc);
}
void xmrig::FetchRequest::setBody(const char *data, size_t size, const char *contentType)
{
if (!data) {
return;
}
assert(method != HTTP_GET && method != HTTP_HEAD);
if (method == HTTP_GET || method == HTTP_HEAD) {
return;
}
body = size ? std::string(data, size) : data;
if (contentType) {
headers.insert({ HttpData::kContentType, contentType });
}
}
void xmrig::FetchRequest::setBody(const rapidjson::Document &doc)
{
assert(method != HTTP_GET && method != HTTP_HEAD);
if (method == HTTP_GET || method == HTTP_HEAD) {
return;
}
using namespace rapidjson;
StringBuffer buffer(nullptr, 512);
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
setBody(buffer.GetString(), buffer.GetSize(), HttpData::kApplicationJson.c_str());
}
void xmrig::fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type)
{
# ifdef APP_DEBUG
LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD(" body: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"),
req.tls ? "s" : "", req.host.data(), req.port, http_method_str(req.method), req.path.data(), req.body.size());
if (req.hasBody() && req.body.size() < (Log::kMaxBufferSize - 1024) && req.headers.count(HttpData::kContentType) && req.headers.at(HttpData::kContentType) == HttpData::kApplicationJson) {
Log::print(BLUE_BG_BOLD("%s:") BLACK_BOLD_S " %.*s", req.headers.at(HttpData::kContentType).c_str(), static_cast<int>(req.body.size()), req.body.c_str());
}
# endif
HttpClient *client;
# ifdef XMRIG_FEATURE_TLS
if (req.tls) {
client = new HttpsClient(std::move(req), listener);
}
else
# endif
{
client = new HttpClient(std::move(req), listener);
}
client->userType = type;
client->connect();
}

74
src/base/net/http/Fetch.h Normal file
View file

@ -0,0 +1,74 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_FETCH_H
#define XMRIG_FETCH_H
#include "3rdparty/http-parser/http_parser.h"
#include "base/tools/String.h"
#include "rapidjson/fwd.h"
#include <map>
#include <memory>
#include <string>
namespace xmrig {
class IHttpListener;
class Pool;
class FetchRequest
{
public:
FetchRequest() = default;
FetchRequest(http_method method, const String &host, uint16_t port, const String &path, bool tls = false, bool quiet = false, const char *data = nullptr, size_t size = 0, const char *contentType = nullptr);
FetchRequest(http_method method, const String &host, uint16_t port, const String &path, const rapidjson::Document &doc, bool tls = false, bool quiet = false);
FetchRequest(int method, const Pool &pool, const String &path, bool quiet = false, const char *data = nullptr, size_t size = 0, const char *contentType = nullptr);
FetchRequest(int method, const Pool &pool, const String &path, const rapidjson::Document &doc, bool quiet = false);
void setBody(const char *data, size_t size, const char *contentType = nullptr);
void setBody(const rapidjson::Document &doc);
inline bool hasBody() const { return method != HTTP_GET && method != HTTP_HEAD && !body.empty(); }
bool quiet = false;
bool tls = false;
http_method method = HTTP_GET;
std::map<const std::string, const std::string> headers;
std::string body;
String fingerprint;
String host;
String path;
uint16_t port = 0;
};
void fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type = 0);
} // namespace xmrig
#endif // XMRIG_FETCH_H

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -24,8 +24,9 @@
*/
#include "3rdparty/http-parser/http_parser.h"
#include "base/net/http/HttpApiResponse.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/net/http/HttpData.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
@ -75,7 +76,7 @@ void xmrig::HttpApiResponse::end()
return HttpResponse::end();
}
setHeader("Content-Type", "application/json");
setHeader(HttpData::kContentType, HttpData::kApplicationJson);
StringBuffer buffer(nullptr, 4096);
PrettyWriter<StringBuffer> writer(buffer);

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View file

@ -69,17 +69,15 @@ private:
} // namespace xmrig
xmrig::HttpClient::HttpClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size) :
HttpContext(HTTP_RESPONSE, listener)
xmrig::HttpClient::HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpContext(HTTP_RESPONSE, listener),
m_req(std::move(req))
{
this->method = method;
this->url = url;
if (data) {
body = size ? std::string(data, size) : data;
}
m_dns = new Dns(this);
method = m_req.method;
url = std::move(m_req.path);
body = std::move(m_req.body);
headers = std::move(m_req.headers);
m_dns = new Dns(this);
}
@ -89,17 +87,9 @@ xmrig::HttpClient::~HttpClient()
}
bool xmrig::HttpClient::connect(const String &host, uint16_t port)
bool xmrig::HttpClient::connect()
{
m_port = port;
return m_dns->resolve(host);
}
const xmrig::String &xmrig::HttpClient::host() const
{
return m_dns->host();
return m_dns->resolve(m_req.host);
}
@ -108,14 +98,14 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
this->status = status;
if (status < 0 && dns.isEmpty()) {
if (!m_quiet) {
LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), m_port, uv_strerror(status));
if (!isQuiet()) {
LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), port(), uv_strerror(status));
}
return;
}
sockaddr *addr = dns.get().addr(m_port);
sockaddr *addr = dns.get().addr(port());
auto req = new uv_connect_t;
req->data = this;
@ -128,7 +118,7 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
void xmrig::HttpClient::handshake()
{
headers.insert({ "Host", m_dns->host().data() });
headers.insert({ "Host", host() });
headers.insert({ "Connection", "close" });
headers.insert({ "User-Agent", Platform::userAgent() });
@ -175,8 +165,8 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
}
if (status < 0) {
if (!client->m_quiet) {
LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->m_port, uv_strerror(status));
if (!client->isQuiet()) {
LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(status));
}
delete req;
@ -197,8 +187,8 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
if (nread >= 0) {
client->read(buf->base, static_cast<size_t>(nread));
} else {
if (!client->m_quiet && nread != UV_EOF) {
LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->m_port, uv_strerror(static_cast<int>(nread)));
if (!client->isQuiet() && nread != UV_EOF) {
LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(static_cast<int>(nread)));
}
client->close(static_cast<int>(nread));

View file

@ -28,8 +28,9 @@
#define XMRIG_HTTPCLIENT_H
#include "base/net/http/HttpContext.h"
#include "base/kernel/interfaces/IDnsListener.h"
#include "base/net/http/Fetch.h"
#include "base/net/http/HttpContext.h"
#include "base/tools/Object.h"
@ -44,14 +45,14 @@ class HttpClient : public HttpContext, public IDnsListener
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient);
HttpClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data = nullptr, size_t size = 0);
HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpClient() override;
inline uint16_t port() const { return m_port; }
inline void setQuiet(bool quiet) { m_quiet = quiet; }
inline bool isQuiet() const { return m_req.quiet; }
inline const char *host() const override { return m_req.host; }
inline uint16_t port() const override { return m_req.port; }
bool connect(const String &host, uint16_t port);
const String &host() const;
bool connect();
protected:
void onResolved(const Dns &dns, int status) override;
@ -60,13 +61,14 @@ protected:
virtual void read(const char *data, size_t size);
virtual void write(const std::string &header);
bool m_quiet = false;
protected:
inline const FetchRequest &req() const { return m_req; }
private:
static void onConnect(uv_connect_t *req, int status);
Dns *m_dns;
uint16_t m_port = 0;
FetchRequest m_req;
};

View file

@ -75,6 +75,12 @@ xmrig::HttpContext::~HttpContext()
}
bool xmrig::HttpContext::isRequest() const
{
return m_parser->type == HTTP_REQUEST;
}
size_t xmrig::HttpContext::parse(const char *data, size_t size)
{
return http_parser_execute(m_parser, &http_settings, data, size);

View file

@ -55,13 +55,19 @@ public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpContext)
HttpContext(int parser_type, const std::weak_ptr<IHttpListener> &listener);
virtual ~HttpContext();
~HttpContext() override;
inline uv_stream_t *stream() const { return reinterpret_cast<uv_stream_t *>(m_tcp); }
inline uv_handle_t *handle() const { return reinterpret_cast<uv_handle_t *>(m_tcp); }
inline const char *host() const override { return nullptr; }
inline const char *tlsFingerprint() const override { return nullptr; }
inline const char *tlsVersion() const override { return nullptr; }
inline uint16_t port() const override { return 0; }
bool isRequest() const override;
size_t parse(const char *data, size_t size);
std::string ip() const;
std::string ip() const override;
uint64_t elapsed() const;
void close(int status = 0);

View file

@ -0,0 +1,31 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/http/HttpData.h"
namespace xmrig {
const std::string HttpData::kApplicationJson = "application/json";
const std::string HttpData::kContentType = "Content-Type";
const std::string HttpData::kContentTypeL = "content-type";
} // namespace xmrig

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -28,6 +28,9 @@
#define XMRIG_HTTPDATA_H
#include "base/tools/Object.h"
#include <map>
#include <string>
@ -38,12 +41,28 @@ namespace xmrig {
class HttpData
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpData)
static const std::string kApplicationJson;
static const std::string kContentType;
static const std::string kContentTypeL;
inline HttpData(uint64_t id) : m_id(id) {}
virtual ~HttpData() = default;
inline uint64_t id() const { return m_id; }
int method = 0;
int status = 0;
virtual bool isRequest() const = 0;
virtual const char *host() const = 0;
virtual const char *tlsFingerprint() const = 0;
virtual const char *tlsVersion() const = 0;
virtual std::string ip() const = 0;
virtual uint16_t port() const = 0;
int method = 0;
int status = 0;
int userType = 0;
std::map<const std::string, const std::string> headers;
std::string body;
std::string url;

View file

@ -0,0 +1,41 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/http/HttpListener.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpData.h"
void xmrig::HttpListener::onHttpData(const HttpData &data)
{
# ifdef APP_DEBUG
if (!data.isRequest()) {
LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\" ") CSI "1;%dm%d" CLEAR BLACK_BOLD(" received: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"),
data.tlsVersion() ? "s" : "", data.host(), data.port(), http_method_str(static_cast<http_method>(data.method)), data.url.data(),
data.status >= 400 ? 31 : 32, data.status, data.body.size());
if (data.body.size() < (Log::kMaxBufferSize - 1024) && data.headers.count(HttpData::kContentTypeL) && data.headers.at(HttpData::kContentTypeL) == HttpData::kApplicationJson) {
Log::print(BLUE_BG_BOLD("%s:") BLACK_BOLD_S " %.*s", data.headers.at(HttpData::kContentTypeL).c_str(), static_cast<int>(data.body.size()), data.body.c_str());
}
}
# endif
m_listener->onHttpData(data);
}

View file

@ -32,7 +32,7 @@ public:
inline HttpListener(IHttpListener *listener) : m_listener(listener) {}
protected:
inline void onHttpData(const HttpData &data) override { m_listener->onHttpData(data); };
void onHttpData(const HttpData &data) override;
private:
IHttpListener *m_listener;

View file

@ -39,9 +39,8 @@
#endif
xmrig::HttpsClient::HttpsClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size, const String &fingerprint) :
HttpClient(method, url, listener, data, size),
m_fp(fingerprint)
xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpClient(std::move(req), listener)
{
m_ctx = SSL_CTX_new(SSLv23_method());
assert(m_ctx != nullptr);
@ -68,13 +67,13 @@ xmrig::HttpsClient::~HttpsClient()
}
const char *xmrig::HttpsClient::fingerprint() const
const char *xmrig::HttpsClient::tlsFingerprint() const
{
return m_ready ? m_fingerprint : nullptr;
}
const char *xmrig::HttpsClient::version() const
const char *xmrig::HttpsClient::tlsVersion() const
{
return m_ready ? SSL_get_version(m_ssl) : nullptr;
}
@ -91,7 +90,7 @@ void xmrig::HttpsClient::handshake()
SSL_set_connect_state(m_ssl);
SSL_set_bio(m_ssl, m_readBio, m_writeBio);
SSL_set_tlsext_host_name(m_ssl, host().data());
SSL_set_tlsext_host_name(m_ssl, host());
SSL_do_handshake(m_ssl);
@ -147,12 +146,12 @@ bool xmrig::HttpsClient::verify(X509 *cert)
}
if (!verifyFingerprint(cert)) {
if (!m_quiet) {
LOG_ERR("[%s:%d] Failed to verify server certificate fingerprint", host().data(), port());
if (!isQuiet()) {
LOG_ERR("[%s:%d] Failed to verify server certificate fingerprint", host(), port());
if (strlen(m_fingerprint) == 64 && !m_fp.isNull()) {
if (strlen(m_fingerprint) == 64 && !req().fingerprint.isNull()) {
LOG_ERR("\"%s\" was given", m_fingerprint);
LOG_ERR("\"%s\" was configured", m_fp.data());
LOG_ERR("\"%s\" was configured", req().fingerprint.data());
}
}
@ -179,7 +178,7 @@ bool xmrig::HttpsClient::verifyFingerprint(X509 *cert)
Buffer::toHex(md, 32, m_fingerprint);
return m_fp.isNull() || strncasecmp(m_fingerprint, m_fp.data(), 64) == 0;
return req().fingerprint.isNull() || strncasecmp(m_fingerprint, req().fingerprint.data(), 64) == 0;
}

View file

@ -46,11 +46,11 @@ class HttpsClient : public HttpClient
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient)
HttpsClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size, const String &fingerprint);
HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpsClient() override;
const char *fingerprint() const;
const char *version() const;
const char *tlsFingerprint() const override;
const char *tlsVersion() const override;
protected:
void handshake() override;
@ -69,7 +69,6 @@ private:
char m_fingerprint[32 * 2 + 8]{};
SSL *m_ssl = nullptr;
SSL_CTX *m_ctx = nullptr;
String m_fp;
};

View file

@ -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);
}

View file

@ -71,16 +71,16 @@ private:
bool parseJob(const rapidjson::Value &params, 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;

View file

@ -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()) {

View file

@ -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);

View file

@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -26,7 +26,7 @@
#define XMRIG_STORAGE_H
#include <assert.h>
#include <cassert>
#include <map>
@ -37,10 +37,7 @@ template <class TYPE>
class Storage
{
public:
inline Storage() :
m_counter(0)
{
}
inline Storage() = default;
inline uintptr_t add(TYPE *ptr)
@ -87,7 +84,7 @@ public:
private:
std::map<uintptr_t, TYPE *> m_data;
uint64_t m_counter;
uint64_t m_counter = 0;
};