Initial multiple pools support [1/2].

This commit is contained in:
XMRig 2017-06-26 21:13:05 +03:00
parent faf793b0aa
commit 952017ae7a
6 changed files with 162 additions and 111 deletions

View file

@ -150,7 +150,6 @@ Options::Options(int argc, char **argv) :
m_syslog(false), m_syslog(false),
m_logFile(nullptr), m_logFile(nullptr),
m_pass(nullptr), m_pass(nullptr),
m_user(nullptr),
m_algo(0), m_algo(0),
m_algoVariant(0), m_algoVariant(0),
m_donateLevel(kDonateLevel), m_donateLevel(kDonateLevel),
@ -159,10 +158,10 @@ Options::Options(int argc, char **argv) :
m_retries(5), m_retries(5),
m_retryPause(5), m_retryPause(5),
m_threads(0), m_threads(0),
m_affinity(-1L), m_affinity(-1L)
m_backupUrl(nullptr),
m_url(nullptr)
{ {
m_pools.push_back(new Url());
int key; int key;
while (1) { while (1) {
@ -181,18 +180,18 @@ Options::Options(int argc, char **argv) :
return; return;
} }
if (!m_url) { if (!m_pools[0]->isValid()) {
fprintf(stderr, "No pool URL supplied. Exiting."); fprintf(stderr, "No pool URL supplied. Exiting.");
return; return;
} }
if (!m_nicehash && m_url->isNicehash()) { // if (!m_nicehash && m_url->isNicehash()) {
m_nicehash = true; // m_nicehash = true;
} // }
if (!m_user) { // if (!m_user) {
m_user = strdup("x"); // m_user = strdup("x");
} // }
if (!m_pass) { if (!m_pass) {
m_pass = strdup("x"); m_pass = strdup("x");
@ -219,10 +218,6 @@ Options::Options(int argc, char **argv) :
Options::~Options() Options::~Options()
{ {
delete m_url;
delete m_backupUrl;
free(m_user);
free(m_pass); free(m_pass);
} }
@ -232,7 +227,6 @@ bool Options::parseArg(int key, char *arg)
char *p; char *p;
int v; int v;
uint64_t ul; uint64_t ul;
Url *url;
switch (key) { switch (key) {
case 'a': /* --algo */ case 'a': /* --algo */
@ -248,29 +242,26 @@ bool Options::parseArg(int key, char *arg)
break; break;
case 'o': /* --url */ case 'o': /* --url */
url = parseUrl(arg); if (m_pools[0]->isValid()) {
if (url) { Url *url = new Url(arg);
free(m_url); if (url->isValid()) {
m_url = url; m_pools.push_back(url);
}
else {
delete url;
}
} }
break; else {
m_pools[0]->parse(arg);
case 'b': /* --backup-url */
url = parseUrl(arg);
if (url) {
free(m_backupUrl);
m_backupUrl = url;
} }
break; break;
case 'u': /* --user */ case 'u': /* --user */
free(m_user); m_pools.back()->setUser(arg);
m_user = strdup(arg);
break; break;
case 'p': /* --pass */ case 'p': /* --pass */
free(m_pass); m_pools.back()->setPassword(arg);
m_pass = strdup(arg);
break; break;
case 'l': /* --log-file */ case 'l': /* --log-file */
@ -485,18 +476,18 @@ bool Options::setAlgo(const char *algo)
bool Options::setUserpass(const char *userpass) bool Options::setUserpass(const char *userpass)
{ {
const char *p = strchr(userpass, ':'); // const char *p = strchr(userpass, ':');
if (!p) { // if (!p) {
showUsage(1); // showUsage(1);
return false; // return false;
} // }
free(m_user); //// free(m_user);
free(m_pass); // free(m_pass);
m_user = static_cast<char*>(calloc(p - userpass + 1, 1)); //// m_user = static_cast<char*>(calloc(p - userpass + 1, 1));
strncpy(m_user, userpass, p - userpass); // strncpy(m_user, userpass, p - userpass);
m_pass = strdup(p + 1); // m_pass = strdup(p + 1);
return true; return true;
} }

View file

@ -25,6 +25,7 @@
#define __OPTIONS_H__ #define __OPTIONS_H__
#include <vector>
#include <stdint.h> #include <stdint.h>
@ -51,26 +52,24 @@ public:
static inline Options* i() { return m_self; } static inline Options* i() { return m_self; }
static Options *parse(int argc, char **argv); static Options *parse(int argc, char **argv);
inline bool background() const { return m_background; } inline bool background() const { return m_background; }
inline bool colors() const { return m_colors; } inline bool colors() const { return m_colors; }
inline bool doubleHash() const { return m_doubleHash; } inline bool doubleHash() const { return m_doubleHash; }
inline bool isReady() const { return m_ready; } inline bool isReady() const { return m_ready; }
inline bool keepAlive() const { return m_keepAlive; } inline bool keepAlive() const { return m_keepAlive; }
inline bool nicehash() const { return m_nicehash; } inline bool nicehash() const { return m_nicehash; }
inline bool syslog() const { return m_syslog; } inline bool syslog() const { return m_syslog; }
inline const char *logFile() const { return m_logFile; } inline const char *logFile() const { return m_logFile; }
inline const char *pass() const { return m_pass; } inline const char *pass() const { return m_pass; }
inline const char *user() const { return m_user; } inline const std::vector<Url*> &pools() const { return m_pools; }
inline const Url *backupUrl() const { return m_backupUrl; } inline int algo() const { return m_algo; }
inline const Url *url() const { return m_url; } inline int algoVariant() const { return m_algoVariant; }
inline int algo() const { return m_algo; } inline int donateLevel() const { return m_donateLevel; }
inline int algoVariant() const { return m_algoVariant; } inline int printTime() const { return m_printTime; }
inline int donateLevel() const { return m_donateLevel; } inline int retries() const { return m_retries; }
inline int printTime() const { return m_printTime; } inline int retryPause() const { return m_retryPause; }
inline int retries() const { return m_retries; } inline int threads() const { return m_threads; }
inline int retryPause() const { return m_retryPause; } inline int64_t affinity() const { return m_affinity; }
inline int threads() const { return m_threads; }
inline int64_t affinity() const { return m_affinity; }
const char *algoName() const; const char *algoName() const;
@ -103,7 +102,7 @@ private:
bool m_syslog; bool m_syslog;
char *m_logFile; char *m_logFile;
char *m_pass; char *m_pass;
char *m_user; // char *m_user;
int m_algo; int m_algo;
int m_algoVariant; int m_algoVariant;
int m_donateLevel; int m_donateLevel;
@ -113,8 +112,7 @@ private:
int m_retryPause; int m_retryPause;
int m_threads; int m_threads;
int64_t m_affinity; int64_t m_affinity;
Url *m_backupUrl; std::vector<Url*> m_pools;
Url *m_url;
}; };
#endif /* __OPTIONS_H__ */ #endif /* __OPTIONS_H__ */

View file

@ -111,17 +111,20 @@ static void print_threads()
static void print_pools() static void print_pools()
{ {
Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #1: \x1B[01;36m%s:%d" : " * POOL #1: %s:%d", const std::vector<Url*> &pools = Options::i()->pools();
Options::i()->url()->host(),
Options::i()->url()->port());
if (!Options::i()->backupUrl()) { for (size_t i = 0; i < pools.size(); ++i) {
return; Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d",
i + 1,
pools[i]->host(),
pools[i]->port());
} }
Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #2: \x1B[01;36m%s:%d" : " * POOL #2: %s:%d", # ifdef APP_DEBUG
Options::i()->backupUrl()->host(), for (size_t i = 0; i < pools.size(); ++i) {
Options::i()->backupUrl()->port()); Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i]->host(), pools[i]->port(), pools[i]->user(), pools[i]->password(), pools[i]->isKeepAlive(), pools[i]->isNicehash());
}
# endif
} }

