2017-08-31 01:30:59 +00:00
|
|
|
/* 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>
|
2018-03-27 07:01:38 +00:00
|
|
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
2019-02-16 23:59:19 +00:00
|
|
|
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
|
|
|
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
2017-08-31 01:30:59 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
|
|
|
|
#include <uv.h>
|
2017-09-02 11:33:53 +00:00
|
|
|
|
2017-08-31 01:30:59 +00:00
|
|
|
|
2019-03-31 16:22:36 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-03-29 19:26:30 +00:00
|
|
|
#include "3rdparty/http-parser/http_parser.h"
|
2017-08-31 01:30:59 +00:00
|
|
|
#include "api/Api.h"
|
2019-03-29 20:10:27 +00:00
|
|
|
#include "api/interfaces/IApiListener.h"
|
2019-03-29 19:26:30 +00:00
|
|
|
#include "api/requests/HttpApiRequest.h"
|
2019-04-05 15:14:01 +00:00
|
|
|
#include "base/kernel/Base.h"
|
2019-03-29 17:16:01 +00:00
|
|
|
#include "base/tools/Buffer.h"
|
2019-05-12 17:11:57 +00:00
|
|
|
#include "base/tools/Chrono.h"
|
2019-03-30 14:27:54 +00:00
|
|
|
#include "core/config/Config.h"
|
2019-03-29 17:16:01 +00:00
|
|
|
#include "core/Controller.h"
|
2019-06-29 03:57:05 +00:00
|
|
|
#include "crypto/common/Algorithm.h"
|
2019-06-16 21:06:38 +00:00
|
|
|
#include "crypto/common/keccak.h"
|
2019-03-29 17:16:01 +00:00
|
|
|
#include "version.h"
|
2017-08-31 01:30:59 +00:00
|
|
|
|
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
#ifdef XMRIG_FEATURE_HTTP
|
|
|
|
# include "api/Httpd.h"
|
|
|
|
#endif
|
2017-08-31 01:30:59 +00:00
|
|
|
|
|
|
|
|
2019-04-05 15:14:01 +00:00
|
|
|
xmrig::Api::Api(Base *base) :
|
|
|
|
m_base(base),
|
2019-03-29 17:16:01 +00:00
|
|
|
m_id(),
|
|
|
|
m_workerId(),
|
2019-05-12 17:11:57 +00:00
|
|
|
m_httpd(nullptr),
|
|
|
|
m_timestamp(Chrono::steadyMSecs())
|
2017-08-31 01:30:59 +00:00
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
base->addListener(this);
|
2017-08-31 01:30:59 +00:00
|
|
|
|
2019-04-05 15:14:01 +00:00
|
|
|
genId(base->config()->apiId());
|
2017-08-31 01:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
xmrig::Api::~Api()
|
2017-08-31 01:30:59 +00:00
|
|
|
{
|
2019-03-29 17:16:01 +00:00
|
|
|
# ifdef XMRIG_FEATURE_HTTP
|
|
|
|
delete m_httpd;
|
|
|
|
# endif
|
2017-08-31 01:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-09 17:10:02 +00:00
|
|
|
void xmrig::Api::request(const HttpData &req)
|
2017-08-31 01:30:59 +00:00
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
HttpApiRequest request(req, m_base->config()->http().isRestricted());
|
2017-08-31 01:30:59 +00:00
|
|
|
|
2019-03-29 19:26:30 +00:00
|
|
|
exec(request);
|
2019-03-29 17:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xmrig::Api::start()
|
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
genWorkerId(m_base->config()->apiWorkerId());
|
2019-03-29 17:16:01 +00:00
|
|
|
|
|
|
|
# ifdef XMRIG_FEATURE_HTTP
|
2019-04-05 15:14:01 +00:00
|
|
|
m_httpd = new Httpd(m_base);
|
2019-03-29 17:16:01 +00:00
|
|
|
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());
|
2018-03-27 07:01:38 +00:00
|
|
|
}
|
2018-03-08 18:47:44 +00:00
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
if (config->apiWorkerId() != previousConfig->apiWorkerId()) {
|
|
|
|
genWorkerId(config->apiWorkerId());
|
|
|
|
}
|
2017-08-31 01:30:59 +00:00
|
|
|
}
|
2017-09-01 00:45:08 +00:00
|
|
|
|
|
|
|
|
2019-03-29 19:26:30 +00:00
|
|
|
void xmrig::Api::exec(IApiRequest &request)
|
|
|
|
{
|
2019-03-30 18:31:40 +00:00
|
|
|
using namespace rapidjson;
|
|
|
|
|
2019-07-18 19:24:37 +00:00
|
|
|
if (request.type() == IApiRequest::REQ_SUMMARY) {
|
2019-05-12 17:11:57 +00:00
|
|
|
auto &allocator = request.doc().GetAllocator();
|
|
|
|
|
2019-03-30 13:46:08 +00:00
|
|
|
request.accept();
|
2019-05-12 17:11:57 +00:00
|
|
|
request.reply().AddMember("id", StringRef(m_id), allocator);
|
|
|
|
request.reply().AddMember("worker_id", StringRef(m_workerId), allocator);
|
|
|
|
request.reply().AddMember("uptime", (Chrono::steadyMSecs() - m_timestamp) / 1000, allocator);
|
2019-06-29 03:57:05 +00:00
|
|
|
|
|
|
|
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_TLS
|
|
|
|
features.PushBack("tls", allocator);
|
|
|
|
# endif
|
|
|
|
request.reply().AddMember("features", features, allocator);
|
2019-03-30 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:10:27 +00:00
|
|
|
for (IApiListener *listener : m_listeners) {
|
|
|
|
listener->onRequest(request);
|
|
|
|
|
|
|
|
if (request.isDone()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-29 19:26:30 +00:00
|
|
|
}
|
2019-03-29 20:10:27 +00:00
|
|
|
|
|
|
|
request.done(request.isNew() ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK);
|
2019-03-29 19:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
void xmrig::Api::genId(const String &id)
|
2017-09-01 05:02:56 +00:00
|
|
|
{
|
2019-03-29 17:16:01 +00:00
|
|
|
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) {
|
2017-09-01 05:02:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:16:01 +00:00
|
|
|
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);
|
2019-04-05 15:14:01 +00:00
|
|
|
const uint16_t port = static_cast<uint16_t>(m_base->config()->http().port());
|
2019-03-29 17:16:01 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-09-01 05:02:56 +00:00
|
|
|
}
|