Added initial CC Server / Webservice

This commit is contained in:
Ben Gräf 2017-10-06 22:01:17 +02:00
parent eebb10c18e
commit eed483581a
13 changed files with 1177 additions and 21 deletions

6
.gitignore vendored
View file

@ -1,2 +1,8 @@
/build /build
/CMakeLists.txt.user /CMakeLists.txt.user
/.idea
/CMakeFiles
/src/3rdparty
CMakeCache.txt
cmake_install.cmake
Makefile

View file

@ -1,9 +1,12 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(xmrig) project(xmrig)
set(CMAKE_BUILD_TYPE Debug)
option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_LIBCPUID "Use Libcpuid" ON)
option(WITH_AEON "CryptoNight-Lite support" ON) option(WITH_AEON "CryptoNight-Lite support" ON)
option(WITH_HTTPD "HTTP REST API" ON) option(WITH_HTTPD "HTTP REST API" ON)
option(WITH_CC_SERVER "CC Server" ON)
include (CheckIncludeFile) include (CheckIncludeFile)
@ -14,19 +17,12 @@ set(HEADERS
src/api/ApiState.h src/api/ApiState.h
src/api/NetworkState.h src/api/NetworkState.h
src/App.h src/App.h
src/Console.h
src/Cpu.h
src/interfaces/IClientListener.h src/interfaces/IClientListener.h
src/interfaces/IConsoleListener.h
src/interfaces/IJobResultListener.h src/interfaces/IJobResultListener.h
src/interfaces/ILogBackend.h src/interfaces/ILogBackend.h
src/interfaces/IStrategy.h src/interfaces/IStrategy.h
src/interfaces/IStrategyListener.h src/interfaces/IStrategyListener.h
src/interfaces/IWorker.h src/interfaces/IWorker.h
src/log/ConsoleLog.h
src/log/FileLog.h
src/log/Log.h
src/Mem.h
src/net/Client.h src/net/Client.h
src/net/Job.h src/net/Job.h
src/net/JobResult.h src/net/JobResult.h
@ -35,10 +31,6 @@ set(HEADERS
src/net/strategies/FailoverStrategy.h src/net/strategies/FailoverStrategy.h
src/net/strategies/SinglePoolStrategy.h src/net/strategies/SinglePoolStrategy.h
src/net/SubmitResult.h src/net/SubmitResult.h
src/net/Url.h
src/Options.h
src/Platform.h
src/Summary.h
src/version.h src/version.h
src/workers/DoubleWorker.h src/workers/DoubleWorker.h
src/workers/Handle.h src/workers/Handle.h
@ -67,11 +59,6 @@ set(SOURCES
src/api/ApiState.cpp src/api/ApiState.cpp
src/api/NetworkState.cpp src/api/NetworkState.cpp
src/App.cpp src/App.cpp
src/Console.cpp
src/log/ConsoleLog.cpp
src/log/FileLog.cpp
src/log/Log.cpp
src/Mem.cpp
src/net/Client.cpp src/net/Client.cpp
src/net/Job.cpp src/net/Job.cpp
src/net/Network.cpp src/net/Network.cpp
@ -79,9 +66,6 @@ set(SOURCES
src/net/strategies/FailoverStrategy.cpp src/net/strategies/FailoverStrategy.cpp
src/net/strategies/SinglePoolStrategy.cpp src/net/strategies/SinglePoolStrategy.cpp
src/net/SubmitResult.cpp src/net/SubmitResult.cpp
src/net/Url.cpp
src/Options.cpp
src/Platform.cpp
src/Summary.cpp src/Summary.cpp
src/workers/DoubleWorker.cpp src/workers/DoubleWorker.cpp
src/workers/Handle.cpp src/workers/Handle.cpp
@ -103,6 +87,31 @@ set(SOURCES_CRYPTO
src/crypto/CryptoNight.cpp src/crypto/CryptoNight.cpp
) )
set(HEADERS_COMMON
src/interfaces/IConsoleListener.h
src/Console.h
src/Mem.h
src/Cpu.h
src/net/Url.h
src/Options.h
src/log/ConsoleLog.h
src/log/FileLog.h
src/log/Log.h
src/Platform.h
src/Summary.h
)
set(SOURCES_COMMON
src/Console.cpp
src/Mem.cpp
src/net/Url.cpp
src/Options.cpp
src/log/ConsoleLog.cpp
src/log/FileLog.cpp
src/log/Log.cpp
src/Platform.cpp
)
if (WIN32) if (WIN32)
set(SOURCES_OS set(SOURCES_OS
res/app.rc res/app.rc
@ -134,7 +143,7 @@ endif()
add_definitions(/D__STDC_FORMAT_MACROS) add_definitions(/D__STDC_FORMAT_MACROS)
add_definitions(/DUNICODE) add_definitions(/DUNICODE)
#add_definitions(/DAPP_DEBUG) add_definitions(/DAPP_DEBUG)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
@ -207,6 +216,26 @@ else()
add_definitions(/DXMRIG_NO_HTTPD) add_definitions(/DXMRIG_NO_HTTPD)
endif() endif()
if (WITH_CC_SERVER AND MHD_FOUND)
include_directories(${MHD_INCLUDE_DIRS})
set(CC_SERVER_SOURCES
src/server/ClientStatus.cpp
src/server/xmrigCC.cpp
src/server/CCServer.cpp
src/server/Service.cpp
src/server/Summary.cpp
src/server/Httpd.cpp)
set(CC_SERVER_HEADERS
src/server/ClientStatus.h
src/server/CCServer.h
src/server/Service.h
src/server/version.h
src/server/Httpd.h)
endif()
include_directories(src) include_directories(src)
include_directories(src/3rdparty) include_directories(src/3rdparty)
include_directories(src/3rdparty/jansson) include_directories(src/3rdparty/jansson)
@ -214,5 +243,10 @@ include_directories(${UV_INCLUDE_DIR})
add_subdirectory(src/3rdparty/jansson) add_subdirectory(src/3rdparty/jansson)
add_executable(xmrig ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES}) add_executable(xmrig ${HEADERS} ${SOURCES} ${HEADERS_COMMON} ${SOURCES_COMMON} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES})
target_link_libraries(xmrig jansson ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) target_link_libraries(xmrig jansson ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB})
if (WITH_CC_SERVER AND MHD_FOUND)
add_executable(xmrigCC ${HEADERS_COMMON} ${SOURCES_COMMON} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CC_SERVER} ${CC_SERVER_SOURCES} ${SOURCES_SYSLOG})
target_link_libraries(xmrigCC jansson ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB})
endif()

170
src/server/CCServer.cpp Normal file
View file

@ -0,0 +1,170 @@
/* XMRigCC
* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 <uv.h>
#include "server/Service.h"
#include "CCServer.h"
#include "Console.h"
#include "log/ConsoleLog.h"
#include "log/FileLog.h"
#include "log/Log.h"
#include "Options.h"
#include "Summary.h"
#include "server/Httpd.h"
#ifdef HAVE_SYSLOG_H
# include "log/SysLog.h"
#endif
CCServer *CCServer::m_self = nullptr;
CCServer::CCServer(int argc, char **argv) :
m_console(nullptr),
m_httpd(nullptr),
m_options(nullptr)
{
m_self = this;
Log::init();
m_options = Options::parse(argc, argv);
if (!m_options) {
return;
}
if (!m_options->background()) {
Log::add(new ConsoleLog(m_options->colors()));
m_console = new Console(this);
}
if (m_options->logFile()) {
Log::add(new FileLog(m_options->logFile()));
}
# ifdef HAVE_SYSLOG_H
if (m_options->syslog()) {
Log::add(new SysLog());
}
# endif
uv_signal_init(uv_default_loop(), &m_signal);
}
CCServer::~CCServer()
{
uv_tty_reset_mode();
delete m_httpd;
delete m_console;
}
int CCServer::start()
{
if (!m_options) {
return 0;
}
uv_signal_start(&m_signal, CCServer::onSignal, SIGHUP);
uv_signal_start(&m_signal, CCServer::onSignal, SIGTERM);
uv_signal_start(&m_signal, CCServer::onSignal, SIGINT);
Summary::print();
Service::start();
m_httpd = new Httpd(m_options->apiPort(), m_options->apiToken());
m_httpd->start();
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
Options::release();
return r;
}
void CCServer::onConsoleCommand(char command)
{
switch (command) {
case 'c':
case 'C':
break;
case 'h':
case 'H':
break;
case 'r':
case 'R':
break;
case 3:
LOG_WARN("Ctrl+C received, exiting");
stop();
break;
default:
break;
}
}
void CCServer::stop()
{
uv_stop(uv_default_loop());
}
void CCServer::onSignal(uv_signal_t *handle, int signum)
{
switch (signum)
{
case SIGHUP:
LOG_WARN("SIGHUP received, exiting");
break;
case SIGTERM:
LOG_WARN("SIGTERM received, exiting");
break;
case SIGINT:
LOG_WARN("SIGINT received, exiting");
break;
default:
break;
}
uv_signal_stop(handle);
m_self->stop();
}

65
src/server/CCServer.h Normal file
View file

@ -0,0 +1,65 @@
/* XMRigCC
* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 __CC_SERVER_H__
#define __CC_SERVER_H__
#include <uv.h>
#include "interfaces/IConsoleListener.h"
class Console;
class Httpd;
class Options;
class CCServer : public IConsoleListener
{
public:
CCServer(int argc, char **argv);
~CCServer();
int start();
protected:
void onConsoleCommand(char command) override;
private:
void stop();
void printCommands();
static void onSignal(uv_signal_t *handle, int signum);
static CCServer *m_self;
Console *m_console;
Httpd *m_httpd;
Options *m_options;
uv_signal_t m_signal;
};
#endif /* __CC_SERVER_H__ */

145
src/server/ClientStatus.cpp Normal file
View file

@ -0,0 +1,145 @@
/* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 <cstring>
#include "server/ClientStatus.h"
ClientStatus::ClientStatus()
: hashrateShort(0), hashrateMedium(0), hashrateLong(0), sharesGood(0), sharedTotal(0), hashesTotal(0),
lastStatusUpdate(0)
{
}
const std::string ClientStatus::getMiner() const
{
return miner;
}
void ClientStatus::setMiner(const std::string &miner)
{
ClientStatus::miner = miner;
}
const std::string ClientStatus::getCurrentPool() const
{
return currentPool;
}
void ClientStatus::setCurrentPool(const std::string &currentPool)
{
ClientStatus::currentPool = currentPool;
}
const std::string ClientStatus::getCurrentStatus() const
{
return currentStatus;
}
void ClientStatus::setCurrentStatus(const std::string &currentStatus)
{
ClientStatus::currentStatus = currentStatus;
}
double ClientStatus::getHashrateShort() const
{
return hashrateShort;
}
void ClientStatus::setHashrateShort(double hashrateShort)
{
ClientStatus::hashrateShort = hashrateShort;
}
double ClientStatus::getHashrateMedium() const
{
return hashrateMedium;
}
void ClientStatus::setHashrateMedium(double hashrateMedium)
{
ClientStatus::hashrateMedium = hashrateMedium;
}
double ClientStatus::getHashrateLong() const
{
return hashrateLong;
}
void ClientStatus::setHashrateLong(double hashrateLong)
{
ClientStatus::hashrateLong = hashrateLong;
}
uint64_t ClientStatus::getSharesGood() const
{
return sharesGood;
}
void ClientStatus::setSharesGood(uint64_t sharesGood)
{
ClientStatus::sharesGood = sharesGood;
}
uint64_t ClientStatus::getSharedTotal() const
{
return sharedTotal;
}
void ClientStatus::setSharedTotal(uint64_t sharedTotal)
{
ClientStatus::sharedTotal = sharedTotal;
}
uint64_t ClientStatus::getHashesTotal() const
{
return hashesTotal;
}
void ClientStatus::setHashesTotal(uint64_t hashesTotal)
{
ClientStatus::hashesTotal = hashesTotal;
}
const uint32_t ClientStatus::getLastStatusUpdate() const
{
return lastStatusUpdate;
}
void ClientStatus::setLastStatusUpdate(uint32_t lastStatusUpdate)
{
ClientStatus::lastStatusUpdate = lastStatusUpdate;
}
void ClientStatus::parseFromJson(const json_t &json)
{
}
std::string ClientStatus::toJson()
{
}

86
src/server/ClientStatus.h Normal file
View file

@ -0,0 +1,86 @@
/* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 __CLIENTSTATUS_H__
#define __CLIENTSTATUS_H__
#include <string>
#include <3rdparty/jansson/jansson.h>
class ClientStatus
{
public:
ClientStatus();
const std::string getMiner() const;
void setMiner(const std::string &miner);
const std::string getCurrentPool() const;
void setCurrentPool(const std::string &currentPool);
const std::string getCurrentStatus() const;
void setCurrentStatus(const std::string &currentStatus);
double getHashrateShort() const;
void setHashrateShort(double hashrateShort);
double getHashrateMedium() const;
void setHashrateMedium(double hashrateMedium);
double getHashrateLong() const;
void setHashrateLong(double hashrateLong);
uint64_t getSharesGood() const;
void setSharesGood(uint64_t sharesGood);
uint64_t getSharedTotal() const;
void setSharedTotal(uint64_t sharedTotal);
uint64_t getHashesTotal() const;
void setHashesTotal(uint64_t hashesTotal);
const uint32_t getLastStatusUpdate() const;
void setLastStatusUpdate(uint32_t lastStatusUpdate);
std::string toJson();
void parseFromJson(const json_t& json);
private:
std::string miner;
std::string currentPool;
std::string currentStatus;
double hashrateShort;
double hashrateMedium;
double hashrateLong;
uint64_t sharesGood;
uint64_t sharedTotal;
uint64_t hashesTotal;
uint32_t lastStatusUpdate;
};
#endif /* __CLIENTSTATUS_H__ */

230
src/server/Httpd.cpp Normal file
View file

@ -0,0 +1,230 @@
/* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 <cstring>
#include <microhttpd.h>
#include <memory>
#include "version.h"
#include "server/Service.h"
#include "server/Httpd.h"
#include "log/Log.h"
Httpd::Httpd(int port, const char *accessToken) :
m_accessToken(accessToken),
m_adminUser("admin"),
m_adminPassword("passw0rd"),
m_port(port),
m_daemon(nullptr)
{
}
bool Httpd::start()
{
if (!m_port) {
return false;
}
m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler,
this, MHD_OPTION_END);
if (!m_daemon) {
LOG_ERR("HTTP Daemon failed to start.");
return false;
} else {
LOG_INFO("%s Server started on Port: %d", APP_NAME, m_port);
}
return true;
}
unsigned Httpd::tokenAuth(struct MHD_Connection *connection)
{
if (!m_accessToken) {
LOG_WARN("AccessToken not set. Access Granted!");
return MHD_HTTP_OK;
}
const char *header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_AUTHORIZATION);
if (m_accessToken && !header) {
return MHD_HTTP_UNAUTHORIZED;
}
const size_t size = strlen(header);
if (size < 8 || strlen(m_accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) {
LOG_WARN("AccessToken wrong. Access Forbidden!");
return MHD_HTTP_FORBIDDEN;
}
return strncmp(m_accessToken, header + 7, strlen(m_accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN;
}
unsigned Httpd::basicAuth(struct MHD_Connection *connection, std::string &resp)
{
if (!m_adminUser || !m_adminPassword) {
resp = std::string("<html><body\\>"
"Please configure adminUser and adminPass to view this Page."
"</body><html\\>");
LOG_WARN("AdminUser/AdminPassword not set. Access Forbidden!");
return MHD_HTTP_FORBIDDEN;
}
const char *header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_AUTHORIZATION);
if (!header) {
return MHD_HTTP_UNAUTHORIZED;
}
char* user;
char* pass;
user = MHD_basic_auth_get_username_password(connection, &pass);
if (user == nullptr || strcmp(user, m_adminUser) != 0 ||
pass == nullptr || strcmp(pass, m_adminPassword) != 0) {
LOG_WARN("AdminUser/AdminPassword wrong. Access Unauthorized!");
return MHD_HTTP_UNAUTHORIZED;
}
return MHD_HTTP_OK;
}
int Httpd::sendJSONResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp)
{
return sendResponse(connection, status, rsp, "application/json");
}
int Httpd::sendHTMLResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp)
{
return sendResponse(connection, status, rsp, "text/html");
}
int Httpd::sendResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp, const char *contentType)
{
if (!rsp) {
rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT);
}
MHD_add_response_header(rsp, "Content-Type", contentType);
MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*");
MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "POST, GET, OPTIONS");
MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization");
MHD_add_response_header(rsp, "WWW-Authenticate", "Basic");
MHD_add_response_header(rsp, "WWW-Authenticate", "Bearer");
int ret = MHD_queue_response(connection, status, rsp);
MHD_destroy_response(rsp);
return ret;
}
int Httpd::handler(void *cls, MHD_Connection *connection, const char *url, const char *method,
const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
{
if (strcmp(method, MHD_HTTP_METHOD_OPTIONS) == 0) {
LOG_INFO("OPTIONS Requested");
return sendHTMLResponse(connection, MHD_HTTP_OK, nullptr);
}
if (strcmp(method, MHD_HTTP_METHOD_GET) != 0 && strcmp(method, MHD_HTTP_METHOD_POST) != 0) {
LOG_ERR("HTTP_METHOD_NOT_ALLOWED");
return sendHTMLResponse(connection, MHD_HTTP_METHOD_NOT_ALLOWED, nullptr);
}
if (strstr(url, "/client/")) {
unsigned status = static_cast<Httpd *>(cls)->tokenAuth(connection);
if (status != MHD_HTTP_OK) {
return sendJSONResponse(connection, status, nullptr);
}
} else {
std::string resp;
unsigned status = static_cast<Httpd *>(cls)->basicAuth(connection, resp);
if (status != MHD_HTTP_OK) {
MHD_Response *rsp = nullptr;
if (!resp.empty()) {
rsp = MHD_create_response_from_buffer(resp.length(), (void *)resp.c_str(), MHD_RESPMEM_PERSISTENT);
}
return sendHTMLResponse(connection, status, rsp);
}
}
if (strcmp(method, MHD_HTTP_METHOD_GET) == 0) {
return handleGET(connection, url);
} else {
return handlePOST(connection, url, upload_data, upload_data_size, con_cls);
}
return MHD_NO;
}
int Httpd::handleGET(struct MHD_Connection *connection, const char *url)
{LOG_INFO("HANDLE GET REQUEST");
std::string resp;
unsigned status = Service::get(url, resp);
MHD_Response *rsp = nullptr;
if (!resp.empty()) {
rsp = MHD_create_response_from_buffer(resp.length(), (void *) resp.c_str(), MHD_RESPMEM_PERSISTENT);
}
return sendJSONResponse(connection, status, rsp);
}
int Httpd::handlePOST(struct MHD_Connection *connection, const char* url, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
LOG_INFO("HANDLE POST REQUEST");
auto *cc = (ConnectionContext*) *con_cls;
if (cc == nullptr) {
cc = new ConnectionContext();
*con_cls = (void *) cc;
} else {
if (*upload_data_size != 0) {
cc->data << std::string(upload_data, *upload_data_size);
*upload_data_size = 0;
} else {
std::string resp;
unsigned status = Service::post(url, cc->data.str(), resp);
MHD_Response *rsp = nullptr;
if (!resp.empty()) {
rsp = MHD_create_response_from_buffer(resp.length(), (void *) resp.c_str(), MHD_RESPMEM_PERSISTENT);
}
delete cc;
*con_cls = nullptr;
return sendJSONResponse(connection, status, rsp);
}
}
return MHD_YES;
}

69
src/server/Httpd.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 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 __HTTPD_H__
#define __HTTPD_H__
#include <string>
#include <sstream>
#include <uv.h>
struct MHD_Connection;
struct MHD_Daemon;
struct MHD_Response;
class Httpd
{
public:
Httpd(int port, const char *accessToken);
bool start();
private:
typedef struct PostContext
{
std::stringstream data;
} ConnectionContext;
static int sendHTMLResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp);
static int sendJSONResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp);
static int sendResponse(MHD_Connection *connection, unsigned status, MHD_Response *rsp, const char *contentType);
unsigned basicAuth(MHD_Connection *connection, std::string &resp);
unsigned tokenAuth(MHD_Connection *connection);
static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls);
static int handleGET(MHD_Connection *connection, const char *url);
static int handlePOST(MHD_Connection *connection, const char *url, const char *upload_data, size_t *upload_data_size, void **con_cls);
const char *m_accessToken;
const char *m_adminUser;
const char *m_adminPassword;
const int m_port;
MHD_Daemon *m_daemon;
};
#endif /* __HTTPD_H__ */

141
src/server/Service.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 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 <cstring>
#include <3rdparty/jansson/jansson.h>
#include "server/Service.h"
#include "log/Log.h"
char Service::m_buf[4096];
uv_mutex_t Service::m_mutex;
bool Service::start()
{
uv_mutex_init(&m_mutex);
return true;
}
void Service::release()
{
}
unsigned Service::get(const char *url, std::string &resp)
{
//if (!m_state) {
// *size = 0;
// return nullptr;
//}
uv_mutex_lock(&m_mutex);
LOG_INFO("GET(%s)", url);
/*
Handle request here
const char *buf = m_state->get(url, size);
if (*size) {
memcpy(m_buf, buf, *size);
}
else {
*status = 500;
}
*/
uv_mutex_unlock(&m_mutex);
return 200;
}
unsigned Service::post(const char *url, const std::string &data, std::string &resp)
{
//if (!m_state) {
// *size = 0;
// return nullptr;
//}
uv_mutex_lock(&m_mutex);
LOG_INFO("POST(%s, %s)", url, data.c_str());
json_error_t err;
json_t *val = json_loads(data.c_str(), 0, &err);
if (val) {
const char *miner = json_string_value(json_object_get(val, "miner"));
const char *currentPool = json_string_value(json_object_get(val, "currentPool"));
LOG_INFO("received = miner: %s on pool: %s", miner, currentPool);
} else {
LOG_ERR("BAD POST REQUEST: %s", err.text);
return MHD_HTTP_BAD_REQUEST;
}
/*
Handle request here
const char *buf = m_state->get(url, size);
if (*size) {
memcpy(m_buf, buf, *size);
}
else {
*status = 500;
}
*/
uv_mutex_unlock(&m_mutex);
return 200;
}
/*
void Service::tick(const Hashrate *hashrate)
{
if (!m_state) {
return;
}
uv_mutex_lock(&m_mutex);
m_state->tick(hashrate);
uv_mutex_unlock(&m_mutex);
}
void Service::tick(const NetworkState &network)
{
if (!m_state) {
return;
}
uv_mutex_lock(&m_mutex);
m_state->tick(network);
uv_mutex_unlock(&m_mutex);
}
*/

55
src/server/Service.h Normal file
View file

@ -0,0 +1,55 @@
/* 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 __SERVICE_H__
#define __SERVICE_H__
#include <string>
#include <uv.h>
#include <microhttpd.h>
//class ServiceState;
//class Hashrate;
//class NetworkState;
class Service
{
public:
static bool start();
static void release();
static unsigned get(const char *url, std::string &resp);
static unsigned post(const char *url, const std::string &data, std::string &resp);
// static void tick(const Hashrate *hashrate);
// static void tick(const NetworkState &results);
private:
//static ServiceState *m_state;
static char m_buf[4096];
static uv_mutex_t m_mutex;
};
#endif /* __SERVICE_H__ */

66
src/server/Summary.cpp Normal file
View file

@ -0,0 +1,66 @@
/* 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 <uv.h>
#include "log/Log.h"
#include "Options.h"
#include "Summary.h"
#include "version.h"
static void print_versions()
{
char buf[16];
# if defined(__clang__)
snprintf(buf, 16, " clang/%d.%d.%d", __clang_major__, __clang_minor__, __clang_patchlevel__);
# elif defined(__GNUC__)
snprintf(buf, 16, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
# elif defined(_MSC_VER)
snprintf(buf, 16, " MSVC/%d", MSVC_VERSION);
# else
buf[0] = '\0';
# endif
Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mVERSIONS: \x1B[01;36m%s/%s\x1B[01;37m libuv/%s%s" : " * VERSIONS: %s/%s libuv/%s%s",
APP_NAME, APP_VERSION, uv_version_string(), buf);
}
static void print_commands()
{
if (Options::i()->colors()) {
Log::i()->text("\x1B[01;32m * \x1B[01;37mCOMMANDS: \x1B[01;35mm\x1B[01;37miners");
}
else {
Log::i()->text(" * COMMANDS: 'm' miners");
}
}
void Summary::print()
{
print_versions();
//print_commands();
}

57
src/server/version.h Normal file
View file

@ -0,0 +1,57 @@
/* 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 __VERSION_H__
#define __VERSION_H__
#define APP_ID "xmrigCC"
#define APP_NAME "XMRigCC"
#define APP_DESC "XMRigCC Command'n'Control Server"
#define APP_VERSION "1.0.0"
#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
#define APP_VER_BUILD 1
#define APP_VER_REV 0
#ifdef _MSC_VER
# if _MSC_VER == 1910
# define MSVC_VERSION 2017
# elif _MSC_VER == 1900
# define MSVC_VERSION 2015
# elif _MSC_VER == 1800
# define MSVC_VERSION 2013
# elif _MSC_VER == 1700
# define MSVC_VERSION 2012
# elif _MSC_VER == 1600
# define MSVC_VERSION 2010
# else
# define MSVC_VERSION 0
# endif
#endif
#endif /* __VERSION_H__ */

32
src/server/xmrigCC.cpp Normal file
View file

@ -0,0 +1,32 @@
/* XMRigCC
* 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>
* Copyright 2017- BenDr0id <ben@graef.in>
*
*
* 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 "CCServer.h"
int main(int argc, char **argv) {
CCServer ccServer(argc, argv);
return ccServer.start();
}