Reduced memory consumption on network level.
This commit is contained in:
parent
bb96684daf
commit
1b875fdabb
17 changed files with 462 additions and 205 deletions
|
@ -38,13 +38,14 @@
|
|||
#endif
|
||||
|
||||
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "base/io/json/Json.h"
|
||||
#include "base/io/json/JsonRequest.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/kernel/interfaces/IClientListener.h"
|
||||
#include "base/net/dns/Dns.h"
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "base/net/stratum/Socks5.h"
|
||||
#include "base/net/tools/NetBuffer.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
#include "base/tools/Chrono.h"
|
||||
#include "net/JobResult.h"
|
||||
|
@ -83,6 +84,7 @@ xmrig::Client::Client(int id, const char *agent, IClientListener *listener) :
|
|||
m_agent(agent),
|
||||
m_sendBuf(1024)
|
||||
{
|
||||
m_reader.setListener(this);
|
||||
m_key = m_storage.add(this);
|
||||
m_dns = new Dns(this);
|
||||
}
|
||||
|
@ -542,7 +544,7 @@ int xmrig::Client::resolve(const String &host)
|
|||
{
|
||||
setState(HostLookupState);
|
||||
|
||||
m_recvBuf.reset();
|
||||
m_reader.reset();
|
||||
|
||||
if (m_failures == -1) {
|
||||
m_failures = 0;
|
||||
|
@ -837,14 +839,10 @@ void xmrig::Client::ping()
|
|||
}
|
||||
|
||||
|
||||
void xmrig::Client::read(ssize_t nread)
|
||||
void xmrig::Client::read(ssize_t nread, const uv_buf_t *buf)
|
||||
{
|
||||
const auto size = static_cast<size_t>(nread);
|
||||
|
||||
if (nread > 0 && size > m_recvBuf.available()) {
|
||||
nread = UV_ENOBUFS;
|
||||
}
|
||||
|
||||
if (nread < 0) {
|
||||
if (!isQuiet()) {
|
||||
LOG_ERR("[%s] read error: \"%s\"", url(), uv_strerror(static_cast<int>(nread)));
|
||||
|
@ -859,12 +857,8 @@ void xmrig::Client::read(ssize_t nread)
|
|||
return reconnect();
|
||||
}
|
||||
|
||||
m_recvBuf.nread(size);
|
||||
|
||||
if (m_socks5) {
|
||||
if (m_socks5->read(m_recvBuf.base(), m_recvBuf.pos())) {
|
||||
m_recvBuf.reset();
|
||||
}
|
||||
m_socks5->read(buf->base, size);
|
||||
|
||||
if (m_socks5->isReady()) {
|
||||
delete m_socks5;
|
||||
|
@ -886,13 +880,12 @@ void xmrig::Client::read(ssize_t nread)
|
|||
if (isTLS()) {
|
||||
LOG_DEBUG("[%s] TLS received (%d bytes)", url(), static_cast<int>(nread));
|
||||
|
||||
m_tls->read(m_recvBuf.base(), m_recvBuf.pos());
|
||||
m_recvBuf.reset();
|
||||
m_tls->read(buf->base, size);
|
||||
}
|
||||
else
|
||||
# endif
|
||||
{
|
||||
m_recvBuf.getline(this);
|
||||
m_reader.parse(buf->base, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -959,23 +952,6 @@ void xmrig::Client::startTimeout()
|
|||
}
|
||||
|
||||
|
||||
void xmrig::Client::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
|
||||
{
|
||||
auto client = getClient(handle->data);
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
buf->base = client->m_recvBuf.current();
|
||||
|
||||
# ifdef _WIN32
|
||||
buf->len = static_cast<ULONG>(client->m_recvBuf.available());
|
||||
# else
|
||||
buf->len = client->m_recvBuf.available();
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Client::onClose(uv_handle_t *handle)
|
||||
{
|
||||
auto client = getClient(handle->data);
|
||||
|
@ -1027,17 +1003,19 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status)
|
|||
client->m_stream->data = req->data;
|
||||
client->setState(ConnectedState);
|
||||
|
||||
uv_read_start(client->m_stream, onAllocBuffer, onRead);
|
||||
uv_read_start(client->m_stream, NetBuffer::onAlloc, onRead);
|
||||
delete req;
|
||||
|
||||
client->handshake();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *)
|
||||
void xmrig::Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
|
||||
{
|
||||
auto client = getClient(stream->data);
|
||||
if (client) {
|
||||
client->read(nread);
|
||||
client->read(nread, buf);
|
||||
}
|
||||
|
||||
NetBuffer::release(buf);
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
#include "base/net/stratum/Job.h"
|
||||
#include "base/net/stratum/Pool.h"
|
||||
#include "base/net/stratum/SubmitResult.h"
|
||||
#include "base/net/tools/RecvBuf.h"
|
||||
#include "base/net/tools/LineReader.h"
|
||||
#include "base/net/tools/Storage.h"
|
||||
#include "base/tools/Object.h"
|
||||
|
||||
|
@ -61,7 +61,6 @@ public:
|
|||
|
||||
constexpr static uint64_t kConnectTimeout = 20 * 1000;
|
||||
constexpr static uint64_t kResponseTimeout = 20 * 1000;
|
||||
constexpr static size_t kInputBufferSize = 1024 * 16;
|
||||
constexpr static size_t kMaxSendBufferSize = 1024 * 16;
|
||||
|
||||
Client(int id, const char *agent, IClientListener *listener);
|
||||
|
@ -108,7 +107,7 @@ private:
|
|||
void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error);
|
||||
void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error);
|
||||
void ping();
|
||||
void read(ssize_t nread);
|
||||
void read(ssize_t nread, const uv_buf_t *buf);
|
||||
void reconnect();
|
||||
void setState(SocketState state);
|
||||
void startTimeout();
|
||||
|
@ -118,7 +117,6 @@ private:
|
|||
inline void setExtension(Extension ext, bool enable) noexcept { m_extensions.set(ext, enable); }
|
||||
template<Extension ext> inline bool has() const noexcept { return m_extensions.test(ext); }
|
||||
|
||||
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
|
||||
static void onClose(uv_handle_t *handle);
|
||||
static void onConnect(uv_connect_t *req, int status);
|
||||
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
|
||||
|
@ -127,7 +125,7 @@ private:
|
|||
|
||||
const char *m_agent;
|
||||
Dns *m_dns;
|
||||
RecvBuf<kInputBufferSize> m_recvBuf;
|
||||
LineReader m_reader;
|
||||
Socks5 *m_socks5 = nullptr;
|
||||
std::bitset<EXT_MAX> m_extensions;
|
||||
std::vector<char> m_sendBuf;
|
||||
|
|
|
@ -78,7 +78,7 @@ void xmrig::Client::Socks5::connect()
|
|||
|
||||
const auto &host = m_client->pool().host();
|
||||
std::vector<uint8_t> buf;
|
||||
sockaddr_storage addr;
|
||||
sockaddr_storage addr{};
|
||||
|
||||
if (isIPv4(host, &addr)) {
|
||||
buf.resize(10);
|
||||
|
|
|
@ -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 2018 Lee Clagett <https://github.com/vtnerd>
|
||||
* 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,12 +24,9 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
#include "base/net/stratum/Tls.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "base/net/stratum/Tls.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
|
||||
|
||||
|
@ -38,12 +35,12 @@
|
|||
#endif
|
||||
|
||||
|
||||
#include <cassert>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
|
||||
xmrig::Client::Tls::Tls(Client *client) :
|
||||
m_ready(false),
|
||||
m_buf(),
|
||||
m_fingerprint(),
|
||||
m_client(client),
|
||||
m_ssl(nullptr)
|
||||
m_client(client)
|
||||
{
|
||||
m_ctx = SSL_CTX_new(SSLv23_method());
|
||||
assert(m_ctx != nullptr);
|
||||
|
@ -52,8 +49,8 @@ xmrig::Client::Tls::Tls(Client *client) :
|
|||
return;
|
||||
}
|
||||
|
||||
m_writeBio = BIO_new(BIO_s_mem());
|
||||
m_readBio = BIO_new(BIO_s_mem());
|
||||
m_write = BIO_new(BIO_s_mem());
|
||||
m_read = BIO_new(BIO_s_mem());
|
||||
SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
|
||||
}
|
||||
|
||||
|
@ -80,7 +77,7 @@ bool xmrig::Client::Tls::handshake()
|
|||
}
|
||||
|
||||
SSL_set_connect_state(m_ssl);
|
||||
SSL_set_bio(m_ssl, m_readBio, m_writeBio);
|
||||
SSL_set_bio(m_ssl, m_read, m_write);
|
||||
SSL_do_handshake(m_ssl);
|
||||
|
||||
return send();
|
||||
|
@ -109,7 +106,7 @@ const char *xmrig::Client::Tls::version() const
|
|||
|
||||
void xmrig::Client::Tls::read(const char *data, size_t size)
|
||||
{
|
||||
BIO_write(m_readBio, data, size);
|
||||
BIO_write(m_read, data, size);
|
||||
|
||||
if (!SSL_is_init_finished(m_ssl)) {
|
||||
const int rc = SSL_connect(m_ssl);
|
||||
|
@ -133,17 +130,18 @@ void xmrig::Client::Tls::read(const char *data, size_t size)
|
|||
return;
|
||||
}
|
||||
|
||||
static char buf[16384]{};
|
||||
int bytes_read = 0;
|
||||
while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) {
|
||||
m_buf[bytes_read - 1] = '\0';
|
||||
m_client->parse(m_buf, static_cast<size_t>(bytes_read));
|
||||
|
||||
while ((bytes_read = SSL_read(m_ssl, buf, sizeof(buf))) > 0) {
|
||||
m_client->m_reader.parse(buf, static_cast<size_t>(bytes_read));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool xmrig::Client::Tls::send()
|
||||
{
|
||||
return m_client->send(m_writeBio);
|
||||
return m_client->send(m_write);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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,10 +26,14 @@
|
|||
#define XMRIG_CLIENT_TLS_H
|
||||
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
using BIO = struct bio_st;
|
||||
using SSL = struct ssl_st;
|
||||
using SSL_CTX = struct ssl_ctx_st;
|
||||
using X509 = struct x509_st;
|
||||
|
||||
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "base/tools/Object.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
@ -38,6 +42,8 @@ namespace xmrig {
|
|||
class Client::Tls
|
||||
{
|
||||
public:
|
||||
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Tls)
|
||||
|
||||
Tls(Client *client);
|
||||
~Tls();
|
||||
|
||||
|
@ -52,13 +58,12 @@ private:
|
|||
bool verify(X509 *cert);
|
||||
bool verifyFingerprint(X509 *cert);
|
||||
|
||||
BIO *m_readBio;
|
||||
BIO *m_writeBio;
|
||||
bool m_ready;
|
||||
char m_buf[1024 * 2];
|
||||
char m_fingerprint[32 * 2 + 8];
|
||||
BIO *m_read = nullptr;
|
||||
BIO *m_write = nullptr;
|
||||
bool m_ready = false;
|
||||
char m_fingerprint[32 * 2 + 8]{};
|
||||
Client *m_client;
|
||||
SSL *m_ssl;
|
||||
SSL *m_ssl = nullptr;
|
||||
SSL_CTX *m_ctx;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue