New API class.

This commit is contained in:
XMRig 2019-03-30 00:16:01 +07:00
parent 9cb43f9883
commit dd036368e2
11 changed files with 178 additions and 74 deletions

View file

@ -203,13 +203,13 @@ endif()
if (WITH_HTTPD) if (WITH_HTTPD)
set(HTTPD_SOURCES set(HTTPD_SOURCES
# src/api/Api.h src/api/Api.h
# src/api/ApiRouter.h # src/api/ApiRouter.h
# src/common/api/HttpBody.h # src/common/api/HttpBody.h
src/api/Httpd.h src/api/Httpd.h
# src/common/api/HttpReply.h # src/common/api/HttpReply.h
# src/common/api/HttpRequest.h # src/common/api/HttpRequest.h
# src/api/Api.cpp src/api/Api.cpp
# src/api/ApiRouter.cpp # src/api/ApiRouter.cpp
src/api/Httpd.cpp src/api/Httpd.cpp
# src/common/api/HttpRequest.cpp # src/common/api/HttpRequest.cpp

View file

@ -45,14 +45,8 @@
#include "workers/Workers.h" #include "workers/Workers.h"
#ifdef XMRIG_FEATURE_HTTP
# include "api/Httpd.h"
#endif
xmrig::App::App(Process *process) : xmrig::App::App(Process *process) :
m_console(nullptr), m_console(nullptr),
m_httpd(nullptr),
m_signals(nullptr) m_signals(nullptr)
{ {
m_controller = new Controller(process); m_controller = new Controller(process);
@ -71,10 +65,6 @@ xmrig::App::~App()
delete m_signals; delete m_signals;
delete m_console; delete m_console;
delete m_controller; delete m_controller;
# ifdef XMRIG_FEATURE_HTTP
delete m_httpd;
# endif
} }
@ -98,18 +88,9 @@ int xmrig::App::exec()
return 0; return 0;
} }
# ifdef XMRIG_FEATURE_API
Api::start(m_controller);
# endif
# ifdef XMRIG_FEATURE_HTTP
m_httpd = new Httpd(m_controller);
m_httpd->start();
# endif
Workers::start(m_controller); Workers::start(m_controller);
m_controller->network()->connect(); m_controller->start();
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop()); uv_loop_close(uv_default_loop());
@ -179,10 +160,6 @@ void xmrig::App::onSignal(int signum)
void xmrig::App::close() void xmrig::App::close()
{ {
# ifdef XMRIG_FEATURE_HTTP
m_httpd->stop();
# endif
m_signals->stop(); m_signals->stop();
m_console->stop(); m_console->stop();
m_controller->stop(); m_controller->stop();

View file

@ -36,7 +36,6 @@ namespace xmrig {
class Console; class Console;
class Controller; class Controller;
class Httpd;
class Network; class Network;
class Process; class Process;
class Signals; class Signals;
@ -60,7 +59,6 @@ private:
Console *m_console; Console *m_console;
Controller *m_controller; Controller *m_controller;
Httpd *m_httpd;
Signals *m_signals; Signals *m_signals;
}; };

View file

@ -22,52 +22,128 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <uv.h>
#include "api/Api.h" #include "api/Api.h"
#include "api/ApiRouter.h" #include "base/tools/Buffer.h"
#include "common/api/HttpReply.h" #include "common/crypto/keccak.h"
#include "common/api/HttpRequest.h" #include "core/Config.h"
#include "core/Controller.h"
#include "version.h"
ApiRouter *Api::m_router = nullptr; #ifdef XMRIG_FEATURE_HTTP
# include "api/Httpd.h"
#endif
bool Api::start(xmrig::Controller *controller) xmrig::Api::Api(Controller *controller) :
m_id(),
m_workerId(),
m_controller(controller),
m_httpd(nullptr)
{ {
m_router = new ApiRouter(controller); controller->addListener(this);
return true; genId(m_controller->config()->apiId());
} }
void Api::release() xmrig::Api::~Api()
{ {
delete m_router; # ifdef XMRIG_FEATURE_HTTP
delete m_httpd;
# endif
} }
void Api::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) void xmrig::Api::request(const HttpRequest &req)
{ {
if (!m_router) {
reply.status = 500; }
void xmrig::Api::start()
{
genWorkerId(m_controller->config()->apiWorkerId());
# ifdef XMRIG_FEATURE_HTTP
m_httpd = new Httpd(m_controller);
m_httpd->start();
# endif
}
void xmrig::Api::stop()
{
# ifdef XMRIG_FEATURE_HTTP
m_httpd->stop();
# endif
}
void xmrig::Api::onConfigChanged(Config *config, Config *previousConfig)
{
if (config->apiId() != previousConfig->apiId()) {
genId(config->apiId());
}
if (config->apiWorkerId() != previousConfig->apiWorkerId()) {
genWorkerId(config->apiWorkerId());
}
}
void xmrig::Api::genId(const String &id)
{
memset(m_id, 0, sizeof(m_id));
if (id.size() > 0) {
strncpy(m_id, id.data(), sizeof(m_id) - 1);
return; return;
} }
if (req.method() == xmrig::HttpRequest::Get) { uv_interface_address_t *interfaces;
return m_router->get(req, reply); int count = 0;
}
m_router->exec(req, reply); if (uv_interface_addresses(&interfaces, &count) < 0) {
}
void Api::tick(const xmrig::NetworkState &network)
{
if (!m_router) {
return; return;
} }
m_router->tick(network); for (int i = 0; i < count; i++) {
if (!interfaces[i].is_internal && interfaces[i].address.address4.sin_family == AF_INET) {
uint8_t hash[200];
const size_t addrSize = sizeof(interfaces[i].phys_addr);
const size_t inSize = strlen(APP_KIND) + addrSize + sizeof(uint16_t);
const uint16_t port = static_cast<uint16_t>(m_controller->config()->http().port());
uint8_t *input = new uint8_t[inSize]();
memcpy(input, &port, sizeof(uint16_t));
memcpy(input + sizeof(uint16_t), interfaces[i].phys_addr, addrSize);
memcpy(input + sizeof(uint16_t) + addrSize, APP_KIND, strlen(APP_KIND));
xmrig::keccak(input, inSize, hash);
xmrig::Buffer::toHex(hash, 8, m_id);
delete [] input;
break;
}
}
uv_free_interface_addresses(interfaces, count);
}
void xmrig::Api::genWorkerId(const String &id)
{
memset(m_workerId, 0, sizeof(m_workerId));
if (id.size() > 0) {
strncpy(m_workerId, id.data(), sizeof(m_workerId) - 1);
}
else {
gethostname(m_workerId, sizeof(m_workerId) - 1);
}
} }

View file

@ -26,32 +26,46 @@
#define XMRIG_API_H #define XMRIG_API_H
#include <uv.h> #include "base/kernel/interfaces/IControllerListener.h"
class ApiRouter;
class Hashrate;
namespace xmrig { namespace xmrig {
class Controller; class Controller;
class HttpReply; class Httpd;
class HttpRequest; class HttpRequest;
class NetworkState; class String;
}
class Api class Api : public IControllerListener
{ {
public: public:
static bool start(xmrig::Controller *controller); Api(Controller *controller);
static void release(); ~Api() override;
static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); inline const char *id() const { return m_id; }
static void tick(const xmrig::NetworkState &results); inline const char *workerId() const { return m_workerId; }
void request(const HttpRequest &req);
void start();
void stop();
protected:
void onConfigChanged(Config *config, Config *previousConfig) override;
private: private:
static ApiRouter *m_router; void genId(const String &id);
void genWorkerId(const String &id);
char m_id[32];
char m_workerId[128];
Controller *m_controller;
Httpd *m_httpd;
}; };
} // namespace xmrig
#endif /* XMRIG_API_H */ #endif /* XMRIG_API_H */

View file

@ -27,7 +27,7 @@
#include "api/NetworkState.h" #include "api/NetworkState.h"
#include "common/interfaces/IControllerListener.h" #include "base/kernel/interfaces/IControllerListener.h"
#include "rapidjson/fwd.h" #include "rapidjson/fwd.h"

View file

