mirror of
https://github.com/xmrig/xmrig.git
synced 2024-12-22 19:49:36 +00:00
New API class.
This commit is contained in:
parent
9cb43f9883
commit
dd036368e2
11 changed files with 178 additions and 74 deletions
|
@ -203,13 +203,13 @@ endif()
|
|||
|
||||
if (WITH_HTTPD)
|
||||
set(HTTPD_SOURCES
|
||||
# src/api/Api.h
|
||||
src/api/Api.h
|
||||
# src/api/ApiRouter.h
|
||||
# src/common/api/HttpBody.h
|
||||
src/api/Httpd.h
|
||||
# src/common/api/HttpReply.h
|
||||
# src/common/api/HttpRequest.h
|
||||
# src/api/Api.cpp
|
||||
src/api/Api.cpp
|
||||
# src/api/ApiRouter.cpp
|
||||
src/api/Httpd.cpp
|
||||
# src/common/api/HttpRequest.cpp
|
||||
|
|
25
src/App.cpp
25
src/App.cpp
|
@ -45,14 +45,8 @@
|
|||
#include "workers/Workers.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_HTTP
|
||||
# include "api/Httpd.h"
|
||||
#endif
|
||||
|
||||
|
||||
xmrig::App::App(Process *process) :
|
||||
m_console(nullptr),
|
||||
m_httpd(nullptr),
|
||||
m_signals(nullptr)
|
||||
{
|
||||
m_controller = new Controller(process);
|
||||
|
@ -71,10 +65,6 @@ xmrig::App::~App()
|
|||
delete m_signals;
|
||||
delete m_console;
|
||||
delete m_controller;
|
||||
|
||||
# ifdef XMRIG_FEATURE_HTTP
|
||||
delete m_httpd;
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -98,18 +88,9 @@ int xmrig::App::exec()
|
|||
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);
|
||||
|
||||
m_controller->network()->connect();
|
||||
m_controller->start();
|
||||
|
||||
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||
uv_loop_close(uv_default_loop());
|
||||
|
@ -179,10 +160,6 @@ void xmrig::App::onSignal(int signum)
|
|||
|
||||
void xmrig::App::close()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_HTTP
|
||||
m_httpd->stop();
|
||||
# endif
|
||||
|
||||
m_signals->stop();
|
||||
m_console->stop();
|
||||
m_controller->stop();
|
||||
|
|
|
@ -36,7 +36,6 @@ namespace xmrig {
|
|||
|
||||
class Console;
|
||||
class Controller;
|
||||
class Httpd;
|
||||
class Network;
|
||||
class Process;
|
||||
class Signals;
|
||||
|
@ -60,7 +59,6 @@ private:
|
|||
|
||||
Console *m_console;
|
||||
Controller *m_controller;
|
||||
Httpd *m_httpd;
|
||||
Signals *m_signals;
|
||||
};
|
||||
|
||||
|
|
124
src/api/Api.cpp
124
src/api/Api.cpp
|
@ -22,52 +22,128 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
#include "api/Api.h"
|
||||
#include "api/ApiRouter.h"
|
||||
#include "common/api/HttpReply.h"
|
||||
#include "common/api/HttpRequest.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
#include "common/crypto/keccak.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;
|
||||
}
|
||||
|
||||
if (req.method() == xmrig::HttpRequest::Get) {
|
||||
return m_router->get(req, reply);
|
||||
}
|
||||
uv_interface_address_t *interfaces;
|
||||
int count = 0;
|
||||
|
||||
m_router->exec(req, reply);
|
||||
}
|
||||
|
||||
|
||||
void Api::tick(const xmrig::NetworkState &network)
|
||||
{
|
||||
if (!m_router) {
|
||||
if (uv_interface_addresses(&interfaces, &count) < 0) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,32 +26,46 @@
|
|||
#define XMRIG_API_H
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
class ApiRouter;
|
||||
class Hashrate;
|
||||
#include "base/kernel/interfaces/IControllerListener.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
class Controller;
|
||||
class HttpReply;
|
||||
class HttpRequest;
|
||||
class NetworkState;
|
||||
}
|
||||
|
||||
|
||||
class Api
|
||||
class Controller;
|
||||
class Httpd;
|
||||
class HttpRequest;
|
||||
class String;
|
||||
|
||||
|
||||
class Api : public IControllerListener
|
||||
{
|
||||
public:
|
||||
static bool start(xmrig::Controller *controller);
|
||||
static void release();
|
||||
Api(Controller *controller);
|
||||
~Api() override;
|
||||
|
||||
static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply);
|
||||
static void tick(const xmrig::NetworkState &results);
|
||||
inline const char *id() const { return m_id; }
|
||||
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:
|
||||
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 */
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
|
||||
#include "api/NetworkState.h"
|
||||
#include "common/interfaces/IControllerListener.h"
|
||||
#include "base/kernel/interfaces/IControllerListener.h"
|
||||
#include "rapidjson/fwd.h"
|
||||
|
||||
|
||||
|
|
|
@ -24,10 +24,11 @@
|
|||
|
||||
|
||||
#include "3rdparty/http-parser/http_parser.h"
|
||||
#include "api/Api.h"
|
||||
#include "api/Httpd.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/net/http/HttpRequest.h"
|
||||
#include "base/net/http/HttpApiResponse.h"
|
||||
#include "base/net/http/HttpRequest.h"
|
||||
#include "base/net/http/HttpServer.h"
|
||||
#include "base/net/tools/TcpServer.h"
|
||||
#include "core/Config.h"
|
||||
|
@ -136,8 +137,7 @@ void xmrig::Httpd::onHttpRequest(const HttpRequest &req)
|
|||
}
|
||||
}
|
||||
|
||||
HttpApiResponse res(req.id());
|
||||
res.end();
|
||||
m_controller->api()->request(req);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -106,7 +106,6 @@ if (WITH_HTTPD)
|
|||
|
||||
add_definitions(/DXMRIG_FEATURE_HTTP)
|
||||
add_definitions(/DXMRIG_FEATURE_API)
|
||||
remove_definitions(/DXMRIG_FEATURE_API)
|
||||
else()
|
||||
set(HEADERS_BASE_HTTP "")
|
||||
set(SOURCES_BASE_HTTP "")
|
||||
|
|
|
@ -43,10 +43,16 @@
|
|||
#endif
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_API
|
||||
# include "api/Api.h"
|
||||
#endif
|
||||
|
||||
|
||||
class xmrig::ControllerPrivate
|
||||
{
|
||||
public:
|
||||
inline ControllerPrivate(Process *process) :
|
||||
api(nullptr),
|
||||
config(nullptr),
|
||||
network(nullptr),
|
||||
process(process)
|
||||
|
@ -55,11 +61,16 @@ public:
|
|||
|
||||
inline ~ControllerPrivate()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
delete api;
|
||||
# endif
|
||||
|
||||
delete network;
|
||||
delete config;
|
||||
}
|
||||
|
||||
|
||||
Api *api;
|
||||
Config *config;
|
||||
Network *network;
|
||||
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
|
||||
{
|
||||
return d_ptr->config && d_ptr->network;
|
||||
|
@ -102,6 +121,10 @@ int xmrig::Controller::init()
|
|||
return 1;
|
||||
}
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
d_ptr->api = new Api(this);
|
||||
# endif
|
||||
|
||||
Platform::init(config()->userAgent());
|
||||
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()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
api()->stop();
|
||||
# endif
|
||||
|
||||
ConfigLoader::release();
|
||||
|
||||
delete d_ptr->network;
|
||||
|
|
|
@ -35,6 +35,7 @@ class StatsData;
|
|||
namespace xmrig {
|
||||
|
||||
|
||||
class Api;
|
||||
class Config;
|
||||
class ControllerPrivate;
|
||||
class IControllerListener;
|
||||
|
@ -48,12 +49,14 @@ public:
|
|||
Controller(Process *process);
|
||||
~Controller() override;
|
||||
|
||||
Api *api() const;
|
||||
bool isReady() const;
|
||||
Config *config() const;
|
||||
int init();
|
||||
Network *network() const;
|
||||
void addListener(IControllerListener *listener);
|
||||
void save();
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
|
|
|
@ -198,6 +198,6 @@ void xmrig::Network::tick()
|
|||
}
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
Api::tick(m_state);
|
||||
//Api::tick(m_state);
|
||||
# endif
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue