Common API code moved to base/api.
This commit is contained in:
parent
a9c1c1ac64
commit
2ec257284f
19 changed files with 36 additions and 46 deletions
208
src/base/api/Api.cpp
Normal file
208
src/base/api/Api.cpp
Normal file
|
@ -0,0 +1,208 @@
|
|||
/* 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 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 <uv.h>
|
||||
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "3rdparty/http-parser/http_parser.h"
|
||||
#include "base/api/Api.h"
|
||||
#include "base/api/interfaces/IApiListener.h"
|
||||
#include "base/api/requests/HttpApiRequest.h"
|
||||
#include "base/kernel/Base.h"
|
||||
#include "base/tools/Buffer.h"
|
||||
#include "base/tools/Chrono.h"
|
||||
#include "core/config/Config.h"
|
||||
#include "core/Controller.h"
|
||||
#include "crypto/common/keccak.h"
|
||||
#include "version.h"
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_HTTP
|
||||
# include "base/api/Httpd.h"
|
||||
#endif
|
||||
|
||||
|
||||
xmrig::Api::Api(Base *base) :
|
||||
m_base(base),
|
||||
m_id(),
|
||||
m_workerId(),
|
||||
m_timestamp(Chrono::currentMSecsSinceEpoch()),
|
||||
m_httpd(nullptr)
|
||||
{
|
||||
base->addListener(this);
|
||||
|
||||
genId(base->config()->apiId());
|
||||
}
|
||||
|
||||
|
||||
xmrig::Api::~Api()
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_HTTP
|
||||
delete m_httpd;
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Api::request(const HttpData &req)
|
||||
{
|
||||
HttpApiRequest request(req, m_base->config()->http().isRestricted());
|
||||
|
||||
exec(request);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Api::start()
|
||||
{
|
||||
genWorkerId(m_base->config()->apiWorkerId());
|
||||
|
||||
# ifdef XMRIG_FEATURE_HTTP
|
||||
m_httpd = new Httpd(m_base);
|
||||
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::exec(IApiRequest &request)
|
||||
{
|
||||
using namespace rapidjson;
|
||||
|
||||
if (request.type() == IApiRequest::REQ_SUMMARY) {
|
||||
auto &allocator = request.doc().GetAllocator();
|
||||
|
||||
request.accept();
|
||||
request.reply().AddMember("id", StringRef(m_id), allocator);
|
||||
request.reply().AddMember("worker_id", StringRef(m_workerId), allocator);
|
||||
request.reply().AddMember("uptime", (Chrono::currentMSecsSinceEpoch() - m_timestamp) / 1000, allocator);
|
||||
|
||||
Value features(kArrayType);
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
features.PushBack("api", allocator);
|
||||
# endif
|
||||
# ifdef XMRIG_FEATURE_ASM
|
||||
features.PushBack("asm", allocator);
|
||||
# endif
|
||||
# ifdef XMRIG_FEATURE_HTTP
|
||||
features.PushBack("http", allocator);
|
||||
# endif
|
||||
# ifdef XMRIG_FEATURE_LIBCPUID
|
||||
features.PushBack("cpuid", allocator);
|
||||
# endif
|
||||
# ifdef XMRIG_FEATURE_HWLOC
|
||||
features.PushBack("hwloc", allocator);
|
||||
# endif
|
||||
# ifdef XMRIG_FEATURE_TLS
|
||||
features.PushBack("tls", allocator);
|
||||
# endif
|
||||
request.reply().AddMember("features", features, allocator);
|
||||
}
|
||||
|
||||
for (IApiListener *listener : m_listeners) {
|
||||
listener->onRequest(request);
|
||||
|
||||
if (request.isDone()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
request.done(request.isNew() ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
uv_interface_address_t *interfaces;
|
||||
int count = 0;
|
||||
|
||||
if (uv_interface_addresses(&interfaces, &count) < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
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_base->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));
|
||||
|
||||
keccak(input, inSize, hash);
|
||||
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);
|
||||
}
|
||||
}
|
81
src/base/api/Api.h
Normal file
81
src/base/api/Api.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/* 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 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_API_H
|
||||
#define XMRIG_API_H
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/IBaseListener.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class Base;
|
||||
class Httpd;
|
||||
class HttpData;
|
||||
class IApiListener;
|
||||
class IApiRequest;
|
||||
class String;
|
||||
|
||||
|
||||
class Api : public IBaseListener
|
||||
{
|
||||
public:
|
||||
Api(Base *base);
|
||||
~Api() override;
|
||||
|
||||
inline const char *id() const { return m_id; }
|
||||
inline const char *workerId() const { return m_workerId; }
|
||||
inline void addListener(IApiListener *listener) { m_listeners.push_back(listener); }
|
||||
|
||||
void request(const HttpData &req);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
void onConfigChanged(Config *config, Config *previousConfig) override;
|
||||
|
||||
private:
|
||||
void exec(IApiRequest &request);
|
||||
void genId(const String &id);
|
||||
void genWorkerId(const String &id);
|
||||
|
||||
Base *m_base;
|
||||
char m_id[32];
|
||||
char m_workerId[128];
|
||||
const uint64_t m_timestamp;
|
||||
Httpd *m_httpd;
|
||||
std::vector<IApiListener *> m_listeners;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
#endif /* XMRIG_API_H */
|
193
src/base/api/Httpd.cpp
Normal file
193
src/base/api/Httpd.cpp
Normal file
|
@ -0,0 +1,193 @@
|
|||
/* 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 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 "3rdparty/http-parser/http_parser.h"
|
||||
#include "base/api/Api.h"
|
||||
#include "base/api/Httpd.h"
|
||||
#include "base/io/log/Log.h"
|
||||
#include "base/net/http/HttpApiResponse.h"
|
||||
#include "base/net/http/HttpData.h"
|
||||
#include "base/net/http/HttpServer.h"
|
||||
#include "base/net/tools/TcpServer.h"
|
||||
#include "core/config/Config.h"
|
||||
#include "core/Controller.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
static const char *kAuthorization = "authorization";
|
||||
static const char *kContentType = "content-type";
|
||||
|
||||
#ifdef _WIN32
|
||||
static const char *favicon = nullptr;
|
||||
static size_t faviconSize = 0;
|
||||
#endif
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
xmrig::Httpd::Httpd(Base *base) :
|
||||
m_base(base),
|
||||
m_http(nullptr),
|
||||
m_server(nullptr),
|
||||
m_port(0)
|
||||
{
|
||||
base->addListener(this);
|
||||
}
|
||||
|
||||
|
||||
xmrig::Httpd::~Httpd()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool xmrig::Httpd::start()
|
||||
{
|
||||
const Http &config = m_base->config()->http();
|
||||
|
||||
if (!config.isEnabled()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
m_http = new HttpServer(this);
|
||||
m_server = new TcpServer(config.host(), config.port(), m_http);
|
||||
|
||||
const int rc = m_server->bind();
|
||||
Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") BLUE_BOLD("%s:%d") " " RED_BOLD("%s"),
|
||||
"HTTP API",
|
||||
config.host().data(),
|
||||
rc < 0 ? config.port() : rc,
|
||||
rc < 0 ? uv_strerror(rc) : ""
|
||||
);
|
||||
|
||||
if (rc < 0) {
|
||||
stop();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
m_port = static_cast<uint16_t>(rc);
|
||||
|
||||
# ifdef _WIN32
|
||||
HRSRC src = FindResource(nullptr, MAKEINTRESOURCE(1), RT_ICON);
|
||||
if (src != nullptr) {
|
||||
HGLOBAL res = LoadResource(nullptr, src);
|
||||
if (res != nullptr) {
|
||||
favicon = static_cast<const char *>(LockResource(res));
|
||||
faviconSize = SizeofResource(nullptr, src);
|
||||
}
|
||||
}
|
||||
# endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Httpd::stop()
|
||||
{
|
||||
delete m_server;
|
||||
delete m_http;
|
||||
|
||||
m_server = nullptr;
|
||||
m_http = nullptr;
|
||||
m_port = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void xmrig::Httpd::onConfigChanged(Config *config, Config *previousConfig)
|
||||
{
|
||||
if (config->http() == previousConfig->http()) {
|
||||
return;
|
||||
}
|
||||
|
||||
stop();
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Httpd::onHttpData(const HttpData &data)
|
||||
{
|
||||
if (data.method == HTTP_OPTIONS) {
|
||||
return HttpApiResponse(data.id()).end();
|
||||
}
|
||||
|
||||
if (data.method == HTTP_GET && data.url == "/favicon.ico") {
|
||||
# ifdef _WIN32
|
||||
if (favicon != nullptr) {
|
||||
HttpResponse response(data.id());
|
||||
response.setHeader("Content-Type", "image/x-icon");
|
||||
|
||||
return response.end(favicon, faviconSize);
|
||||
}
|
||||
# endif
|
||||
|
||||
return HttpResponse(data.id(), 404).end();
|
||||
}
|
||||
|
||||
if (data.method > 4) {
|
||||
return HttpApiResponse(data.id(), HTTP_STATUS_METHOD_NOT_ALLOWED).end();
|
||||
}
|
||||
|
||||
const int status = auth(data);
|
||||
if (status != HTTP_STATUS_OK) {
|
||||
return HttpApiResponse(data.id(), status).end();
|
||||
}
|
||||
|
||||
if (data.method != HTTP_GET) {
|
||||
if (m_base->config()->http().isRestricted()) {
|
||||
return HttpApiResponse(data.id(), HTTP_STATUS_FORBIDDEN).end();
|
||||
}
|
||||
|
||||
if (!data.headers.count(kContentType) || data.headers.at(kContentType) != "application/json") {
|
||||
return HttpApiResponse(data.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end();
|
||||
}
|
||||
}
|
||||
|
||||
m_base->api()->request(data);
|
||||
}
|
||||
|
||||
|
||||
int xmrig::Httpd::auth(const HttpData &req) const
|
||||
{
|
||||
const Http &config = m_base->config()->http();
|
||||
|
||||
if (!req.headers.count(kAuthorization)) {
|
||||
return config.isAuthRequired() ? HTTP_STATUS_UNAUTHORIZED : HTTP_STATUS_OK;
|
||||
}
|
||||
|
||||
if (config.token().isNull()) {
|
||||
return HTTP_STATUS_UNAUTHORIZED;
|
||||
}
|
||||
|
||||
const std::string &token = req.headers.at(kAuthorization);
|
||||
const size_t size = token.size();
|
||||
|
||||
if (token.size() < 8 || config.token().size() != size - 7 || memcmp("Bearer ", token.c_str(), 7) != 0) {
|
||||
return HTTP_STATUS_FORBIDDEN;
|
||||
}
|
||||
|
||||
return strncmp(config.token().data(), token.c_str() + 7, config.token().size()) == 0 ? HTTP_STATUS_OK : HTTP_STATUS_FORBIDDEN;
|
||||
}
|
70
src/base/api/Httpd.h
Normal file
70
src/base/api/Httpd.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
/* 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 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_HTTPD_H
|
||||
#define XMRIG_HTTPD_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/IBaseListener.h"
|
||||
#include "base/kernel/interfaces/IHttpListener.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class Base;
|
||||
class HttpServer;
|
||||
class TcpServer;
|
||||
|
||||
|
||||
class Httpd : public IBaseListener, public IHttpListener
|
||||
{
|
||||
public:
|
||||
Httpd(Base *base);
|
||||
~Httpd() override;
|
||||
|
||||
bool start();
|
||||
void stop();
|
||||
|
||||
protected:
|
||||
void onConfigChanged(Config *config, Config *previousConfig) override;
|
||||
void onHttpData(const HttpData &data) override;
|
||||
|
||||
private:
|
||||
int auth(const HttpData &req) const;
|
||||
|
||||
Base *m_base;
|
||||
HttpServer *m_http;
|
||||
TcpServer *m_server;
|
||||
uint16_t m_port;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_HTTPD_H */
|
47
src/base/api/interfaces/IApiListener.h
Normal file
47
src/base/api/interfaces/IApiListener.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* 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 2016-2018 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_IAPILISTENER_H
|
||||
#define XMRIG_IAPILISTENER_H
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class IApiRequest;
|
||||
|
||||
|
||||
class IApiListener
|
||||
{
|
||||
public:
|
||||
virtual ~IApiListener() = default;
|
||||
|
||||
# ifdef XMRIG_FEATURE_API
|
||||
virtual void onRequest(IApiRequest &request) = 0;
|
||||
# endif
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // XMRIG_IAPILISTENER_H
|
82
src/base/api/interfaces/IApiRequest.h
Normal file
82
src/base/api/interfaces/IApiRequest.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/* 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 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_IAPIREQUEST_H
|
||||
#define XMRIG_IAPIREQUEST_H
|
||||
|
||||
|
||||
#include "rapidjson/fwd.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class String;
|
||||
|
||||
|
||||
class IApiRequest
|
||||
{
|
||||
public:
|
||||
enum Method {
|
||||
METHOD_DELETE,
|
||||
METHOD_GET,
|
||||
METHOD_HEAD,
|
||||
METHOD_POST,
|
||||
METHOD_PUT
|
||||
};
|
||||
|
||||
|
||||
enum Source {
|
||||
SOURCE_HTTP
|
||||
};
|
||||
|
||||
|
||||
enum RequestType {
|
||||
REQ_UNKNOWN,
|
||||
REQ_SUMMARY
|
||||
};
|
||||
|
||||
|
||||
virtual ~IApiRequest() = default;
|
||||
|
||||
virtual bool isDone() const = 0;
|
||||
virtual bool isNew() const = 0;
|
||||
virtual bool isRestricted() const = 0;
|
||||
virtual const rapidjson::Value &json() const = 0;
|
||||
virtual const String &url() const = 0;
|
||||
virtual int version() const = 0;
|
||||
virtual Method method() const = 0;
|
||||
virtual rapidjson::Document &doc() = 0;
|
||||
virtual rapidjson::Value &reply() = 0;
|
||||
virtual RequestType type() const = 0;
|
||||
virtual Source source() const = 0;
|
||||
virtual void accept() = 0;
|
||||
virtual void done(int status) = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // XMRIG_IAPIREQUEST_H
|
38
src/base/api/requests/ApiRequest.cpp
Normal file
38
src/base/api/requests/ApiRequest.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* 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 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/api/requests/ApiRequest.h"
|
||||
|
||||
|
||||
xmrig::ApiRequest::ApiRequest(Source source, bool restricted) :
|
||||
m_restricted(restricted),
|
||||
m_source(source)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
xmrig::ApiRequest::~ApiRequest()
|
||||
{
|
||||
}
|
72
src/base/api/requests/ApiRequest.h
Normal file
72
src/base/api/requests/ApiRequest.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* 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 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_APIREQUEST_H
|
||||
#define XMRIG_APIREQUEST_H
|
||||
|
||||
|
||||
#include "base/api/interfaces/IApiRequest.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class ApiRequest : public IApiRequest
|
||||
{
|
||||
public:
|
||||
ApiRequest(Source source, bool restricted);
|
||||
~ApiRequest() override;
|
||||
|
||||
protected:
|
||||
inline bool isDone() const override { return m_state == STATE_DONE; }
|
||||
inline bool isNew() const override { return m_state == STATE_NEW; }
|
||||
inline bool isRestricted() const override { return m_restricted; }
|
||||
inline int version() const override { return m_version; }
|
||||
inline RequestType type() const override { return m_type; }
|
||||
inline Source source() const override { return m_source; }
|
||||
inline void accept() override { m_state = STATE_ACCEPTED; }
|
||||
inline void done(int) override { m_state = STATE_DONE; }
|
||||
|
||||
int m_version = 1;
|
||||
RequestType m_type = REQ_UNKNOWN;
|
||||
|
||||
private:
|
||||
enum State {
|
||||
STATE_NEW,
|
||||
STATE_ACCEPTED,
|
||||
STATE_DONE
|
||||
};
|
||||
|
||||
bool m_restricted;
|
||||
Source m_source;
|
||||
State m_state = STATE_NEW;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
#endif // XMRIG_APIREQUEST_H
|
||||
|
87
src/base/api/requests/HttpApiRequest.cpp
Normal file
87
src/base/api/requests/HttpApiRequest.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
/* 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 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/api/requests/HttpApiRequest.h"
|
||||
#include "base/net/http/HttpData.h"
|
||||
#include "rapidjson/error/en.h"
|
||||
|
||||
|
||||
xmrig::HttpApiRequest::HttpApiRequest(const HttpData &req, bool restricted) :
|
||||
ApiRequest(SOURCE_HTTP, restricted),
|
||||
m_parsed(false),
|
||||
m_req(req),
|
||||
m_res(req.id()),
|
||||
m_url(req.url.c_str())
|
||||
{
|
||||
if (method() == METHOD_GET) {
|
||||
if (url() == "/1/summary" || url() == "/2/summary" || url() == "/api.json") {
|
||||
m_type = REQ_SUMMARY;
|
||||
}
|
||||
}
|
||||
|
||||
if (url().size() > 4) {
|
||||
if (memcmp(url().data(), "/2/", 3) == 0) {
|
||||
m_version = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const rapidjson::Value &xmrig::HttpApiRequest::json() const
|
||||
{
|
||||
return m_body;
|
||||
}
|
||||
|
||||
|
||||
xmrig::IApiRequest::Method xmrig::HttpApiRequest::method() const
|
||||
{
|
||||
return static_cast<IApiRequest::Method>(m_req.method);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::HttpApiRequest::accept()
|
||||
{
|
||||
using namespace rapidjson;
|
||||
|
||||
ApiRequest::accept();
|
||||
|
||||
if (!m_parsed && !m_req.body.empty()) {
|
||||
m_parsed = true;
|
||||
m_body.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(m_req.body.c_str());
|
||||
|
||||
if (m_body.HasParseError()) {
|
||||
reply().AddMember("error", StringRef(GetParseError_En(m_body.GetParseError())), doc().GetAllocator());;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::HttpApiRequest::done(int status)
|
||||
{
|
||||
ApiRequest::done(status);
|
||||
|
||||
m_res.setStatus(status);
|
||||
m_res.end();
|
||||
}
|
69
src/base/api/requests/HttpApiRequest.h
Normal file
69
src/base/api/requests/HttpApiRequest.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/* 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 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_HTTPAPIREQUEST_H
|
||||
#define XMRIG_HTTPAPIREQUEST_H
|
||||
|
||||
|
||||
#include "base/api/requests/ApiRequest.h"
|
||||
#include "base/net/http/HttpApiResponse.h"
|
||||
#include "base/tools/String.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class HttpData;
|
||||
|
||||
|
||||
class HttpApiRequest : public ApiRequest
|
||||
{
|
||||
public:
|
||||
HttpApiRequest(const HttpData &req, bool restricted);
|
||||
|
||||
protected:
|
||||
inline rapidjson::Document &doc() override { return m_res.doc(); }
|
||||
inline rapidjson::Value &reply() override { return m_res.doc(); }
|
||||
inline const String &url() const override { return m_url; }
|
||||
|
||||
const rapidjson::Value &json() const override;
|
||||
Method method() const override;
|
||||
void accept() override;
|
||||
void done(int status) override;
|
||||
|
||||
private:
|
||||
bool m_parsed;
|
||||
const HttpData &m_req;
|
||||
HttpApiResponse m_res;
|
||||
rapidjson::Document m_body;
|
||||
String m_url;
|
||||
};
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
#endif // XMRIG_HTTPAPIREQUEST_H
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
set(HEADERS_BASE
|
||||
src/base/api/interfaces/IApiListener.h
|
||||
src/base/io/Console.h
|
||||
src/base/io/json/Json.h
|
||||
src/base/io/json/JsonChain.h
|
||||
|
@ -114,6 +115,11 @@ endif()
|
|||
if (WITH_HTTP)
|
||||
set(HEADERS_BASE_HTTP
|
||||
src/3rdparty/http-parser/http_parser.h
|
||||
src/base/api/Api.h
|
||||
src/base/api/Httpd.h
|
||||
src/base/api/interfaces/IApiRequest.h
|
||||
src/base/api/requests/ApiRequest.h
|
||||
src/base/api/requests/HttpApiRequest.h
|
||||
src/base/kernel/interfaces/IHttpListener.h
|
||||
src/base/kernel/interfaces/IJsonReader.h
|
||||
src/base/kernel/interfaces/ITcpServerListener.h
|
||||
|
@ -129,6 +135,10 @@ if (WITH_HTTP)
|
|||
|
||||
set(SOURCES_BASE_HTTP
|
||||
src/3rdparty/http-parser/http_parser.c
|
||||
src/base/api/Api.cpp
|
||||
src/base/api/Httpd.cpp
|
||||
src/base/api/requests/ApiRequest.cpp
|
||||
src/base/api/requests/HttpApiRequest.cpp
|
||||
src/base/net/http/HttpApiResponse.cpp
|
||||
src/base/net/http/HttpClient.cpp
|
||||
src/base/net/http/HttpContext.cpp
|
||||
|
|
|
@ -47,15 +47,8 @@
|
|||
|
||||
|
||||
#ifdef XMRIG_FEATURE_API
|
||||
# include "api/Api.h"
|
||||
# include "api/interfaces/IApiRequest.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_EMBEDDED_CONFIG
|
||||
# include "core/config/Config_default.h"
|
||||
#endif
|
||||
|
||||
# include "base/api/Api.h"
|
||||
# include "base/api/interfaces/IApiRequest.h"
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
@ -63,6 +56,12 @@ static const char *kConfigPathV1 = "/1/config";
|
|||
static const char *kConfigPathV2 = "/2/config";
|
||||
|
||||
} // namespace xmrig
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_EMBEDDED_CONFIG
|
||||
# include "core/config/Config_default.h"
|
||||
#endif
|
||||
|
||||
|
||||
class xmrig::BasePrivate
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#define XMRIG_BASE_H
|
||||
|
||||
|
||||
#include "api/interfaces/IApiListener.h"
|
||||
#include "base/api/interfaces/IApiListener.h"
|
||||
#include "base/kernel/interfaces/IConfigListener.h"
|
||||
#include "base/kernel/interfaces/IWatcherListener.h"
|
||||
#include "rapidjson/fwd.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue