diff --git a/src/base/base.cmake b/src/base/base.cmake index 8e43b6e08..6a59c8acf 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -41,6 +41,7 @@ set(HEADERS_BASE src/base/net/stratum/strategies/FailoverStrategy.h src/base/net/stratum/strategies/SinglePoolStrategy.h src/base/net/stratum/SubmitResult.h + src/base/net/stratum/Url.h src/base/net/tools/RecvBuf.h src/base/net/tools/Storage.h src/base/tools/Arguments.h @@ -78,6 +79,7 @@ set(SOURCES_BASE src/base/net/stratum/Pools.cpp src/base/net/stratum/strategies/FailoverStrategy.cpp src/base/net/stratum/strategies/SinglePoolStrategy.cpp + src/base/net/stratum/Url.cpp src/base/tools/Arguments.cpp src/base/tools/Buffer.cpp src/base/tools/String.cpp diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 15586fe87..072dc5347 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -24,10 +24,10 @@ */ -#include -#include -#include -#include +#include +#include +#include +#include #include "base/io/json/Json.h" @@ -40,11 +40,6 @@ #endif -#ifdef _MSC_VER -# define strncasecmp _strnicmp -#endif - - namespace xmrig { static const char *kAlgo = "algo"; @@ -64,54 +59,23 @@ static const char *kUser = "user"; const String Pool::kDefaultPassword = "x"; const String Pool::kDefaultUser = "x"; -static const char kStratumTcp[] = "stratum+tcp://"; -static const char kStratumSsl[] = "stratum+ssl://"; - -#ifdef XMRIG_FEATURE_HTTP -static const char kDaemonHttp[] = "daemon+http://"; -static const char kDaemonHttps[] = "daemon+https://"; -#endif - } -xmrig::Pool::Pool() : - m_keepAlive(0), - m_flags(0), - m_port(kDefaultPort), - m_pollInterval(kDefaultPollInterval) -{ -} - - -/** - * @brief Parse url. - * - * Valid urls: - * example.com - * example.com:3333 - * stratum+tcp://example.com - * stratum+tcp://example.com:3333 - * - * @param url - */ xmrig::Pool::Pool(const char *url) : - m_keepAlive(0), - m_flags(1), - m_port(kDefaultPort), - m_pollInterval(kDefaultPollInterval) + m_flags(1 << FLAG_ENABLED), + m_pollInterval(kDefaultPollInterval), + m_url(url) { - parse(url); } xmrig::Pool::Pool(const rapidjson::Value &object) : - m_keepAlive(0), - m_flags(1), - m_port(kDefaultPort), - m_pollInterval(kDefaultPollInterval) + m_flags(1 << FLAG_ENABLED), + m_pollInterval(kDefaultPollInterval), + m_url(Json::getString(object, kUrl)) { - if (!parse(Json::getString(object, kUrl))) { + if (!m_url.isValid()) { return; } @@ -125,8 +89,8 @@ xmrig::Pool::Pool(const rapidjson::Value &object) : m_flags.set(FLAG_ENABLED, Json::getBool(object, kEnabled, true)); m_flags.set(FLAG_NICEHASH, Json::getBool(object, kNicehash)); - m_flags.set(FLAG_TLS, Json::getBool(object, kTls, m_flags.test(FLAG_TLS))); - m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon, m_flags.test(FLAG_DAEMON))); + m_flags.set(FLAG_TLS, Json::getBool(object, kTls) || m_url.isTLS()); + m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon)); const rapidjson::Value &keepalive = Json::getValue(object, kKeepalive); if (keepalive.IsInt()) { @@ -140,21 +104,12 @@ xmrig::Pool::Pool(const rapidjson::Value &object) : xmrig::Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, bool tls) : m_keepAlive(keepAlive), - m_flags(1), - m_host(host), + m_flags(1 << FLAG_ENABLED), m_password(password), m_user(user), - m_port(port), - m_pollInterval(kDefaultPollInterval) + m_pollInterval(kDefaultPollInterval), + m_url(host, port, tls) { - const size_t size = m_host.size() + 8; - assert(size > 8); - - char *url = new char[size](); - snprintf(url, size - 1, "%s:%d", m_host.data(), m_port); - - m_url = url; - m_flags.set(FLAG_NICEHASH, nicehash); m_flags.set(FLAG_TLS, tls); } @@ -186,11 +141,9 @@ bool xmrig::Pool::isEqual(const Pool &other) const { return (m_flags == other.m_flags && m_keepAlive == other.m_keepAlive - && m_port == other.m_port && m_algorithm == other.m_algorithm && m_coin == other.m_coin && m_fingerprint == other.m_fingerprint - && m_host == other.m_host && m_password == other.m_password && m_rigId == other.m_rigId && m_url == other.m_url @@ -200,68 +153,6 @@ bool xmrig::Pool::isEqual(const Pool &other) const } -bool xmrig::Pool::parse(const char *url) -{ - assert(url != nullptr); - if (url == nullptr) { - return false; - } - - const char *p = strstr(url, "://"); - const char *base = url; - - if (p) { - if (strncasecmp(url, kStratumTcp, sizeof(kStratumTcp) - 1) == 0) { - m_flags.set(FLAG_DAEMON, false); - m_flags.set(FLAG_TLS, false); - } - else if (strncasecmp(url, kStratumSsl, sizeof(kStratumSsl) - 1) == 0) { - m_flags.set(FLAG_DAEMON, false); - m_flags.set(FLAG_TLS, true); - } -# ifdef XMRIG_FEATURE_HTTP - else if (strncasecmp(url, kDaemonHttps, sizeof(kDaemonHttps) - 1) == 0) { - m_flags.set(FLAG_DAEMON, true); - m_flags.set(FLAG_TLS, true); - } - else if (strncasecmp(url, kDaemonHttp, sizeof(kDaemonHttp) - 1) == 0) { - m_flags.set(FLAG_DAEMON, true); - m_flags.set(FLAG_TLS, false); - } -# endif - else { - return false; - } - - base = p + 3; - } - - if (!strlen(base) || *base == '/') { - return false; - } - - m_url = url; - if (base[0] == '[') { - return parseIPv6(base); - } - - const char *port = strchr(base, ':'); - if (!port) { - m_host = base; - return true; - } - - const size_t size = static_cast(port++ - base + 1); - char *host = new char[size](); - memcpy(host, base, size - 1); - - m_host = host; - m_port = static_cast(strtol(port, nullptr, 10)); - - return true; -} - - rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const { using namespace rapidjson; @@ -272,7 +163,7 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const obj.AddMember(StringRef(kAlgo), m_algorithm.toJSON(), allocator); obj.AddMember(StringRef(kCoin), m_coin.toJSON(), allocator); - obj.AddMember(StringRef(kUrl), m_url.toJSON(), allocator); + obj.AddMember(StringRef(kUrl), url().toJSON(), allocator); obj.AddMember(StringRef(kUser), m_user.toJSON(), allocator); if (!isDaemon()) { @@ -307,9 +198,9 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const #ifdef APP_DEBUG void xmrig::Pool::print() const { - LOG_NOTICE("url: %s", m_url.data()); - LOG_DEBUG ("host: %s", m_host.data()); - LOG_DEBUG ("port: %d", static_cast(m_port)); + LOG_NOTICE("url: %s", url().data()); + LOG_DEBUG ("host: %s", host().data()); + LOG_DEBUG ("port: %d", static_cast(port())); LOG_DEBUG ("user: %s", m_user.data()); LOG_DEBUG ("pass: %s", m_password.data()); LOG_DEBUG ("rig-id %s", m_rigId.data()); @@ -318,26 +209,3 @@ void xmrig::Pool::print() const LOG_DEBUG ("keepAlive: %d", m_keepAlive); } #endif - - -bool xmrig::Pool::parseIPv6(const char *addr) -{ - const char *end = strchr(addr, ']'); - if (!end) { - return false; - } - - const char *port = strchr(end, ':'); - if (!port) { - return false; - } - - const size_t size = static_cast(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)); - - return true; -} diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index 15d31ccc7..fc6b6326a 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -31,7 +31,7 @@ #include -#include "base/tools/String.h" +#include "base/net/stratum/Url.h" #include "crypto/common/Coin.h" #include "rapidjson/fwd.h" @@ -57,7 +57,7 @@ public: constexpr static uint16_t kDefaultPort = 3333; constexpr static uint64_t kDefaultPollInterval = 1000; - Pool(); + Pool() = default; Pool(const char *url); Pool(const rapidjson::Value &object); Pool(const char *host, @@ -72,17 +72,17 @@ public: inline bool isDaemon() const { return m_flags.test(FLAG_DAEMON); } inline bool isNicehash() const { return m_flags.test(FLAG_NICEHASH); } inline bool isTLS() const { return m_flags.test(FLAG_TLS); } - inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline bool isValid() const { return m_url.isValid(); } inline const Algorithm &algorithm() const { return m_algorithm; } inline const Coin &coin() const { return m_coin; } inline const String &fingerprint() const { return m_fingerprint; } - inline const String &host() const { return m_host; } + inline const String &host() const { return m_url.host(); } inline const String &password() const { return !m_password.isNull() ? m_password : kDefaultPassword; } inline const String &rigId() const { return m_rigId; } - inline const String &url() const { return m_url; } + inline const String &url() const { return m_url.url(); } inline const String &user() const { return !m_user.isNull() ? m_user : kDefaultUser; } inline int keepAlive() const { return m_keepAlive; } - inline uint16_t port() const { return m_port; } + inline uint16_t port() const { return m_url.port(); } inline uint64_t pollInterval() const { return m_pollInterval; } inline void setAlgo(const Algorithm &algorithm) { m_algorithm = algorithm; } inline void setPassword(const String &password) { m_password = password; } @@ -94,7 +94,6 @@ public: bool isEnabled() const; bool isEqual(const Pool &other) const; - bool parse(const char *url); rapidjson::Value toJSON(rapidjson::Document &doc) const; # ifdef APP_DEBUG @@ -105,20 +104,16 @@ private: inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); } inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } - bool parseIPv6(const char *addr); - Algorithm m_algorithm; Coin m_coin; - int m_keepAlive; - std::bitset m_flags; + int m_keepAlive = 0; + std::bitset m_flags = 0; String m_fingerprint; - String m_host; String m_password; String m_rigId; - String m_url; String m_user; - uint16_t m_port; - uint64_t m_pollInterval; + uint64_t m_pollInterval = kDefaultPollInterval; + Url m_url; }; diff --git a/src/base/net/stratum/Url.cpp b/src/base/net/stratum/Url.cpp new file mode 100644 index 000000000..193c72913 --- /dev/null +++ b/src/base/net/stratum/Url.cpp @@ -0,0 +1,165 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu + * 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 + * 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/net/stratum/Url.h" + + +#include +#include +#include +#include + + +#ifdef _MSC_VER +# define strncasecmp _strnicmp +#endif + + +namespace xmrig { + +static const char kStratumTcp[] = "stratum+tcp://"; +static const char kStratumSsl[] = "stratum+ssl://"; + +#ifdef XMRIG_FEATURE_HTTP +static const char kDaemonHttp[] = "daemon+http://"; +static const char kDaemonHttps[] = "daemon+https://"; +#endif + +} + + +xmrig::Url::Url(const char *url) +{ + parse(url); +} + + +xmrig::Url::Url(const char *host, uint16_t port, bool tls, Scheme scheme) : + m_tls(tls), + m_scheme(scheme), + m_host(host), + m_port(port) +{ + const size_t size = m_host.size() + 8; + assert(size > 8); + + char *url = new char[size](); + snprintf(url, size - 1, "%s:%d", m_host.data(), m_port); + + m_url = url; +} + + +bool xmrig::Url::isEqual(const Url &other) const +{ + return (m_tls == other.m_tls && m_scheme == other.m_scheme && m_host == other.m_host && m_url == other.m_url && m_port == other.m_port); +} + + +bool xmrig::Url::parse(const char *url) +{ + assert(url != nullptr); + + if (url == nullptr) { + return false; + } + + const char *p = strstr(url, "://"); + const char *base = url; + + if (p) { + if (strncasecmp(url, kStratumTcp, sizeof(kStratumTcp) - 1) == 0) { + m_scheme = STRATUM; + m_tls = false; + } + else if (strncasecmp(url, kStratumSsl, sizeof(kStratumSsl) - 1) == 0) { + m_scheme = STRATUM; + m_tls = true; + } +# ifdef XMRIG_FEATURE_HTTP + else if (strncasecmp(url, kDaemonHttps, sizeof(kDaemonHttps) - 1) == 0) { + m_scheme = DAEMON; + m_tls = true; + } + else if (strncasecmp(url, kDaemonHttp, sizeof(kDaemonHttp) - 1) == 0) { + m_scheme = DAEMON; + m_tls = false; + } +# endif + else { + return false; + } + + base = p + 3; + } + + if (!strlen(base) || *base == '/') { + return false; + } + + m_url = url; + if (base[0] == '[') { + return parseIPv6(base); + } + + const char *port = strchr(base, ':'); + if (!port) { + m_host = base; + return true; + } + + const auto size = static_cast(port++ - base + 1); + char *host = new char[size](); + memcpy(host, base, size - 1); + + m_host = host; + m_port = static_cast(strtol(port, nullptr, 10)); + + return true; +} + + +bool xmrig::Url::parseIPv6(const char *addr) +{ + const char *end = strchr(addr, ']'); + if (!end) { + return false; + } + + const char *port = strchr(end, ':'); + if (!port) { + return false; + } + + const auto size = static_cast(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)); + + return true; +} diff --git a/src/base/net/stratum/Url.h b/src/base/net/stratum/Url.h new file mode 100644 index 000000000..23fd750e9 --- /dev/null +++ b/src/base/net/stratum/Url.h @@ -0,0 +1,77 @@ +/* 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-2019 SChernykh + * Copyright 2019 Howard Chu + * 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 + * 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_URL_H +#define XMRIG_URL_H + + +#include "base/tools/String.h" + + +namespace xmrig { + + +class Url +{ +public: + enum Scheme { + UNSPECIFIED, + STRATUM, + DAEMON + }; + + Url() = default; + Url(const char *url); + Url(const char *host, uint16_t port, bool tls = false, Scheme scheme = UNSPECIFIED); + + inline bool isTLS() const { return m_tls; } + inline bool isValid() const { return !m_host.isNull() && m_port > 0; } + inline const String &host() const { return m_host; } + inline const String &url() const { return m_url; } + inline Scheme scheme() const { return m_scheme; } + inline uint16_t port() const { return m_port; } + + inline bool operator!=(const Url &other) const { return !isEqual(other); } + inline bool operator==(const Url &other) const { return isEqual(other); } + + bool isEqual(const Url &other) const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + +private: + bool parse(const char *url); + bool parseIPv6(const char *addr); + + bool m_tls = false; + Scheme m_scheme = UNSPECIFIED; + String m_host; + String m_url; + uint16_t m_port = 3333; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_URL_H */