View file

@ -45,8 +45,8 @@ Network::Network(const Options *options) :
m_agent = userAgent(); m_agent = userAgent();
addPool(std::make_unique<Url>().get()); addPool(std::make_unique<Url>().get());
addPool(m_options->url()); // addPool(m_options->url());
addPool(m_options->backupUrl()); // addPool(m_options->backupUrl());
m_timer.data = this; m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer); uv_timer_init(uv_default_loop(), &m_timer);
@ -120,7 +120,7 @@ void Network::onJobResult(const JobResult &result)
void Network::onLoginCredentialsRequired(Client *client) void Network::onLoginCredentialsRequired(Client *client)
{ {
client->login(m_options->user(), m_options->pass(), m_agent); // client->login(m_options->user(), m_options->pass(), m_agent);
} }

View file

@ -35,8 +35,12 @@
Url::Url() : Url::Url() :
m_keepAlive(false),
m_nicehash(false),
m_host(nullptr), m_host(nullptr),
m_port(3333) m_password(nullptr),
m_user(nullptr),
m_port(kDefaultPort)
{ {
} }
@ -53,40 +57,22 @@ Url::Url() :
* @param url * @param url
*/ */
Url::Url(const char *url) : Url::Url(const char *url) :
m_keepAlive(false),
m_nicehash(false),
m_host(nullptr), m_host(nullptr),
m_port(3333) m_password(nullptr),
m_user(nullptr),
m_port(kDefaultPort)
{ {
const char *p = strstr(url, "://"); parse(url);
const char *base = url;
if (p) {
if (strncasecmp(url, "stratum+tcp://", 14)) {
return;
}
base = url + 14;
}
if (!strlen(base) || *base == '/') {
return;
}
const char *port = strchr(base, ':');
if (!port) {
m_host = strdup(base);
return;
}
const size_t size = port++ - base + 1;
m_host = static_cast<char*>(malloc(size));
memcpy(m_host, base, size - 1);
m_host[size - 1] = '\0';
m_port = strtol(port, nullptr, 10);
} }
Url::Url(const char *host, uint16_t port) : Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) :
m_keepAlive(false),
m_nicehash(false),
m_password(nullptr),
m_user(nullptr),
m_port(port) m_port(port)
{ {
m_host = strdup(host); m_host = strdup(host);
@ -96,10 +82,67 @@ Url::Url(const char *host, uint16_t port) :
Url::~Url() Url::~Url()
{ {
free(m_host); free(m_host);
free(m_password);
free(m_user);
} }
bool Url::isNicehash() const bool Url::isNicehash() const
{ {
return isValid() && strstr(m_host, ".nicehash.com"); return isValid() && (m_nicehash || strstr(m_host, ".nicehash.com"));
}
bool Url::parse(const char *url)
{
const char *p = strstr(url, "://");
const char *base = url;
if (p) {
if (strncasecmp(url, "stratum+tcp://", 14)) {
return false;
}
base = url + 14;
}
if (!strlen(base) || *base == '/') {
return false;
}
const char *port = strchr(base, ':');
if (!port) {
m_host = strdup(base);
return false;
}
const size_t size = port++ - base + 1;
m_host = static_cast<char*>(malloc(size));
memcpy(m_host, base, size - 1);
m_host[size - 1] = '\0';
m_port = strtol(port, nullptr, 10);
return true;
}
void Url::setPassword(const char *password, bool force)
{
if (m_password != nullptr && !force) {
return;
}
free(m_password);
m_password = strdup(password);
}
void Url::setUser(const char *user, bool force)
{
if (m_user != nullptr && !force) {
return;
}
free(m_user);
m_user = strdup(user);
} }

View file

@ -31,19 +31,35 @@
class Url class Url
{ {
public: public:
constexpr static const char *kDefaultPassword = "x";
constexpr static const char *kDefaultUser = "x";
constexpr static uint16_t kDefaultPort = 3333;
Url(); Url();
Url(const char *url); Url(const char *url);
Url(const char *host, uint16_t port); Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false );
~Url(); ~Url();
bool isNicehash() const; inline bool isKeepAlive() const { return m_keepAlive; }
inline bool isValid() const { return m_host && m_port > 0; }
inline const char *host() const { return m_host; }
inline const char *password() const { return m_password ? m_password : kDefaultPassword; }
inline const char *user() const { return m_user ? m_user : kDefaultUser; }
inline uint16_t port() const { return m_port; }
inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline bool isValid() const { return m_host && m_port > 0; } bool isNicehash() const;
inline const char *host() const { return m_host; } bool parse(const char *url);
inline uint16_t port() const { return m_port; } void setPassword(const char *password, bool force = true);
void setUser(const char *user, bool force = true);
private: private:
bool m_keepAlive;
bool m_nicehash;
char *m_host; char *m_host;
char *m_password;
char *m_user;
uint16_t m_port; uint16_t m_port;
}; };