Added "donate-over-proxy" option.
This commit is contained in:
parent
55686c7d57
commit
0907d1eb0c
15 changed files with 229 additions and 141 deletions
|
@ -54,8 +54,8 @@ xmrig::Network::Network(Controller *controller) :
|
|||
const Pools &pools = controller->config()->pools();
|
||||
m_strategy = pools.createStrategy(this);
|
||||
|
||||
if (controller->config()->donateLevel() > 0) {
|
||||
m_donate = new DonateStrategy(controller->config()->donateLevel(), pools.data().front().user(), controller->config()->algorithm().algo(), this);
|
||||
if (pools.donateLevel() > 0) {
|
||||
m_donate = new DonateStrategy(controller, this);
|
||||
}
|
||||
|
||||
m_timer = new Timer(this, kTickInterval, kTickInterval);
|
||||
|
|
|
@ -23,6 +23,9 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
#include "base/net/stratum/Client.h"
|
||||
#include "base/net/stratum/Job.h"
|
||||
#include "base/net/stratum/strategies/FailoverStrategy.h"
|
||||
|
@ -32,40 +35,50 @@
|
|||
#include "common/crypto/keccak.h"
|
||||
#include "common/Platform.h"
|
||||
#include "common/xmrig.h"
|
||||
#include "core/Config.h"
|
||||
#include "core/Controller.h"
|
||||
#include "net/Network.h"
|
||||
#include "net/strategies/DonateStrategy.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
static inline double randomf(double min, double max) { return (max - min) * (((static_cast<double>(rand())) / static_cast<double>(RAND_MAX))) + min; }
|
||||
static inline double randomf(double min, double max) { return (max - min) * (((static_cast<double>(rand())) / static_cast<double>(RAND_MAX))) + min; }
|
||||
static inline uint64_t random(uint64_t base, double min, double max) { return static_cast<uint64_t>(base * randomf(min, max)); }
|
||||
|
||||
static const char *kDonateHost = "donate.v2.xmrig.com";
|
||||
#ifdef XMRIG_FEATURE_TLS
|
||||
static const char *kDonateHostTls = "donate.ssl.xmrig.com";
|
||||
#endif
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener) :
|
||||
m_active(false),
|
||||
m_donateTime(static_cast<uint64_t>(level) * 60 * 1000),
|
||||
m_idleTime((100 - static_cast<uint64_t>(level)) * 60 * 1000),
|
||||
xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener *listener) :
|
||||
m_proxy(nullptr),
|
||||
m_donateTime(static_cast<uint64_t>(controller->config()->pools().donateLevel()) * 60 * 1000),
|
||||
m_idleTime((100 - static_cast<uint64_t>(controller->config()->pools().donateLevel())) * 60 * 1000),
|
||||
m_controller(controller),
|
||||
m_strategy(nullptr),
|
||||
m_listener(listener),
|
||||
m_state(STATE_NEW),
|
||||
m_now(0),
|
||||
m_stop(0)
|
||||
{
|
||||
uint8_t hash[200];
|
||||
char userId[65] = { 0 };
|
||||
|
||||
keccak(reinterpret_cast<const uint8_t *>(user), strlen(user), hash);
|
||||
const String &user = controller->config()->pools().data().front().user();
|
||||
keccak(reinterpret_cast<const uint8_t *>(user.data()), user.size(), hash);
|
||||
Buffer::toHex(hash, 32, userId);
|
||||
|
||||
# ifdef XMRIG_FEATURE_TLS
|
||||
m_pools.push_back(Pool("donate.ssl.xmrig.com", 443, userId, nullptr, false, true, true));
|
||||
m_pools.push_back(Pool(kDonateHostTls, 443, userId, nullptr, false, true, true));
|
||||
# endif
|
||||
|
||||
m_pools.push_back(Pool("donate.v2.xmrig.com", 3333, userId, nullptr, false, true));
|
||||
m_pools.push_back(Pool(kDonateHost, 3333, userId, nullptr, false, true));
|
||||
|
||||
for (Pool &pool : m_pools) {
|
||||
pool.adjust(Algorithm(algo, VARIANT_AUTO));
|
||||
pool.adjust(Algorithm(controller->config()->algorithm().algo(), VARIANT_AUTO));
|
||||
}
|
||||
|
||||
if (m_pools.size() > 1) {
|
||||
|
@ -77,7 +90,7 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS
|
|||
|
||||
m_timer = new Timer(this);
|
||||
|
||||
idle(random(m_idleTime, 0.5, 1.5));
|
||||
setState(STATE_IDLE);
|
||||
}
|
||||
|
||||
|
||||
|
@ -119,20 +132,19 @@ void xmrig::DonateStrategy::tick(uint64_t now)
|
|||
|
||||
m_strategy->tick(now);
|
||||
|
||||
if (m_stop && now > m_stop) {
|
||||
m_strategy->stop();
|
||||
m_stop = 0;
|
||||
if (state() == STATE_WAIT && now > m_stop) {
|
||||
setState(STATE_IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DonateStrategy::onActive(IStrategy *strategy, Client *client)
|
||||
{
|
||||
if (!isActive()) {
|
||||
m_timer->start(m_donateTime, 0);
|
||||
if (isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_active = true;
|
||||
setState(STATE_ACTIVE);
|
||||
m_listener->onActive(this, client);
|
||||
}
|
||||
|
||||
|
@ -158,30 +170,53 @@ void xmrig::DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client
|
|||
|
||||
void xmrig::DonateStrategy::onTimer(const Timer *)
|
||||
{
|
||||
if (!isActive()) {
|
||||
return connect();
|
||||
setState(isActive() ? STATE_WAIT : STATE_CONNECT);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DonateStrategy::idle(double min, double max)
|
||||
{
|
||||
m_timer->start(random(m_idleTime, min, max), 0);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DonateStrategy::setState(State state)
|
||||
{
|
||||
constexpr const uint64_t waitTime = 3000;
|
||||
|
||||
assert(m_state != state && state != STATE_NEW);
|
||||
if (m_state == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
suspend();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DonateStrategy::idle(uint64_t timeout)
|
||||
{
|
||||
m_timer->start(timeout, 0);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::DonateStrategy::suspend()
|
||||
{
|
||||
# if defined(XMRIG_AMD_PROJECT) || defined(XMRIG_NVIDIA_PROJECT)
|
||||
m_stop = m_now + 5000;
|
||||
# else
|
||||
m_stop = m_now + 500;
|
||||
# endif
|
||||
|
||||
m_active = false;
|
||||
m_listener->onPause(this);
|
||||
|
||||
idle(random(m_idleTime, 0.8, 1.2));
|
||||
const State prev = m_state;
|
||||
m_state = state;
|
||||
|
||||
switch (state) {
|
||||
case STATE_NEW:
|
||||
break;
|
||||
|
||||
case STATE_IDLE:
|
||||
if (prev == STATE_NEW) {
|
||||
idle(0.5, 1.5);
|
||||
}
|
||||
else {
|
||||
m_strategy->stop();
|
||||
idle(0.8, 1.2);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_CONNECT:
|
||||
connect();
|
||||
break;
|
||||
|
||||
case STATE_ACTIVE:
|
||||
m_timer->start(m_donateTime, 0);
|
||||
break;
|
||||
|
||||
case STATE_WAIT:
|
||||
m_stop = m_now + waitTime;
|
||||
m_listener->onPause(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#define XMRIG_DONATESTRATEGY_H
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
@ -41,17 +40,18 @@ namespace xmrig {
|
|||
|
||||
|
||||
class Client;
|
||||
class Controller;
|
||||
class IStrategyListener;
|
||||
|
||||
|
||||
class DonateStrategy : public IStrategy, public IStrategyListener, public ITimerListener
|
||||
{
|
||||
public:
|
||||
DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener);
|
||||
DonateStrategy(Controller *controller, IStrategyListener *listener);
|
||||
~DonateStrategy() override;
|
||||
|
||||
public:
|
||||
inline bool isActive() const override { return m_active; }
|
||||
inline bool isActive() const override { return state() == STATE_ACTIVE; }
|
||||
inline void resume() override {}
|
||||
|
||||
int64_t submit(const JobResult &result) override;
|
||||
|
@ -68,20 +68,30 @@ protected:
|
|||
void onTimer(const Timer *timer) override;
|
||||
|
||||
private:
|
||||
void idle(uint64_t timeout);
|
||||
void suspend();
|
||||
enum State {
|
||||
STATE_NEW,
|
||||
STATE_IDLE,
|
||||
STATE_CONNECT,
|
||||
STATE_ACTIVE,
|
||||
STATE_WAIT
|
||||
};
|
||||
|
||||
static void onTimer(uv_timer_t *handle);
|
||||
inline State state() const { return m_state; }
|
||||
|
||||
bool m_active;
|
||||
void idle(double min, double max);
|
||||
void setState(State state);
|
||||
|
||||
Client *m_proxy;
|
||||
const uint64_t m_donateTime;
|
||||
const uint64_t m_idleTime;
|
||||
Controller *m_controller;
|
||||
IStrategy *m_strategy;
|
||||
IStrategyListener *m_listener;
|
||||
State m_state;
|
||||
std::vector<Pool> m_pools;
|
||||
Timer *m_timer;
|
||||
uint64_t m_now;
|
||||
uint64_t m_stop;
|
||||
Timer *m_timer;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue