Move common parts of API.

This commit is contained in:
XMRig 2018-04-13 07:12:53 +07:00
parent 51422f4b1e
commit a6b698d4eb
10 changed files with 17 additions and 17 deletions

69
src/common/api/HttpBody.h Normal file
View 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 2016-2018 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 __HTTPBODY_H__
#define __HTTPBODY_H__
#include <string.h>
namespace xmrig {
class HttpBody
{
public:
inline HttpBody() :
m_pos(0)
{}
inline bool write(const char *data, size_t size)
{
if (size > (sizeof(m_data) - m_pos - 1)) {
return false;
}
memcpy(m_data + m_pos, data, size);
m_pos += size;
m_data[m_pos] = '\0';
return true;
}
inline const char *data() const { return m_data; }
private:
char m_data[32768];
size_t m_pos;
};
} /* namespace xmrig */
#endif /* __HTTPBODY_H__ */

View file

@ -0,0 +1,53 @@
/* 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 2016-2018 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 __HTTPREPLY_H__
#define __HTTPREPLY_H__
#include <stdint.h>
namespace xmrig {
class HttpReply
{
public:
HttpReply() :
buf(nullptr),
status(200),
size(0)
{}
char *buf;
int status;
size_t size;
};
} /* namespace xmrig */
#endif /* __HTTPREPLY_H__ */

View file

@ -0,0 +1,175 @@
/* 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 2016-2018 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 <microhttpd.h>
#include <string.h>
#include "common/api/HttpBody.h"
#include "common/api/HttpRequest.h"
#include "common/api/HttpReply.h"
#ifndef MHD_HTTP_PAYLOAD_TOO_LARGE
# define MHD_HTTP_PAYLOAD_TOO_LARGE 413
#endif
xmrig::HttpRequest::HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls) :
m_fulfilled(true),
m_restricted(true),
m_uploadData(uploadData),
m_url(url),
m_body(static_cast<HttpBody*>(*cls)),
m_method(Unsupported),
m_connection(connection),
m_uploadSize(uploadSize),
m_cls(cls)
{
if (strcmp(method, MHD_HTTP_METHOD_OPTIONS) == 0) {
m_method = Options;
}
else if (strcmp(method, MHD_HTTP_METHOD_GET) == 0) {
m_method = Get;
}
else if (strcmp(method, MHD_HTTP_METHOD_PUT) == 0) {
m_method = Put;
}
}
xmrig::HttpRequest::~HttpRequest()
{
if (m_fulfilled) {
delete m_body;
}
}
bool xmrig::HttpRequest::match(const char *path) const
{
return strcmp(m_url, path) == 0;
}
bool xmrig::HttpRequest::process(const char *accessToken, bool restricted, xmrig::HttpReply &reply)
{
m_restricted = restricted || !accessToken;
if (m_body) {
if (*m_uploadSize != 0) {
if (!m_body->write(m_uploadData, *m_uploadSize)) {
*m_cls = nullptr;
m_fulfilled = true;
reply.status = MHD_HTTP_PAYLOAD_TOO_LARGE;
return false;
}
*m_uploadSize = 0;
m_fulfilled = false;
return true;
}
m_fulfilled = true;
return true;
}
reply.status = auth(accessToken);
if (reply.status != MHD_HTTP_OK) {
return false;
}
if (m_restricted && m_method != Get) {
reply.status = MHD_HTTP_FORBIDDEN;
return false;
}
if (m_method == Get) {
return true;
}
const char *contentType = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Content-Type");
if (!contentType || strcmp(contentType, "application/json") != 0) {
reply.status = MHD_HTTP_UNSUPPORTED_MEDIA_TYPE;
return false;
}
m_body = new xmrig::HttpBody();
m_fulfilled = false;
*m_cls = m_body;
return true;
}
const char *xmrig::HttpRequest::body() const
{
return m_body ? m_body->data() : nullptr;
}
int xmrig::HttpRequest::end(const HttpReply &reply)
{
if (reply.buf) {
return end(reply.status, MHD_create_response_from_buffer(reply.size ? reply.size : strlen(reply.buf), (void*) reply.buf, MHD_RESPMEM_MUST_FREE));
}
return end(reply.status, nullptr);
}
int xmrig::HttpRequest::end(int status, MHD_Response *rsp)
{
if (!rsp) {
rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT);
}
MHD_add_response_header(rsp, "Content-Type", "application/json");
MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*");
MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET, PUT");
MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization");
const int ret = MHD_queue_response(m_connection, status, rsp);
MHD_destroy_response(rsp);
return ret;
}
int xmrig::HttpRequest::auth(const char *accessToken)
{
if (!accessToken) {
return MHD_HTTP_OK;
}
const char *header = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Authorization");
if (accessToken && !header) {
return MHD_HTTP_UNAUTHORIZED;
}
const size_t size = strlen(header);
if (size < 8 || strlen(accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) {
return MHD_HTTP_FORBIDDEN;
}
return strncmp(accessToken, header + 7, strlen(accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN;
}

View file

@ -0,0 +1,84 @@
/* 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 2016-2018 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 __HTTPREQUEST_H__
#define __HTTPREQUEST_H__
#include <stdint.h>
struct MHD_Connection;
struct MHD_Response;
namespace xmrig {
class HttpBody;
class HttpReply;
class HttpRequest
{
public:
enum Method {
Unsupported,
Options,
Get,
Put
};
HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls);
~HttpRequest();
inline bool isFulfilled() const { return m_fulfilled; }
inline bool isRestricted() const { return m_restricted; }
inline Method method() const { return m_method; }
bool match(const char *path) const;
bool process(const char *accessToken, bool restricted, xmrig::HttpReply &reply);
const char *body() const;
int end(const HttpReply &reply);
int end(int status, MHD_Response *rsp);
private:
int auth(const char *accessToken);
bool m_fulfilled;
bool m_restricted;
const char *m_uploadData;
const char *m_url;
HttpBody *m_body;
Method m_method;
MHD_Connection *m_connection;
size_t *m_uploadSize;
void **m_cls;
};
} /* namespace xmrig */
#endif /* __HTTPREQUEST_H__ */

