Added class Url.

This commit is contained in:
XMRig 2019-10-11 09:58:11 +07:00
parent 71108e1bde
commit 4dc7a8103b
5 changed files with 274 additions and 167 deletions

View file

@ -41,6 +41,7 @@ set(HEADERS_BASE
src/base/net/stratum/strategies/FailoverStrategy.h src/base/net/stratum/strategies/FailoverStrategy.h
src/base/net/stratum/strategies/SinglePoolStrategy.h src/base/net/stratum/strategies/SinglePoolStrategy.h
src/base/net/stratum/SubmitResult.h src/base/net/stratum/SubmitResult.h
src/base/net/stratum/Url.h
src/base/net/tools/RecvBuf.h src/base/net/tools/RecvBuf.h
src/base/net/tools/Storage.h src/base/net/tools/Storage.h
src/base/tools/Arguments.h src/base/tools/Arguments.h
@ -78,6 +79,7 @@ set(SOURCES_BASE
src/base/net/stratum/Pools.cpp src/base/net/stratum/Pools.cpp
src/base/net/stratum/strategies/FailoverStrategy.cpp src/base/net/stratum/strategies/FailoverStrategy.cpp
src/base/net/stratum/strategies/SinglePoolStrategy.cpp src/base/net/stratum/strategies/SinglePoolStrategy.cpp
src/base/net/stratum/Url.cpp
src/base/tools/Arguments.cpp src/base/tools/Arguments.cpp
src/base/tools/Buffer.cpp src/base/tools/Buffer.cpp
src/base/tools/String.cpp src/base/tools/String.cpp

View file

@ -24,10 +24,10 @@
*/ */
#include <assert.h> #include <cassert>
#include <string.h> #include <cstring>
#include <stdlib.h> #include <cstdlib>
#include <stdio.h> #include <cstdio>
#include "base/io/json/Json.h" #include "base/io/json/Json.h"
@ -40,11 +40,6 @@
#endif #endif
#ifdef _MSC_VER
# define strncasecmp _strnicmp
#endif
namespace xmrig { namespace xmrig {
static const char *kAlgo = "algo"; static const char *kAlgo = "algo";
@ -64,54 +59,23 @@ static const char *kUser = "user";
const String Pool::kDefaultPassword = "x"; const String Pool::kDefaultPassword = "x";
const String Pool::kDefaultUser = "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) : xmrig::Pool::Pool(const char *url) :
m_keepAlive(0), m_flags(1 << FLAG_ENABLED),
m_flags(1), m_pollInterval(kDefaultPollInterval),
m_port(kDefaultPort), m_url(url)
m_pollInterval(kDefaultPollInterval)
{ {
parse(url);
} }
xmrig::Pool::Pool(const rapidjson::Value &object) : xmrig::Pool::Pool(const rapidjson::Value &object) :
m_keepAlive(0), m_flags(1 << FLAG_ENABLED),
m_flags(1), m_pollInterval(kDefaultPollInterval),
m_port(kDefaultPort), m_url(Json::getString(object, kUrl))
m_pollInterval(kDefaultPollInterval)
{ {
if (!parse(Json::getString(object, kUrl))) { if (!m_url.isValid()) {
return; 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_ENABLED, Json::getBool(object, kEnabled, true));
m_flags.set(FLAG_NICEHASH, Json::getBool(object, kNicehash)); 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_TLS, Json::getBool(object, kTls) || m_url.isTLS());
m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon, m_flags.test(FLAG_DAEMON))); m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon));
const rapidjson::Value &keepalive = Json::getValue(object, kKeepalive); const rapidjson::Value &keepalive = Json::getValue(object, kKeepalive);
if (keepalive.IsInt()) { 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) : 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_keepAlive(keepAlive),
m_flags(1), m_flags(1 << FLAG_ENABLED),
m_host(host),
m_password(password), m_password(password),
m_user(user), 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_NICEHASH, nicehash);
m_flags.set(FLAG_TLS, tls); m_flags.set(FLAG_TLS, tls);
} }
@ -186,11 +141,9 @@ bool xmrig::Pool::isEqual(const Pool &other) const
{ {
return (m_flags == other.m_flags return (m_flags == other.m_flags
&& m_keepAlive == other.m_keepAlive && m_keepAlive == other.m_keepAlive
&& m_port == other.m_port
&& m_algorithm == other.m_algorithm && m_algorithm == other.m_algorithm
&& m_coin == other.m_coin && m_coin == other.m_coin
&& m_fingerprint == other.m_fingerprint && m_fingerprint == other.m_fingerprint
&& m_host == other.m_host
&& m_password == other.m_password && m_password == other.m_password
&& m_rigId == other.m_rigId && m_rigId == other.m_rigId
&& m_url == other.m_url && 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<size_t>(port++ - base + 1);
char *host = new char[size]();
memcpy(host, base, size - 1);
m_host = host;
m_port = static_cast<uint16_t>(strtol(port, nullptr, 10));
return true;
}
rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const
{ {
using namespace rapidjson; 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(kAlgo), m_algorithm.toJSON(), allocator);
obj.AddMember(StringRef(kCoin), m_coin.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); obj.AddMember(StringRef(kUser), m_user.toJSON(), allocator);
if (!isDaemon()) { if (!isDaemon()) {
@ -307,9 +198,9 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const
#ifdef APP_DEBUG #ifdef APP_DEBUG
void xmrig::Pool::print() const void xmrig::Pool::print() const
{ {
LOG_NOTICE("url: %s", m_url.data()); LOG_NOTICE("url: %s", url().data());
LOG_DEBUG ("host: %s", m_host.data()); LOG_DEBUG ("host: %s", host().data());
LOG_DEBUG ("port: %d", static_cast<int>(m_port)); LOG_DEBUG ("port: %d", static_cast<int>(port()));
LOG_DEBUG ("user: %s", m_user.data()); LOG_DEBUG ("user: %s", m_user.data());
LOG_DEBUG ("pass: %s", m_password.data()); LOG_DEBUG ("pass: %s", m_password.data());
LOG_DEBUG ("rig-id %s", m_rigId.data()); LOG_DEBUG ("rig-id %s", m_rigId.data());
@ -318,26 +209,3 @@ void xmrig::Pool::print() const
LOG_DEBUG ("keepAlive: %d", m_keepAlive); LOG_DEBUG ("keepAlive: %d", m_keepAlive);
} }
#endif #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<size_t>(end - addr);
char *host = new char[size]();
memcpy(host, addr + 1, size - 1);
m_host = host;
m_port = static_cast<uint16_t>(strtol(port + 1, nullptr, 10));
return true;
}

View file

@ -31,7 +31,7 @@
#include <vector> #include <vector>
#include "base/tools/String.h" #include "base/net/stratum/Url.h"
#include "crypto/common/Coin.h" #include "crypto/common/Coin.h"
#include "rapidjson/fwd.h" #include "rapidjson/fwd.h"
@ -57,7 +57,7 @@ public:
constexpr static uint16_t kDefaultPort = 3333; constexpr static uint16_t kDefaultPort = 3333;
constexpr static uint64_t kDefaultPollInterval = 1000; constexpr static uint64_t kDefaultPollInterval = 1000;
Pool(); Pool() = default;
Pool(const char *url); Pool(const char *url);
Pool(const rapidjson::Value &object); Pool(const rapidjson::Value &object);
Pool(const char *host, Pool(const char *host,
@ -72,17 +72,17 @@ public:
inline bool isDaemon() const { return m_flags.test(FLAG_DAEMON); } inline bool isDaemon() const { return m_flags.test(FLAG_DAEMON); }
inline bool isNicehash() const { return m_flags.test(FLAG_NICEHASH); } inline bool isNicehash() const { return m_flags.test(FLAG_NICEHASH); }
inline bool isTLS() const { return m_flags.test(FLAG_TLS); } 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 Algorithm &algorithm() const { return m_algorithm; }
inline const Coin &coin() const { return m_coin; } inline const Coin &coin() const { return m_coin; }
inline const String &fingerprint() const { return m_fingerprint; } 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 &password() const { return !m_password.isNull() ? m_password : kDefaultPassword; }
inline const String &rigId() const { return m_rigId; } 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 const String &user() const { return !m_user.isNull() ? m_user : kDefaultUser; }
inline int keepAlive() const { return m_keepAlive; } 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 uint64_t pollInterval() const { return m_pollInterval; }
inline void setAlgo(const Algorithm &algorithm) { m_algorithm = algorithm; } inline void setAlgo(const Algorithm &algorithm) { m_algorithm = algorithm; }
inline void setPassword(const String &password) { m_password = password; } inline void setPassword(const String &password) { m_password = password; }
@ -94,7 +94,6 @@ public:
bool isEnabled() const; bool isEnabled() const;
bool isEqual(const Pool &other) const; bool isEqual(const Pool &other) const;
bool parse(const char *url);
rapidjson::Value toJSON(rapidjson::Document &doc) const; rapidjson::Value toJSON(rapidjson::Document &doc) const;
# ifdef APP_DEBUG # ifdef APP_DEBUG
@ -105,20 +104,16 @@ private:
inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); } inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); }
inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; }
bool parseIPv6(const char *addr);
Algorithm m_algorithm; Algorithm m_algorithm;
Coin m_coin; Coin m_coin;
int m_keepAlive; int m_keepAlive = 0;
std::bitset<FLAG_MAX> m_flags; std::bitset<FLAG_MAX> m_flags = 0;
String m_fingerprint; String m_fingerprint;
String m_host;
String m_password; String m_password;
String m_rigId; String m_rigId;
String m_url;
String m_user; String m_user;
uint16_t m_port; uint64_t m_pollInterval = kDefaultPollInterval;
uint64_t m_pollInterval; Url m_url;
}; };

View file

@ -0,0 +1,165 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2019 Howard Chu <https://github.com/hyc>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/stratum/Url.h"
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#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<size_t>(port++ - base + 1);
char *host = new char[size]();
memcpy(host, base, size - 1);
m_host = host;
m_port = static_cast<uint16_t>(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<size_t>(end - addr);
char *host = new char[size]();
memcpy(host, addr + 1, size - 1);
m_host = host;
m_port = static_cast<uint16_t>(strtol(port + 1, nullptr, 10));
return true;
}

View file

@ -0,0 +1,77 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2019 Howard Chu <https://github.com/hyc>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_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 */