@ -24,10 +24,11 @@
#include "3rdparty/http-parser/http_parser.h" #include "3rdparty/http-parser/http_parser.h"
#include "api/Api.h"
#include "api/Httpd.h" #include "api/Httpd.h"
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/net/http/HttpRequest.h"
#include "base/net/http/HttpApiResponse.h" #include "base/net/http/HttpApiResponse.h"
#include "base/net/http/HttpRequest.h"
#include "base/net/http/HttpServer.h" #include "base/net/http/HttpServer.h"
#include "base/net/tools/TcpServer.h" #include "base/net/tools/TcpServer.h"
#include "core/Config.h" #include "core/Config.h"
@ -136,8 +137,7 @@ void xmrig::Httpd::onHttpRequest(const HttpRequest &req)
} }
} }
HttpApiResponse res(req.id()); m_controller->api()->request(req);
res.end();
} }

View file

@ -106,7 +106,6 @@ if (WITH_HTTPD)
add_definitions(/DXMRIG_FEATURE_HTTP) add_definitions(/DXMRIG_FEATURE_HTTP)
add_definitions(/DXMRIG_FEATURE_API) add_definitions(/DXMRIG_FEATURE_API)
remove_definitions(/DXMRIG_FEATURE_API)
else() else()
set(HEADERS_BASE_HTTP "") set(HEADERS_BASE_HTTP "")
set(SOURCES_BASE_HTTP "") set(SOURCES_BASE_HTTP "")

View file

@ -43,10 +43,16 @@
#endif #endif
#ifdef XMRIG_FEATURE_API
# include "api/Api.h"
#endif
class xmrig::ControllerPrivate class xmrig::ControllerPrivate
{ {
public: public:
inline ControllerPrivate(Process *process) : inline ControllerPrivate(Process *process) :
api(nullptr),
config(nullptr), config(nullptr),
network(nullptr), network(nullptr),
process(process) process(process)
@ -55,11 +61,16 @@ public:
inline ~ControllerPrivate() inline ~ControllerPrivate()
{ {
# ifdef XMRIG_FEATURE_API
delete api;
# endif
delete network; delete network;
delete config; delete config;
} }
Api *api;
Config *config; Config *config;
Network *network; Network *network;
Process *process; Process *process;
@ -79,6 +90,14 @@ xmrig::Controller::~Controller()
} }
xmrig::Api *xmrig::Controller::api() const
{
assert(d_ptr->api != nullptr);
return d_ptr->api;
}
bool xmrig::Controller::isReady() const bool xmrig::Controller::isReady() const
{ {
return d_ptr->config && d_ptr->network; return d_ptr->config && d_ptr->network;
@ -102,6 +121,10 @@ int xmrig::Controller::init()
return 1; return 1;
} }
# ifdef XMRIG_FEATURE_API
d_ptr->api = new Api(this);
# endif
Platform::init(config()->userAgent()); Platform::init(config()->userAgent());
Platform::setProcessPriority(d_ptr->config->priority()); Platform::setProcessPriority(d_ptr->config->priority());
@ -165,8 +188,22 @@ void xmrig::Controller::onNewConfig(IConfig *config)
} }
void xmrig::Controller::start()
{
network()->connect();
# ifdef XMRIG_FEATURE_API
api()->start();
# endif
}
void xmrig::Controller::stop() void xmrig::Controller::stop()
{ {
# ifdef XMRIG_FEATURE_API
api()->stop();
# endif
ConfigLoader::release(); ConfigLoader::release();
delete d_ptr->network; delete d_ptr->network;

View file

@ -35,6 +35,7 @@ class StatsData;
namespace xmrig { namespace xmrig {
class Api;
class Config; class Config;
class ControllerPrivate; class ControllerPrivate;
class IControllerListener; class IControllerListener;
@ -48,12 +49,14 @@ public:
Controller(Process *process); Controller(Process *process);
~Controller() override; ~Controller() override;
Api *api() const;
bool isReady() const; bool isReady() const;
Config *config() const; Config *config() const;
int init(); int init();
Network *network() const; Network *network() const;
void addListener(IControllerListener *listener); void addListener(IControllerListener *listener);
void save(); void save();
void start();
void stop(); void stop();
protected: protected:

View file

@ -198,6 +198,6 @@ void xmrig::Network::tick()
} }
# ifdef XMRIG_FEATURE_API # ifdef XMRIG_FEATURE_API
Api::tick(m_state); //Api::tick(m_state);
# endif # endif
} }