141
src/common/api/Httpd.cpp Normal file
View file

@ -0,0 +1,141 @@
/* 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 2016-2018 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 <microhttpd.h>
#include <string.h>
#include "api/Api.h"
#include "common/api/Httpd.h"
#include "common/api/HttpReply.h"
#include "common/api/HttpRequest.h"
#include "log/Log.h"
Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) :
m_idle(true),
m_IPv6(IPv6),
m_restricted(restricted),
m_accessToken(accessToken ? strdup(accessToken) : nullptr),
m_port(port),
m_daemon(nullptr)
{
uv_timer_init(uv_default_loop(), &m_timer);
m_timer.data = this;
}
Httpd::~Httpd()
{
uv_timer_stop(&m_timer);
if (m_daemon) {
MHD_stop_daemon(m_daemon);
}
delete m_accessToken;
}
bool Httpd::start()
{
if (!m_port) {
return false;
}
unsigned int flags = 0;
# if MHD_VERSION >= 0x00093500
if (m_IPv6 && MHD_is_feature_supported(MHD_FEATURE_IPv6)) {
flags |= MHD_USE_DUAL_STACK;
}
if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) {
flags |= MHD_USE_EPOLL_LINUX_ONLY;
}
# endif
m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END);
if (!m_daemon) {
LOG_ERR("HTTP Daemon failed to start.");
return false;
}
uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval);
return true;
}
int Httpd::process(xmrig::HttpRequest &req)
{
xmrig::HttpReply reply;
if (!req.process(m_accessToken, m_restricted, reply)) {
return req.end(reply);
}
if (!req.isFulfilled()) {
return MHD_YES;
}
Api::exec(req, reply);
return req.end(reply);
}
void Httpd::run()
{
MHD_run(m_daemon);
const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS);
if (m_idle && info->num_connections) {
uv_timer_set_repeat(&m_timer, kActiveInterval);
m_idle = false;
}
else if (!m_idle && !info->num_connections) {
uv_timer_set_repeat(&m_timer, kIdleInterval);
m_idle = true;
}
}
int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls)
{
xmrig::HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls);
if (req.method() == xmrig::HttpRequest::Options) {
return req.end(MHD_HTTP_OK, nullptr);
}
if (req.method() == xmrig::HttpRequest::Unsupported) {
return req.end(MHD_HTTP_METHOD_NOT_ALLOWED, nullptr);
}
return static_cast<Httpd*>(cls)->process(req);
}
void Httpd::onTimer(uv_timer_t *handle)
{
static_cast<Httpd*>(handle->data)->run();
}

70
src/common/api/Httpd.h Normal file
View 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 2016-2018 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 __HTTPD_H__
#define __HTTPD_H__
#include <uv.h>
struct MHD_Connection;
struct MHD_Daemon;
struct MHD_Response;
class UploadCtx;
namespace xmrig {
class HttpRequest;
}
class Httpd
{
public:
Httpd(int port, const char *accessToken, bool IPv6, bool restricted);
~Httpd();
bool start();
private:
constexpr static const int kIdleInterval = 200;
constexpr static const int kActiveInterval = 25;
int process(xmrig::HttpRequest &req);
void run();
static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls);
static void onTimer(uv_timer_t *handle);
bool m_idle;
bool m_IPv6;
bool m_restricted;
const char *m_accessToken;
const int m_port;
MHD_Daemon *m_daemon;
uv_timer_t m_timer;
};
#endif /* __HTTPD_H__ */