From a4626cccd112010c899369f2ef88e1b4d8b0db45 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 15 Oct 2018 12:17:15 +0700 Subject: [PATCH 01/34] v2.8.2-dev --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 8fd241aa8..8023dcfc1 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig-proxy" #define APP_NAME "xmrig-proxy" #define APP_DESC "XMRig Stratum proxy" -#define APP_VERSION "2.8.1" +#define APP_VERSION "2.8.2-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 8 -#define APP_VER_PATCH 1 +#define APP_VER_PATCH 2 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From c268baa83334b036d7bba67cb91407ca4e3e922c Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 15 Oct 2018 12:34:38 +0700 Subject: [PATCH 02/34] Class Addr replaced to xmrig::BindHost. --- CMakeLists.txt | 3 +- src/Summary.cpp | 14 ++-- src/core/Config.cpp | 28 ++++---- src/core/Config.h | 6 +- src/proxy/Addr.h | 118 ------------------------------- src/proxy/BindHost.cpp | 153 +++++++++++++++++++++++++++++++++++++++++ src/proxy/BindHost.h | 78 +++++++++++++++++++++ src/proxy/Proxy.cpp | 11 ++- src/proxy/Proxy.h | 9 +-- src/proxy/Server.cpp | 16 ++--- src/proxy/Server.h | 14 ++-- 11 files changed, 285 insertions(+), 165 deletions(-) delete mode 100644 src/proxy/Addr.h create mode 100644 src/proxy/BindHost.cpp create mode 100644 src/proxy/BindHost.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 58e279bcf..75cd05d77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ set(HEADERS src/log/ShareLog.h src/net/JobResult.h src/net/strategies/DonateStrategy.h - src/proxy/Addr.h + src/proxy/BindHost.h src/proxy/Counters.h src/proxy/CustomDiff.h src/proxy/Error.h @@ -112,6 +112,7 @@ set(SOURCES src/log/ShareLog.cpp src/net/JobResult.cpp src/net/strategies/DonateStrategy.cpp + src/proxy/BindHost.cpp src/proxy/Counters.cpp src/proxy/CustomDiff.cpp src/proxy/Error.cpp diff --git a/src/Summary.cpp b/src/Summary.cpp index fbbca4715..0f1b4b2d7 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -30,7 +30,7 @@ #include "common/net/Pool.h" #include "core/Config.h" #include "core/Controller.h" -#include "proxy/Addr.h" +#include "proxy/BindHost.h" #include "Summary.h" #include "version.h" @@ -53,16 +53,16 @@ static void print_algo(xmrig::Controller *controller) static void print_bind(xmrig::Controller *controller) { - const std::vector &addrs = controller->config()->addrs(); + const xmrig::BindHosts &bind = controller->config()->bind(); - for (size_t i = 0; i < addrs.size(); ++i) { + for (size_t i = 0; i < bind.size(); ++i) { Log::i()->text(controller->config()->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("BIND #%-7zu") CYAN("%s%s%s:") CYAN_BOLD("%d") : " * BIND #%-7zu%s%s%s:%d", i + 1, - addrs[i].isIPv6() ? "[" : "", - addrs[i].ip(), - addrs[i].isIPv6() ? "]" : "", - addrs[i].port()); + bind[i].isIPv6() ? "[" : "", + bind[i].host(), + bind[i].isIPv6() ? "]" : "", + bind[i].port()); } } diff --git a/src/core/Config.cpp b/src/core/Config.cpp index dbaf52926..e4c7022e1 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -96,8 +96,8 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("background", isBackground(), allocator); Value bind(kArrayType); - for (const Addr &addr : m_addrs) { - bind.PushBack(StringRef(addr.addr()), allocator); + for (const xmrig::BindHost &host : m_bind) { + bind.PushBack(host.toJSON(doc), allocator); } doc.AddMember("bind", bind, allocator); @@ -146,9 +146,9 @@ bool xmrig::Config::finalize() return false; } - if (m_addrs.empty()) { - m_addrs.push_back(Addr("0.0.0.0:3333")); - m_addrs.push_back(Addr("[::]:3333")); + if (m_bind.empty()) { + m_bind.push_back(xmrig::BindHost("0.0.0.0", 3333, 4)); + m_bind.push_back(xmrig::BindHost("::", 3333, 6)); } return true; @@ -196,9 +196,9 @@ bool xmrig::Config::parseString(int key, const char *arg) case BindKey: /* --bind */ { - Addr addr(arg); - if (addr.isValid()) { - m_addrs.push_back(std::move(addr)); + xmrig::BindHost host(arg); + if (host.isValid()) { + m_bind.push_back(std::move(host)); } } break; @@ -262,11 +262,15 @@ void xmrig::Config::parseJSON(const rapidjson::Document &doc) const rapidjson::Value &bind = doc["bind"]; if (bind.IsArray()) { for (const rapidjson::Value &value : bind.GetArray()) { - if (!value.IsString()) { - continue; + if (value.IsObject()) { + xmrig::BindHost host(value); + if (host.isValid()) { + m_bind.push_back(std::move(host)); + } + } + else if (value.IsString()) { + parseString(BindKey, value.GetString()); } - - parseString(BindKey, value.GetString()); } } } diff --git a/src/core/Config.h b/src/core/Config.h index 5be10efc1..d8d9aa585 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -31,7 +31,7 @@ #include "common/config/CommonConfig.h" #include "common/utils/c_str.h" -#include "proxy/Addr.h" +#include "proxy/BindHost.h" #include "proxy/workers/Workers.h" #include "rapidjson/fwd.h" @@ -78,7 +78,7 @@ class Config : public CommonConfig inline bool isDebug() const { return m_debug; } inline bool isVerbose() const { return m_verbose; } inline const char *accessLog() const { return m_accessLog.data(); } - inline const std::vector &addrs() const { return m_addrs; } + inline const xmrig::BindHosts &bind() const { return m_bind; } inline int mode() const { return m_mode; } inline int reuseTimeout() const { return m_reuseTimeout; } inline uint64_t diff() const { return m_diff; } @@ -103,9 +103,9 @@ class Config : public CommonConfig int m_mode; int m_reuseTimeout; - std::vector m_addrs; uint64_t m_diff; Workers::Mode m_workersMode; + xmrig::BindHosts m_bind; xmrig::c_str m_accessLog; }; diff --git a/src/proxy/Addr.h b/src/proxy/Addr.h deleted file mode 100644 index 6c4750f70..000000000 --- a/src/proxy/Addr.h +++ /dev/null @@ -1,118 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * 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 . - */ - -#ifndef __ADDR_H__ -#define __ADDR_H__ - - -#include -#include -#include - - -#include "common/utils/c_str.h" - - -class Addr -{ -public: - constexpr static uint16_t kDefaultPort = 3333; - - - inline Addr() : - m_version(0), - m_port(0) - {} - - - inline Addr(const char *addr) : - m_version(0), - m_port(0), - m_addr(addr) - { - if (!addr || strlen(addr) < 5) { - return; - } - - if (addr[0] == '[') { - parseIPv6(addr); - return; - } - - parseIPv4(addr); - } - - - inline bool isIPv6() const { return m_version == 6; } - inline bool isValid() const { return m_version && !m_ip.isNull() && m_port > 0; } - inline const char *addr() const { return m_addr.data(); } - inline const char *ip() const { return m_ip.data(); } - inline uint16_t port() const { return m_port; } - -private: - void parseIPv4(const char *addr) - { - const char *port = strchr(addr, ':'); - if (!port) { - return; - } - - m_version = 4; - const size_t size = port++ - addr + 1; - char *ip = new char[size](); - memcpy(ip, addr, size - 1); - - m_ip = ip; - m_port = static_cast(strtol(port, nullptr, 10)); - } - - - void parseIPv6(const char *addr) - { - const char *end = strchr(addr, ']'); - if (!end) { - return; - } - - const char *port = strchr(end, ':'); - if (!port) { - return; - } - - m_version = 6; - const size_t size = end - addr; - char *ip = new char[size](); - memcpy(ip, addr + 1, size - 1); - - m_ip = ip; - m_port = static_cast(strtol(port + 1, nullptr, 10)); - } - - - int m_version; - uint16_t m_port; - xmrig::c_str m_addr; - xmrig::c_str m_ip; -}; - -#endif /* __ADDR_H__ */ diff --git a/src/proxy/BindHost.cpp b/src/proxy/BindHost.cpp new file mode 100644 index 000000000..3ecc6b2fe --- /dev/null +++ b/src/proxy/BindHost.cpp @@ -0,0 +1,153 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include +#include +#include + + +#include "proxy/BindHost.h" +#include "rapidjson/document.h" + + +xmrig::BindHost::BindHost(const char *addr) : + m_version(0), + m_port(0) +{ + if (!addr || strlen(addr) < 5) { + return; + } + + if (addr[0] == '[') { + parseIPv6(addr); + return; + } + + parseIPv4(addr); +} + + +xmrig::BindHost::BindHost(const char *host, uint16_t port, int version) : + m_version(version), + m_port(port), + m_host(host) +{ +} + + +xmrig::BindHost::BindHost(const rapidjson::Value &object) : + m_version(0), + m_port(0) +{ + if (!parseHost(object["host"].GetString())) { + return; + } + + m_port = object["port"].GetUint(); +} + + +rapidjson::Value xmrig::BindHost::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + + Value obj(kObjectType); + + obj.AddMember("host", StringRef(host()), allocator); + obj.AddMember("port", port(), allocator); + + return obj; +} + + +bool xmrig::BindHost::parseHost(const char *host) +{ + assert(host != nullptr && strlen(host) >= 2); + m_version = 0; + + if (host == nullptr || strlen(host) < 2) { + return false; + } + + if (host[0] == '[') { + const char *end = strchr(host, ']'); + if (!end) { + return false; + } + + const size_t size = end - host; + char *buf = new char[size](); + memcpy(buf, host + 1, size - 1); + + m_version = 6; + m_host = buf; + } + else { + m_version = strchr(host, ':') != nullptr ? 6 : 4; + m_host = host; + } + + return m_version > 0; +} + + +void xmrig::BindHost::parseIPv4(const char *addr) +{ + const char *port = strchr(addr, ':'); + if (!port) { + return; + } + + m_version = 4; + const size_t size = port++ - addr + 1; + char *host = new char[size](); + memcpy(host, addr, size - 1); + + m_host = host; + m_port = static_cast(strtol(port, nullptr, 10)); +} + + +void xmrig::BindHost::parseIPv6(const char *addr) +{ + const char *end = strchr(addr, ']'); + if (!end) { + return; + } + + const char *port = strchr(end, ':'); + if (!port) { + return; + } + + m_version = 6; + const size_t size = end - addr; + char *host = new char[size](); + memcpy(host, addr + 1, size - 1); + + m_host = host; + m_port = static_cast(strtol(port + 1, nullptr, 10)); +} diff --git a/src/proxy/BindHost.h b/src/proxy/BindHost.h new file mode 100644 index 000000000..6e0192f22 --- /dev/null +++ b/src/proxy/BindHost.h @@ -0,0 +1,78 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_BINDHOST_H +#define XMRIG_BINDHOST_H + + +#include +#include + + +#include "common/utils/c_str.h" +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class BindHost +{ +public: + constexpr static uint16_t kDefaultPort = 3333; + + + inline BindHost() : + m_version(0), + m_port(0) + {} + + + BindHost(const char *addr); + BindHost(const char *host, uint16_t port, int version); + BindHost(const rapidjson::Value &object); + + rapidjson::Value toJSON(rapidjson::Document &doc) const; + + inline bool isIPv6() const { return m_version == 6; } + inline bool isValid() const { return m_version && !m_host.isNull() && m_port > 0; } + inline const char *host() const { return m_host.data(); } + inline uint16_t port() const { return m_port; } + +private: + bool parseHost(const char *host); + void parseIPv4(const char *addr); + void parseIPv6(const char *addr); + + int m_version; + uint16_t m_port; + xmrig::c_str m_host; +}; + + +typedef std::vector BindHosts; + + +} /* namespace xmrig */ + +#endif /* XMRIG_BINDHOST_H */ diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index 33db10f7b..0946f4755 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -37,7 +37,6 @@ #include "Counters.h" #include "log/AccessLog.h" #include "log/ShareLog.h" -#include "proxy/Addr.h" #include "proxy/Events.h" #include "proxy/events/ConnectionEvent.h" #include "proxy/Login.h" @@ -127,9 +126,9 @@ void Proxy::connect() { m_splitter->connect(); - const std::vector &addrs = m_controller->config()->addrs(); - for (const Addr &addr : addrs) { - bind(addr); + const xmrig::BindHosts &bind = m_controller->config()->bind(); + for (const xmrig::BindHost &host : bind) { + this->bind(host); } uv_timer_start(&m_timer, Proxy::onTick, 1000, 1000); @@ -204,9 +203,9 @@ bool Proxy::isColors() const } -void Proxy::bind(const Addr &addr) +void Proxy::bind(const xmrig::BindHost &host) { - auto server = new Server(addr, m_controller->config()->mode() == xmrig::Config::NICEHASH_MODE); + auto server = new Server(host, m_controller->config()->mode() == xmrig::Config::NICEHASH_MODE); if (server->bind()) { m_servers.push_back(server); diff --git a/src/proxy/Proxy.h b/src/proxy/Proxy.h index 69fbc66dd..a355d1087 100644 --- a/src/proxy/Proxy.h +++ b/src/proxy/Proxy.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __PROXY_H__ -#define __PROXY_H__ +#ifndef XMRIG_PROXY_H +#define XMRIG_PROXY_H #include @@ -51,6 +51,7 @@ class Workers; namespace xmrig { class Controller; + class BindHost; } @@ -82,7 +83,7 @@ class Proxy : public xmrig::IControllerListener constexpr static int kGCInterval = 60; bool isColors() const; - void bind(const Addr &addr); + void bind(const xmrig::BindHost &host); void gc(); void print(); void tick(); @@ -106,4 +107,4 @@ class Proxy : public xmrig::IControllerListener }; -#endif /* __PROXY_H__ */ +#endif /* XMRIG_PROXY_H */ diff --git a/src/proxy/Server.cpp b/src/proxy/Server.cpp index 972cf1556..a922b2380 100644 --- a/src/proxy/Server.cpp +++ b/src/proxy/Server.cpp @@ -23,29 +23,29 @@ #include "common/log/Log.h" -#include "proxy/Addr.h" +#include "proxy/BindHost.h" #include "proxy/events/ConnectionEvent.h" #include "proxy/Miner.h" #include "proxy/Server.h" -Server::Server(const Addr &addr, bool nicehash) : +Server::Server(const xmrig::BindHost &host, bool nicehash) : m_nicehash(nicehash), m_version(0), - m_port(addr.port()), - m_ip(addr.ip()) + m_port(host.port()), + m_host(host.host()) { uv_tcp_init(uv_default_loop(), &m_server); m_server.data = this; uv_tcp_nodelay(&m_server, 1); - if (addr.isIPv6() && uv_ip6_addr(m_ip.data(), m_port, &m_addr6) == 0) { + if (host.isIPv6() && uv_ip6_addr(m_host.data(), m_port, &m_addr6) == 0) { m_version = 6; return; } - if (uv_ip4_addr(m_ip.data(), m_port, &m_addr) == 0) { + if (uv_ip4_addr(m_host.data(), m_port, &m_addr) == 0) { m_version = 4; } } @@ -62,7 +62,7 @@ bool Server::bind() const int r = uv_listen(reinterpret_cast(&m_server), 511, Server::onConnection); if (r) { - LOG_ERR("[%s:%u] listen error: \"%s\"", m_ip.data(), m_port, uv_strerror(r)); + LOG_ERR("[%s:%u] listen error: \"%s\"", m_host.data(), m_port, uv_strerror(r)); return false; } @@ -73,7 +73,7 @@ bool Server::bind() void Server::create(uv_stream_t *server, int status) { if (status < 0) { - LOG_ERR("[%s:%u] new connection error: \"%s\"", m_ip.data(), m_port, uv_strerror(status)); + LOG_ERR("[%s:%u] new connection error: \"%s\"", m_host.data(), m_port, uv_strerror(status)); return; } diff --git a/src/proxy/Server.h b/src/proxy/Server.h index 8cdec4205..00c3823c9 100644 --- a/src/proxy/Server.h +++ b/src/proxy/Server.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __SERVER_H__ -#define __SERVER_H__ +#ifndef XMRIG_SERVER_H +#define XMRIG_SERVER_H #include @@ -31,13 +31,15 @@ #include "common/utils/c_str.h" -class Addr; +namespace xmrig { + class BindHost; +} class Server { public: - Server(const Addr &addr, bool nicehash); + Server(const xmrig::BindHost &host, bool nicehash); bool bind(); private: @@ -51,7 +53,7 @@ class Server sockaddr_in6 m_addr6; uint16_t m_port; uv_tcp_t m_server; - xmrig::c_str m_ip; + xmrig::c_str m_host; }; -#endif /* __SERVER_H__ */ +#endif /* XMRIG_SERVER_H */ From c3c640533294cc80acf11456a42944a20649cc4f Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 16 Oct 2018 12:46:13 +0700 Subject: [PATCH 03/34] Nicehash related logic removed from Server class. --- src/proxy/Miner.cpp | 5 +++-- src/proxy/Miner.h | 5 ++++- src/proxy/Proxy.cpp | 2 +- src/proxy/Server.cpp | 5 ++--- src/proxy/Server.h | 3 +-- src/proxy/splitters/nicehash/NonceMapper.cpp | 1 + src/proxy/splitters/simple/SimpleMapper.cpp | 1 + 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/proxy/Miner.cpp b/src/proxy/Miner.cpp index b501f27a4..2aeed7409 100644 --- a/src/proxy/Miner.cpp +++ b/src/proxy/Miner.cpp @@ -48,15 +48,16 @@ static int64_t nextId = 0; xmrig::Storage Miner::m_storage; -Miner::Miner(bool nicehash, bool ipv6) : +Miner::Miner(bool ipv6, uint16_t port) : m_ipv6(ipv6), - m_nicehash(nicehash), + m_nicehash(true), m_ip(), m_id(++nextId), m_loginId(0), m_recvBufPos(0), m_mapperId(-1), m_state(WaitLoginState), + m_localPort(port), m_customDiff(0), m_diff(0), m_expire(uv_now(uv_default_loop()) + kLoginTimeout), diff --git a/src/proxy/Miner.h b/src/proxy/Miner.h index 8fa9caae5..3b267c673 100644 --- a/src/proxy/Miner.h +++ b/src/proxy/Miner.h @@ -48,7 +48,7 @@ class Miner ClosingState }; - Miner(bool nicehash, bool ipv6); + Miner(bool ipv6, uint16_t port); ~Miner(); bool accept(uv_stream_t *server); void replyWithError(int64_t id, const char *message); @@ -63,6 +63,7 @@ class Miner inline int64_t id() const { return m_id; } inline ssize_t mapperId() const { return m_mapperId; } inline State state() const { return m_state; } + inline uint16_t localPort() const { return m_localPort; } inline uint64_t customDiff() const { return m_customDiff; } inline uint64_t diff() const { return (m_customDiff ? std::min(m_customDiff, m_diff) : m_diff); } inline uint64_t expire() const { return m_expire; } @@ -74,6 +75,7 @@ class Miner inline void setCustomDiff(uint64_t diff) { m_customDiff = diff; } inline void setFixedByte(uint8_t fixedByte) { m_fixedByte = fixedByte; } inline void setMapperId(ssize_t mapperId) { m_mapperId = mapperId; } + inline void setNiceHash(bool nicehash) { m_nicehash = nicehash; } private: constexpr static size_t kLoginTimeout = 10 * 1000; @@ -103,6 +105,7 @@ class Miner size_t m_recvBufPos; ssize_t m_mapperId; State m_state; + uint16_t m_localPort; uint64_t m_customDiff; uint64_t m_diff; uint64_t m_expire; diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index 0946f4755..94f79b32a 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -205,7 +205,7 @@ bool Proxy::isColors() const void Proxy::bind(const xmrig::BindHost &host) { - auto server = new Server(host, m_controller->config()->mode() == xmrig::Config::NICEHASH_MODE); + auto server = new Server(host); if (server->bind()) { m_servers.push_back(server); diff --git a/src/proxy/Server.cpp b/src/proxy/Server.cpp index a922b2380..3b7198c6e 100644 --- a/src/proxy/Server.cpp +++ b/src/proxy/Server.cpp @@ -29,8 +29,7 @@ #include "proxy/Server.h" -Server::Server(const xmrig::BindHost &host, bool nicehash) : - m_nicehash(nicehash), +Server::Server(const xmrig::BindHost &host) : m_version(0), m_port(host.port()), m_host(host.host()) @@ -77,7 +76,7 @@ void Server::create(uv_stream_t *server, int status) return; } - Miner *miner = new Miner(m_nicehash, m_version == 6); + Miner *miner = new Miner(m_version == 6, m_port); if (!miner) { return; } diff --git a/src/proxy/Server.h b/src/proxy/Server.h index 00c3823c9..20438698f 100644 --- a/src/proxy/Server.h +++ b/src/proxy/Server.h @@ -39,7 +39,7 @@ namespace xmrig { class Server { public: - Server(const xmrig::BindHost &host, bool nicehash); + Server(const xmrig::BindHost &host); bool bind(); private: @@ -47,7 +47,6 @@ class Server static void onConnection(uv_stream_t *server, int status); - bool m_nicehash; int m_version; sockaddr_in m_addr; sockaddr_in6 m_addr6; diff --git a/src/proxy/splitters/nicehash/NonceMapper.cpp b/src/proxy/splitters/nicehash/NonceMapper.cpp index afcd49673..d83b75e6e 100644 --- a/src/proxy/splitters/nicehash/NonceMapper.cpp +++ b/src/proxy/splitters/nicehash/NonceMapper.cpp @@ -79,6 +79,7 @@ bool NonceMapper::add(Miner *miner) connect(); } + miner->setNiceHash(true); miner->setMapperId(m_id); return true; } diff --git a/src/proxy/splitters/simple/SimpleMapper.cpp b/src/proxy/splitters/simple/SimpleMapper.cpp index 7c05d3656..b8f5b5a52 100644 --- a/src/proxy/splitters/simple/SimpleMapper.cpp +++ b/src/proxy/splitters/simple/SimpleMapper.cpp @@ -71,6 +71,7 @@ SimpleMapper::~SimpleMapper() void SimpleMapper::add(Miner *miner) { m_miner = miner; + m_miner->setNiceHash(false); m_miner->setMapperId(m_id); connect(); From 63e520b5f9083438ea9d972104f2e29b10b7391e Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 16 Oct 2018 15:03:13 +0700 Subject: [PATCH 04/34] First working TLS implementation for incoming connections. --- cmake/OpenSSL.cmake | 9 +- src/common/net/Tls.h | 6 +- src/core/Config.cpp | 12 +++ src/core/Config.h | 4 +- src/proxy/BindHost.cpp | 5 + src/proxy/BindHost.h | 3 + src/proxy/Miner.cpp | 194 +++++++++++++++++++++++++++-------- src/proxy/Miner.h | 30 +++++- src/proxy/Proxy.cpp | 18 +++- src/proxy/Proxy.h | 2 + src/proxy/Server.cpp | 10 +- src/proxy/Server.h | 9 +- src/proxy/tls/Tls.cpp | 123 ++++++++++++++++++++++ src/proxy/tls/Tls.h | 62 +++++++++++ src/proxy/tls/TlsContext.cpp | 70 +++++++++++++ src/proxy/tls/TlsContext.h | 52 ++++++++++ 16 files changed, 550 insertions(+), 59 deletions(-) create mode 100644 src/proxy/tls/Tls.cpp create mode 100644 src/proxy/tls/Tls.h create mode 100644 src/proxy/tls/TlsContext.cpp create mode 100644 src/proxy/tls/TlsContext.h diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index ed287e7e3..3f17154ea 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -11,7 +11,14 @@ if (WITH_TLS) find_package(OpenSSL) if (OPENSSL_FOUND) - set(TLS_SOURCES src/common/net/Tls.h src/common/net/Tls.cpp) + set(TLS_SOURCES + src/common/net/Tls.cpp + src/common/net/Tls.h + src/proxy/tls/Tls.cpp + src/proxy/tls/Tls.h + src/proxy/tls/TlsContext.cpp + src/proxy/tls/TlsContext.h + ) include_directories(${OPENSSL_INCLUDE_DIR}) else() message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") diff --git a/src/common/net/Tls.h b/src/common/net/Tls.h index 6e38f32f7..083adfc45 100644 --- a/src/common/net/Tls.h +++ b/src/common/net/Tls.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef XMRIG_TLS_H -#define XMRIG_TLS_H +#ifndef XMRIG_CLIENT_TLS_H +#define XMRIG_CLIENT_TLS_H #include @@ -59,4 +59,4 @@ class Client::Tls }; -#endif /* XMRIG_TLS_H */ +#endif /* XMRIG_CLIENT_TLS_H */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp index e4c7022e1..9a6e2b602 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -61,6 +61,18 @@ xmrig::Config::Config() : xmrig::CommonConfig(), } +bool xmrig::Config::isTLS() const +{ + for (const BindHost &host : m_bind) { + if (host.isTLS()) { + return true; + } + } + + return false; +} + + bool xmrig::Config::reload(const char *json) { return xmrig::ConfigLoader::reload(this, json); diff --git a/src/core/Config.h b/src/core/Config.h index d8d9aa585..61c3738e7 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -68,11 +68,13 @@ class Config : public CommonConfig Config(); + bool isTLS() const; bool reload(const char *json); - const char *modeName() const; + void getJSON(rapidjson::Document &doc) const override; + static Config *load(int argc, char **argv, IWatcherListener *listener); inline bool isDebug() const { return m_debug; } diff --git a/src/proxy/BindHost.cpp b/src/proxy/BindHost.cpp index 3ecc6b2fe..a5c7735d3 100644 --- a/src/proxy/BindHost.cpp +++ b/src/proxy/BindHost.cpp @@ -32,6 +32,7 @@ xmrig::BindHost::BindHost(const char *addr) : + m_tls(false), m_version(0), m_port(0) { @@ -49,6 +50,7 @@ xmrig::BindHost::BindHost(const char *addr) : xmrig::BindHost::BindHost(const char *host, uint16_t port, int version) : + m_tls(false), m_version(version), m_port(port), m_host(host) @@ -57,6 +59,7 @@ xmrig::BindHost::BindHost(const char *host, uint16_t port, int version) : xmrig::BindHost::BindHost(const rapidjson::Value &object) : + m_tls(false), m_version(0), m_port(0) { @@ -65,6 +68,7 @@ xmrig::BindHost::BindHost(const rapidjson::Value &object) : } m_port = object["port"].GetUint(); + m_tls = object["tls"].GetBool(); } @@ -78,6 +82,7 @@ rapidjson::Value xmrig::BindHost::toJSON(rapidjson::Document &doc) const obj.AddMember("host", StringRef(host()), allocator); obj.AddMember("port", port(), allocator); + obj.AddMember("tls", isTLS(), allocator); return obj; } diff --git a/src/proxy/BindHost.h b/src/proxy/BindHost.h index 6e0192f22..d2dd4d1c4 100644 --- a/src/proxy/BindHost.h +++ b/src/proxy/BindHost.h @@ -43,6 +43,7 @@ class BindHost inline BindHost() : + m_tls(false), m_version(0), m_port(0) {} @@ -57,6 +58,7 @@ class BindHost inline bool isIPv6() const { return m_version == 6; } inline bool isValid() const { return m_version && !m_host.isNull() && m_port > 0; } inline const char *host() const { return m_host.data(); } + inline isTLS() const { return m_tls; } inline uint16_t port() const { return m_port; } private: @@ -64,6 +66,7 @@ class BindHost void parseIPv4(const char *addr); void parseIPv6(const char *addr); + bool m_tls; int m_version; uint16_t m_port; xmrig::c_str m_host; diff --git a/src/proxy/Miner.cpp b/src/proxy/Miner.cpp index 2aeed7409..cb9e1028c 100644 --- a/src/proxy/Miner.cpp +++ b/src/proxy/Miner.cpp @@ -37,6 +37,7 @@ #include "proxy/events/LoginEvent.h" #include "proxy/events/SubmitEvent.h" #include "proxy/Miner.h" +#include "proxy/tls/TlsContext.h" #include "proxy/Uuid.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" @@ -44,11 +45,16 @@ #include "rapidjson/writer.h" +#ifndef XMRIG_NO_TLS +# include "proxy/tls/Tls.h" +#endif + + static int64_t nextId = 0; xmrig::Storage Miner::m_storage; -Miner::Miner(bool ipv6, uint16_t port) : +Miner::Miner(const xmrig::TlsContext *ctx, bool ipv6, uint16_t port) : m_ipv6(ipv6), m_nicehash(true), m_ip(), @@ -57,6 +63,7 @@ Miner::Miner(bool ipv6, uint16_t port) : m_recvBufPos(0), m_mapperId(-1), m_state(WaitLoginState), + m_tls(nullptr), m_localPort(port), m_customDiff(0), m_diff(0), @@ -76,6 +83,12 @@ Miner::Miner(bool ipv6, uint16_t port) : m_recvBuf.base = m_buf; m_recvBuf.len = sizeof(m_buf); +# ifndef XMRIG_NO_TLS + if (ctx != nullptr) { + m_tls = new Tls(ctx->ctx(), this); + } +# endif + Counters::connections++; } @@ -84,6 +97,10 @@ Miner::~Miner() { m_socket.data = nullptr; +# ifndef XMRIG_NO_TLS + delete m_tls; +# endif + Counters::connections--; } @@ -109,6 +126,12 @@ bool Miner::accept(uv_stream_t *server) uv_read_start(reinterpret_cast(&m_socket), Miner::onAllocBuffer, Miner::onRead); +# ifndef XMRIG_NO_TLS + if (isTLS()) { + return m_tls->accept(); + } +# endif + return true; } @@ -182,20 +205,7 @@ void Miner::setJob(Job &job) doc.AddMember("params", params, allocator); } - StringBuffer buffer(0, 512); - Writer writer(buffer); - doc.Accept(writer); - - const size_t size = buffer.GetSize(); - if (size > (sizeof(m_sendBuf) - 2)) { - return; - } - - memcpy(m_sendBuf, buffer.GetString(), size); - m_sendBuf[size] = '\n'; - m_sendBuf[size + 1] = '\0'; - - send(size + 1); + send(doc); } @@ -205,6 +215,12 @@ void Miner::success(int64_t id, const char *status) } +bool Miner::isWritable() const +{ + return m_state != ClosingState && uv_is_writable(reinterpret_cast(&m_socket)) == 1; +} + + bool Miner::parseRequest(int64_t id, const char *method, const rapidjson::Value ¶ms) { if (!method || !params.IsObject()) { @@ -294,6 +310,40 @@ bool Miner::parseRequest(int64_t id, const char *method, const rapidjson::Value } +bool Miner::send(BIO *bio) +{ +# ifndef XMRIG_NO_TLS + uv_buf_t buf; + buf.len = BIO_get_mem_data(bio, &buf.base); + + if (buf.len == 0) { + return true; + } + + LOG_DEBUG("[%s] TLS send (%d bytes)", m_ip, static_cast(buf.len)); + + if (!isWritable()) { + return false; + } + + const int rc = uv_try_write(reinterpret_cast(&m_socket), &buf, 1); + (void) BIO_reset(bio); + + if (rc < 0) { + shutdown(true); + + return false; + } + + m_tx += buf.len; + + return true; +# else + return false; +# endif +} + + void Miner::heartbeat() { m_expire = uv_now(uv_default_loop()) + kSocketTimeout; @@ -334,16 +384,97 @@ void Miner::parse(char *line, size_t len) } +void Miner::read() +{ + char* end; + char* start = m_recvBuf.base; + size_t remaining = m_recvBufPos; + + while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { + end++; + size_t len = end - start; + parse(start, len); + + remaining -= len; + start = end; + } + + if (remaining == 0) { + m_recvBufPos = 0; + return; + } + + if (start == m_recvBuf.base) { + return; + } + + memcpy(m_recvBuf.base, start, remaining); + m_recvBufPos = remaining; +} + + +void Miner::readTLS(int nread) +{ +# ifndef XMRIG_NO_TLS + if (isTLS()) { + LOG_DEBUG("[%s] TLS received (%d bytes)", m_ip, nread); + + m_tls->read(m_recvBuf.base, m_recvBufPos); + m_recvBufPos = 0; + } + else + { + read(); + } +# else + read(); +# endif +} + + +void Miner::send(const rapidjson::Document &doc) +{ + using namespace rapidjson; + + StringBuffer buffer(0, 512); + Writer writer(buffer); + doc.Accept(writer); + + const size_t size = buffer.GetSize(); + if (size > (sizeof(m_sendBuf) - 2)) { + LOG_ERR("[%s] send failed: \"send buffer overflow: %zu > %zu\"", m_ip, size, (sizeof(m_sendBuf) - 2)); + shutdown(true); + + return; + } + + memcpy(m_sendBuf, buffer.GetString(), size); + m_sendBuf[size] = '\n'; + m_sendBuf[size + 1] = '\0'; + + return send(size + 1); +} + + void Miner::send(int size) { LOG_DEBUG("[%s] send (%d bytes): \"%s\"", m_ip, size, m_sendBuf); - if (size <= 0 || (m_state != ReadyState && m_state != WaitReadyState) || uv_is_writable(reinterpret_cast(&m_socket)) == 0) { + if (size <= 0 || !isWritable()) { return; } - uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size); - const int rc = uv_try_write(reinterpret_cast(&m_socket), &buf, 1); + int rc = -1; +# ifndef XMRIG_NO_TLS + if (isTLS()) { + rc = m_tls->send(m_sendBuf, size) ? 0 : -1; + } + else +# endif + { + uv_buf_t buf = uv_buf_init(m_sendBuf, (unsigned int) size); + rc = uv_try_write(reinterpret_cast(&m_socket), &buf, 1); + } if (rc < 0) { return shutdown(true); @@ -414,7 +545,7 @@ void Miner::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t * void Miner::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { - auto miner = getMiner(stream->data); + Miner *miner = getMiner(stream->data); if (!miner) { return; } @@ -426,30 +557,7 @@ void Miner::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) miner->m_rx += nread; miner->m_recvBufPos += nread; - char* end; - char* start = miner->m_recvBuf.base; - size_t remaining = miner->m_recvBufPos; - - while ((end = static_cast(memchr(start, '\n', remaining))) != nullptr) { - end++; - size_t len = end - start; - miner->parse(start, len); - - remaining -= len; - start = end; - } - - if (remaining == 0) { - miner->m_recvBufPos = 0; - return; - } - - if (start == miner->m_recvBuf.base) { - return; - } - - memcpy(miner->m_recvBuf.base, start, remaining); - miner->m_recvBufPos = remaining; + miner->readTLS(static_cast(nread)); } diff --git a/src/proxy/Miner.h b/src/proxy/Miner.h index 3b267c673..38d8f0113 100644 --- a/src/proxy/Miner.h +++ b/src/proxy/Miner.h @@ -38,6 +38,14 @@ class Job; class RejectEvent; +namespace xmrig { + class TlsContext; +} + + +typedef struct bio_st BIO; + + class Miner { public: @@ -48,7 +56,13 @@ class Miner ClosingState }; - Miner(bool ipv6, uint16_t port); +# ifndef XMRIG_NO_TLS + constexpr static int kInputBufferSize = 1024 * 16; +# else + constexpr static int kInputBufferSize = 1024 * 2; +# endif + + Miner(const xmrig::TlsContext *ctx, bool ipv6, uint16_t port); ~Miner(); bool accept(uv_stream_t *server); void replyWithError(int64_t id, const char *message); @@ -78,12 +92,19 @@ class Miner inline void setNiceHash(bool nicehash) { m_nicehash = nicehash; } private: + class Tls; + constexpr static size_t kLoginTimeout = 10 * 1000; constexpr static size_t kSocketTimeout = 60 * 10 * 1000; + bool isWritable() const; bool parseRequest(int64_t id, const char *method, const rapidjson::Value ¶ms); + bool send(BIO *bio); void heartbeat(); void parse(char *line, size_t len); + void read(); + void readTLS(int nread); + void send(const rapidjson::Document &doc); void send(int size); void setState(State state); void shutdown(bool had_error); @@ -92,19 +113,22 @@ class Miner static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); static void onTimeout(uv_timer_t *handle); + inline bool isTLS() const { return m_tls != nullptr; } + static inline Miner *getMiner(void *data) { return m_storage.get(data); } bool m_ipv6; bool m_nicehash; - char m_buf[2048]; + char m_buf[kInputBufferSize]; char m_ip[46]; char m_rpcId[37]; - char m_sendBuf[768]; + char m_sendBuf[2048]; int64_t m_id; int64_t m_loginId; size_t m_recvBufPos; ssize_t m_mapperId; State m_state; + Tls *m_tls; uint16_t m_localPort; uint64_t m_customDiff; uint64_t m_diff; diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index 94f79b32a..e74393c6f 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -51,6 +51,11 @@ #include "proxy/workers/Workers.h" +#ifndef XMRIG_NO_TLS +# include "proxy/tls/TlsContext.h" +#endif + + Proxy::Proxy(xmrig::Controller *controller) : m_customDiff(controller), m_ticks(0), @@ -119,11 +124,22 @@ Proxy::~Proxy() delete m_accessLog; delete m_debug; delete m_workers; + +# ifndef XMRIG_NO_TLS + delete m_tls; +# endif } void Proxy::connect() { +# ifndef XMRIG_NO_TLS + if (m_controller->config()->isTLS()) { + m_tls = new xmrig::TlsContext(); + m_tls->load("cert.pem", "key.pem"); + } +# endif + m_splitter->connect(); const xmrig::BindHosts &bind = m_controller->config()->bind(); @@ -205,7 +221,7 @@ bool Proxy::isColors() const void Proxy::bind(const xmrig::BindHost &host) { - auto server = new Server(host); + auto server = new Server(host, m_tls); if (server->bind()) { m_servers.push_back(server); diff --git a/src/proxy/Proxy.h b/src/proxy/Proxy.h index a355d1087..f43abfbbb 100644 --- a/src/proxy/Proxy.h +++ b/src/proxy/Proxy.h @@ -52,6 +52,7 @@ class Workers; namespace xmrig { class Controller; class BindHost; + class TlsContext; } @@ -104,6 +105,7 @@ class Proxy : public xmrig::IControllerListener uv_timer_t m_timer; Workers *m_workers; xmrig::Controller *m_controller; + xmrig::TlsContext *m_tls; }; diff --git a/src/proxy/Server.cpp b/src/proxy/Server.cpp index 3b7198c6e..9a1a0ee6f 100644 --- a/src/proxy/Server.cpp +++ b/src/proxy/Server.cpp @@ -29,10 +29,12 @@ #include "proxy/Server.h" -Server::Server(const xmrig::BindHost &host) : - m_version(0), +Server::Server(const xmrig::BindHost &host, const xmrig::TlsContext *ctx) : + m_tls(host.isTLS()), m_port(host.port()), - m_host(host.host()) + m_host(host.host()), + m_ctx(ctx), + m_version(0) { uv_tcp_init(uv_default_loop(), &m_server); m_server.data = this; @@ -76,7 +78,7 @@ void Server::create(uv_stream_t *server, int status) return; } - Miner *miner = new Miner(m_version == 6, m_port); + Miner *miner = new Miner(m_tls ? m_ctx : nullptr, m_version == 6, m_port); if (!miner) { return; } diff --git a/src/proxy/Server.h b/src/proxy/Server.h index 20438698f..e5a1fee22 100644 --- a/src/proxy/Server.h +++ b/src/proxy/Server.h @@ -33,13 +33,14 @@ namespace xmrig { class BindHost; + class TlsContext; } class Server { public: - Server(const xmrig::BindHost &host); + Server(const xmrig::BindHost &host, const xmrig::TlsContext *ctx); bool bind(); private: @@ -47,12 +48,14 @@ class Server static void onConnection(uv_stream_t *server, int status); + const bool m_tls; + const uint16_t m_port; + const xmrig::c_str m_host; + const xmrig::TlsContext *m_ctx; int m_version; sockaddr_in m_addr; sockaddr_in6 m_addr6; - uint16_t m_port; uv_tcp_t m_server; - xmrig::c_str m_host; }; #endif /* XMRIG_SERVER_H */ diff --git a/src/proxy/tls/Tls.cpp b/src/proxy/tls/Tls.cpp new file mode 100644 index 000000000..ede7dfcaa --- /dev/null +++ b/src/proxy/tls/Tls.cpp @@ -0,0 +1,123 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018 SChernykh + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include + + +#include "proxy/tls/Tls.h" + + +Miner::Tls::Tls(SSL_CTX *ctx, Miner *miner) : + m_ready(false), + m_buf(), + m_fingerprint(), + m_miner(miner), + m_ssl(nullptr), + m_ctx(ctx) +{ + m_writeBio = BIO_new(BIO_s_mem()); + m_readBio = BIO_new(BIO_s_mem()); +} + + +Miner::Tls::~Tls() +{ + if (m_ssl) { + SSL_free(m_ssl); + } +} + + +bool Miner::Tls::accept() +{ + m_ssl = SSL_new(m_ctx); + assert(m_ssl != nullptr); + + if (!m_ssl) { + return false; + } + + SSL_set_accept_state(m_ssl); + SSL_set_bio(m_ssl, m_readBio, m_writeBio); + + return send(); +} + + +bool Miner::Tls::send(const char *data, size_t size) +{ + SSL_write(m_ssl, data, size); + + return send(); +} + + +const char *Miner::Tls::fingerprint() const +{ + return m_ready ? m_fingerprint : nullptr; +} + + +const char *Miner::Tls::version() const +{ + return m_ready ? SSL_get_version(m_ssl) : nullptr; +} + + +void Miner::Tls::read(const char *data, size_t size) +{ + BIO_write(m_readBio, data, size); + + if (!SSL_is_init_finished(m_ssl)) { + const int rc = SSL_do_handshake(m_ssl); + + if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { + send(); + } else if (rc == 1) { + m_ready = true; + read(); + } + + return; + } + + read(); +} + + +bool Miner::Tls::send() +{ + return m_miner->send(m_writeBio); +} + + +void Miner::Tls::read() +{ + int bytes_read = 0; + while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) { + m_miner->parse(m_buf, bytes_read); + } +} diff --git a/src/proxy/tls/Tls.h b/src/proxy/tls/Tls.h new file mode 100644 index 000000000..d74ecb2e1 --- /dev/null +++ b/src/proxy/tls/Tls.h @@ -0,0 +1,62 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_MINER_TLS_H +#define XMRIG_MINER_TLS_H + + +#include + + +#include "proxy/Miner.h" + + +class Miner::Tls +{ +public: + Tls(SSL_CTX *ctx, Miner *miner); + ~Tls(); + + bool accept(); + bool send(const char *data, size_t size); + const char *fingerprint() const; + const char *version() const; + void read(const char *data, size_t size); + +private: + bool send(); + bool verify(X509 *cert); + void read(); + + BIO *m_readBio; + BIO *m_writeBio; + bool m_ready; + char m_buf[1024 * 2]; + char m_fingerprint[32 * 2 + 8]; + Miner *m_miner; + SSL *m_ssl; + SSL_CTX *m_ctx; +}; + + +#endif /* XMRIG_MINER_TLS_H */ diff --git a/src/proxy/tls/TlsContext.cpp b/src/proxy/tls/TlsContext.cpp new file mode 100644 index 000000000..364c5fb9a --- /dev/null +++ b/src/proxy/tls/TlsContext.cpp @@ -0,0 +1,70 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include +#include + + +#include "common/log/Log.h" +#include "proxy/tls/TlsContext.h" + + +xmrig::TlsContext::TlsContext() : + m_ctx(nullptr) +{ + m_ctx = SSL_CTX_new(SSLv23_server_method()); +} + + +xmrig::TlsContext::~TlsContext() +{ + SSL_CTX_free(m_ctx); +} + + +bool xmrig::TlsContext::load(const char *cert, const char *key) +{ + if (m_ctx == nullptr) { + LOG_ERR("Unable to create SSL context"); + + return false; + } + + if (SSL_CTX_use_certificate_chain_file(m_ctx, cert) <= 0) { + LOG_ERR("Failed to load certificate chain file \"%s\"", cert); + + return false; + } + + if (SSL_CTX_use_PrivateKey_file(m_ctx, key, SSL_FILETYPE_PEM) <= 0) { + LOG_ERR("Failed to load private key file \"%s\"", key); + + return false; + } + + SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); + + return true; +} diff --git a/src/proxy/tls/TlsContext.h b/src/proxy/tls/TlsContext.h new file mode 100644 index 000000000..df29f2ecd --- /dev/null +++ b/src/proxy/tls/TlsContext.h @@ -0,0 +1,52 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_TLSCONTEXT_H +#define XMRIG_TLSCONTEXT_H + + +typedef struct ssl_ctx_st SSL_CTX; + + +namespace xmrig { + + +class TlsContext +{ +public: + TlsContext(); + ~TlsContext(); + + bool load(const char *cert, const char *key); + + inline SSL_CTX *ctx() const { return m_ctx; } + +private: + SSL_CTX *m_ctx; +}; + + +} /* namespace xmrig */ + +#endif /* XMRIG_TLSCONTEXT_H */ From bd4eae2da5b30cdff906cad85091fcd2ce277bd4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 17 Oct 2018 09:50:52 +0700 Subject: [PATCH 05/34] Fix missing return type. --- src/proxy/BindHost.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proxy/BindHost.h b/src/proxy/BindHost.h index d2dd4d1c4..5151f641a 100644 --- a/src/proxy/BindHost.h +++ b/src/proxy/BindHost.h @@ -56,9 +56,9 @@ class BindHost rapidjson::Value toJSON(rapidjson::Document &doc) const; inline bool isIPv6() const { return m_version == 6; } + inline bool isTLS() const { return m_tls; } inline bool isValid() const { return m_version && !m_host.isNull() && m_port > 0; } inline const char *host() const { return m_host.data(); } - inline isTLS() const { return m_tls; } inline uint16_t port() const { return m_port; } private: From 3ce14afdcb9c77a4c814373e5ce5a8a66f73a1cf Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 17 Oct 2018 15:52:37 +0700 Subject: [PATCH 06/34] Only TLS v1.3 was work. --- src/proxy/tls/Tls.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/proxy/tls/Tls.cpp b/src/proxy/tls/Tls.cpp index ede7dfcaa..798c86612 100644 --- a/src/proxy/tls/Tls.cpp +++ b/src/proxy/tls/Tls.cpp @@ -98,6 +98,7 @@ void Miner::Tls::read(const char *data, size_t size) send(); } else if (rc == 1) { m_ready = true; + send(); read(); } From 358ee9fd39fb0592bba2a0c0cbe7f37412841921 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 22 Oct 2018 14:29:10 +0700 Subject: [PATCH 07/34] Optimize memory usage. --- src/core/Config.cpp | 2 ++ src/proxy/Miner.cpp | 2 ++ src/proxy/Miner.h | 13 +++++-------- src/proxy/tls/Tls.cpp | 3 +++ 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 9a6e2b602..eba4c99eb 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -63,11 +63,13 @@ xmrig::Config::Config() : xmrig::CommonConfig(), bool xmrig::Config::isTLS() const { +# ifndef XMRIG_NO_TLS for (const BindHost &host : m_bind) { if (host.isTLS()) { return true; } } +# endif return false; } diff --git a/src/proxy/Miner.cpp b/src/proxy/Miner.cpp index cb9e1028c..3dccc2e42 100644 --- a/src/proxy/Miner.cpp +++ b/src/proxy/Miner.cpp @@ -51,6 +51,7 @@ static int64_t nextId = 0; +char Miner::m_sendBuf[2048] = { 0 }; xmrig::Storage Miner::m_storage; @@ -58,6 +59,7 @@ Miner::Miner(const xmrig::TlsContext *ctx, bool ipv6, uint16_t port) : m_ipv6(ipv6), m_nicehash(true), m_ip(), + m_routeId(-1), m_id(++nextId), m_loginId(0), m_recvBufPos(0), diff --git a/src/proxy/Miner.h b/src/proxy/Miner.h index 38d8f0113..cb1dd50cf 100644 --- a/src/proxy/Miner.h +++ b/src/proxy/Miner.h @@ -56,12 +56,6 @@ class Miner ClosingState }; -# ifndef XMRIG_NO_TLS - constexpr static int kInputBufferSize = 1024 * 16; -# else - constexpr static int kInputBufferSize = 1024 * 2; -# endif - Miner(const xmrig::TlsContext *ctx, bool ipv6, uint16_t port); ~Miner(); bool accept(uv_stream_t *server); @@ -74,6 +68,7 @@ class Miner inline const char *password() const { return m_password.data(); } inline const char *rigId(bool safe = false) const { return (safe ? (m_rigId.size() > 0 ? m_rigId.data() : m_user.data()) : m_rigId.data()); } inline const char *user() const { return m_user.data(); } + inline int32_t routeId() const { return m_routeId; } inline int64_t id() const { return m_id; } inline ssize_t mapperId() const { return m_mapperId; } inline State state() const { return m_state; } @@ -90,6 +85,7 @@ class Miner inline void setFixedByte(uint8_t fixedByte) { m_fixedByte = fixedByte; } inline void setMapperId(ssize_t mapperId) { m_mapperId = mapperId; } inline void setNiceHash(bool nicehash) { m_nicehash = nicehash; } + inline void setRouteId(int32_t id) { m_routeId = id; } private: class Tls; @@ -119,10 +115,10 @@ class Miner bool m_ipv6; bool m_nicehash; - char m_buf[kInputBufferSize]; + char m_buf[1024]; char m_ip[46]; char m_rpcId[37]; - char m_sendBuf[2048]; + int32_t m_routeId; int64_t m_id; int64_t m_loginId; size_t m_recvBufPos; @@ -145,6 +141,7 @@ class Miner xmrig::c_str m_rigId; xmrig::c_str m_user; + static char m_sendBuf[2048]; static xmrig::Storage m_storage; }; diff --git a/src/proxy/tls/Tls.cpp b/src/proxy/tls/Tls.cpp index 798c86612..7aadebc1f 100644 --- a/src/proxy/tls/Tls.cpp +++ b/src/proxy/tls/Tls.cpp @@ -101,6 +101,9 @@ void Miner::Tls::read(const char *data, size_t size) send(); read(); } + else { + m_miner->close(); + } return; } From b4f1900d805ded1c8da7125e4d67aff40480f8c6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 23 Oct 2018 12:24:30 +0700 Subject: [PATCH 08/34] Added TlsConfig class. --- cmake/OpenSSL.cmake | 2 ++ src/common/interfaces/IConfig.h | 4 +++ src/core/Config.cpp | 12 +++++++ src/core/Config.h | 13 ++++++-- src/proxy/Proxy.cpp | 14 +++++++- src/proxy/tls/Tls.h | 2 +- src/proxy/tls/TlsConfig.cpp | 58 ++++++++++++++++++++++++++++++++ src/proxy/tls/TlsConfig.h | 59 +++++++++++++++++++++++++++++++++ src/proxy/tls/TlsContext.cpp | 17 +++++++--- src/proxy/tls/TlsContext.h | 5 ++- 10 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 src/proxy/tls/TlsConfig.cpp create mode 100644 src/proxy/tls/TlsConfig.h diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index 3f17154ea..ff9d71676 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -16,6 +16,8 @@ if (WITH_TLS) src/common/net/Tls.h src/proxy/tls/Tls.cpp src/proxy/tls/Tls.h + src/proxy/tls/TlsConfig.cpp + src/proxy/tls/TlsConfig.h src/proxy/tls/TlsContext.cpp src/proxy/tls/TlsContext.h ) diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 69f2ffab9..2eebb7aa2 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -107,6 +107,10 @@ class IConfig ReuseTimeoutKey = 1106, WorkersKey = 1103, WorkersAdvKey = 1107, + TlsBindKey = 1108, + TlsCertKey = 1109, + TlsCertKeyKey = 1110, + TlsDHparamKey = 1111, // xmrig nvidia CudaMaxThreadsKey = 1200, diff --git a/src/core/Config.cpp b/src/core/Config.cpp index eba4c99eb..43c718209 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -132,6 +132,11 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("retries", retries(), allocator); doc.AddMember("retry-pause", retryPause(), allocator); doc.AddMember("reuse-timeout", reuseTimeout(), allocator); + +# ifndef XMRIG_NO_TLS + doc.AddMember("tls", m_tls.toJSON(doc), allocator); +# endif + doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); # ifdef HAVE_SYSLOG_H @@ -287,6 +292,13 @@ void xmrig::Config::parseJSON(const rapidjson::Document &doc) } } } + +# ifndef XMRIG_NO_TLS + const rapidjson::Value &tls = doc["tls"]; + if (tls.IsObject()) { + m_tls = std::move(TlsConfig(tls)); + } +# endif } diff --git a/src/core/Config.h b/src/core/Config.h index 61c3738e7..5183fe681 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -36,8 +36,9 @@ #include "rapidjson/fwd.h" -class Addr; -class Url; +#ifndef XMRIG_NO_TLS +# include "proxy/tls/TlsConfig.h" +#endif namespace xmrig { @@ -89,6 +90,10 @@ class Config : public CommonConfig inline void toggleVerbose() { m_verbose = !m_verbose; } inline Workers::Mode workersMode() const { return m_workersMode; } +# ifndef XMRIG_NO_TLS + inline const xmrig::TlsConfig &tls() const { return m_tls; } +# endif + protected: bool finalize() override; bool parseBoolean(int key, bool enable) override; @@ -109,6 +114,10 @@ class Config : public CommonConfig Workers::Mode m_workersMode; xmrig::BindHosts m_bind; xmrig::c_str m_accessLog; + +# ifndef XMRIG_NO_TLS + xmrig::TlsConfig m_tls; +# endif }; diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index e74393c6f..c216c8004 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -136,7 +136,11 @@ void Proxy::connect() # ifndef XMRIG_NO_TLS if (m_controller->config()->isTLS()) { m_tls = new xmrig::TlsContext(); - m_tls->load("cert.pem", "key.pem"); + + if (!m_tls->load(m_controller->config()->tls())) { + delete m_tls; + m_tls = nullptr; + } } # endif @@ -221,6 +225,14 @@ bool Proxy::isColors() const void Proxy::bind(const xmrig::BindHost &host) { +# ifndef XMRIG_NO_TLS + if (host.isTLS() && !m_tls) { + LOG_ERR("Failed to bind \"%s:%d\" error: \"TLS not available\".", host.host(), host.port()); + + return; + } +# endif + auto server = new Server(host, m_tls); if (server->bind()) { diff --git a/src/proxy/tls/Tls.h b/src/proxy/tls/Tls.h index d74ecb2e1..3c6e200e6 100644 --- a/src/proxy/tls/Tls.h +++ b/src/proxy/tls/Tls.h @@ -51,7 +51,7 @@ class Miner::Tls BIO *m_readBio; BIO *m_writeBio; bool m_ready; - char m_buf[1024 * 2]; + char m_buf[1024 * 1]; char m_fingerprint[32 * 2 + 8]; Miner *m_miner; SSL *m_ssl; diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp new file mode 100644 index 000000000..36f69fa35 --- /dev/null +++ b/src/proxy/tls/TlsConfig.cpp @@ -0,0 +1,58 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include "proxy/tls/TlsConfig.h" +#include "rapidjson/document.h" + + +xmrig::TlsConfig::TlsConfig() +{ +} + + +xmrig::TlsConfig::TlsConfig(const rapidjson::Value &object) +{ + setCert(object["cert"].GetString()); + setKey(object["key"].GetString()); +} + + +xmrig::TlsConfig::~TlsConfig() +{ +} + + +rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + auto &allocator = doc.GetAllocator(); + Value obj(kObjectType); + + obj.AddMember("cert", cert() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); + obj.AddMember("key", key() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); + + return obj; +} diff --git a/src/proxy/tls/TlsConfig.h b/src/proxy/tls/TlsConfig.h new file mode 100644 index 000000000..f1e814358 --- /dev/null +++ b/src/proxy/tls/TlsConfig.h @@ -0,0 +1,59 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_TLSCONFIG_H +#define XMRIG_TLSCONFIG_H + + +#include "common/utils/c_str.h" +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class TlsConfig +{ +public: + TlsConfig(); + TlsConfig(const rapidjson::Value &object); + ~TlsConfig(); + + inline bool isValid() const { return !m_cert.isNull() && !m_key.isNull(); } + inline const char *cert() const { return m_cert.data(); } + inline const char *key() const { return m_key.data(); } + inline void setCert(const char *cert) { m_cert = cert; } + inline void setKey(const char *key) { m_key = key; } + + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + c_str m_cert; + c_str m_key; +}; + + +} /* namespace xmrig */ + +#endif /* XMRIG_TLSCONFIG_H */ diff --git a/src/proxy/tls/TlsContext.cpp b/src/proxy/tls/TlsContext.cpp index 364c5fb9a..9b81701d1 100644 --- a/src/proxy/tls/TlsContext.cpp +++ b/src/proxy/tls/TlsContext.cpp @@ -28,6 +28,7 @@ #include "common/log/Log.h" +#include "proxy/tls/TlsConfig.h" #include "proxy/tls/TlsContext.h" @@ -44,7 +45,7 @@ xmrig::TlsContext::~TlsContext() } -bool xmrig::TlsContext::load(const char *cert, const char *key) +bool xmrig::TlsContext::load(const TlsConfig &config) { if (m_ctx == nullptr) { LOG_ERR("Unable to create SSL context"); @@ -52,14 +53,20 @@ bool xmrig::TlsContext::load(const char *cert, const char *key) return false; } - if (SSL_CTX_use_certificate_chain_file(m_ctx, cert) <= 0) { - LOG_ERR("Failed to load certificate chain file \"%s\"", cert); + if (!config.isValid()) { + LOG_ERR("No valid TLS configuration provided"); return false; } - if (SSL_CTX_use_PrivateKey_file(m_ctx, key, SSL_FILETYPE_PEM) <= 0) { - LOG_ERR("Failed to load private key file \"%s\"", key); + if (SSL_CTX_use_certificate_chain_file(m_ctx, config.cert()) <= 0) { + LOG_ERR("Failed to load certificate chain file \"%s\"", config.cert()); + + return false; + } + + if (SSL_CTX_use_PrivateKey_file(m_ctx, config.key(), SSL_FILETYPE_PEM) <= 0) { + LOG_ERR("Failed to load private key file \"%s\"", config.key()); return false; } diff --git a/src/proxy/tls/TlsContext.h b/src/proxy/tls/TlsContext.h index df29f2ecd..d158a8951 100644 --- a/src/proxy/tls/TlsContext.h +++ b/src/proxy/tls/TlsContext.h @@ -32,13 +32,16 @@ typedef struct ssl_ctx_st SSL_CTX; namespace xmrig { +class TlsConfig; + + class TlsContext { public: TlsContext(); ~TlsContext(); - bool load(const char *cert, const char *key); + bool load(const TlsConfig &config); inline SSL_CTX *ctx() const { return m_ctx; } From 49aea7e80ec506cbecc52a8a35d59423f57f0466 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 23 Oct 2018 14:49:17 +0700 Subject: [PATCH 09/34] Fix error in TlsConfig. --- src/proxy/tls/TlsConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index 36f69fa35..a12a0226a 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -52,7 +52,7 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const Value obj(kObjectType); obj.AddMember("cert", cert() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); - obj.AddMember("key", key() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); + obj.AddMember("key", key() ? Value(StringRef(key())).Move() : Value(kNullType).Move(), allocator); return obj; } From 4981dc34446eec3367c3eb7aeef5381623bcf5b8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 25 Oct 2018 16:32:48 +0700 Subject: [PATCH 10/34] Use better string wrapper. --- CMakeLists.txt | 2 + src/base/tools/String.cpp | 42 +++++++++++++++ src/base/tools/String.h | 104 ++++++++++++++++++++++++++++++++++++ src/common/utils/c_str.h | 73 ++----------------------- src/proxy/tls/TlsConfig.cpp | 4 +- src/proxy/tls/TlsConfig.h | 8 +-- 6 files changed, 159 insertions(+), 74 deletions(-) create mode 100644 src/base/tools/String.cpp create mode 100644 src/base/tools/String.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 75cd05d77..2ac831fa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ include (CheckIncludeFile) set(HEADERS src/3rdparty/align.h src/App.h + src/base/tools/String.h src/common/config/CommonConfig.h src/common/config/ConfigLoader.h src/common/config/ConfigWatcher.h @@ -89,6 +90,7 @@ set(HEADERS set(SOURCES src/App.cpp + src/base/tools/String.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp new file mode 100644 index 000000000..adad51e6c --- /dev/null +++ b/src/base/tools/String.cpp @@ -0,0 +1,42 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + + +#include "base/tools/String.h" +#include "rapidjson/document.h" + + +rapidjson::Value xmrig::String::toJSON() const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(StringRef(m_data)); +} + + +rapidjson::Value xmrig::String::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + + return isNull() ? Value(kNullType) : Value(m_data, doc.GetAllocator()); +} diff --git a/src/base/tools/String.h b/src/base/tools/String.h new file mode 100644 index 000000000..d5f0e844f --- /dev/null +++ b/src/base/tools/String.h @@ -0,0 +1,104 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_STRING_H +#define XMRIG_STRING_H + + +#include +#include +#include + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +/** + * @brief Simple C string wrapper. + * + * 1. I know about std:string. + * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. + * 3. nullptr and JSON conversion supported. + */ +class String +{ +public: + inline String() : m_data(nullptr) {} + inline String(char *str) : m_data(str) {} + inline String(const char *str) : m_data(nullptr) { set(str); } + inline String(const String &other) : m_data(nullptr) { set(other.data()); } + inline String(String &&other) : m_data(other.m_data) { other.m_data = nullptr; } + inline ~String() { free(m_data); } + + + inline bool isEqual(const char *str) const { return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); } + inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; } + + + inline bool isEmpty() const { return size() == 0; } + inline bool isNull() const { return m_data == nullptr; } + inline const char *data() const { return m_data; } + inline size_t size() const { return isNull() ? 0 : strlen(m_data); } + + + inline bool operator!=(const char *str) const { return !isEqual(str); } + inline bool operator!=(const String &str) const { return !isEqual(str.data()); } + inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; } + inline bool operator==(const char *str) const { return isEqual(str); } + inline bool operator==(const String &str) const { return isEqual(str.data()); } + inline String &operator=(char *str) { set(str); return *this; } + inline String &operator=(const char *str) { set(str); return *this; } + inline String &operator=(const String &str) { set(str.data()); return *this; } + inline String &operator=(String &&str) { m_data = str.m_data; str.m_data = nullptr; return *this; } + + rapidjson::Value toJSON() const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + inline void set(const char *str) + { + free(m_data); + + m_data = str != nullptr ? strdup(str) : nullptr; + } + + + inline void set(char *str) + { + free(m_data); + + m_data = str; + } + + + char *m_data; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_STRING_H */ diff --git a/src/common/utils/c_str.h b/src/common/utils/c_str.h index 7ce63754e..fe0164b9e 100644 --- a/src/common/utils/c_str.h +++ b/src/common/utils/c_str.h @@ -21,82 +21,19 @@ * along with this program. If not, see . */ -#ifndef __C_STR_H__ -#define __C_STR_H__ +#ifndef XMRIG_C_STR_H +#define XMRIG_C_STR_H -#include -#include - -#include +#include "base/tools/String.h" namespace xmrig { -/** - * @brief Simple C string wrapper. - * - * 1. I know about std:string. - * 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds. - */ -class c_str -{ -public: - inline c_str() : m_data(nullptr) {} - inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; } - inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); } - inline c_str(const char *str) : m_data(nullptr) { set(str); } - inline ~c_str() { free(m_data); } - - - inline void set(const char *str) - { - free(m_data); - - m_data = str != nullptr ? strdup(str) : nullptr; - } - - - inline void set(char *str) - { - free(m_data); - - m_data = str; - } - - - inline bool isEqual(const char *str) const - { - return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); - } - - - inline bool contains(const char *str) const - { - return strstr(m_data, str) != nullptr; - } - - - inline bool isNull() const { return m_data == nullptr; } - inline const char *data() const { return m_data; } - inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); } - - - inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); } - inline bool operator!=(const char *str) const { return !isEqual(str); } - inline bool operator==(const c_str &str) const { return isEqual(str.data()); } - inline bool operator==(const char *str) const { return isEqual(str); } - inline c_str &operator=(char *str) { set(str); return *this; } - inline c_str &operator=(const c_str &str) { set(str.data()); return *this; } - inline c_str &operator=(const char *str) { set(str); return *this; } - - -private: - char *m_data; -}; +typedef String c_str; } /* namespace xmrig */ -#endif /* __C_STR_H__ */ +#endif /* XMRIG_C_STR_H */ diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index a12a0226a..2ad9a7b0a 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -51,8 +51,8 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value obj(kObjectType); - obj.AddMember("cert", cert() ? Value(StringRef(cert())).Move() : Value(kNullType).Move(), allocator); - obj.AddMember("key", key() ? Value(StringRef(key())).Move() : Value(kNullType).Move(), allocator); + obj.AddMember("cert", m_cert.toJSON(), allocator); + obj.AddMember("key", m_key.toJSON(), allocator); return obj; } diff --git a/src/proxy/tls/TlsConfig.h b/src/proxy/tls/TlsConfig.h index f1e814358..6a12a2bad 100644 --- a/src/proxy/tls/TlsConfig.h +++ b/src/proxy/tls/TlsConfig.h @@ -26,7 +26,7 @@ #define XMRIG_TLSCONFIG_H -#include "common/utils/c_str.h" +#include "base/tools/String.h" #include "rapidjson/fwd.h" @@ -40,7 +40,7 @@ class TlsConfig TlsConfig(const rapidjson::Value &object); ~TlsConfig(); - inline bool isValid() const { return !m_cert.isNull() && !m_key.isNull(); } + inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); } inline const char *cert() const { return m_cert.data(); } inline const char *key() const { return m_key.data(); } inline void setCert(const char *cert) { m_cert = cert; } @@ -49,8 +49,8 @@ class TlsConfig rapidjson::Value toJSON(rapidjson::Document &doc) const; private: - c_str m_cert; - c_str m_key; + String m_cert; + String m_key; }; From 00faf6769cbb3fc5b776a8d803c8c72e1abcf549 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 26 Oct 2018 22:33:00 +0700 Subject: [PATCH 11/34] "key" renamed to "cert_key", added "protocols", "ciphers", "ciphersuites", "dhparam" options. --- src/base/tools/String.h | 1 - src/proxy/tls/TlsConfig.cpp | 85 +++++++++++++++++++++++++-- src/proxy/tls/TlsConfig.h | 30 ++++++++-- src/proxy/tls/TlsContext.cpp | 111 ++++++++++++++++++++++++++++++++++- src/proxy/tls/TlsContext.h | 8 +++ 5 files changed, 222 insertions(+), 13 deletions(-) diff --git a/src/base/tools/String.h b/src/base/tools/String.h index d5f0e844f..ef6f17f64 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -25,7 +25,6 @@ #define XMRIG_STRING_H -#include #include #include diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index 2ad9a7b0a..d7aee6f0a 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -27,15 +27,28 @@ #include "rapidjson/document.h" -xmrig::TlsConfig::TlsConfig() +xmrig::TlsConfig::TlsConfig() : + m_protocols(0) { } -xmrig::TlsConfig::TlsConfig(const rapidjson::Value &object) +/** + * "cert" load TLS certificate chain from file. + * "cert_key" load TLS private key from file. + * "ciphers" set list of available ciphers (TLSv1.2 and below). + * "ciphersuites" set list of available TLSv1.3 ciphersuites. + * "dhparam" load DH parameters for DHE ciphers from file. + */ +xmrig::TlsConfig::TlsConfig(const rapidjson::Value &object) : + m_protocols(0) { + setProtocols(object["protocols"]); setCert(object["cert"].GetString()); - setKey(object["key"].GetString()); + setKey(object["cert_key"].GetString()); + setCiphers(object["ciphers"].GetString()); + setCipherSuites(object["ciphersuites"].GetString()); + setDH(object["dhparam"].GetString()); } @@ -51,8 +64,70 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value obj(kObjectType); - obj.AddMember("cert", m_cert.toJSON(), allocator); - obj.AddMember("key", m_key.toJSON(), allocator); + if (m_protocols > 0) { + Value protocols(kArrayType); + + if (m_protocols & TLSv1) { + protocols.PushBack("TLSv1", allocator); + } + + if (m_protocols & TLSv1_1) { + protocols.PushBack("TLSv1.1", allocator); + } + + if (m_protocols & TLSv1_2) { + protocols.PushBack("TLSv1.2", allocator); + } + + if (m_protocols & TLSv1_3) { + protocols.PushBack("TLSv1.3", allocator); + } + + obj.AddMember("protocols", protocols, allocator); + } + else { + obj.AddMember("protocols", kNullType, allocator); + } + + obj.AddMember("cert", m_cert.toJSON(), allocator); + obj.AddMember("cert_key", m_key.toJSON(), allocator); + obj.AddMember("ciphers", m_ciphers.toJSON(), allocator); + obj.AddMember("ciphersuites", m_cipherSuites.toJSON(), allocator); + obj.AddMember("dhparam", m_dhparam.toJSON(), allocator); return obj; } + + +void xmrig::TlsConfig::setProtocols(const rapidjson::Value &protocols) +{ + m_protocols = 0; + + if (protocols.IsUint()) { + return setProtocols(protocols.GetUint()); + } + + if (!protocols.IsArray()) { + return; + } + + for (const rapidjson::Value &value : protocols.GetArray()) { + const char *protocol = value.GetString(); + if (protocol == nullptr) { + continue; + } + + if (strcmp(protocol, "TLSv1") == 0) { + m_protocols |= TLSv1; + } + else if (strcmp(protocol, "TLSv1.1") == 0) { + m_protocols |= TLSv1_1; + } + else if (strcmp(protocol, "TLSv1.2") == 0) { + m_protocols |= TLSv1_2; + } + else if (strcmp(protocol, "TLSv1.3") == 0) { + m_protocols |= TLSv1_3; + } + } +} diff --git a/src/proxy/tls/TlsConfig.h b/src/proxy/tls/TlsConfig.h index 6a12a2bad..f67e1ed6d 100644 --- a/src/proxy/tls/TlsConfig.h +++ b/src/proxy/tls/TlsConfig.h @@ -36,20 +36,40 @@ namespace xmrig { class TlsConfig { public: + enum Versions { + TLSv1 = 1, + TLSv1_1 = 2, + TLSv1_2 = 4, + TLSv1_3 = 8 + }; + TlsConfig(); TlsConfig(const rapidjson::Value &object); ~TlsConfig(); - inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); } - inline const char *cert() const { return m_cert.data(); } - inline const char *key() const { return m_key.data(); } - inline void setCert(const char *cert) { m_cert = cert; } - inline void setKey(const char *key) { m_key = key; } + inline bool isValid() const { return !m_cert.isEmpty() && !m_key.isEmpty(); } + inline const char *cert() const { return m_cert.data(); } + inline const char *ciphers() const { return m_ciphers.isEmpty() ? nullptr : m_ciphers.data(); } + inline const char *cipherSuites() const { return m_cipherSuites.isEmpty() ? nullptr : m_cipherSuites.data(); } + inline const char *dhparam() const { return m_dhparam.isEmpty() ? nullptr : m_dhparam.data(); } + inline const char *key() const { return m_key.data(); } + inline uint32_t protocols() const { return m_protocols; } + inline void setCert(const char *cert) { m_cert = cert; } + inline void setCiphers(const char *ciphers) { m_ciphers = ciphers; } + inline void setCipherSuites(const char *ciphers) { m_cipherSuites = ciphers; } + inline void setDH(const char *dhparam) { m_dhparam = dhparam; } + inline void setKey(const char *key) { m_key = key; } + inline void setProtocols(uint32_t protocols) { m_protocols = protocols; } rapidjson::Value toJSON(rapidjson::Document &doc) const; + void setProtocols(const rapidjson::Value &protocols); private: + uint32_t m_protocols; String m_cert; + String m_ciphers; + String m_cipherSuites; + String m_dhparam; String m_key; }; diff --git a/src/proxy/tls/TlsContext.cpp b/src/proxy/tls/TlsContext.cpp index 9b81701d1..0ff3fd77f 100644 --- a/src/proxy/tls/TlsContext.cpp +++ b/src/proxy/tls/TlsContext.cpp @@ -60,18 +60,125 @@ bool xmrig::TlsContext::load(const TlsConfig &config) } if (SSL_CTX_use_certificate_chain_file(m_ctx, config.cert()) <= 0) { - LOG_ERR("Failed to load certificate chain file \"%s\"", config.cert()); + LOG_ERR("SSL_CTX_use_certificate_chain_file(\"%s\") failed.", config.cert()); return false; } if (SSL_CTX_use_PrivateKey_file(m_ctx, config.key(), SSL_FILETYPE_PEM) <= 0) { - LOG_ERR("Failed to load private key file \"%s\"", config.key()); + LOG_ERR("SSL_CTX_use_PrivateKey_file(\"%s\") failed.", config.key()); return false; } SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); + SSL_CTX_set_options(m_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); + +# if OPENSSL_VERSION_NUMBER >= 0x1010100fL + SSL_CTX_set_max_early_data(m_ctx, 0); +# endif + + setProtocols(config.protocols()); + + return setCiphers(config.ciphers()) && setCipherSuites(config.cipherSuites()) && setDH(config.dhparam()); +} + + +bool xmrig::TlsContext::setCiphers(const char *ciphers) +{ + if (ciphers == nullptr || SSL_CTX_set_cipher_list(m_ctx, ciphers) == 1) { + return true; + } + + LOG_ERR("SSL_CTX_set_cipher_list(\"%s\") failed.", ciphers); return true; } + + +bool xmrig::TlsContext::setCipherSuites(const char *ciphersuites) +{ + if (ciphersuites == nullptr) { + return true; + } + +# if OPENSSL_VERSION_NUMBER >= 0x1010100fL + if (SSL_CTX_set_ciphersuites(m_ctx, ciphersuites) == 1) { + return true; + } +# endif + + LOG_ERR("SSL_CTX_set_ciphersuites(\"%s\") failed.", ciphersuites); + + return false; +} + + +bool xmrig::TlsContext::setDH(const char *dhparam) +{ + if (dhparam == nullptr) { + return true; + } + + BIO *bio = BIO_new_file(dhparam, "r"); + if (bio == nullptr) { + LOG_ERR("BIO_new_file(\"%s\") failed.", dhparam); + + return false; + } + + DH *dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr); + if (dh == nullptr) { + LOG_ERR("PEM_read_bio_DHparams(\"%s\") failed.", dhparam); + + BIO_free(bio); + + return false; + } + + const int rc = SSL_CTX_set_tmp_dh(m_ctx, dh); + + DH_free(dh); + BIO_free(bio); + + if (rc == 0) { + LOG_ERR("SSL_CTX_set_tmp_dh(\"%s\") failed.", dhparam); + + return false; + } + + return true; +} + + +void xmrig::TlsContext::setProtocols(uint32_t protocols) +{ + if (protocols == 0) { + return; + } + + if (!(protocols & TlsConfig::TLSv1)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1); + } + +# ifdef SSL_OP_NO_TLSv1_1 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_1); + if (!(protocols & TlsConfig::TLSv1_1)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_1); + } +# endif + +# ifdef SSL_OP_NO_TLSv1_2 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_2); + if (!(protocols & TlsConfig::TLSv1_2)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_2); + } +# endif + +# ifdef SSL_OP_NO_TLSv1_3 + SSL_CTX_clear_options(m_ctx, SSL_OP_NO_TLSv1_3); + if (!(protocols & TlsConfig::TLSv1_3)) { + SSL_CTX_set_options(m_ctx, SSL_OP_NO_TLSv1_3); + } +# endif +} diff --git a/src/proxy/tls/TlsContext.h b/src/proxy/tls/TlsContext.h index d158a8951..638a500a2 100644 --- a/src/proxy/tls/TlsContext.h +++ b/src/proxy/tls/TlsContext.h @@ -26,6 +26,9 @@ #define XMRIG_TLSCONTEXT_H +#include + + typedef struct ssl_ctx_st SSL_CTX; @@ -46,6 +49,11 @@ class TlsContext inline SSL_CTX *ctx() const { return m_ctx; } private: + bool setCiphers(const char *ciphers); + bool setCipherSuites(const char *ciphersuites); + bool setDH(const char *dhparam); + void setProtocols(uint32_t protocols); + SSL_CTX *m_ctx; }; From a3433de30bd2c023c158d4edc0a7e608cfea819c Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 28 Oct 2018 14:40:32 +0700 Subject: [PATCH 12/34] Add command line TLS options. --- README.md | 8 +++++++ src/common/interfaces/IConfig.h | 31 ++++++++++++++------------- src/common/log/Log.h | 8 +++---- src/core/Config.cpp | 36 ++++++++++++++++++++++++++++++-- src/core/ConfigLoader_platform.h | 26 +++++++++++++++++++---- src/proxy/BindHost.h | 1 + src/proxy/tls/TlsConfig.cpp | 8 ------- 7 files changed, 86 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b0a92a5f5..59b83e86c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,14 @@ Use [config.xmrig.com](https://config.xmrig.com/proxy) to generate, edit or shar --api-worker-id=ID custom worker-id for API --api-no-ipv6 disable IPv6 support for API --api-no-restricted enable full remote access (only if API token set) + --tls enable SSL/TLS support for pool connection (needs pool support) + --tls-bind=ADDR bind to specified address with enabled TLS + --tls-cert=FILE load TLS certificate chain from a file in the PEM format + --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format + --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format + --tls-protocols=N enable specified TLS protocols, value is bitmask: v1=1, v1.1=2, v1.2=4, v1.3=8 + --tls-ciphers=S set list of available ciphers (TLSv1.2 and below) + --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites -h, --help display this help and exit -V, --version output version information and exit ``` diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 2eebb7aa2..0c8cfc28d 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -97,20 +97,23 @@ class IConfig OclCompModeKey = 1410, // xmrig-proxy - AccessLogFileKey = 'A', - BindKey = 'b', - CoinKey = 1104, - CustomDiffKey = 1102, - DebugKey = 1101, - ModeKey = 'm', - PoolCoinKey = 'C', - ReuseTimeoutKey = 1106, - WorkersKey = 1103, - WorkersAdvKey = 1107, - TlsBindKey = 1108, - TlsCertKey = 1109, - TlsCertKeyKey = 1110, - TlsDHparamKey = 1111, + AccessLogFileKey = 'A', + BindKey = 'b', + CoinKey = 1104, + CustomDiffKey = 1102, + DebugKey = 1101, + ModeKey = 'm', + PoolCoinKey = 'C', + ReuseTimeoutKey = 1106, + WorkersKey = 1103, + WorkersAdvKey = 1107, + TlsBindKey = 1108, + TlsCertKey = 1109, + TlsCertKeyKey = 1110, + TlsDHparamKey = 1111, + TlsCiphersKey = 1112, + TlsCipherSuitesKey = 1113, + TlsProtocolsKey = 1114, // xmrig nvidia CudaMaxThreadsKey = 1200, diff --git a/src/common/log/Log.h b/src/common/log/Log.h index 2774ae0c5..788ad2631 100644 --- a/src/common/log/Log.h +++ b/src/common/log/Log.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __LOG_H__ -#define __LOG_H__ +#ifndef XMRIG_LOG_H +#define XMRIG_LOG_H #include @@ -39,7 +39,7 @@ class Log static inline Log* i() { if (!m_self) { defaultInit(); } return m_self; } static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } static inline void init() { if (!m_self) { new Log(); } } - static inline void release() { assert(m_self != nullptr); delete m_self; } + static inline void release() { delete m_self; } void message(ILogBackend::Level level, const char* fmt, ...); void text(const char* fmt, ...); @@ -98,4 +98,4 @@ class Log # define LOG_DEBUG_WARN(x, ...) #endif -#endif /* __LOG_H__ */ +#endif /* XMRIG_LOG_H */ diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 43c718209..5c0e5fdc0 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -213,9 +213,12 @@ bool xmrig::Config::parseString(int key, const char *arg) setMode(arg); break; - case BindKey: /* --bind */ + case BindKey: /* --bind */ + case TlsBindKey: /* --tls-bind */ { xmrig::BindHost host(arg); + host.setTLS(key == TlsBindKey); + if (host.isValid()) { m_bind.push_back(std::move(host)); } @@ -240,9 +243,32 @@ bool xmrig::Config::parseString(int key, const char *arg) m_workersMode = Workers::parseMode(arg); break; - case CustomDiffKey: /* --custom-diff */ + case CustomDiffKey: /* --custom-diff */ + case TlsProtocolsKey: /* --tls-protocols */ return parseUint64(key, strtol(arg, nullptr, 10)); +# ifndef XMRIG_NO_TLS + case TlsCertKey: /* --tls-cert */ + m_tls.setCert(arg); + break; + + case TlsCertKeyKey: /* --tls-cert-key */ + m_tls.setKey(arg); + break; + + case TlsDHparamKey: /* --tls-dhparam */ + m_tls.setDH(arg); + break; + + case TlsCiphersKey: /* --tls-ciphers */ + m_tls.setCiphers(arg); + break; + + case TlsCipherSuitesKey: /* --tls-ciphersuites */ + m_tls.setCipherSuites(arg); + break; +# endif + default: break; } @@ -268,6 +294,12 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) m_reuseTimeout = static_cast(arg); break; +# ifndef XMRIG_NO_TLS + case TlsProtocolsKey: /* --tls-protocols */ + m_tls.setProtocols(static_cast(arg)); + break; +# endif + default: break; } diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index 6f9cb686a..a0432ac6a 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -51,7 +51,6 @@ Options:\n\ -u, --user=USERNAME username for mining server\n\ -p, --pass=PASSWORD password for mining server\n\ --rig-id=ID rig identifier for pool-side statistics (needs pool support)\n\ - --tls enable SSL/TLS support (needs pool support)\n\ --tls-fingerprint=F pool TLS certificate fingerprint, if set enable strict certificate pinning\n\ -k, --keepalive prevent timeout (needs pool support)\n\ -r, --retries=N number of times to retry before switch to backup server (default: 1)\n\ @@ -68,10 +67,10 @@ Options:\n\ -c, --config=FILE load a JSON-format configuration file\n\ --no-watch disable configuration file watching\n\ -l, --log-file=FILE log all output to a file\n" -# ifdef HAVE_SYSLOG_H +#ifdef HAVE_SYSLOG_H "\ -S, --syslog use system log for output messages\n" -# endif +#endif "\ -A --access-log-file=N log all workers access to a file\n\ --api-port=N port for the miner API\n\ @@ -79,7 +78,19 @@ Options:\n\ --api-worker-id=ID custom worker-id (instance name) for API\n\ --api-id=ID custom instance ID for API\n\ --api-ipv6 enable IPv6 support for API\n\ - --api-no-restricted enable full remote access (only if API token set)\n\ + --api-no-restricted enable full remote access (only if API token set)\n" +#ifndef XMRIG_NO_TLS +"\ + --tls enable SSL/TLS support for pool connection (needs pool support)\n\ + --tls-bind=ADDR bind to specified address with enabled TLS\n\ + --tls-cert=FILE load TLS certificate chain from a file in the PEM format\n\ + --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format\n\ + --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format\n\ + --tls-protocols=N enable specified TLS protocols, value is bitmask: v1=1, v1.1=2, v1.2=4, v1.3=8\n\ + --tls-ciphers=S set list of available ciphers (TLSv1.2 and below)\n\ + --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites\n" +#endif +"\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ "; @@ -128,6 +139,13 @@ static struct option const options[] = { { "rig-id", 1, nullptr, xmrig::IConfig::RigIdKey }, { "tls", 0, nullptr, xmrig::IConfig::TlsKey }, { "tls-fingerprint", 1, nullptr, xmrig::IConfig::FingerprintKey }, + { "tls-bind", 1, nullptr, xmrig::IConfig::TlsBindKey }, + { "tls-cert", 1, nullptr, xmrig::IConfig::TlsCertKey }, + { "tls-cert-key", 1, nullptr, xmrig::IConfig::TlsCertKeyKey }, + { "tls-dhparam", 1, nullptr, xmrig::IConfig::TlsDHparamKey }, + { "tls-protocols", 1, nullptr, xmrig::IConfig::TlsProtocolsKey }, + { "tls-ciphers", 1, nullptr, xmrig::IConfig::TlsCiphersKey }, + { "tls-ciphersuites", 1, nullptr, xmrig::IConfig::TlsCipherSuitesKey}, { nullptr, 0, nullptr, 0 } }; diff --git a/src/proxy/BindHost.h b/src/proxy/BindHost.h index 5151f641a..e142b1fa7 100644 --- a/src/proxy/BindHost.h +++ b/src/proxy/BindHost.h @@ -60,6 +60,7 @@ class BindHost inline bool isValid() const { return m_version && !m_host.isNull() && m_port > 0; } inline const char *host() const { return m_host.data(); } inline uint16_t port() const { return m_port; } + inline void setTLS(bool enable) { m_tls = enable; } private: bool parseHost(const char *host); diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index d7aee6f0a..7ea8d83e0 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -32,14 +32,6 @@ xmrig::TlsConfig::TlsConfig() : { } - -/** - * "cert" load TLS certificate chain from file. - * "cert_key" load TLS private key from file. - * "ciphers" set list of available ciphers (TLSv1.2 and below). - * "ciphersuites" set list of available TLSv1.3 ciphersuites. - * "dhparam" load DH parameters for DHE ciphers from file. - */ xmrig::TlsConfig::TlsConfig(const rapidjson::Value &object) : m_protocols(0) { From 053e40172bf5899c4f01822777f54300029da896 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 28 Oct 2018 15:07:48 +0700 Subject: [PATCH 13/34] Add information about TLS bind to summary. --- src/Summary.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Summary.cpp b/src/Summary.cpp index 0f1b4b2d7..2cce673fd 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -55,14 +55,27 @@ static void print_bind(xmrig::Controller *controller) { const xmrig::BindHosts &bind = controller->config()->bind(); - for (size_t i = 0; i < bind.size(); ++i) { - Log::i()->text(controller->config()->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("BIND #%-7zu") CYAN("%s%s%s:") CYAN_BOLD("%d") - : " * BIND #%-7zu%s%s%s:%d", - i + 1, - bind[i].isIPv6() ? "[" : "", - bind[i].host(), - bind[i].isIPv6() ? "]" : "", - bind[i].port()); + if (controller->config()->isColors()) { + for (size_t i = 0; i < bind.size(); ++i) { + Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("BIND #%-7zu") CYAN("%s%s%s:") "\x1B[1;%dm%d\x1B[0m", + i + 1, + bind[i].isIPv6() ? "[" : "", + bind[i].host(), + bind[i].isIPv6() ? "]" : "", + bind[i].isTLS() ? 32 : 36, + bind[i].port()); + } + } + else { + for (size_t i = 0; i < bind.size(); ++i) { + Log::i()->text(" * BIND #%-7zu%s%s%s:%d TLS=%d", + i + 1, + bind[i].isIPv6() ? "[" : "", + bind[i].host(), + bind[i].isIPv6() ? "]" : "", + bind[i].port(), + static_cast(bind[i].isTLS())); + } } } From ec598387af1e8727a4179f3b2bc657ecbe0ed2c4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 28 Oct 2018 15:18:05 +0700 Subject: [PATCH 14/34] Update default config.json. --- src/config.json | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/config.json b/src/config.json index a206e9bcf..c742bf401 100644 --- a/src/config.json +++ b/src/config.json @@ -11,8 +11,16 @@ }, "background": false, "bind": [ - "0.0.0.0:3333", - "[::]:3333" + { + "host": "0.0.0.0", + "port": 3333, + "tls": false + }, + { + "host": "::", + "port": 3333, + "tls": false + } ], "colors": true, "custom-diff": 0, @@ -34,6 +42,14 @@ "retries": 2, "retry-pause": 1, "reuse-timeout": 0, + "tls": { + "protocols": null, + "cert": null, + "cert_key": null, + "ciphers": null, + "ciphersuites": null, + "dhparam": null + }, "user-agent": null, "verbose": false, "watch": true, From 9570b72ea8ad71666f77c054d3c83cc4830d4d4e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 28 Oct 2018 15:33:17 +0700 Subject: [PATCH 15/34] v2.9.0-dev --- src/version.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/version.h b/src/version.h index 8023dcfc1..3fedce1fb 100644 --- a/src/version.h +++ b/src/version.h @@ -27,15 +27,15 @@ #define APP_ID "xmrig-proxy" #define APP_NAME "xmrig-proxy" #define APP_DESC "XMRig Stratum proxy" -#define APP_VERSION "2.8.2-dev" +#define APP_VERSION "2.9.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "proxy" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 8 -#define APP_VER_PATCH 2 +#define APP_VER_MINOR 9 +#define APP_VER_PATCH 0 #ifdef _MSC_VER # if (_MSC_VER >= 1910) From daaf55f98d18fe45499579ba9f862361115c1544 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 29 Oct 2018 21:17:45 +0700 Subject: [PATCH 16/34] Fixed uninitialized variable. --- src/proxy/Proxy.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/proxy/Proxy.cpp b/src/proxy/Proxy.cpp index c216c8004..173000ba1 100644 --- a/src/proxy/Proxy.cpp +++ b/src/proxy/Proxy.cpp @@ -59,7 +59,8 @@ Proxy::Proxy(xmrig::Controller *controller) : m_customDiff(controller), m_ticks(0), - m_controller(controller) + m_controller(controller), + m_tls(nullptr) { srand(time(0) ^ (uintptr_t) this); From 1379d83e008710c4b43a84f4aad17b301c0009fa Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 29 Oct 2018 23:35:16 +0700 Subject: [PATCH 17/34] Optimize Server class. --- src/proxy/Server.cpp | 9 ++++----- src/proxy/Server.h | 5 ++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/proxy/Server.cpp b/src/proxy/Server.cpp index 9a1a0ee6f..684b5e5a7 100644 --- a/src/proxy/Server.cpp +++ b/src/proxy/Server.cpp @@ -30,11 +30,10 @@ Server::Server(const xmrig::BindHost &host, const xmrig::TlsContext *ctx) : - m_tls(host.isTLS()), + m_ctx(host.isTLS() ? ctx : nullptr), + m_version(0), m_port(host.port()), - m_host(host.host()), - m_ctx(ctx), - m_version(0) + m_host(host.host()) { uv_tcp_init(uv_default_loop(), &m_server); m_server.data = this; @@ -78,7 +77,7 @@ void Server::create(uv_stream_t *server, int status) return; } - Miner *miner = new Miner(m_tls ? m_ctx : nullptr, m_version == 6, m_port); + Miner *miner = new Miner(m_ctx, m_version == 6, m_port); if (!miner) { return; } diff --git a/src/proxy/Server.h b/src/proxy/Server.h index e5a1fee22..60e6721e3 100644 --- a/src/proxy/Server.h +++ b/src/proxy/Server.h @@ -48,14 +48,13 @@ class Server static void onConnection(uv_stream_t *server, int status); - const bool m_tls; - const uint16_t m_port; - const xmrig::c_str m_host; const xmrig::TlsContext *m_ctx; int m_version; sockaddr_in m_addr; sockaddr_in6 m_addr6; + uint16_t m_port; uv_tcp_t m_server; + xmrig::c_str m_host; }; #endif /* XMRIG_SERVER_H */ From 438409caea10558dd6209c64b99caf242709cb5f Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 30 Oct 2018 19:45:13 +0700 Subject: [PATCH 18/34] Simplify protocols option. --- src/base/tools/String.cpp | 157 ++++++++++++++++++++++++++++++++++++ src/base/tools/String.h | 61 +++++++------- src/core/Config.cpp | 11 +-- src/proxy/tls/TlsConfig.cpp | 61 ++++++++------ src/proxy/tls/TlsConfig.h | 1 + 5 files changed, 227 insertions(+), 64 deletions(-) diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp index adad51e6c..ca5dfbe73 100644 --- a/src/base/tools/String.cpp +++ b/src/base/tools/String.cpp @@ -26,6 +26,22 @@ #include "rapidjson/document.h" +bool xmrig::String::isEqual(const char *str) const +{ + return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && str == nullptr); +} + + +bool xmrig::String::isEqual(const String &other) const +{ + if (m_size != other.m_size) { + return false; + } + + return (m_data != nullptr && other.m_data != nullptr && memcmp(m_data, other.m_data, m_size) == 0) || (m_data == nullptr && other.m_data == nullptr); +} + + rapidjson::Value xmrig::String::toJSON() const { using namespace rapidjson; @@ -40,3 +56,144 @@ rapidjson::Value xmrig::String::toJSON(rapidjson::Document &doc) const return isNull() ? Value(kNullType) : Value(m_data, doc.GetAllocator()); } + + +std::vector xmrig::String::split(char sep) const +{ + std::vector out; + if (m_size == 0) { + return out; + } + + size_t start = 0; + size_t pos = 0; + + for (pos = 0; pos < m_size; ++pos) { + if (m_data[pos] == sep) { + if ((pos - start) > 0) { + out.push_back(std::move(String(m_data + start, pos - start))); + } + + start = pos + 1; + } + } + + if ((pos - start) > 0) { + out.push_back(std::move(String(m_data + start, pos - start))); + } + + return out; +} + + +xmrig::String xmrig::String::join(const std::vector &vec, char sep) +{ + if (vec.empty()) { + return String(); + } + + size_t size = vec.size(); + for (const String &str : vec) { + size += str.size(); + } + + size_t offset = 0; + char *buf = new char[size]; + + for (const String &str : vec) { + memcpy(buf + offset, str.data(), str.size()); + + offset += str.size() + 1; + + if (offset < size) { + buf[offset - 1] = sep; + } + } + + buf[size - 1] = '\0'; + + return String(buf); +} + + +void xmrig::String::copy(const char *str) +{ + delete [] m_data; + + if (str == nullptr) { + m_size = 0; + m_data = nullptr; + + return; + } + + m_size = strlen(str); + m_data = new char[m_size + 1]; + + memcpy(m_data, str, m_size + 1); +} + + +void xmrig::String::copy(const String &other) +{ + if (m_size > 0) { + if (m_size == other.m_size) { + memcpy(m_data, other.m_data, m_size + 1); + + return; + } + + delete [] m_data; + } + + if (other.m_data == nullptr) { + m_size = 0; + m_data = nullptr; + + return; + } + + m_size = other.m_size; + m_data = new char[m_size + 1]; + + memcpy(m_data, other.m_data, m_size + 1); +} + + +void xmrig::String::create(const char *str, size_t size) +{ + if (str == nullptr) { + m_size = 0; + m_data = nullptr; + + return; + } + + m_size = size; + m_data = new char[m_size + 1]; + + memcpy(m_data, str, m_size); + + m_data[m_size] = '\0'; +} + + +void xmrig::String::move(char *str) +{ + delete [] m_data; + + m_size = str == nullptr ? 0 : strlen(str); + m_data = str; +} + + +void xmrig::String::move(String &&other) +{ + delete [] m_data; + + m_data = other.m_data; + m_size = other.m_size; + + other.m_data = nullptr; + other.m_size = 0; +} diff --git a/src/base/tools/String.h b/src/base/tools/String.h index ef6f17f64..129aa544f 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -25,8 +25,8 @@ #define XMRIG_STRING_H -#include -#include +#include +#include #include "rapidjson/fwd.h" @@ -45,55 +45,54 @@ namespace xmrig { class String { public: - inline String() : m_data(nullptr) {} - inline String(char *str) : m_data(str) {} - inline String(const char *str) : m_data(nullptr) { set(str); } - inline String(const String &other) : m_data(nullptr) { set(other.data()); } - inline String(String &&other) : m_data(other.m_data) { other.m_data = nullptr; } - inline ~String() { free(m_data); } + inline String() : m_data(nullptr), m_size(0) {} + inline String(char *str) : m_data(str), m_size(str == nullptr ? 0 : strlen(str)) {} + inline String(const char *str) : m_data(nullptr) { copy(str); } + inline String(const char *str, size_t size) : m_data(nullptr) { create(str, size); } + inline String(const String &other) : m_data(nullptr) { copy(other); } + inline String(String &&other) : m_data(other.m_data), m_size(other.m_size) { other.m_data = nullptr; other.m_size = 0; } + inline ~String() { delete [] m_data; } + + + bool isEqual(const char *str) const; + bool isEqual(const String &other) const; - inline bool isEqual(const char *str) const { return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && m_data == nullptr); } inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; } inline bool isEmpty() const { return size() == 0; } inline bool isNull() const { return m_data == nullptr; } + inline char *data() { return m_data; } inline const char *data() const { return m_data; } - inline size_t size() const { return isNull() ? 0 : strlen(m_data); } + inline size_t size() const { return m_size; } inline bool operator!=(const char *str) const { return !isEqual(str); } - inline bool operator!=(const String &str) const { return !isEqual(str.data()); } + inline bool operator!=(const String &other) const { return !isEqual(other); } inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; } inline bool operator==(const char *str) const { return isEqual(str); } - inline bool operator==(const String &str) const { return isEqual(str.data()); } - inline String &operator=(char *str) { set(str); return *this; } - inline String &operator=(const char *str) { set(str); return *this; } - inline String &operator=(const String &str) { set(str.data()); return *this; } - inline String &operator=(String &&str) { m_data = str.m_data; str.m_data = nullptr; return *this; } + inline bool operator==(const String &other) const { return isEqual(other); } + inline String &operator=(char *str) { move(str); return *this; } + inline String &operator=(const char *str) { copy(str); return *this; } + inline String &operator=(const String &str) { copy(str); return *this; } + inline String &operator=(String &&other) { move(std::move(other)); return *this; } rapidjson::Value toJSON() const; rapidjson::Value toJSON(rapidjson::Document &doc) const; + std::vector split(char sep) const; -private: - inline void set(const char *str) - { - free(m_data); - - m_data = str != nullptr ? strdup(str) : nullptr; - } - - - inline void set(char *str) - { - free(m_data); - - m_data = str; - } + static String join(const std::vector &vec, char sep); +private: + void copy(const char *str); + void copy(const String &other); + void create(const char *str, size_t size); + void move(char *str); + void move(String &&other); char *m_data; + size_t m_size; }; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 5c0e5fdc0..c681473d4 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -244,7 +244,6 @@ bool xmrig::Config::parseString(int key, const char *arg) break; case CustomDiffKey: /* --custom-diff */ - case TlsProtocolsKey: /* --tls-protocols */ return parseUint64(key, strtol(arg, nullptr, 10)); # ifndef XMRIG_NO_TLS @@ -267,6 +266,10 @@ bool xmrig::Config::parseString(int key, const char *arg) case TlsCipherSuitesKey: /* --tls-ciphersuites */ m_tls.setCipherSuites(arg); break; + + case TlsProtocolsKey: /* --tls-protocols */ + m_tls.setProtocols(arg); + break; # endif default: @@ -294,12 +297,6 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) m_reuseTimeout = static_cast(arg); break; -# ifndef XMRIG_NO_TLS - case TlsProtocolsKey: /* --tls-protocols */ - m_tls.setProtocols(static_cast(arg)); - break; -# endif - default: break; } diff --git a/src/proxy/tls/TlsConfig.cpp b/src/proxy/tls/TlsConfig.cpp index 7ea8d83e0..52370c1da 100644 --- a/src/proxy/tls/TlsConfig.cpp +++ b/src/proxy/tls/TlsConfig.cpp @@ -32,6 +32,14 @@ xmrig::TlsConfig::TlsConfig() : { } + +/** + * "cert" load TLS certificate chain from file. + * "cert_key" load TLS private key from file. + * "ciphers" set list of available ciphers (TLSv1.2 and below). + * "ciphersuites" set list of available TLSv1.3 ciphersuites. + * "dhparam" load DH parameters for DHE ciphers from file. + */ xmrig::TlsConfig::TlsConfig(const rapidjson::Value &object) : m_protocols(0) { @@ -57,25 +65,25 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const Value obj(kObjectType); if (m_protocols > 0) { - Value protocols(kArrayType); + std::vector protocols; if (m_protocols & TLSv1) { - protocols.PushBack("TLSv1", allocator); + protocols.push_back("TLSv1"); } if (m_protocols & TLSv1_1) { - protocols.PushBack("TLSv1.1", allocator); + protocols.push_back("TLSv1.1"); } if (m_protocols & TLSv1_2) { - protocols.PushBack("TLSv1.2", allocator); + protocols.push_back("TLSv1.2"); } if (m_protocols & TLSv1_3) { - protocols.PushBack("TLSv1.3", allocator); + protocols.push_back("TLSv1.3"); } - obj.AddMember("protocols", protocols, allocator); + obj.AddMember("protocols", String::join(protocols, ' ').toJSON(doc), allocator); } else { obj.AddMember("protocols", kNullType, allocator); @@ -91,35 +99,36 @@ rapidjson::Value xmrig::TlsConfig::toJSON(rapidjson::Document &doc) const } -void xmrig::TlsConfig::setProtocols(const rapidjson::Value &protocols) +void xmrig::TlsConfig::setProtocols(const char *protocols) { - m_protocols = 0; - - if (protocols.IsUint()) { - return setProtocols(protocols.GetUint()); - } - - if (!protocols.IsArray()) { - return; - } - - for (const rapidjson::Value &value : protocols.GetArray()) { - const char *protocol = value.GetString(); - if (protocol == nullptr) { - continue; - } + const std::vector vec = String(protocols).split(' '); - if (strcmp(protocol, "TLSv1") == 0) { + for (const String &value : vec) { + if (value == "TLSv1") { m_protocols |= TLSv1; } - else if (strcmp(protocol, "TLSv1.1") == 0) { + else if (value == "TLSv1.1") { m_protocols |= TLSv1_1; } - else if (strcmp(protocol, "TLSv1.2") == 0) { + else if (value == "TLSv1.2") { m_protocols |= TLSv1_2; } - else if (strcmp(protocol, "TLSv1.3") == 0) { + else if (value == "TLSv1.3") { m_protocols |= TLSv1_3; } } } + + +void xmrig::TlsConfig::setProtocols(const rapidjson::Value &protocols) +{ + m_protocols = 0; + + if (protocols.IsUint()) { + return setProtocols(protocols.GetUint()); + } + + if (protocols.IsString()) { + return setProtocols(protocols.GetString()); + } +} diff --git a/src/proxy/tls/TlsConfig.h b/src/proxy/tls/TlsConfig.h index f67e1ed6d..461abd5b3 100644 --- a/src/proxy/tls/TlsConfig.h +++ b/src/proxy/tls/TlsConfig.h @@ -62,6 +62,7 @@ class TlsConfig inline void setProtocols(uint32_t protocols) { m_protocols = protocols; } rapidjson::Value toJSON(rapidjson::Document &doc) const; + void setProtocols(const char *protocols); void setProtocols(const rapidjson::Value &protocols); private: From 704b07028e4a2565a13eb168cb9f2e5a22ee6d44 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 30 Oct 2018 20:30:53 +0700 Subject: [PATCH 19/34] Update help output. --- README.md | 2 +- src/core/ConfigLoader_platform.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 59b83e86c..2de6801e2 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Use [config.xmrig.com](https://config.xmrig.com/proxy) to generate, edit or shar --tls-cert=FILE load TLS certificate chain from a file in the PEM format --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format - --tls-protocols=N enable specified TLS protocols, value is bitmask: v1=1, v1.1=2, v1.2=4, v1.3=8 + --tls-protocols=N enable specified TLS protocols, example: "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3" --tls-ciphers=S set list of available ciphers (TLSv1.2 and below) --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites -h, --help display this help and exit diff --git a/src/core/ConfigLoader_platform.h b/src/core/ConfigLoader_platform.h index a0432ac6a..59ae18bd8 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/ConfigLoader_platform.h @@ -86,7 +86,7 @@ Options:\n\ --tls-cert=FILE load TLS certificate chain from a file in the PEM format\n\ --tls-cert-key=FILE load TLS certificate private key from a file in the PEM format\n\ --tls-dhparam=FILE load DH parameters for DHE ciphers from a file in the PEM format\n\ - --tls-protocols=N enable specified TLS protocols, value is bitmask: v1=1, v1.1=2, v1.2=4, v1.3=8\n\ + --tls-protocols=N enable specified TLS protocols, example: \"TLSv1 TLSv1.1 TLSv1.2 TLSv1.3\"\n\ --tls-ciphers=S set list of available ciphers (TLSv1.2 and below)\n\ --tls-ciphersuites=S set list of available TLSv1.3 ciphersuites\n" #endif From 5c1235181a657524f38b23774c244d58b869c0cf Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 1 Nov 2018 21:49:58 +0700 Subject: [PATCH 20/34] Optimize String creation. --- src/base/tools/String.cpp | 62 +++++++++++++++++++++++++++------------ src/base/tools/String.h | 11 +++---- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp index ca5dfbe73..fe2792c72 100644 --- a/src/base/tools/String.cpp +++ b/src/base/tools/String.cpp @@ -26,6 +26,48 @@ #include "rapidjson/document.h" +xmrig::String::String(const char *str) : + m_data(nullptr), + m_size(str == nullptr ? 0 : strlen(str)) +{ + if (m_size == 0) { + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, str, m_size + 1); +} + + +xmrig::String::String(const char *str, size_t size) : + m_data(nullptr), + m_size(size) +{ + if (str == nullptr) { + m_size = 0; + + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, str, m_size); + m_data[m_size] = '\0'; +} + + +xmrig::String::String(const String &other) : + m_data(nullptr), + m_size(other.m_size) +{ + if (other.m_data == nullptr) { + return; + } + + m_data = new char[m_size + 1]; + memcpy(m_data, other.m_data, m_size + 1); +} + + bool xmrig::String::isEqual(const char *str) const { return (m_data != nullptr && str != nullptr && strcmp(m_data, str) == 0) || (m_data == nullptr && str == nullptr); @@ -146,6 +188,8 @@ void xmrig::String::copy(const String &other) delete [] m_data; } + delete [] m_data; + if (other.m_data == nullptr) { m_size = 0; m_data = nullptr; @@ -160,24 +204,6 @@ void xmrig::String::copy(const String &other) } -void xmrig::String::create(const char *str, size_t size) -{ - if (str == nullptr) { - m_size = 0; - m_data = nullptr; - - return; - } - - m_size = size; - m_data = new char[m_size + 1]; - - memcpy(m_data, str, m_size); - - m_data[m_size] = '\0'; -} - - void xmrig::String::move(char *str) { delete [] m_data; diff --git a/src/base/tools/String.h b/src/base/tools/String.h index 129aa544f..b2da0940b 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -47,11 +47,13 @@ class String public: inline String() : m_data(nullptr), m_size(0) {} inline String(char *str) : m_data(str), m_size(str == nullptr ? 0 : strlen(str)) {} - inline String(const char *str) : m_data(nullptr) { copy(str); } - inline String(const char *str, size_t size) : m_data(nullptr) { create(str, size); } - inline String(const String &other) : m_data(nullptr) { copy(other); } inline String(String &&other) : m_data(other.m_data), m_size(other.m_size) { other.m_data = nullptr; other.m_size = 0; } - inline ~String() { delete [] m_data; } + + String(const char *str); + String(const char *str, size_t size); + String(const String &other); + + inline ~String() { delete [] m_data; } bool isEqual(const char *str) const; @@ -87,7 +89,6 @@ class String private: void copy(const char *str); void copy(const String &other); - void create(const char *str, size_t size); void move(char *str); void move(String &&other); From 6e4e45010fda6f32af3b1b1146aebe988a53255a Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 2 Nov 2018 21:53:41 +0700 Subject: [PATCH 21/34] Add memory information to "GET /1/summary" --- src/api/ApiRouter.cpp | 18 +++++++++++++++++- src/api/ApiRouter.h | 7 ++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 04d059ae4..6ca10ec3b 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -110,6 +110,7 @@ void ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) cons getIdentify(doc); getMiner(doc); getHashrate(doc); + getMemory(doc); getMinersSummary(doc, req.match("/1/summary")); getResults(doc); @@ -211,6 +212,21 @@ void ApiRouter::getIdentify(rapidjson::Document &doc) const } +void ApiRouter::getMemory(rapidjson::Document &doc) const +{ + auto &allocator = doc.GetAllocator(); + + size_t rss = 0; + uv_resident_set_memory(&rss); + + rapidjson::Value memory(rapidjson::kObjectType); + memory.AddMember("total", uv_get_total_memory(), allocator); + memory.AddMember("res", static_cast(rss), allocator); + + doc.AddMember("memory", memory, allocator); +} + + void ApiRouter::getMiner(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -317,7 +333,7 @@ void ApiRouter::getResources(rapidjson::Document &doc) const uv_resident_set_memory(&rss); doc.AddMember("total_memory", uv_get_total_memory(), allocator); - doc.AddMember("resident_set_memory", (uint64_t) rss, allocator); + doc.AddMember("resident_set_memory", static_cast(rss), allocator); } diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index b0230778c..4b0aebe2d 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __APIROUTER_H__ -#define __APIROUTER_H__ +#ifndef XMRIG_APIROUTER_H +#define XMRIG_APIROUTER_H #include "common/interfaces/IControllerListener.h" @@ -57,6 +57,7 @@ class ApiRouter : public xmrig::IControllerListener void genId(const char *id); void getHashrate(rapidjson::Document &doc) const; void getIdentify(rapidjson::Document &doc) const; + void getMemory(rapidjson::Document &doc) const; void getMiner(rapidjson::Document &doc) const; void getMiners(rapidjson::Document &doc) const; void getMinersSummary(rapidjson::Document &doc, bool advanced) const; @@ -71,4 +72,4 @@ class ApiRouter : public xmrig::IControllerListener xmrig::Controller *m_controller; }; -#endif /* __APIROUTER_H__ */ +#endif /* XMRIG_APIROUTER_H */ From bd3424f7c9e2cd034f77f0641dcf3acb264df957 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 3 Nov 2018 15:28:44 +0700 Subject: [PATCH 22/34] Add load_average to API. --- src/api/ApiRouter.cpp | 44 +++++++++++++++++++++++++++---------------- src/api/ApiRouter.h | 2 +- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 6ca10ec3b..169245791 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -110,7 +110,7 @@ void ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) cons getIdentify(doc); getMiner(doc); getHashrate(doc); - getMemory(doc); + getResourcesSummary(doc); getMinersSummary(doc, req.match("/1/summary")); getResults(doc); @@ -212,21 +212,6 @@ void ApiRouter::getIdentify(rapidjson::Document &doc) const } -void ApiRouter::getMemory(rapidjson::Document &doc) const -{ - auto &allocator = doc.GetAllocator(); - - size_t rss = 0; - uv_resident_set_memory(&rss); - - rapidjson::Value memory(rapidjson::kObjectType); - memory.AddMember("total", uv_get_total_memory(), allocator); - memory.AddMember("res", static_cast(rss), allocator); - - doc.AddMember("memory", memory, allocator); -} - - void ApiRouter::getMiner(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); @@ -326,6 +311,33 @@ void ApiRouter::getMinersSummary(rapidjson::Document &doc, bool advanced) const +void ApiRouter::getResourcesSummary(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + size_t rss = 0; + uv_resident_set_memory(&rss); + + Value resources(kObjectType); + Value memory(kObjectType); + Value load_average(kArrayType); + + memory.AddMember("total", uv_get_total_memory(), allocator); + memory.AddMember("resident_set_memory", static_cast(rss), allocator); + + double loadavg[3] = { 1.0 }; + uv_loadavg(loadavg); + load_average.PushBack(loadavg[0], allocator); + load_average.PushBack(loadavg[1], allocator); + load_average.PushBack(loadavg[2], allocator); + + resources.AddMember("memory", memory, allocator); + resources.AddMember("load_average", load_average, allocator); + doc.AddMember("resources", resources, allocator); +} + + void ApiRouter::getResources(rapidjson::Document &doc) const { auto &allocator = doc.GetAllocator(); diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h index 4b0aebe2d..f2a12615a 100644 --- a/src/api/ApiRouter.h +++ b/src/api/ApiRouter.h @@ -57,10 +57,10 @@ class ApiRouter : public xmrig::IControllerListener void genId(const char *id); void getHashrate(rapidjson::Document &doc) const; void getIdentify(rapidjson::Document &doc) const; - void getMemory(rapidjson::Document &doc) const; void getMiner(rapidjson::Document &doc) const; void getMiners(rapidjson::Document &doc) const; void getMinersSummary(rapidjson::Document &doc, bool advanced) const; + void getResourcesSummary(rapidjson::Document &doc) const; void getResources(rapidjson::Document &doc) const; void getResults(rapidjson::Document &doc) const; void getWorkers(rapidjson::Document &doc) const; From d3440c9dedf72bc52cd445b0cbce96f3ffd659a6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 3 Nov 2018 16:27:48 +0700 Subject: [PATCH 23/34] Add "hardware_concurrency" field, needs for correctly interpreting "load_average" values. --- src/api/ApiRouter.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp index 169245791..0af4dbed6 100644 --- a/src/api/ApiRouter.cpp +++ b/src/api/ApiRouter.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #if _WIN32 # include "winsock2.h" @@ -334,6 +335,8 @@ void ApiRouter::getResourcesSummary(rapidjson::Document &doc) const resources.AddMember("memory", memory, allocator); resources.AddMember("load_average", load_average, allocator); + resources.AddMember("hardware_concurrency", std::thread::hardware_concurrency(), allocator); + doc.AddMember("resources", resources, allocator); } From 42963fbf99987b1487e256366574642f44e7ef9e Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 4 Nov 2018 16:52:16 +0700 Subject: [PATCH 24/34] #271 Fixed broken pool options cascading (mixed configuration). --- src/common/config/CommonConfig.cpp | 51 +++++++++++++++++++++++------- src/common/config/CommonConfig.h | 4 ++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index beb2d0c92..904f8063a 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -280,16 +280,16 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) break; case KeepAliveKey: /* --keepalive */ - m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); + currentPool().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); break; case TlsKey: /* --tls */ - m_pools.back().setTLS(enable); + currentPool().setTLS(enable); break; # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ - m_pools.back().setNicehash(enable); + currentPool().setNicehash(enable); break; # endif @@ -333,13 +333,15 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserpassKey: /* --userpass */ - if (!m_pools.back().setUserpass(arg)) { + if (!currentPool().setUserpass(arg)) { return false; } break; case UrlKey: /* --url */ + fixup(); + if (m_pools.size() > 1 || m_pools[0].isValid()) { Pool pool(arg); @@ -358,23 +360,23 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserKey: /* --user */ - m_pools.back().setUser(arg); + currentPool().setUser(arg); break; case PasswordKey: /* --pass */ - m_pools.back().setPassword(arg); + currentPool().setPassword(arg); break; case RigIdKey: /* --rig-id */ - m_pools.back().setRigId(arg); + currentPool().setRigId(arg); break; case FingerprintKey: /* --tls-fingerprint */ - m_pools.back().setFingerprint(arg); + currentPool().setFingerprint(arg); break; case VariantKey: /* --variant */ - m_pools.back().algorithm().parseVariant(arg); + currentPool().algorithm().parseVariant(arg); break; case LogFileKey: /* --log-file */ @@ -462,11 +464,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case KeepAliveKey: /* --keepalive */ - m_pools.back().setKeepAlive(arg); + currentPool().setKeepAlive(arg); break; case VariantKey: /* --variant */ - m_pools.back().algorithm().parseVariant(arg); + currentPool().algorithm().parseVariant(arg); break; case DonateLevelKey: /* --donate-level */ @@ -493,3 +495,30 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) return true; } + + +Pool &xmrig::CommonConfig::currentPool() +{ + fixup(); + + return m_pools.back(); +} + + +void xmrig::CommonConfig::fixup() +{ + if (m_state == NoneState) { + return; + } + + if (m_pools.empty()) { + if (!m_activePools.empty()) { + std::swap(m_pools, m_activePools); + } + else { + m_pools.push_back(Pool()); + } + + m_state = NoneState; + } +} diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 422a6bb2f..a864033b0 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -112,9 +112,11 @@ class CommonConfig : public IConfig private: bool parseInt(int key, int arg); + Pool ¤tPool(); + void fixup(); }; } /* namespace xmrig */ -#endif /* __COMMONCONFIG_H__ */ +#endif /* XMRIG_COMMONCONFIG_H */ From a2b9a0654bb4da5ab77cadc3209716000c94e21c Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Nov 2018 20:31:18 +0700 Subject: [PATCH 25/34] Sync changes with CPU miner. --- src/common/config/CommonConfig.cpp | 2 +- src/common/xmrig.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 904f8063a..94399d7d7 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -402,7 +402,7 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case RetriesKey: /* --retries */ case RetryPauseKey: /* --retry-pause */ case ApiPort: /* --api-port */ - case PrintTimeKey: /* --cpu-priority */ + case PrintTimeKey: /* --print-time */ return parseUint64(key, strtol(arg, nullptr, 10)); case BackgroundKey: /* --background */ diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 52650f0d2..20306d1ce 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -99,6 +100,7 @@ enum Assembly { ASM_AUTO, ASM_INTEL, ASM_RYZEN, + ASM_BULLDOZER, ASM_MAX }; From 19a895e3a168287543cece6614ad8f998dc7ec17 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 10 Nov 2018 02:50:42 +0700 Subject: [PATCH 26/34] Automatically add "-notls" suffix. --- cmake/OpenSSL.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index ff9d71676..b4b9f107a 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -29,4 +29,6 @@ else() set(TLS_SOURCES "") set(OPENSSL_LIBRARIES "") add_definitions(/DXMRIG_NO_TLS) + + set(CMAKE_PROJECT_NAME "${CMAKE_PROJECT_NAME}-notls") endif() From 646d87a559f5a48a97eb4f23569d2448220dac60 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 10 Nov 2018 03:14:24 +0700 Subject: [PATCH 27/34] Fix compile warning if http and ssl disabled. --- src/common/config/CommonConfig.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 94399d7d7..dfa1c93fd 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -174,6 +174,9 @@ void xmrig::CommonConfig::printVersions() int length = snprintf(buf, sizeof buf, "CUDA/%d.%d ", cudaVersion / 1000, cudaVersion % 100); # else memset(buf, 0, 16); +# endif + +# if !defined(XMRIG_NO_HTTPD) || !defined(XMRIG_NO_TLS) int length = 0; # endif From 2bdea49bd10b0ffc1b0298ea7d6495c0fc48f0c7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 10 Nov 2018 16:05:40 +0700 Subject: [PATCH 28/34] Use static libuuid. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ac831fa9..2485d9d77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,7 +170,7 @@ else() if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) set(EXTRA_LIBS pthread) else() - set(EXTRA_LIBS pthread uuid rt dl) + set(EXTRA_LIBS pthread uuid.a rt dl) endif() endif() From 15262b2a33218841a756a2e5da9ca187c15a8e26 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 11 Nov 2018 01:15:48 +0700 Subject: [PATCH 29/34] Sync changes. --- src/common/config/CommonConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index dfa1c93fd..f2a001829 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -174,11 +174,11 @@ void xmrig::CommonConfig::printVersions() int length = snprintf(buf, sizeof buf, "CUDA/%d.%d ", cudaVersion / 1000, cudaVersion % 100); # else memset(buf, 0, 16); -# endif # if !defined(XMRIG_NO_HTTPD) || !defined(XMRIG_NO_TLS) int length = 0; # endif +# endif # if !defined(XMRIG_NO_TLS) && defined(OPENSSL_VERSION_TEXT) { From 736908098d3aebd91fe42d1235c65e15adc4ae1c Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 9 Jan 2019 11:02:17 +0700 Subject: [PATCH 30/34] Update year. --- src/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/version.h b/src/version.h index 3fedce1fb..d34f500c0 100644 --- a/src/version.h +++ b/src/version.h @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -30,7 +30,7 @@ #define APP_VERSION "2.9.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" -#define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" +#define APP_COPYRIGHT "Copyright (C) 2016-2019 xmrig.com" #define APP_KIND "proxy" #define APP_VER_MAJOR 2 From 396c06fc5329e73014497c0033c7cbe82b51d410 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 9 Jan 2019 12:44:45 +0700 Subject: [PATCH 31/34] Sync changes. --- src/base/tools/String.h | 3 ++- src/common/Platform.cpp | 7 ++--- src/common/Platform.h | 11 +++++--- src/common/Platform_mac.cpp | 27 ++++++++++++++----- src/common/Platform_unix.cpp | 19 +++++++++++--- src/common/Platform_win.cpp | 42 +++++++++++++++++++++++++++--- src/common/config/ConfigLoader.cpp | 7 ++++- src/common/config/ConfigLoader.h | 11 +++++--- src/common/net/Client.cpp | 10 ++++++- src/common/net/Client.h | 2 +- src/common/net/Id.h | 8 +++--- src/common/net/Job.cpp | 20 +++++--------- src/common/net/Job.h | 7 ++--- src/common/utils/timestamp.h | 12 +++++++-- 14 files changed, 136 insertions(+), 50 deletions(-) diff --git a/src/base/tools/String.h b/src/base/tools/String.h index b2da0940b..0c191dfde 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -60,7 +60,7 @@ class String bool isEqual(const String &other) const; - inline bool contains(const char *str) const { return strstr(m_data, str) != nullptr; } + inline bool contains(const char *str) const { return isNull() ? false : strstr(m_data, str) != nullptr; } inline bool isEmpty() const { return size() == 0; } @@ -75,6 +75,7 @@ class String inline bool operator<(const String &str) const { return strcmp(data(), str.data()) < 0; } inline bool operator==(const char *str) const { return isEqual(str); } inline bool operator==(const String &other) const { return isEqual(other); } + inline operator const char*() const { return m_data; } inline String &operator=(char *str) { move(str); return *this; } inline String &operator=(const char *str) { copy(str); return *this; } inline String &operator=(const String &str) { copy(str); return *this; } diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index a95f78e7e..17fcc38e6 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh + * Copyright 2016-2018 XMRig , * * 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 @@ -36,7 +37,7 @@ char Platform::m_defaultConfigName[520] = { 0 }; -xmrig::c_str Platform::m_userAgent; +xmrig::String Platform::m_userAgent; const char *Platform::defaultConfigName() diff --git a/src/common/Platform.h b/src/common/Platform.h index 5dfb9ff7f..fc10e83b3 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh + * Copyright 2016-2018 XMRig , * * 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,7 +29,7 @@ #include -#include "common/utils/c_str.h" +#include "base/tools/String.h" class Platform @@ -36,7 +37,9 @@ class Platform public: static bool setThreadAffinity(uint64_t cpu_id); static const char *defaultConfigName(); + static uint32_t setTimerResolution(uint32_t resolution); static void init(const char *userAgent); + static void restoreTimerResolution(); static void setProcessPriority(int priority); static void setThreadPriority(int priority); @@ -46,7 +49,7 @@ class Platform static char *createUserAgent(); static char m_defaultConfigName[520]; - static xmrig::c_str m_userAgent; + static xmrig::String m_userAgent; }; diff --git a/src/common/Platform_mac.cpp b/src/common/Platform_mac.cpp index d0c533b08..4e4aa0ad1 100644 --- a/src/common/Platform_mac.cpp +++ b/src/common/Platform_mac.cpp @@ -40,15 +40,20 @@ char *Platform::createUserAgent() { - const size_t max = 160; + constexpr const size_t max = 256; - char *buf = new char[max]; + char *buf = new char[max](); + int length = snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s", APP_NAME, APP_VERSION, uv_version_string()); # ifdef XMRIG_NVIDIA_PROJECT const int cudaVersion = cuda_get_runtime_version(); - snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s CUDA/%d.%d clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), cudaVersion / 1000, cudaVersion % 100, __clang_major__, __clang_minor__, __clang_patchlevel__); -# else - snprintf(buf, max, "%s/%s (Macintosh; Intel Mac OS X) libuv/%s clang/%d.%d.%d", APP_NAME, APP_VERSION, uv_version_string(), __clang_major__, __clang_minor__, __clang_patchlevel__); + length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); +# endif + +# ifdef __clang__ + length += snprintf(buf + length, max - length, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); +# elif defined(__GNUC__) + length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); # endif return buf; @@ -65,9 +70,19 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } -void Platform::setProcessPriority(int priority) +uint32_t Platform::setTimerResolution(uint32_t resolution) { + return resolution; +} + +void Platform::restoreTimerResolution() +{ +} + + +void Platform::setProcessPriority(int priority) +{ } diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index 058920ec5..901df4be5 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -54,9 +54,9 @@ typedef cpuset_t cpu_set_t; char *Platform::createUserAgent() { - const size_t max = 160; + constexpr const size_t max = 256; - char *buf = new char[max]; + char *buf = new char[max](); int length = snprintf(buf, max, "%s/%s (Linux ", APP_NAME, APP_VERSION); # if defined(__x86_64__) @@ -70,7 +70,9 @@ char *Platform::createUserAgent() length += snprintf(buf + length, max - length, " CUDA/%d.%d", cudaVersion / 1000, cudaVersion % 100); # endif -# ifdef __GNUC__ +# ifdef __clang__ + length += snprintf(buf + length, max - length, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__); +# elif defined(__GNUC__) length += snprintf(buf + length, max - length, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); # endif @@ -92,6 +94,17 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } +uint32_t Platform::setTimerResolution(uint32_t resolution) +{ + return resolution; +} + + +void Platform::restoreTimerResolution() +{ +} + + void Platform::setProcessPriority(int priority) { } diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 32b850d14..356b20f64 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -22,6 +23,7 @@ */ +#include #include #include #include @@ -37,6 +39,9 @@ #endif +static uint32_t timerResolution = 0; + + static inline OSVERSIONINFOEX winOsVersion() { typedef NTSTATUS (NTAPI *RtlGetVersionFunction)(LPOSVERSIONINFO); @@ -58,9 +63,9 @@ static inline OSVERSIONINFOEX winOsVersion() char *Platform::createUserAgent() { const auto osver = winOsVersion(); - const size_t max = 160; + constexpr const size_t max = 256; - char *buf = new char[max]; + char *buf = new char[max](); int length = snprintf(buf, max, "%s/%s (Windows NT %lu.%lu", APP_NAME, APP_VERSION, osver.dwMajorVersion, osver.dwMinorVersion); # if defined(__x86_64__) || defined(_M_AMD64) @@ -94,6 +99,34 @@ bool Platform::setThreadAffinity(uint64_t cpu_id) } +uint32_t Platform::setTimerResolution(uint32_t resolution) +{ +# ifdef XMRIG_AMD_PROJECT + TIMECAPS tc; + + if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) { + return 0; + } + + timerResolution = std::min(std::max(tc.wPeriodMin, resolution), tc.wPeriodMax); + + return timeBeginPeriod(timerResolution) == TIMERR_NOERROR ? timerResolution : 0; +# else + return resolution; +# endif +} + + +void Platform::restoreTimerResolution() +{ +# ifdef XMRIG_AMD_PROJECT + if (timerResolution) { + timeEndPeriod(timerResolution); + } +# endif +} + + void Platform::setProcessPriority(int priority) { if (priority == -1) { @@ -121,6 +154,7 @@ void Platform::setProcessPriority(int priority) case 5: prio = REALTIME_PRIORITY_CLASS; + break; default: break; diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index 484c2f8fb..b3b3ecb0f 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -50,6 +50,7 @@ #include "rapidjson/filereadstream.h" +bool xmrig::ConfigLoader::m_done = false; xmrig::ConfigWatcher *xmrig::ConfigLoader::m_watcher = nullptr; xmrig::IConfigCreator *xmrig::ConfigLoader::m_creator = nullptr; xmrig::IWatcherListener *xmrig::ConfigLoader::m_listener = nullptr; @@ -283,12 +284,16 @@ void xmrig::ConfigLoader::parseJSON(xmrig::IConfig *config, const struct option void xmrig::ConfigLoader::showUsage() { + m_done = true; + printf(usage); } void xmrig::ConfigLoader::showVersion() { + m_done = true; + printf(APP_NAME " " APP_VERSION "\n built on " __DATE__ # if defined(__clang__) diff --git a/src/common/config/ConfigLoader.h b/src/common/config/ConfigLoader.h index 64638af3d..b9e045379 100644 --- a/src/common/config/ConfigLoader.h +++ b/src/common/config/ConfigLoader.h @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __CONFIGLOADER_H__ -#define __CONFIGLOADER_H__ +#ifndef XMRIG_CONFIGLOADER_H +#define XMRIG_CONFIGLOADER_H #include @@ -53,6 +53,8 @@ class ConfigLoader static IConfig *load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener); static void release(); + static inline bool isDone() { return m_done; } + private: static bool getJSON(const char *fileName, rapidjson::Document &doc); static bool parseArg(IConfig *config, int key, const char *arg); @@ -60,6 +62,7 @@ class ConfigLoader static void showUsage(); static void showVersion(); + static bool m_done; static ConfigWatcher *m_watcher; static IConfigCreator *m_creator; static IWatcherListener *m_listener; @@ -68,4 +71,4 @@ class ConfigLoader } /* namespace xmrig */ -#endif /* __CONFIGLOADER_H__ */ +#endif /* XMRIG_CONFIGLOADER_H */ diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 1d1d86c7f..8458b1e2e 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -212,6 +212,12 @@ const char *Client::tlsVersion() const int64_t Client::submit(const JobResult &result) { +# ifndef XMRIG_PROXY_PROJECT + if (result.clientId != m_rpcId) { + return -1; + } +# endif + using namespace rapidjson; # ifdef XMRIG_PROXY_PROJECT @@ -355,6 +361,8 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + m_job.setClientId(m_rpcId); + if (m_job != job) { m_jobs++; m_job = std::move(job); diff --git a/src/common/net/Client.h b/src/common/net/Client.h index d64183384..a05710fc3 100644 --- a/src/common/net/Client.h +++ b/src/common/net/Client.h @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 diff --git a/src/common/net/Id.h b/src/common/net/Id.h index 5fb2db521..999e7837a 100644 --- a/src/common/net/Id.h +++ b/src/common/net/Id.h @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __ID_H__ -#define __ID_H__ +#ifndef XMRIG_ID_H +#define XMRIG_ID_H #include @@ -95,4 +95,4 @@ class Id } /* namespace xmrig */ -#endif /* __ID_H__ */ +#endif /* XMRIG_ID_H */ diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 2bfb39f0e..acb3b3f46 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -7,7 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -91,6 +91,12 @@ Job::~Job() } +bool Job::isEqual(const Job &other) const +{ + return m_id == other.m_id && m_clientId == other.m_clientId && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; +} + + bool Job::setBlob(const char *blob) { if (!blob) { @@ -214,18 +220,6 @@ char *Job::toHex(const unsigned char* in, unsigned int len) #endif -bool Job::operator==(const Job &other) const -{ - return m_id == other.m_id && memcmp(m_blob, other.m_blob, sizeof(m_blob)) == 0; -} - - -bool Job::operator!=(const Job &other) const -{ - return m_id != other.m_id || memcmp(m_blob, other.m_blob, sizeof(m_blob)) != 0; -} - - xmrig::Variant Job::variant() const { using namespace xmrig; diff --git a/src/common/net/Job.h b/src/common/net/Job.h index b561b9c1b..394727dfe 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -7,7 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -42,6 +42,7 @@ class Job Job(int poolId, bool nicehash, const xmrig::Algorithm &algorithm, const xmrig::Id &clientId); ~Job(); + bool isEqual(const Job &other) const; bool setBlob(const char *blob); bool setTarget(const char *target); void setAlgorithm(const char *algo); @@ -81,8 +82,8 @@ class Job static char *toHex(const unsigned char* in, unsigned int len); # endif - bool operator==(const Job &other) const; - bool operator!=(const Job &other) const; + inline bool operator==(const Job &other) const { return isEqual(other); } + inline bool operator!=(const Job &other) const { return !isEqual(other); } private: xmrig::Variant variant() const; diff --git a/src/common/utils/timestamp.h b/src/common/utils/timestamp.h index 6b6a8ab24..55b4c21d6 100644 --- a/src/common/utils/timestamp.h +++ b/src/common/utils/timestamp.h @@ -5,7 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -31,7 +31,7 @@ namespace xmrig { -static inline int64_t currentMSecsSinceEpoch() +static inline int64_t steadyTimestamp() { using namespace std::chrono; if (high_resolution_clock::is_steady) { @@ -42,6 +42,14 @@ static inline int64_t currentMSecsSinceEpoch() } +static inline int64_t currentMSecsSinceEpoch() +{ + using namespace std::chrono; + + return time_point_cast(system_clock::now()).time_since_epoch().count(); +} + + } /* namespace xmrig */ #endif /* XMRIG_TIMESTAMP_H */ From 8f9161fb0ff36a3c45b4b5eaec005dc05900ea7e Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 14 Jan 2019 21:14:36 +0700 Subject: [PATCH 32/34] Sync changes, add "cn/half". --- src/common/Platform_win.cpp | 2 ++ src/common/crypto/Algorithm.cpp | 32 +++++++++++++++++++++++++--- src/common/crypto/Algorithm.h | 27 +++++++++++++++++------- src/common/net/Client.cpp | 3 +-- src/common/net/Job.cpp | 9 ++++++++ src/common/net/Pool.cpp | 37 ++++++++++++++++++++++++--------- src/common/net/Pool.h | 2 +- src/common/xmrig.h | 3 ++- 8 files changed, 90 insertions(+), 25 deletions(-) diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 356b20f64..9e9b772d2 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -39,7 +39,9 @@ #endif +#ifdef XMRIG_AMD_PROJECT static uint32_t timerResolution = 0; +#endif static inline OSVERSIONINFOEX winOsVersion() diff --git a/src/common/crypto/Algorithm.cpp b/src/common/crypto/Algorithm.cpp index a3cf48b2d..2f2593726 100644 --- a/src/common/crypto/Algorithm.cpp +++ b/src/common/crypto/Algorithm.cpp @@ -7,7 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -62,6 +62,8 @@ static AlgoData const algorithms[] = { { "cryptonight/xao", "cn/xao", xmrig::CRYPTONIGHT, xmrig::VARIANT_XAO }, { "cryptonight/rto", "cn/rto", xmrig::CRYPTONIGHT, xmrig::VARIANT_RTO }, { "cryptonight/2", "cn/2", xmrig::CRYPTONIGHT, xmrig::VARIANT_2 }, + { "cryptonight/half", "cn/half", xmrig::CRYPTONIGHT, xmrig::VARIANT_HALF }, + { "cryptonight/xtlv9", "cn/xtlv9", xmrig::CRYPTONIGHT, xmrig::VARIANT_HALF }, # ifndef XMRIG_NO_AEON { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, @@ -109,9 +111,13 @@ static const char *variants[] = { "xao", "rto", "2", + "half" }; +static_assert(xmrig::VARIANT_MAX == ARRAY_SIZE(variants), "variants size mismatch"); + + bool xmrig::Algorithm::isValid() const { if (m_algo == INVALID_ALGO) { @@ -144,10 +150,16 @@ void xmrig::Algorithm::parseAlgorithm(const char *algo) m_variant = VARIANT_AUTO; assert(algo != nullptr); - if (algo == nullptr) { + if (algo == nullptr || strlen(algo) < 1) { return; } + if (*algo == '!') { + m_flags |= Forced; + + return parseAlgorithm(algo + 1); + } + for (size_t i = 0; i < ARRAY_SIZE(algorithms); i++) { if ((strcasecmp(algo, algorithms[i].name) == 0) || (strcasecmp(algo, algorithms[i].shortName) == 0)) { m_algo = algorithms[i].algo; @@ -166,12 +178,26 @@ void xmrig::Algorithm::parseVariant(const char *variant) { m_variant = VARIANT_AUTO; + if (variant == nullptr || strlen(variant) < 1) { + return; + } + + if (*variant == '!') { + m_flags |= Forced; + + return parseVariant(variant + 1); + } + for (size_t i = 0; i < ARRAY_SIZE(variants); i++) { if (strcasecmp(variant, variants[i]) == 0) { m_variant = static_cast(i); - break; + return; } } + + if (strcasecmp(variant, "xtlv9") == 0) { + m_variant = VARIANT_HALF; + } } diff --git a/src/common/crypto/Algorithm.h b/src/common/crypto/Algorithm.h index 731fa7937..f4380d45f 100644 --- a/src/common/crypto/Algorithm.h +++ b/src/common/crypto/Algorithm.h @@ -7,7 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -39,28 +39,38 @@ namespace xmrig { class Algorithm { public: + enum Flags { + None = 0, + Forced = 1 + }; + inline Algorithm() : m_algo(INVALID_ALGO), + m_flags(0), m_variant(VARIANT_AUTO) {} inline Algorithm(Algo algo, Variant variant) : + m_flags(0), m_variant(variant) { setAlgo(algo); } - inline Algorithm(const char *algo) + inline Algorithm(const char *algo) : + m_flags(0) { parseAlgorithm(algo); } - bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; } - inline Algo algo() const { return m_algo; } - inline const char *name() const { return name(false); } - inline const char *shortName() const { return name(true); } - inline Variant variant() const { return m_variant; } - inline void setVariant(Variant variant) { m_variant = variant; } + inline Algo algo() const { return m_algo; } + inline bool isEqual(const Algorithm &other) const { return m_algo == other.m_algo && m_variant == other.m_variant; } + inline bool isForced() const { return m_flags & Forced; } + inline const char *name() const { return name(false); } + inline const char *shortName() const { return name(true); } + inline int flags() const { return m_flags; } + inline Variant variant() const { return m_variant; } + inline void setVariant(Variant variant) { m_variant = variant; } inline bool operator!=(const Algorithm &other) const { return !isEqual(other); } inline bool operator==(const Algorithm &other) const { return isEqual(other); } @@ -80,6 +90,7 @@ class Algorithm const char *name(bool shortName) const; Algo m_algo; + int m_flags; Variant m_variant; }; diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index 8458b1e2e..6da639424 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -342,8 +342,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) if (params.HasMember("algo")) { job.setAlgorithm(params["algo"].GetString()); } - - if (params.HasMember("variant")) { + else if (params.HasMember("variant")) { const rapidjson::Value &variant = params["variant"]; if (variant.IsInt()) { diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index acb3b3f46..7da2ed83b 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -125,6 +125,15 @@ bool Job::setBlob(const char *blob) m_algorithm.setVariant(variant()); } + if (!m_algorithm.isForced()) { + if (m_algorithm.variant() == xmrig::VARIANT_XTL && m_blob[0] >= 9) { + m_algorithm.setVariant(xmrig::VARIANT_HALF); + } + else if (m_algorithm.variant() == xmrig::VARIANT_MSR && m_blob[0] >= 8) { + m_algorithm.setVariant(xmrig::VARIANT_HALF); + } + } + # ifdef XMRIG_PROXY_PROJECT memset(m_rawBlob, 0, sizeof(m_rawBlob)); memcpy(m_rawBlob, blob, m_size * 2); diff --git a/src/common/net/Pool.cpp b/src/common/net/Pool.cpp index 141e51158..a44f8a41f 100644 --- a/src/common/net/Pool.cpp +++ b/src/common/net/Pool.cpp @@ -6,7 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -322,23 +322,39 @@ void Pool::adjustVariant(const xmrig::Variant variantHint) m_nicehash = true; bool valid = true; - if (m_host.contains("cryptonight.") && m_port == 3355) { - valid = m_algorithm.algo() == CRYPTONIGHT; + switch (m_port) { + case 3355: + case 33355: + valid = m_algorithm.algo() == CRYPTONIGHT && m_host.contains("cryptonight."); m_algorithm.setVariant(VARIANT_0); - } - else if (m_host.contains("cryptonightv7.") && m_port == 3363) { - valid = m_algorithm.algo() == CRYPTONIGHT; + break; + + case 3363: + case 33363: + valid = m_algorithm.algo() == CRYPTONIGHT && m_host.contains("cryptonightv7."); m_algorithm.setVariant(VARIANT_1); - } - else if (m_host.contains("cryptonightheavy.") && m_port == 3364) { - valid = m_algorithm.algo() == CRYPTONIGHT_HEAVY; + break; + + case 3364: + valid = m_algorithm.algo() == CRYPTONIGHT_HEAVY && m_host.contains("cryptonightheavy."); m_algorithm.setVariant(VARIANT_0); + break; + + case 3367: + case 33367: + valid = m_algorithm.algo() == CRYPTONIGHT && m_host.contains("cryptonightv8."); + m_algorithm.setVariant(VARIANT_2); + break; + + default: + break; } if (!valid) { m_algorithm.setAlgo(INVALID_ALGO); } + m_tls = m_port > 33000; return; } @@ -349,7 +365,7 @@ void Pool::adjustVariant(const xmrig::Variant variantHint) if (m_host.contains("xmr.pool.")) { valid = m_algorithm.algo() == CRYPTONIGHT; - m_algorithm.setVariant(m_port == 45700 ? VARIANT_1 : VARIANT_0); + m_algorithm.setVariant(m_port == 45700 ? VARIANT_AUTO : VARIANT_0); } else if (m_host.contains("aeon.pool.") && m_port == 45690) { valid = m_algorithm.algo() == CRYPTONIGHT_LITE; @@ -396,6 +412,7 @@ void Pool::rebuild() addVariant(xmrig::VARIANT_2); addVariant(xmrig::VARIANT_1); addVariant(xmrig::VARIANT_0); + addVariant(xmrig::VARIANT_HALF); addVariant(xmrig::VARIANT_XTL); addVariant(xmrig::VARIANT_TUBE); addVariant(xmrig::VARIANT_MSR); diff --git a/src/common/net/Pool.h b/src/common/net/Pool.h index 123cc131f..c051b0ee2 100644 --- a/src/common/net/Pool.h +++ b/src/common/net/Pool.h @@ -6,7 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 diff --git a/src/common/xmrig.h b/src/common/xmrig.h index 20306d1ce..883d866d5 100644 --- a/src/common/xmrig.h +++ b/src/common/xmrig.h @@ -6,7 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018 SChernykh - * Copyright 2016-2018 XMRig , + * Copyright 2016-2019 XMRig , * * 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 @@ -69,6 +69,7 @@ enum Variant { VARIANT_XAO = 6, // Modified CryptoNight variant 0 (Alloy only) VARIANT_RTO = 7, // Modified CryptoNight variant 1 (Arto only) VARIANT_2 = 8, // CryptoNight variant 2 + VARIANT_HALF = 9, // CryptoNight variant 2 with half iterations (Masari/Stellite) VARIANT_MAX }; From 59bd6e7e8cafd43043e8021c15d70d8cae489c42 Mon Sep 17 00:00:00 2001 From: xmrig Date: Tue, 15 Jan 2019 20:28:27 +0700 Subject: [PATCH 33/34] Update STRATUM_EXT.md --- doc/STRATUM_EXT.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/doc/STRATUM_EXT.md b/doc/STRATUM_EXT.md index 2050bf6c7..294442d68 100644 --- a/doc/STRATUM_EXT.md +++ b/doc/STRATUM_EXT.md @@ -84,23 +84,24 @@ Note about xmr-stak, this miner use [different algorithm names](#15-xmr-stak-alg ### 1.4 Algorithm names and variants Both miner and pool should support short algorithm name aliases: -| Long name | Short name | Base algorithm | Variant | Notes | -|--------------------------|-----------------|----------------|-------------|------------------------------------------------------| -| `cryptonight` | `cn` | `cn` | `-1` | Autodetect works only for Monero. | -| `cryptonight/0` | `cn/0` | `cn` | `0` | Original/old CryptoNight. | -| `cryptonight/1` | `cn/1` | `cn` | `1` | Also known as `monero7` and `CryptoNightV7`. | -| `cryptonight/2` | `cn/2` | `cn` | `2` | CryptoNight variant 2. | -| `cryptonight/xtl` | `cn/xtl` | `cn` | `"xtl"` | Stellite (XTL). | -| `cryptonight/msr` | `cn/msr` | `cn` | `"msr"` | Masari (MSR), also known as `cryptonight-fast` | -| `cryptonight/xao` | `cn/xao` | `cn` | `"xao"` | Alloy (XAO) | -| `cryptonight/rto` | `cn/rto` | `cn` | `"rto"` | Arto (RTO) | -| `cryptonight-lite` | `cn-lite` | `cn-lite` | `-1` | Autodetect works only for Aeon. | -| `cryptonight-lite/0` | `cn-lite/0` | `cn-lite` | `0` | Original/old CryptoNight-Lite. | -| `cryptonight-lite/1` | `cn-lite/1` | `cn-lite` | `1` | Also known as `aeon7` | -| `cryptonight-lite/ipbc` | `cn-lite/ipbc` | `cn-lite` | `"ipbc"` | IPBC variant, **obsolete** | -| `cryptonight-heavy` | `cn-heavy` | `cn-heavy` | `0` | Ryo and Loki | -| `cryptonight-heavy/xhv` | `cn-heavy/xhv` | `cn-heavy` | `"xhv"` | Haven Protocol | -| `cryptonight-heavy/tube` | `cn-heavy/tube` | `cn-heavy` | `"tube"` | BitTube (TUBE) | +| Long name | Short name | Variant | Notes | +|--------------------------|-----------------|-------------|------------------------------------------------------| +| `cryptonight` | `cn` | `-1` | Autodetect works only for Monero. | +| `cryptonight/0` | `cn/0` | `0` | Original/old CryptoNight. | +| `cryptonight/1` | `cn/1` | `1` | Also known as `monero7` and `CryptoNightV7`. | +| `cryptonight/2` | `cn/2` | `2` | CryptoNight variant 2. | +| `cryptonight/xtl` | `cn/xtl` | `"xtl"` | Stellite (XTL). | +| `cryptonight/msr` | `cn/msr` | `"msr"` | Masari (MSR), also known as `cryptonight-fast`. | +| `cryptonight/xao` | `cn/xao` | `"xao"` | Alloy (XAO) | +| `cryptonight/rto` | `cn/rto` | `"rto"` | Arto (RTO) | +| `cryptonight/half` | `cn/half` | `"half"` | CryptoNight variant 2 with half iterations. | +| `cryptonight-lite` | `cn-lite` | `-1` | Autodetect works only for Aeon. | +| `cryptonight-lite/0` | `cn-lite/0` | `0` | Original/old CryptoNight-Lite. | +| `cryptonight-lite/1` | `cn-lite/1` | `1` | Also known as `aeon7` | +| `cryptonight-lite/ipbc` | `cn-lite/ipbc` | `"ipbc"` | IPBC variant, **obsolete** | +| `cryptonight-heavy` | `cn-heavy` | `0` | Ryo and Loki | +| `cryptonight-heavy/xhv` | `cn-heavy/xhv` | `"xhv"` | Haven Protocol | +| `cryptonight-heavy/tube` | `cn-heavy/tube` | `"tube"` | BitTube (TUBE) | Proper pool/proxy implementation should avoid any automatic/autodetect variants, variant must explicitly specified. From 7e704acfc7027894d0160fd5e1c85a6d3faa534c Mon Sep 17 00:00:00 2001 From: xmrig Date: Wed, 16 Jan 2019 00:02:41 +0700 Subject: [PATCH 34/34] Update CHANGELOG.md --- CHANGELOG.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad7bf8994..651892f9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ +# v2.9.0 +- [#275](https://github.com/xmrig/xmrig-proxy/issues/275) Added SSL/TLS support for incoming connections. +- [#899](https://github.com/xmrig/xmrig/issues/899) Added support for new algoritm `cn/half` for Masari and Stellite forks. +- [#271](https://github.com/xmrig/xmrig-proxy/issues/271) Fixed broken pool options cascading (mixed configuration). +- Added memory and load_average information to API. + # v2.8.1 -- [#258](https://github.com/xmrig/xmrig/issues/258) Force NDEBUG for release builds. -- [#108](https://github.com/xmrig/xmrig/issues/108) Fixed possible crash in simple mode when heavy load. +- [#258](https://github.com/xmrig/xmrig-proxy/issues/258) Force NDEBUG for release builds. +- [#108](https://github.com/xmrig/xmrig-proxy/issues/108) Fixed possible crash in simple mode when heavy load. - [#777](https://github.com/xmrig/xmrig/issues/777) Better report about pool connection issues. - Fixed error when handle malformed result from miner (divide to zero). - Fixed malformed login reply.