xmrig/src/api/Api.cpp

162 lines
4.1 KiB
C++
Raw Normal View History

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>
* 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
#include "3rdparty/http-parser/http_parser.h"
2017-08-31 01:30:59 +00:00
#include "api/Api.h"
#include "api/requests/HttpApiRequest.h"
2019-03-29 17:16:01 +00:00
#include "base/tools/Buffer.h"
#include "common/crypto/keccak.h"
#include "core/Config.h"
#include "core/Controller.h"
#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-03-29 17:16:01 +00:00
xmrig::Api::Api(Controller *controller) :
m_id(),
m_workerId(),
m_controller(controller),
m_httpd(nullptr)
2017-08-31 01:30:59 +00:00
{
2019-03-29 17:16:01 +00:00
controller->addListener(this);
2017-08-31 01:30:59 +00:00
2019-03-29 17:16:01 +00:00
genId(m_controller->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-03-29 17:16:01 +00:00
void xmrig::Api::request(const HttpRequest &req)
2017-08-31 01:30:59 +00:00
{
HttpApiRequest request(req, m_controller->config()->http().isRestricted());
2017-08-31 01:30:59 +00:00
exec(request);
2019-03-29 17:16:01 +00:00
}
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());
2018-03-27 07:01:38 +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
void xmrig::Api::exec(IApiRequest &request)
{
if (request.isNew()) {
request.done(HTTP_STATUS_NOT_FOUND);
}
}
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);
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);
}
2017-09-01 05:02:56 +00:00
}