DaemonClient: check TLS fingerprint, use SOCKS5 if configured, fixed socket leak when ZMQ connect fails
What changed, and why it matters
This commit fixes several bugs in XMRig's network code. The most important is a socket leak: when an outgoing ZeroMQ connection failed, the program did not properly close the underlying socket, which could waste resources and eventually cause the miner to stop working. The commit also adds the ability to route daemon and ZMQ connections through a SOCKS5 proxy if one is configured, and it now verifies the TLS fingerprint of the daemon server when one is set. These are defensive improvements rather than remote takeover bugs.
Treat as a routine maintenance/security-hardening patch. Users running daemon solo-mining with ZMQ should upgrade to avoid the socket leak. Operators using TLS or SOCKS5 should verify the new behavior works with their proxy and fingerprint settings. No emergency response is indicated by the diff alone.
Security signals we found
Fixes resource leak on failed ZMQ connect (socket leak / potential DoS)
Adds TLS certificate fingerprint validation for daemon HTTP connections
Adds SOCKS5 proxy support for daemon HTTP and ZMQ traffic
Adds localhost bypass to avoid proxying local daemon connections
Evidence from the diff
The patch modifies DaemonClient and HttpClient. It introduces a new Socks5Handshake helper and wires it into both HTTP and ZMQ paths so that configured SOCKS5 proxies are actually used (previously they appear to have been ignored for daemon/ZMQ traffic). It adds TLS fingerprint checking for daemon HTTP requests by copying m_pool.fingerprint() into the FetchRequest. It refactors ZMQ lifecycle handling: m_ZMQSocket is now created immediately before connect, connect errors call ZMQClose(), and the close callback deletes the uv_tcp_t and removes the storage entry only when the client is shutting down. This addresses the ‘socket leak when ZMQ connect fails’ mentioned in the commit title. A localhost bypass is added so SOCKS5 is not used for local daemon hosts.
Changed components
src/base/net/stratum/DaemonClient.cppsrc/base/net/stratum/DaemonClient.hsrc/base/net/http/HttpClient.cppsrc/base/net/http/HttpClient.hsrc/base/net/http/Fetch.hsrc/base/net/tools/Socks5Handshake.cppsrc/base/net/tools/Socks5Handshake.hInspect captured patch +469 / −65
### src/base/base.cmake
@@ -67,6 +67,7 @@ set(HEADERS_BASE
src/base/net/tools/LineReader.h
src/base/net/tools/MemPool.h
src/base/net/tools/NetBuffer.h
+ src/base/net/tools/Socks5Handshake.h
src/base/net/tools/Storage.h
src/base/tools/Alignment.h
src/base/tools/Arguments.h
@@ -132,6 +133,7 @@ set(SOURCES_BASE
src/base/net/stratum/Url.cpp
src/base/net/tools/LineReader.cpp
src/base/net/tools/NetBuffer.cpp
+ src/base/net/tools/Socks5Handshake.cpp
src/base/tools/Arguments.cpp
src/base/tools/Chrono.cpp
src/base/tools/cryptonote/BlockTemplate.cpp
### src/base/net/http/Fetch.h
@@ -57,7 +57,9 @@ class FetchRequest
String fingerprint;
String host;
String path;
+ String proxyHost;
uint16_t port = 0;
+ uint16_t proxyPort = 0;
uint64_t timeout = 0;
};
### src/base/net/http/HttpClient.cpp
@@ -25,6 +25,7 @@
#include "base/net/dns/Dns.h"
#include "base/net/dns/DnsRecords.h"
#include "base/net/tools/NetBuffer.h"
+#include "base/net/tools/Socks5Handshake.h"
#include "base/tools/Timer.h"
@@ -59,7 +60,11 @@ xmrig::HttpClient::HttpClient(const char *tag, FetchRequest &&req, const std::we
bool xmrig::HttpClient::connect()
{
- m_dns = Dns::resolve(m_req.host, this);
+ if (m_req.proxyPort > 0) {
+ m_socks5 = std::make_shared<Socks5Handshake>(m_req.host, m_req.port);
+ }
+
+ m_dns = Dns::resolve(m_socks5 ? m_req.proxyHost : m_req.host, this);
return true;
}
@@ -75,13 +80,22 @@ void xmrig::HttpClient::onResolved(const DnsRecords &records, int status, const
LOG_ERR("%s " RED("DNS error: ") RED_BOLD("\"%s\""), tag(), error);
}
- return;
+ return close(status);
}
auto req = new uv_connect_t;
req->data = this;
- uv_tcp_connect(req, m_tcp, records.get().addr(port()), onConnect);
+ const int rc = uv_tcp_connect(req, m_tcp, records.get().addr(m_socks5 ? m_req.proxyPort : port()), onConnect);
+ if (rc < 0) {
+ delete req;
+
+ if (!isQuiet()) {
+ LOG_ERR("%s " RED("connect error: ") RED_BOLD("\"%s\""), tag(), uv_strerror(rc));
+ }
+
+ close(rc);
+ }
}
@@ -152,7 +166,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
auto client = static_cast<HttpClient*>(tcp->data);
if (nread >= 0) {
- client->read(buf->base, static_cast<size_t>(nread));
+ client->onRead(buf->base, static_cast<size_t>(nread));
} else {
if (!client->isQuiet() && nread != UV_EOF) {
LOG_ERR("%s " RED("read error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(static_cast<int>(nread)));
@@ -164,5 +178,42 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
NetBuffer::release(buf);
});
+ if (client->m_socks5) {
+ return client->HttpContext::write(Socks5Handshake::greeting(), false);
+ }
+
client->handshake();
}
+
+
+void xmrig::HttpClient::onRead(const char *data, size_t size)
+{
+ if (m_socks5) {
+ std::string out;
+
+ if (!m_socks5->read(data, size, out)) {
+ if (!isQuiet()) {
+ LOG_ERR("%s " RED("SOCKS5 error: ") RED_BOLD("\"%s\""), tag(), m_socks5->error());
+ }
+
+ return close(UV_EPROTO);
+ }
+
+ if (!out.empty()) {
+ HttpContext::write(std::move(out), false);
+ }
+
+ if (!m_socks5->isReady()) {
+ return;
+ }
+
+ m_socks5.reset();
+ handshake();
+
+ if (size == 0) {
+ return;
+ }
+ }
+
+ read(data, size);
+}
### src/base/net/http/HttpClient.h
@@ -33,6 +33,7 @@ namespace xmrig {
class DnsRequest;
+class Socks5Handshake;
class HttpClient : public HttpContext, public IDnsListener, public ITimerListener
@@ -63,9 +64,12 @@ class HttpClient : public HttpContext, public IDnsListener, public ITimerListene
private:
static void onConnect(uv_connect_t *req, int status);
+ void onRead(const char *data, size_t size);
+
const char *m_tag;
FetchRequest m_req;
std::shared_ptr<DnsRequest> m_dns;
+ std::shared_ptr<Socks5Handshake> m_socks5;
std::shared_ptr<Timer> m_timer;
};
### src/base/net/stratum/DaemonClient.cpp
@@ -41,6 +41,7 @@
#include "base/net/http/HttpListener.h"
#include "base/net/stratum/SubmitResult.h"
#include "base/net/tools/NetBuffer.h"
+#include "base/net/tools/Socks5Handshake.h"
#include "base/tools/bswap_64.h"
#include "base/tools/cryptonote/Signatures.h"
#include "base/tools/Cvt.h"
@@ -55,9 +56,15 @@
#include <algorithm>
#include <cassert>
+#include <cstring>
#include <random>
+#ifdef _MSC_VER
+# define strcasecmp _stricmp
+#endif
+
+
namespace xmrig {
@@ -80,6 +87,34 @@ static constexpr size_t kZMQGreetingSize1 = 11;
static const char kZMQHandshake[] = "\4\x19\5READY\xbSocket-Type\0\0\0\3SUB";
static const char kZMQSubscribe[] = "\0\x18\1json-minimal-chain_main";
+
+static bool isLocalHost(const String &host)
+{
+ if (host.isEmpty()) {
+ return false;
+ }
+
+ if (strcasecmp(host.data(), "localhost") == 0) {
+ return true;
+ }
+
+ sockaddr_in addr4{};
+ if (uv_ip4_addr(host.data(), 0, &addr4) == 0) {
+ return reinterpret_cast<const uint8_t *>(&addr4.sin_addr)[0] == 127;
+ }
+
+ sockaddr_in6 addr6{};
+ if (uv_ip6_addr(host.data(), 0, &addr6) == 0) {
+ static const uint8_t loopback[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+ static const uint8_t v4mapped[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
+ const auto *addr = reinterpret_cast<const uint8_t *>(&addr6.sin6_addr);
+
+ return memcmp(addr, loopback, sizeof(loopback)) == 0 || (memcmp(addr, v4mapped, sizeof(v4mapped)) == 0 && addr[12] == 127);
+ }
+
+ return false;
+}
+
} // namespace xmrig
@@ -95,17 +130,21 @@ xmrig::DaemonClient::DaemonClient(int id, IClientListener *listener) :
xmrig::DaemonClient::~DaemonClient()
{
delete m_timer;
- delete m_ZMQSocket;
}
void xmrig::DaemonClient::deleteLater()
{
- if (m_pool.zmq_port() >= 0) {
- ZMQClose(true);
+ m_listener = nullptr;
+ m_httpListener.reset();
+ m_dns.reset();
+ m_timer->stop();
+
+ if (m_ZMQSocket) {
+ ZMQClose(false);
}
else {
- delete this;
+ m_storage.remove(m_key);
}
}
@@ -214,8 +253,8 @@ void xmrig::DaemonClient::connect()
return connectError("Invalid wallet address.");
}
- if (m_pool.zmq_port() >= 0) {
- m_dns = Dns::resolve(m_pool.host(), this);
+ if (m_pool.zmq_port() >= 0 && !m_ZMQSocket) {
+ m_dns = Dns::resolve(isProxied() ? m_pool.proxy().host() : m_pool.host(), this);
}
else {
getBlockTemplate();
@@ -349,23 +388,30 @@ void xmrig::DaemonClient::onResolved(const DnsRecords &records, int status, cons
const auto &record = records.get();
m_ip = record.ip();
- auto req = new uv_connect_t;
- req->data = m_storage.ptr(m_key);
+ if (m_pool.zmq_port() <= 0 || m_ZMQSocket) {
+ return;
+ }
- uv_tcp_t* s = new uv_tcp_t;
- s->data = m_storage.ptr(m_key);
+ m_ZMQSocket = new uv_tcp_t();
+ m_ZMQSocket->data = m_storage.ptr(m_key);
- uv_tcp_init(uv_default_loop(), s);
- uv_tcp_nodelay(s, 1);
+ uv_tcp_init(uv_default_loop(), m_ZMQSocket);
+ uv_tcp_nodelay(m_ZMQSocket, 1);
if (Platform::hasKeepalive()) {
- uv_tcp_keepalive(s, 1, 60);
+ uv_tcp_keepalive(m_ZMQSocket, 1, 60);
}
- if (m_pool.zmq_port() > 0) {
- delete m_ZMQSocket;
- m_ZMQSocket = s;
- uv_tcp_connect(req, s, record.addr(m_pool.zmq_port()), onZMQConnect);
+ m_ZMQConnectionState = ZMQ_CONNECTING;
+
+ auto req = new uv_connect_t;
+ req->data = m_storage.ptr(m_key);
+
+ const int rc = uv_tcp_connect(req, m_ZMQSocket, record.addr(isProxied() ? m_pool.proxy().port() : m_pool.zmq_port()), onZMQConnect);
+ if (rc < 0) {
+ delete req;
+ LOG_ERR("%s " RED("ZMQ connect error: ") RED_BOLD("\"%s\""), tag(), uv_strerror(rc));
+ ZMQClose();
}
}
@@ -376,6 +422,12 @@ bool xmrig::DaemonClient::isOutdated(uint64_t height, const char *hash) const
}
+bool xmrig::DaemonClient::isProxied() const
+{
+ return m_pool.proxy().isValid() && !isLocalHost(m_pool.host());
+}
+
+
bool xmrig::DaemonClient::parseJob(const rapidjson::Value ¶ms, int *code)
{
auto jobError = [this, code](const char *message) {
@@ -570,7 +622,7 @@ int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc, const std::
req.headers.insert(header);
}
- fetch(tag(), std::move(req), m_httpListener);
+ send(std::move(req));
return m_sequence++;
}
@@ -589,12 +641,7 @@ void xmrig::DaemonClient::retry()
setState(ConnectingState);
}
- if ((m_ZMQConnectionState != ZMQ_NOT_CONNECTED) && (m_ZMQConnectionState != ZMQ_DISCONNECTING)) {
- if (Platform::hasKeepalive()) {
- uv_tcp_keepalive(m_ZMQSocket, 0, 60);
- }
- uv_close(reinterpret_cast<uv_handle_t*>(m_ZMQSocket), onZMQClose);
- }
+ ZMQClose(false);
m_timer->stop();
m_timer->start(m_retryPause, 0);
@@ -603,7 +650,19 @@ void xmrig::DaemonClient::retry()
void xmrig::DaemonClient::send(const char *path)
{
- FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet());
+ send(FetchRequest(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet()));
+}
+
+
+void xmrig::DaemonClient::send(FetchRequest &&req)
+{
+ req.fingerprint = m_pool.fingerprint();
+
+ if (isProxied()) {
+ req.proxyHost = m_pool.proxy().host();
+ req.proxyPort = m_pool.proxy().port();
+ }
+
fetch(tag(), std::move(req), m_httpListener);
}
@@ -649,13 +708,13 @@ void xmrig::DaemonClient::onZMQConnect(uv_connect_t* req, int status)
DaemonClient* client = getClient(req->data);
delete req;
- if (!client) {
+ if (!client || client->m_ZMQConnectionState != ZMQ_CONNECTING) {
return;
}
if (status < 0) {
LOG_ERR("%s " RED("ZMQ connect error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(status));
- client->retry();
+ client->ZMQClose();
return;
}
@@ -677,23 +736,20 @@ void xmrig::DaemonClient::onZMQRead(uv_stream_t* stream, ssize_t nread, const uv
void xmrig::DaemonClient::onZMQClose(uv_handle_t* handle)
{
DaemonClient* client = getClient(handle->data);
- if (client) {
-# ifdef APP_DEBUG
- LOG_DEBUG(CYAN("tcp-zmq://%s:%u") BLACK_BOLD(" disconnected"), client->m_pool.host().data(), client->m_pool.zmq_port());
-# endif
- client->m_ZMQConnectionState = ZMQ_NOT_CONNECTED;
+ delete reinterpret_cast<uv_tcp_t*>(handle);
+
+ if (!client) {
+ return;
}
-}
+# ifdef APP_DEBUG
+ LOG_DEBUG(CYAN("tcp-zmq://%s:%u") BLACK_BOLD(" %s"), client->m_pool.host().data(), client->m_pool.zmq_port(), client->m_listener ? "disconnected" : "shutdown");
+# endif
+
+ client->m_ZMQSocket = nullptr;
+ client->m_ZMQConnectionState = ZMQ_NOT_CONNECTED;
-void xmrig::DaemonClient::onZMQShutdown(uv_handle_t* handle)
-{
- DaemonClient* client = getClient(handle->data);
- if (client) {
-# ifdef APP_DEBUG
- LOG_DEBUG(CYAN("tcp-zmq://%s:%u") BLACK_BOLD(" shutdown"), client->m_pool.host().data(), client->m_pool.zmq_port());
-# endif
- client->m_ZMQConnectionState = ZMQ_NOT_CONNECTED;
+ if (!client->m_listener) {
m_storage.remove(client->m_key);
}
}
@@ -705,11 +761,25 @@ void xmrig::DaemonClient::ZMQConnected()
LOG_DEBUG(CYAN("tcp-zmq://%s:%u") BLACK_BOLD(" connected"), m_pool.host().data(), m_pool.zmq_port());
# endif
- m_ZMQConnectionState = ZMQ_GREETING_1;
m_ZMQSendBuf.reserve(256);
m_ZMQRecvBuf.reserve(256);
+ m_ZMQRecvBuf.clear();
- if (ZMQWrite(kZMQGreeting, kZMQGreetingSize1)) {
+ bool ok = false;
+
+ if (isProxied()) {
+ m_ZMQConnectionState = ZMQ_SOCKS5;
+ m_ZMQSocks5 = std::make_shared<Socks5Handshake>(m_pool.host(), static_cast<uint16_t>(m_pool.zmq_port()));
+
+ const std::string greeting = Socks5Handshake::greeting();
+ ok = ZMQWrite(greeting.data(), greeting.size());
+ }
+ else {
+ m_ZMQConnectionState = ZMQ_GREETING_1;
+ ok = ZMQWrite(kZMQGreeting, kZMQGreetingSize1);
+ }
+
+ if (ok) {
uv_read_start(reinterpret_cast<uv_stream_t*>(m_ZMQSocket), NetBuffer::onAlloc, onZMQRead);
}
}
@@ -743,7 +813,35 @@ void xmrig::DaemonClient::ZMQRead(ssize_t nread, const uv_buf_t* buf)
return;
}
- m_ZMQRecvBuf.insert(m_ZMQRecvBuf.end(), buf->base, buf->base + nread);
+ const char *data = buf->base;
+ size_t size = static_cast<size_t>(nread);
+
+ if (m_ZMQConnectionState == ZMQ_SOCKS5) {
+ std::string out;
+
+ if (!m_ZMQSocks5->read(data, size, out)) {
+ LOG_ERR("%s " RED("ZMQ SOCKS5 error: ") RED_BOLD("\"%s\""), tag(), m_ZMQSocks5->error());
+ ZMQClose();
+ return;
+ }
+
+ if (!out.empty() && !ZMQWrite(out.data(), out.size())) {
+ return;
+ }
+
+ if (!m_ZMQSocks5->isReady()) {
+ return;
+ }
+
+ m_ZMQSocks5.reset();
+ m_ZMQConnectionState = ZMQ_GREETING_1;
+
+ if (!ZMQWrite(kZMQGreeting, kZMQGreetingSize1)) {
+ return;
+ }
+ }
+
+ m_ZMQRecvBuf.insert(m_ZMQRecvBuf.end(), data, data + size);
do {
switch (m_ZMQConnectionState) {
@@ -908,27 +1006,23 @@ void xmrig::DaemonClient::ZMQParse()
}
-bool xmrig::DaemonClient::ZMQClose(bool shutdown)
+bool xmrig::DaemonClient::ZMQClose(bool reconnect)
{
- if ((m_ZMQConnectionState == ZMQ_NOT_CONNECTED) || (m_ZMQConnectionState == ZMQ_DISCONNECTING)) {
- if (shutdown) {
- m_storage.remove(m_key);
- }
+ if (!m_ZMQSocket || uv_is_closing(reinterpret_cast<uv_handle_t*>(m_ZMQSocket))) {
return false;
}
m_ZMQConnectionState = ZMQ_DISCONNECTING;
- if (uv_is_closing(reinterpret_cast<uv_handle_t*>(m_ZMQSocket)) == 0) {
- if (Platform::hasKeepalive()) {
- uv_tcp_keepalive(m_ZMQSocket, 0, 60);
- }
- uv_close(reinterpret_cast<uv_handle_t*>(m_ZMQSocket), shutdown ? onZMQShutdown : onZMQClose);
- if (!shutdown) {
- retry();
- }
- return true;
+ if (Platform::hasKeepalive()) {
+ uv_tcp_keepalive(m_ZMQSocket, 0, 60);
}
- return false;
+ uv_close(reinterpret_cast<uv_handle_t*>(m_ZMQSocket), onZMQClose);
+
+ if (reconnect) {
+ retry();
+ }
+
+ return true;
}
### src/base/net/stratum/DaemonClient.h
@@ -50,6 +50,8 @@ namespace xmrig {
class DnsRequest;
+class FetchRequest;
+class Socks5Handshake;
class DaemonClient : public BaseClient, public IDnsListener, public ITimerListener, public IHttpListener
@@ -83,12 +85,14 @@ class DaemonClient : public BaseClient, public IDnsListener, public ITimerListen
private:
bool isOutdated(uint64_t height, const char *hash) const;
+ bool isProxied() const;
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, const std::map<std::string, std::string> &headers = {});
void retry();
void send(const char *path);
+ void send(FetchRequest &&req);
void setState(SocketState state);
enum {
@@ -120,19 +124,21 @@ class DaemonClient : public BaseClient, public IDnsListener, public ITimerListen
static void onZMQConnect(uv_connect_t* req, int status);
static void onZMQRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
static void onZMQClose(uv_handle_t* handle);
- static void onZMQShutdown(uv_handle_t* handle);
void ZMQConnected();
bool ZMQWrite(const char* data, size_t size);
void ZMQRead(ssize_t nread, const uv_buf_t* buf);
void ZMQParse();
- bool ZMQClose(bool shutdown = false);
+ bool ZMQClose(bool reconnect = true);
std::shared_ptr<DnsRequest> m_dns;
+ std::shared_ptr<Socks5Handshake> m_ZMQSocks5;
uv_tcp_t* m_ZMQSocket = nullptr;
enum {
ZMQ_NOT_CONNECTED,
+ ZMQ_CONNECTING,
+ ZMQ_SOCKS5,
ZMQ_GREETING_1,
ZMQ_GREETING_2,
ZMQ_HANDSHAKE,
### src/base/net/tools/Socks5Handshake.cpp
@@ -0,0 +1,180 @@
+/* XMRig
+ * Copyright (c) 2018-2026 SChernykh <https://github.com/SChernykh>
+ * Copyright (c) 2016-2026 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/tools/Socks5Handshake.h"
+#include <algorithm>
+#include <uv.h>
+
+namespace xmrig {
+
+static const char kGreeting[] = { 0x05, 0x01, 0x00 }; // version 5, 1 method: no authentication
+static const char kConnect[] = { 0x05, 0x01, 0x00 }; // version 5, command CONNECT, reserved
+
+static const char *replyError(uint8_t code)
+{
+ switch (code) {
+ case 0x01:
+ return "general SOCKS server failure";
+
+ case 0x02:
+ return "connection not allowed by ruleset";
+
+ case 0x03:
+ return "network unreachable";
+
+ case 0x04:
+ return "host unreachable";
+
+ case 0x05:
+ return "connection refused";
+
+ case 0x06:
+ return "TTL expired";
+
+ case 0x07:
+ return "command not supported";
+
+ case 0x08:
+ return "address type not supported";
+
+ default:
+ break;
+ }
+
+ return "unknown error";
+}
+
+} // namespace xmrig
+
+xmrig::Socks5Handshake::Socks5Handshake(const String &host, uint16_t port)
+ : m_host(host)
+ , m_port(port)
+{
+}
+
+std::string xmrig::Socks5Handshake::greeting()
+{
+ return { kGreeting, sizeof(kGreeting) };
+}
+
+bool xmrig::Socks5Handshake::read(const char *&data, size_t &size, std::string &out)
+{
+ while (size > 0 && m_state != READY && m_state != FAILED) {
+ // Take only the bytes of the current reply: anything after the CONNECT reply comes from the destination.
+ const size_t n = std::min(size, m_size - m_reply.size());
+
+ m_reply.append(data, n);
+ data += n;
+ size -= n;
+
+ if (m_reply.size() == m_size) {
+ parse(out);
+ }
+ }
+
+ return m_state != FAILED;
+}
+
+bool xmrig::Socks5Handshake::connectRequest(std::string &out)
+{
+ if (m_host.isEmpty() || m_host.size() > 255) {
+ return false;
+ }
+
+ out.append(kConnect, sizeof(kConnect));
+
+ sockaddr_storage addr{};
+
+ if (uv_ip4_addr(m_host.data(), 0, reinterpret_cast<sockaddr_in *>(&addr)) == 0) {
+ out.push_back(0x01);
+ out.append(reinterpret_cast<const char *>(&reinterpret_cast<const sockaddr_in *>(&addr)->sin_addr), 4);
+ }
+ else if (uv_ip6_addr(m_host.data(), 0, reinterpret_cast<sockaddr_in6 *>(&addr)) == 0) {
+ out.push_back(0x04);
+ out.append(reinterpret_cast<const char *>(&reinterpret_cast<const sockaddr_in6 *>(&addr)->sin6_addr), 16);
+ }
+ else {
+ out.push_back(0x03);
+ out.push_back(static_cast<char>(m_host.size()));
+ out.append(m_host.data(), m_host.size());
+ }
+
+ out.push_back(static_cast<char>(m_port >> 8));
+ out.push_back(static_cast<char>(m_port & 0xFF));
+
+ return true;
+}
+
+void xmrig::Socks5Handshake::fail(const char *error)
+{
+ m_error = error;
+ m_state = FAILED;
+}
+
+void xmrig::Socks5Handshake::parse(std::string &out)
+{
+ const auto *reply = reinterpret_cast<const uint8_t *>(m_reply.data());
+
+ if (reply[0] != 0x05) {
+ return fail("invalid reply");
+ }
+
+ if (m_state == METHOD_REPLY) {
+ if (reply[1] != 0x00) {
+ return fail("proxy requires authentication");
+ }
+
+ if (!connectRequest(out)) {
+ return fail("invalid host name");
+ }
+
+ m_state = CONNECT_REPLY;
+ m_size = 5; // enough to know the size of the whole reply
+ m_reply.clear();
+
+ return;
+ }
+
+ // CONNECT reply: version, status, reserved, address type, bound address, bound port
+ if (reply[1] != 0x00) {
+ return fail(replyError(reply[1]));
+ }
+
+ switch (reply[3]) {
+ case 0x01:
+ m_size = 4 + 4 + 2;
+ break;
+
+ case 0x03:
+ m_size = 4 + 1 + reply[4] + 2;
+ break;
+
+ case 0x04:
+ m_size = 4 + 16 + 2;
+ break;
+
+ default:
+ return fail("invalid reply");
+ }
+
+ if (m_reply.size() == m_size) {
+ m_state = READY;
+ m_reply.clear();
+ }
+}
### src/base/net/tools/Socks5Handshake.h
@@ -0,0 +1,65 @@
+/* XMRig
+ * Copyright (c) 2018-2026 SChernykh <https://github.com/SChernykh>
+ * Copyright (c) 2016-2026 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_SOCKS5HANDSHAKE_H
+#define XMRIG_SOCKS5HANDSHAKE_H
+
+#include "base/tools/Object.h"
+#include "base/tools/String.h"
+#include <cstdint>
+#include <string>
+
+namespace xmrig {
+
+class Socks5Handshake
+{
+public:
+ XMRIG_DISABLE_COPY_MOVE_DEFAULT(Socks5Handshake)
+
+ Socks5Handshake(const String &host, uint16_t port);
+
+ inline bool isReady() const { return m_state == READY; }
+ inline const char *error() const { return m_error; }
+
+ static std::string greeting();
+
+ bool read(const char *&data, size_t &size, std::string &out);
+
+private:
+ enum State {
+ METHOD_REPLY,
+ CONNECT_REPLY,
+ READY,
+ FAILED
+ };
+
+ bool connectRequest(std::string &out);
+ void fail(const char *error);
+ void parse(std::string &out);
+
+ const char *m_error = nullptr;
+ const String m_host;
+ const uint16_t m_port;
+ size_t m_size = 2; // size of the current reply as far as it is known yet
+ State m_state = METHOD_REPLY;
+ std::string m_reply;
+};
+
+} // namespace xmrig
+
+#endif // XMRIG_SOCKS5HANDSHAKE_HWhy this scored 48/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.