2019-03-29 05:33:11 +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>
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-03-29 10:14:30 +00:00
|
|
|
#include "3rdparty/http-parser/http_parser.h"
|
2019-03-29 17:16:01 +00:00
|
|
|
#include "api/Api.h"
|
2019-03-29 05:33:11 +00:00
|
|
|
#include "api/Httpd.h"
|
|
|
|
#include "base/io/log/Log.h"
|
2019-03-29 10:14:30 +00:00
|
|
|
#include "base/net/http/HttpApiResponse.h"
|
2019-04-08 18:59:22 +00:00
|
|
|
#include "base/net/http/HttpData.h"
|
2019-03-29 05:33:11 +00:00
|
|
|
#include "base/net/http/HttpServer.h"
|
|
|
|
#include "base/net/tools/TcpServer.h"
|
2019-03-30 14:27:54 +00:00
|
|
|
#include "core/config/Config.h"
|
2019-03-29 05:33:11 +00:00
|
|
|
#include "core/Controller.h"
|
|
|
|
|
|
|
|
|
2019-03-29 10:14:30 +00:00
|
|
|
namespace xmrig {
|
|
|
|
|
|
|
|
static const char *kAuthorization = "authorization";
|
|
|
|
static const char *kContentType = "content-type";
|
|
|
|
|
2019-04-01 04:50:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static const char *favicon = nullptr;
|
|
|
|
static size_t faviconSize = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 10:14:30 +00:00
|
|
|
} // namespace xmrig
|
|
|
|
|
|
|
|
|
2019-04-05 15:14:01 +00:00
|
|
|
xmrig::Httpd::Httpd(Base *base) :
|
|
|
|
m_base(base),
|
2019-03-29 05:33:11 +00:00
|
|
|
m_http(nullptr),
|
|
|
|
m_server(nullptr),
|
|
|
|
m_port(0)
|
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
base->addListener(this);
|
2019-03-29 05:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xmrig::Httpd::~Httpd()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Httpd::start()
|
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
const Http &config = m_base->config()->http();
|
2019-03-29 05:33:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-04-01 04:50:28 +00:00
|
|
|
# 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
|
|
|
|
|
2019-03-29 05:33:11 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
void xmrig::Httpd::onHttpData(const HttpData &data)
|
2019-03-29 05:33:11 +00:00
|
|
|
{
|
2019-04-08 18:59:22 +00:00
|
|
|
if (data.method == HTTP_OPTIONS) {
|
|
|
|
return HttpApiResponse(data.id()).end();
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
if (data.method == HTTP_GET && data.url == "/favicon.ico") {
|
2019-04-01 04:50:28 +00:00
|
|
|
# ifdef _WIN32
|
|
|
|
if (favicon != nullptr) {
|
2019-04-08 18:59:22 +00:00
|
|
|
HttpResponse response(data.id());
|
2019-04-01 04:50:28 +00:00
|
|
|
response.setHeader("Content-Type", "image/x-icon");
|
|
|
|
|
|
|
|
return response.end(favicon, faviconSize);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
return HttpResponse(data.id(), 404).end();
|
2019-04-01 04:50:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
if (data.method > 4) {
|
|
|
|
return HttpApiResponse(data.id(), HTTP_STATUS_METHOD_NOT_ALLOWED).end();
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
const int status = auth(data);
|
2019-03-29 10:14:30 +00:00
|
|
|
if (status != HTTP_STATUS_OK) {
|
2019-04-08 18:59:22 +00:00
|
|
|
return HttpApiResponse(data.id(), status).end();
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
if (data.method != HTTP_GET) {
|
2019-04-05 15:14:01 +00:00
|
|
|
if (m_base->config()->http().isRestricted()) {
|
2019-04-08 18:59:22 +00:00
|
|
|
return HttpApiResponse(data.id(), HTTP_STATUS_FORBIDDEN).end();
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
if (!data.headers.count(kContentType) || data.headers.at(kContentType) != "application/json") {
|
|
|
|
return HttpApiResponse(data.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end();
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
m_base->api()->request(data);
|
2019-03-29 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-08 18:59:22 +00:00
|
|
|
int xmrig::Httpd::auth(const HttpData &req) const
|
2019-03-29 10:14:30 +00:00
|
|
|
{
|
2019-04-05 15:14:01 +00:00
|
|
|
const Http &config = m_base->config()->http();
|
2019-03-29 10:14:30 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-03-29 05:56:34 +00:00
|
|
|
|
2019-03-29 10:14:30 +00:00
|
|
|
return strncmp(config.token().data(), token.c_str() + 7, config.token().size()) == 0 ? HTTP_STATUS_OK : HTTP_STATUS_FORBIDDEN;
|
2019-03-29 05:33:11 +00:00
|
|
|
}
|