From 71f06530df5fa6fb49dbf2622b1f27b666269694 Mon Sep 17 00:00:00 2001 From: XMRig <support@xmrig.com> Date: Thu, 31 Aug 2017 04:30:59 +0300 Subject: [PATCH] Added classes Api and ApiState. --- CMakeLists.txt | 4 +++ src/App.cpp | 3 ++ src/api/Api.cpp | 69 ++++++++++++++++++++++++++++++++++++++ src/api/Api.h | 48 +++++++++++++++++++++++++++ src/api/ApiState.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/api/ApiState.h | 46 ++++++++++++++++++++++++++ src/api/Httpd.cpp | 24 +++++++++++--- src/version.h | 1 + 8 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 src/api/Api.cpp create mode 100644 src/api/Api.h create mode 100644 src/api/ApiState.cpp create mode 100644 src/api/ApiState.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2afcf70a9..817cdcd68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ include (CheckIncludeFile) set(HEADERS src/3rdparty/align.h + src/api/Api.h + src/api/ApiState.h src/App.h src/Console.h src/Cpu.h @@ -60,6 +62,8 @@ set(HEADERS_CRYPTO ) set(SOURCES + src/api/Api.cpp + src/api/ApiState.cpp src/App.cpp src/Console.cpp src/log/ConsoleLog.cpp diff --git a/src/App.cpp b/src/App.cpp index a944fe84e..a016d6b83 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -26,6 +26,7 @@ #include <uv.h> +#include "api/Api.h" #include "App.h" #include "Console.h" #include "Cpu.h" @@ -124,6 +125,8 @@ int App::exec() Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash(), m_options->hugePages()); Summary::print(); + Api::start(); + # ifndef XMRIG_NO_HTTPD m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken()); m_httpd->start(); diff --git a/src/api/Api.cpp b/src/api/Api.cpp new file mode 100644 index 000000000..6fe1a838f --- /dev/null +++ b/src/api/Api.cpp @@ -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 2016-2017 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 "api/Api.h" +#include "api/ApiState.h" + + +ApiState *Api::m_state = nullptr; +char Api::m_buf[4096]; +uv_mutex_t Api::m_mutex; + + +bool Api::start() +{ + uv_mutex_init(&m_mutex); + m_state = new ApiState(); + + return true; +} + + +void Api::release() +{ + delete m_state; +} + + +const char *Api::get(const char *url, size_t *size, int *status) +{ + if (!m_state) { + *size = 0; + return nullptr; + } + + uv_mutex_lock(&m_mutex); + + const char *buf = m_state->get(url, size); + if (*size) { + memcpy(m_buf, buf, *size); + } + else { + *status = 500; + } + + uv_mutex_unlock(&m_mutex); + + return m_buf; +} diff --git a/src/api/Api.h b/src/api/Api.h new file mode 100644 index 000000000..db053139a --- /dev/null +++ b/src/api/Api.h @@ -0,0 +1,48 @@ +/* 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-2017 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 __API_H__ +#define __API_H__ + + +#include <uv.h> + + +class ApiState; + + +class Api +{ +public: + static bool start(); + static void release(); + + static const char *get(const char *url, size_t *size, int *status); + +private: + static ApiState *m_state; + static char m_buf[4096]; + static uv_mutex_t m_mutex; +}; + +#endif /* __API_H__ */ diff --git a/src/api/ApiState.cpp b/src/api/ApiState.cpp new file mode 100644 index 000000000..69044b14c --- /dev/null +++ b/src/api/ApiState.cpp @@ -0,0 +1,79 @@ +/* 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-2017 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 <string.h> + + +#include "api/ApiState.h" +#include "Cpu.h" +#include "Mem.h" +#include "Options.h" +#include "Platform.h" +#include "version.h" + + +ApiState::ApiState() +{ +} + + +ApiState::~ApiState() +{ +} + + +const char *ApiState::get(const char *url, size_t *size) const +{ + json_t *reply = json_object(); + + getMiner(reply); + + return finalize(reply, size); +} + + +const char *ApiState::finalize(json_t *reply, size_t *size) const +{ + *size = json_dumpb(reply, m_buf, sizeof(m_buf) - 1, JSON_INDENT(4)); + + json_decref(reply); + return m_buf; +} + + +void ApiState::getMiner(json_t *reply) const +{ + json_t *cpu = json_object(); + json_object_set(reply, "version", json_string(APP_VERSION)); + json_object_set(reply, "kind", json_string(APP_KIND)); + json_object_set(reply, "ua", json_string(Platform::userAgent())); + json_object_set(reply, "cpu", cpu); + json_object_set(reply, "algo", json_string(Options::i()->algoName())); + json_object_set(reply, "hugepages", json_boolean(Mem::isHugepagesEnabled())); + json_object_set(reply, "donate", json_integer(Options::i()->donateLevel())); + + json_object_set(cpu, "brand", json_string(Cpu::brand())); + json_object_set(cpu, "aes", json_boolean(Cpu::hasAES())); + json_object_set(cpu, "x64", json_boolean(Cpu::isX64())); + json_object_set(cpu, "sockets", json_integer(Cpu::sockets())); +} diff --git a/src/api/ApiState.h b/src/api/ApiState.h new file mode 100644 index 000000000..8b2a5089a --- /dev/null +++ b/src/api/ApiState.h @@ -0,0 +1,46 @@ +/* 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-2017 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 __APISTATE_H__ +#define __APISTATE_H__ + + +#include "jansson.h" + + +class ApiState +{ +public: + ApiState(); + ~ApiState(); + + const char *get(const char *url, size_t *size) const; + +private: + const char *finalize(json_t *reply, size_t *size) const; + void getMiner(json_t *reply) const; + + mutable char m_buf[4096]; +}; + +#endif /* __APISTATE_H__ */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 9161d082e..29baa2f50 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -25,12 +25,13 @@ #include <microhttpd.h> +#include "api/Api.h" #include "api/Httpd.h" #include "log/Log.h" -static const char kNotFound [] = "{\"error\":\"NOT_FOUND\"}"; -static const size_t kNotFoundSize = sizeof(kNotFound) - 1; +static const char k500 [] = "{\"error\":\"INTERNAL_SERVER_ERROR\"}"; +static const size_t k500Size = sizeof(k500) - 1; Httpd::Httpd(int port, const char *accessToken) : @@ -59,11 +60,26 @@ bool Httpd::start() int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { - struct MHD_Response *rsp = MHD_create_response_from_buffer(kNotFoundSize, (void*)kNotFound, MHD_RESPMEM_PERSISTENT); + if (strcmp(method, "GET") != 0) { + return MHD_NO; + } + + struct MHD_Response *rsp; + + size_t size = 0; + int status = MHD_HTTP_OK; + const char *buf = Api::get(url, &size, &status); + + if (size) { + rsp = MHD_create_response_from_buffer(size, (void*) buf, MHD_RESPMEM_PERSISTENT); + } + else { + rsp = MHD_create_response_from_buffer(k500Size, (void*) k500, MHD_RESPMEM_PERSISTENT); + } MHD_add_response_header(rsp, "Content-Type", "application/json"); - const int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, rsp); + const int ret = MHD_queue_response(connection, status, rsp); MHD_destroy_response(rsp); return ret; } diff --git a/src/version.h b/src/version.h index b272e7b9c..f1bf79cff 100644 --- a/src/version.h +++ b/src/version.h @@ -31,6 +31,7 @@ #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2017 xmrig.com" +#define APP_KIND "cpu" #define APP_VER_MAJOR 2 #define APP_VER_MINOR 3