diff --git a/CHANGELOG.md b/CHANGELOG.md index 0487457d..ada15bed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.15.1-beta +- [#1007](https://github.com/xmrig/xmrig/issues/1007) Old HTTP API backend based on libmicrohttpd, replaced to custom HTTP server (libuv + http_parser). +- [#257](https://github.com/xmrig/xmrig-nvidia/pull/257) New logging subsystem, file and syslog now always without colors. + # v2.15.0-beta - [#314](https://github.com/xmrig/xmrig-proxy/issues/314) Added donate over proxy feature. - Added new option `donate-over-proxy`. diff --git a/CMakeLists.txt b/CMakeLists.txt index c8e68648..c9513258 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,8 @@ include (src/base/base.cmake) set(HEADERS "${HEADERS_BASE}" - src/api/NetworkState.h + "${HEADERS_BASE_HTTP}" + src/api/interfaces/IApiListener.h src/App.h src/common/config/CommonConfig.h src/common/config/ConfigLoader.h @@ -30,19 +31,14 @@ set(HEADERS src/common/crypto/Algorithm.h src/common/crypto/keccak.h src/common/interfaces/IConfig.h - src/common/interfaces/IConfigCreator.h - src/common/interfaces/IControllerListener.h src/common/interfaces/ICpuInfo.h - src/common/interfaces/ILogBackend.h - src/common/log/BasicLog.h - src/common/log/ConsoleLog.h - src/common/log/FileLog.h - src/common/log/Log.h src/common/Platform.h src/common/utils/mm_malloc.h src/common/xmrig.h - src/core/ConfigLoader_default.h - src/core/ConfigLoader_platform.h + src/core/config/Config.h + src/core/config/ConfigLoader_default.h + src/core/config/ConfigLoader_platform.h + src/core/config/usage.h src/core/Controller.h src/interfaces/IJobResultListener.h src/interfaces/IThread.h @@ -50,13 +46,14 @@ set(HEADERS src/Mem.h src/net/JobResult.h src/net/Network.h + src/net/NetworkState.h src/net/strategies/DonateStrategy.h src/Summary.h src/version.h src/workers/CpuThread.h - src/workers/ThreadHandle.h src/workers/Hashrate.h src/workers/MultiWorker.h + src/workers/ThreadHandle.h src/workers/Worker.h src/workers/Workers.h ) @@ -85,28 +82,25 @@ endif() set(SOURCES "${SOURCES_BASE}" - src/api/NetworkState.cpp + "${SOURCES_BASE_HTTP}" src/App.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp src/common/config/ConfigWatcher.cpp src/common/crypto/Algorithm.cpp src/common/crypto/keccak.cpp - src/common/log/BasicLog.cpp - src/common/log/ConsoleLog.cpp - src/common/log/FileLog.cpp - src/common/log/Log.cpp src/common/Platform.cpp - src/core/Config.cpp + src/core/config/Config.cpp src/core/Controller.cpp src/Mem.cpp src/net/Network.cpp + src/net/NetworkState.cpp src/net/strategies/DonateStrategy.cpp src/Summary.cpp src/workers/CpuThread.cpp - src/workers/ThreadHandle.cpp src/workers/Hashrate.cpp src/workers/MultiWorker.cpp + src/workers/ThreadHandle.cpp src/workers/Worker.cpp src/workers/Workers.cpp src/xmrig.cpp @@ -189,12 +183,6 @@ include(cmake/OpenSSL.cmake) include(cmake/asm.cmake) include(cmake/cn-gpu.cmake) -CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) -if (HAVE_SYSLOG_H) - add_definitions(/DHAVE_SYSLOG_H) - set(SOURCES_SYSLOG src/common/log/SysLog.h src/common/log/SysLog.cpp) -endif() - if (NOT WITH_AEON) add_definitions(/DXMRIG_NO_AEON) endif() @@ -216,30 +204,21 @@ if (WITH_EMBEDDED_CONFIG) endif() if (WITH_HTTPD) - find_package(MHD) - - if (MHD_FOUND) - include_directories(${MHD_INCLUDE_DIRS}) - set(HTTPD_SOURCES - src/api/Api.h - src/api/ApiRouter.h - src/common/api/HttpBody.h - src/common/api/Httpd.h - src/common/api/HttpReply.h - src/common/api/HttpRequest.h - src/api/Api.cpp - src/api/ApiRouter.cpp - src/common/api/Httpd.cpp - src/common/api/HttpRequest.cpp - ) - else() - message(FATAL_ERROR "microhttpd NOT found: use `-DWITH_HTTPD=OFF` to build without http deamon support") - endif() + set(HTTPD_SOURCES + src/api/Api.cpp + src/api/Api.h + src/api/Httpd.cpp + src/api/Httpd.h + src/api/interfaces/IApiRequest.h + src/api/requests/ApiRequest.cpp + src/api/requests/ApiRequest.h + src/api/requests/HttpApiRequest.cpp + src/api/requests/HttpApiRequest.h + src/api/v1/ApiRouter.cpp + src/api/v1/ApiRouter.h + ) else() set(HTTPD_SOURCES "") - set(MHD_LIBRARY "") - add_definitions(/DXMRIG_NO_HTTPD) - add_definitions(/DXMRIG_NO_API) endif() include_directories(src) @@ -255,4 +234,4 @@ if (WITH_DEBUG_LOG) endif() add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTPD_SOURCES} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES} ${CN_GPU_SOURCES}) -target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${MHD_LIBRARY} ${EXTRA_LIBS} ${CPUID_LIB}) +target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB}) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake deleted file mode 100644 index 7a598e02..00000000 --- a/cmake/FindMHD.cmake +++ /dev/null @@ -1,49 +0,0 @@ -# - Try to find MHD -# Once done this will define -# -# MHD_FOUND - system has MHD -# MHD_INCLUDE_DIRS - the MHD include directory -# MHD_LIBRARY - Link these to use MHD - -find_path( - MHD_INCLUDE_DIR - NAMES microhttpd.h - PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" - PATH_SUFFIXES "include" - DOC "microhttpd include dir" - NO_DEFAULT_PATH -) - -find_path(MHD_INCLUDE_DIR NAMES microhttpd.h) - -find_library( - MHD_LIBRARY - NAMES libmicrohttpd.a microhttpd libmicrohttpd - PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" - PATH_SUFFIXES "lib" - DOC "microhttpd library" - NO_DEFAULT_PATH -) - -find_library(MHD_LIBRARY NAMES microhttpd libmicrohttpd) - -set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR}) -set(MHD_LIBRARIES ${MHD_LIBRARY}) - -# debug library on windows -# same naming convention as in qt (appending debug library with d) -# boost is using the same "hack" as us with "optimized" and "debug" -# official MHD project actually uses _d suffix -if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) - find_library( - MHD_LIBRARY_DEBUG - NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d - DOC "mhd debug library" - ) - set(MHD_LIBRARIES optimized ${MHD_LIBRARIES} debug ${MHD_LIBRARY_DEBUG}) -endif() - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(MHD DEFAULT_MSG MHD_LIBRARY MHD_INCLUDE_DIR) -mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) - diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index 219d50b9..bcdea0c6 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -18,12 +18,10 @@ if (WITH_TLS) endif() add_definitions(/DXMRIG_FEATURE_TLS) - remove_definitions(/DXMRIG_NO_TLS) else() set(TLS_SOURCES "") set(OPENSSL_LIBRARIES "") remove_definitions(/DXMRIG_FEATURE_TLS) - add_definitions(/DXMRIG_NO_TLS) set(CMAKE_PROJECT_NAME "${CMAKE_PROJECT_NAME}-notls") endif() diff --git a/res/app.rc b/res/app.rc index 037d842a..fb41b8b1 100644 --- a/res/app.rc +++ b/res/app.rc @@ -1,7 +1,7 @@ #include #include "../src/version.h" -IDI_ICON1 ICON DISCARDABLE "app.ico" +101 ICON "app.ico" VS_VERSION_INFO VERSIONINFO FILEVERSION APP_VER_MAJOR,APP_VER_MINOR,APP_VER_PATCH,0 diff --git a/src/3rdparty/http-parser/AUTHORS b/src/3rdparty/http-parser/AUTHORS new file mode 100644 index 00000000..5323b685 --- /dev/null +++ b/src/3rdparty/http-parser/AUTHORS @@ -0,0 +1,68 @@ +# Authors ordered by first contribution. +Ryan Dahl +Jeremy Hinegardner +Sergey Shepelev +Joe Damato +tomika +Phoenix Sol +Cliff Frey +Ewen Cheslack-Postava +Santiago Gala +Tim Becker +Jeff Terrace +Ben Noordhuis +Nathan Rajlich +Mark Nottingham +Aman Gupta +Tim Becker +Sean Cunningham +Peter Griess +Salman Haq +Cliff Frey +Jon Kolb +Fouad Mardini +Paul Querna +Felix Geisendörfer +koichik +Andre Caron +Ivo Raisr +James McLaughlin +David Gwynne +Thomas LE ROUX +Randy Rizun +Andre Louis Caron +Simon Zimmermann +Erik Dubbelboer +Martell Malone +Bertrand Paquet +BogDan Vatra +Peter Faiman +Corey Richardson +Tóth Tamás +Cam Swords +Chris Dickinson +Uli Köhler +Charlie Somerville +Patrik Stutz +Fedor Indutny +runner +Alexis Campailla +David Wragg +Vinnie Falco +Alex Butum +Rex Feng +Alex Kocharin +Mark Koopman +Helge Heß +Alexis La Goutte +George Miroshnykov +Maciej Małecki +Marc O'Morain +Jeff Pinner +Timothy J Fontaine +Akagi201 +Romain Giraud +Jay Satiro +Arne Steen +Kjell Schubert +Olivier Mengué diff --git a/src/3rdparty/http-parser/LICENSE-MIT b/src/3rdparty/http-parser/LICENSE-MIT new file mode 100644 index 00000000..1ec0ab4e --- /dev/null +++ b/src/3rdparty/http-parser/LICENSE-MIT @@ -0,0 +1,19 @@ +Copyright Joyent, Inc. and other Node contributors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/src/3rdparty/http-parser/README.md b/src/3rdparty/http-parser/README.md new file mode 100644 index 00000000..b265d717 --- /dev/null +++ b/src/3rdparty/http-parser/README.md @@ -0,0 +1,246 @@ +HTTP Parser +=========== + +[![Build Status](https://api.travis-ci.org/nodejs/http-parser.svg?branch=master)](https://travis-ci.org/nodejs/http-parser) + +This is a parser for HTTP messages written in C. It parses both requests and +responses. The parser is designed to be used in performance HTTP +applications. It does not make any syscalls nor allocations, it does not +buffer data, it can be interrupted at anytime. Depending on your +architecture, it only requires about 40 bytes of data per message +stream (in a web server that is per connection). + +Features: + + * No dependencies + * Handles persistent streams (keep-alive). + * Decodes chunked encoding. + * Upgrade support + * Defends against buffer overflow attacks. + +The parser extracts the following information from HTTP messages: + + * Header fields and values + * Content-Length + * Request method + * Response status code + * Transfer-Encoding + * HTTP version + * Request URL + * Message body + + +Usage +----- + +One `http_parser` object is used per TCP connection. Initialize the struct +using `http_parser_init()` and set the callbacks. That might look something +like this for a request parser: +```c +http_parser_settings settings; +settings.on_url = my_url_callback; +settings.on_header_field = my_header_field_callback; +/* ... */ + +http_parser *parser = malloc(sizeof(http_parser)); +http_parser_init(parser, HTTP_REQUEST); +parser->data = my_socket; +``` + +When data is received on the socket execute the parser and check for errors. + +```c +size_t len = 80*1024, nparsed; +char buf[len]; +ssize_t recved; + +recved = recv(fd, buf, len, 0); + +if (recved < 0) { + /* Handle error. */ +} + +/* Start up / continue the parser. + * Note we pass recved==0 to signal that EOF has been received. + */ +nparsed = http_parser_execute(parser, &settings, buf, recved); + +if (parser->upgrade) { + /* handle new protocol */ +} else if (nparsed != recved) { + /* Handle error. Usually just close the connection. */ +} +``` + +`http_parser` needs to know where the end of the stream is. For example, sometimes +servers send responses without Content-Length and expect the client to +consume input (for the body) until EOF. To tell `http_parser` about EOF, give +`0` as the fourth parameter to `http_parser_execute()`. Callbacks and errors +can still be encountered during an EOF, so one must still be prepared +to receive them. + +Scalar valued message information such as `status_code`, `method`, and the +HTTP version are stored in the parser structure. This data is only +temporally stored in `http_parser` and gets reset on each new message. If +this information is needed later, copy it out of the structure during the +`headers_complete` callback. + +The parser decodes the transfer-encoding for both requests and responses +transparently. That is, a chunked encoding is decoded before being sent to +the on_body callback. + + +The Special Problem of Upgrade +------------------------------ + +`http_parser` supports upgrading the connection to a different protocol. An +increasingly common example of this is the WebSocket protocol which sends +a request like + + GET /demo HTTP/1.1 + Upgrade: WebSocket + Connection: Upgrade + Host: example.com + Origin: http://example.com + WebSocket-Protocol: sample + +followed by non-HTTP data. + +(See [RFC6455](https://tools.ietf.org/html/rfc6455) for more information the +WebSocket protocol.) + +To support this, the parser will treat this as a normal HTTP message without a +body, issuing both on_headers_complete and on_message_complete callbacks. However +http_parser_execute() will stop parsing at the end of the headers and return. + +The user is expected to check if `parser->upgrade` has been set to 1 after +`http_parser_execute()` returns. Non-HTTP data begins at the buffer supplied +offset by the return value of `http_parser_execute()`. + + +Callbacks +--------- + +During the `http_parser_execute()` call, the callbacks set in +`http_parser_settings` will be executed. The parser maintains state and +never looks behind, so buffering the data is not necessary. If you need to +save certain data for later usage, you can do that from the callbacks. + +There are two types of callbacks: + +* notification `typedef int (*http_cb) (http_parser*);` + Callbacks: on_message_begin, on_headers_complete, on_message_complete. +* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);` + Callbacks: (requests only) on_url, + (common) on_header_field, on_header_value, on_body; + +Callbacks must return 0 on success. Returning a non-zero value indicates +error to the parser, making it exit immediately. + +For cases where it is necessary to pass local information to/from a callback, +the `http_parser` object's `data` field can be used. +An example of such a case is when using threads to handle a socket connection, +parse a request, and then give a response over that socket. By instantiation +of a thread-local struct containing relevant data (e.g. accepted socket, +allocated memory for callbacks to write into, etc), a parser's callbacks are +able to communicate data between the scope of the thread and the scope of the +callback in a threadsafe manner. This allows `http_parser` to be used in +multi-threaded contexts. + +Example: +```c + typedef struct { + socket_t sock; + void* buffer; + int buf_len; + } custom_data_t; + + +int my_url_callback(http_parser* parser, const char *at, size_t length) { + /* access to thread local custom_data_t struct. + Use this access save parsed data for later use into thread local + buffer, or communicate over socket + */ + parser->data; + ... + return 0; +} + +... + +void http_parser_thread(socket_t sock) { + int nparsed = 0; + /* allocate memory for user data */ + custom_data_t *my_data = malloc(sizeof(custom_data_t)); + + /* some information for use by callbacks. + * achieves thread -> callback information flow */ + my_data->sock = sock; + + /* instantiate a thread-local parser */ + http_parser *parser = malloc(sizeof(http_parser)); + http_parser_init(parser, HTTP_REQUEST); /* initialise parser */ + /* this custom data reference is accessible through the reference to the + parser supplied to callback functions */ + parser->data = my_data; + + http_parser_settings settings; /* set up callbacks */ + settings.on_url = my_url_callback; + + /* execute parser */ + nparsed = http_parser_execute(parser, &settings, buf, recved); + + ... + /* parsed information copied from callback. + can now perform action on data copied into thread-local memory from callbacks. + achieves callback -> thread information flow */ + my_data->buffer; + ... +} + +``` + +In case you parse HTTP message in chunks (i.e. `read()` request line +from socket, parse, read half headers, parse, etc) your data callbacks +may be called more than once. `http_parser` guarantees that data pointer is only +valid for the lifetime of callback. You can also `read()` into a heap allocated +buffer to avoid copying memory around if this fits your application. + +Reading headers may be a tricky task if you read/parse headers partially. +Basically, you need to remember whether last header callback was field or value +and apply the following logic: + + (on_header_field and on_header_value shortened to on_h_*) + ------------------------ ------------ -------------------------------------------- + | State (prev. callback) | Callback | Description/action | + ------------------------ ------------ -------------------------------------------- + | nothing (first call) | on_h_field | Allocate new buffer and copy callback data | + | | | into it | + ------------------------ ------------ -------------------------------------------- + | value | on_h_field | New header started. | + | | | Copy current name,value buffers to headers | + | | | list and allocate new buffer for new name | + ------------------------ ------------ -------------------------------------------- + | field | on_h_field | Previous name continues. Reallocate name | + | | | buffer and append callback data to it | + ------------------------ ------------ -------------------------------------------- + | field | on_h_value | Value for current header started. Allocate | + | | | new buffer and copy callback data to it | + ------------------------ ------------ -------------------------------------------- + | value | on_h_value | Value continues. Reallocate value buffer | + | | | and append callback data to it | + ------------------------ ------------ -------------------------------------------- + + +Parsing URLs +------------ + +A simplistic zero-copy URL parser is provided as `http_parser_parse_url()`. +Users of this library may wish to use it to parse URLs constructed from +consecutive `on_url` callbacks. + +See examples of reading in headers: + +* [partial example](http://gist.github.com/155877) in C +* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C +* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript diff --git a/src/3rdparty/http-parser/http_parser.c b/src/3rdparty/http-parser/http_parser.c new file mode 100644 index 00000000..e2fc5d2e --- /dev/null +++ b/src/3rdparty/http-parser/http_parser.c @@ -0,0 +1,2501 @@ +/* Copyright Joyent, Inc. and other Node contributors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "http_parser.h" +#include +#include +#include +#include +#include + +static uint32_t max_header_size = HTTP_MAX_HEADER_SIZE; + +#ifndef ULLONG_MAX +# define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */ +#endif + +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#endif + +#ifndef BIT_AT +# define BIT_AT(a, i) \ + (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ + (1 << ((unsigned int) (i) & 7)))) +#endif + +#ifndef ELEM_AT +# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) +#endif + +#define SET_ERRNO(e) \ +do { \ + parser->nread = nread; \ + parser->http_errno = (e); \ +} while(0) + +#define CURRENT_STATE() p_state +#define UPDATE_STATE(V) p_state = (enum state) (V); +#define RETURN(V) \ +do { \ + parser->nread = nread; \ + parser->state = CURRENT_STATE(); \ + return (V); \ +} while (0); +#define REEXECUTE() \ + goto reexecute; \ + + +#ifdef __GNUC__ +# define LIKELY(X) __builtin_expect(!!(X), 1) +# define UNLIKELY(X) __builtin_expect(!!(X), 0) +#else +# define LIKELY(X) (X) +# define UNLIKELY(X) (X) +#endif + + +/* Run the notify callback FOR, returning ER if it fails */ +#define CALLBACK_NOTIFY_(FOR, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (LIKELY(settings->on_##FOR)) { \ + parser->state = CURRENT_STATE(); \ + if (UNLIKELY(0 != settings->on_##FOR(parser))) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + UPDATE_STATE(parser->state); \ + \ + /* We either errored above or got paused; get out */ \ + if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \ + return (ER); \ + } \ + } \ +} while (0) + +/* Run the notify callback FOR and consume the current byte */ +#define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1) + +/* Run the notify callback FOR and don't consume the current byte */ +#define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data) + +/* Run data callback FOR with LEN bytes, returning ER if it fails */ +#define CALLBACK_DATA_(FOR, LEN, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (FOR##_mark) { \ + if (LIKELY(settings->on_##FOR)) { \ + parser->state = CURRENT_STATE(); \ + if (UNLIKELY(0 != \ + settings->on_##FOR(parser, FOR##_mark, (LEN)))) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + UPDATE_STATE(parser->state); \ + \ + /* We either errored above or got paused; get out */ \ + if (UNLIKELY(HTTP_PARSER_ERRNO(parser) != HPE_OK)) { \ + return (ER); \ + } \ + } \ + FOR##_mark = NULL; \ + } \ +} while (0) + +/* Run the data callback FOR and consume the current byte */ +#define CALLBACK_DATA(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) + +/* Run the data callback FOR and don't consume the current byte */ +#define CALLBACK_DATA_NOADVANCE(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) + +/* Set the mark FOR; non-destructive if mark is already set */ +#define MARK(FOR) \ +do { \ + if (!FOR##_mark) { \ + FOR##_mark = p; \ + } \ +} while (0) + +/* Don't allow the total size of the HTTP headers (including the status + * line) to exceed max_header_size. This check is here to protect + * embedders against denial-of-service attacks where the attacker feeds + * us a never-ending header that the embedder keeps buffering. + * + * This check is arguably the responsibility of embedders but we're doing + * it on the embedder's behalf because most won't bother and this way we + * make the web a little safer. max_header_size is still far bigger + * than any reasonable request or response so this should never affect + * day-to-day operation. + */ +#define COUNT_HEADER_SIZE(V) \ +do { \ + nread += (uint32_t)(V); \ + if (UNLIKELY(nread > max_header_size)) { \ + SET_ERRNO(HPE_HEADER_OVERFLOW); \ + goto error; \ + } \ +} while (0) + + +#define PROXY_CONNECTION "proxy-connection" +#define CONNECTION "connection" +#define CONTENT_LENGTH "content-length" +#define TRANSFER_ENCODING "transfer-encoding" +#define UPGRADE "upgrade" +#define CHUNKED "chunked" +#define KEEP_ALIVE "keep-alive" +#define CLOSE "close" + + +static const char *method_strings[] = + { +#define XX(num, name, string) #string, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +/* Tokens as defined by rfc 2616. Also lowercases them. + * token = 1* + * separators = "(" | ")" | "<" | ">" | "@" + * | "," | ";" | ":" | "\" | <"> + * | "/" | "[" | "]" | "?" | "=" + * | "{" | "}" | SP | HT + */ +static const char tokens[256] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + ' ', '!', 0, '#', '$', '%', '&', '\'', +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 0, 0, '*', '+', 0, '-', '.', 0, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + '0', '1', '2', '3', '4', '5', '6', '7', +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + '8', '9', 0, 0, 0, 0, 0, 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 'x', 'y', 'z', 0, 0, 0, '^', '_', +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 'x', 'y', 'z', 0, '|', 0, '~', 0 }; + + +static const int8_t unhex[256] = + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + }; + + +#if HTTP_PARSER_STRICT +# define T(v) 0 +#else +# define T(v) v +#endif + + +static const uint8_t normal_url_char[32] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; + +#undef T + +enum state + { s_dead = 1 /* important that this is > 0 */ + + , s_start_req_or_res + , s_res_or_resp_H + , s_start_res + , s_res_H + , s_res_HT + , s_res_HTT + , s_res_HTTP + , s_res_http_major + , s_res_http_dot + , s_res_http_minor + , s_res_http_end + , s_res_first_status_code + , s_res_status_code + , s_res_status_start + , s_res_status + , s_res_line_almost_done + + , s_start_req + + , s_req_method + , s_req_spaces_before_url + , s_req_schema + , s_req_schema_slash + , s_req_schema_slash_slash + , s_req_server_start + , s_req_server + , s_req_server_with_at + , s_req_path + , s_req_query_string_start + , s_req_query_string + , s_req_fragment_start + , s_req_fragment + , s_req_http_start + , s_req_http_H + , s_req_http_HT + , s_req_http_HTT + , s_req_http_HTTP + , s_req_http_I + , s_req_http_IC + , s_req_http_major + , s_req_http_dot + , s_req_http_minor + , s_req_http_end + , s_req_line_almost_done + + , s_header_field_start + , s_header_field + , s_header_value_discard_ws + , s_header_value_discard_ws_almost_done + , s_header_value_discard_lws + , s_header_value_start + , s_header_value + , s_header_value_lws + + , s_header_almost_done + + , s_chunk_size_start + , s_chunk_size + , s_chunk_parameters + , s_chunk_size_almost_done + + , s_headers_almost_done + , s_headers_done + + /* Important: 's_headers_done' must be the last 'header' state. All + * states beyond this must be 'body' states. It is used for overflow + * checking. See the PARSING_HEADER() macro. + */ + + , s_chunk_data + , s_chunk_data_almost_done + , s_chunk_data_done + + , s_body_identity + , s_body_identity_eof + + , s_message_done + }; + + +#define PARSING_HEADER(state) (state <= s_headers_done) + + +enum header_states + { h_general = 0 + , h_C + , h_CO + , h_CON + + , h_matching_connection + , h_matching_proxy_connection + , h_matching_content_length + , h_matching_transfer_encoding + , h_matching_upgrade + + , h_connection + , h_content_length + , h_content_length_num + , h_content_length_ws + , h_transfer_encoding + , h_upgrade + + , h_matching_transfer_encoding_chunked + , h_matching_connection_token_start + , h_matching_connection_keep_alive + , h_matching_connection_close + , h_matching_connection_upgrade + , h_matching_connection_token + + , h_transfer_encoding_chunked + , h_connection_keep_alive + , h_connection_close + , h_connection_upgrade + }; + +enum http_host_state + { + s_http_host_dead = 1 + , s_http_userinfo_start + , s_http_userinfo + , s_http_host_start + , s_http_host_v6_start + , s_http_host + , s_http_host_v6 + , s_http_host_v6_end + , s_http_host_v6_zone_start + , s_http_host_v6_zone + , s_http_host_port_start + , s_http_host_port +}; + +/* Macros for character classes; depends on strict-mode */ +#define CR '\r' +#define LF '\n' +#define LOWER(c) (unsigned char)(c | 0x20) +#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') +#define IS_NUM(c) ((c) >= '0' && (c) <= '9') +#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) +#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) +#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ + (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ + (c) == ')') +#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ + (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ + (c) == '$' || (c) == ',') + +#define STRICT_TOKEN(c) ((c == ' ') ? 0 : tokens[(unsigned char)c]) + +#if HTTP_PARSER_STRICT +#define TOKEN(c) STRICT_TOKEN(c) +#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) +#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') +#else +#define TOKEN(c) tokens[(unsigned char)c] +#define IS_URL_CHAR(c) \ + (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) +#define IS_HOST_CHAR(c) \ + (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') +#endif + +/** + * Verify that a char is a valid visible (printable) US-ASCII + * character or %x80-FF + **/ +#define IS_HEADER_CHAR(ch) \ + (ch == CR || ch == LF || ch == 9 || ((unsigned char)ch > 31 && ch != 127)) + +#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) + + +#if HTTP_PARSER_STRICT +# define STRICT_CHECK(cond) \ +do { \ + if (cond) { \ + SET_ERRNO(HPE_STRICT); \ + goto error; \ + } \ +} while (0) +# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) +#else +# define STRICT_CHECK(cond) +# define NEW_MESSAGE() start_state +#endif + + +/* Map errno values to strings for human-readable output */ +#define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s }, +static struct { + const char *name; + const char *description; +} http_strerror_tab[] = { + HTTP_ERRNO_MAP(HTTP_STRERROR_GEN) +}; +#undef HTTP_STRERROR_GEN + +int http_message_needs_eof(const http_parser *parser); + +/* Our URL parser. + * + * This is designed to be shared by http_parser_execute() for URL validation, + * hence it has a state transition + byte-for-byte interface. In addition, it + * is meant to be embedded in http_parser_parse_url(), which does the dirty + * work of turning state transitions URL components for its API. + * + * This function should only be invoked with non-space characters. It is + * assumed that the caller cares about (and can detect) the transition between + * URL and non-URL states by looking for these. + */ +static enum state +parse_url_char(enum state s, const char ch) +{ + if (ch == ' ' || ch == '\r' || ch == '\n') { + return s_dead; + } + +#if HTTP_PARSER_STRICT + if (ch == '\t' || ch == '\f') { + return s_dead; + } +#endif + + switch (s) { + case s_req_spaces_before_url: + /* Proxied requests are followed by scheme of an absolute URI (alpha). + * All methods except CONNECT are followed by '/' or '*'. + */ + + if (ch == '/' || ch == '*') { + return s_req_path; + } + + if (IS_ALPHA(ch)) { + return s_req_schema; + } + + break; + + case s_req_schema: + if (IS_ALPHA(ch)) { + return s; + } + + if (ch == ':') { + return s_req_schema_slash; + } + + break; + + case s_req_schema_slash: + if (ch == '/') { + return s_req_schema_slash_slash; + } + + break; + + case s_req_schema_slash_slash: + if (ch == '/') { + return s_req_server_start; + } + + break; + + case s_req_server_with_at: + if (ch == '@') { + return s_dead; + } + + /* fall through */ + case s_req_server_start: + case s_req_server: + if (ch == '/') { + return s_req_path; + } + + if (ch == '?') { + return s_req_query_string_start; + } + + if (ch == '@') { + return s_req_server_with_at; + } + + if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { + return s_req_server; + } + + break; + + case s_req_path: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + return s_req_query_string_start; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_query_string_start: + case s_req_query_string: + if (IS_URL_CHAR(ch)) { + return s_req_query_string; + } + + switch (ch) { + case '?': + /* allow extra '?' in query string */ + return s_req_query_string; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_fragment_start: + if (IS_URL_CHAR(ch)) { + return s_req_fragment; + } + + switch (ch) { + case '?': + return s_req_fragment; + + case '#': + return s; + } + + break; + + case s_req_fragment: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + case '#': + return s; + } + + break; + + default: + break; + } + + /* We should never fall out of the switch above unless there's an error */ + return s_dead; +} + +size_t http_parser_execute (http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len) +{ + char c, ch; + int8_t unhex_val; + const char *p = data; + const char *header_field_mark = 0; + const char *header_value_mark = 0; + const char *url_mark = 0; + const char *body_mark = 0; + const char *status_mark = 0; + enum state p_state = (enum state) parser->state; + const unsigned int lenient = parser->lenient_http_headers; + uint32_t nread = parser->nread; + + /* We're in an error state. Don't bother doing anything. */ + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + return 0; + } + + if (len == 0) { + switch (CURRENT_STATE()) { + case s_body_identity_eof: + /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if + * we got paused. + */ + CALLBACK_NOTIFY_NOADVANCE(message_complete); + return 0; + + case s_dead: + case s_start_req_or_res: + case s_start_res: + case s_start_req: + return 0; + + default: + SET_ERRNO(HPE_INVALID_EOF_STATE); + return 1; + } + } + + + if (CURRENT_STATE() == s_header_field) + header_field_mark = data; + if (CURRENT_STATE() == s_header_value) + header_value_mark = data; + switch (CURRENT_STATE()) { + case s_req_path: + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_server: + case s_req_server_with_at: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + url_mark = data; + break; + case s_res_status: + status_mark = data; + break; + default: + break; + } + + for (p=data; p != data + len; p++) { + ch = *p; + + if (PARSING_HEADER(CURRENT_STATE())) + COUNT_HEADER_SIZE(1); + +reexecute: + switch (CURRENT_STATE()) { + + case s_dead: + /* this state is used after a 'Connection: close' message + * the parser will error out if it reads another message + */ + if (LIKELY(ch == CR || ch == LF)) + break; + + SET_ERRNO(HPE_CLOSED_CONNECTION); + goto error; + + case s_start_req_or_res: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (ch == 'H') { + UPDATE_STATE(s_res_or_resp_H); + + CALLBACK_NOTIFY(message_begin); + } else { + parser->type = HTTP_REQUEST; + UPDATE_STATE(s_start_req); + REEXECUTE(); + } + + break; + } + + case s_res_or_resp_H: + if (ch == 'T') { + parser->type = HTTP_RESPONSE; + UPDATE_STATE(s_res_HT); + } else { + if (UNLIKELY(ch != 'E')) { + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + parser->type = HTTP_REQUEST; + parser->method = HTTP_HEAD; + parser->index = 2; + UPDATE_STATE(s_req_method); + } + break; + + case s_start_res: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (ch == 'H') { + UPDATE_STATE(s_res_H); + } else { + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + CALLBACK_NOTIFY(message_begin); + break; + } + + case s_res_H: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_res_HT); + break; + + case s_res_HT: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_res_HTT); + break; + + case s_res_HTT: + STRICT_CHECK(ch != 'P'); + UPDATE_STATE(s_res_HTTP); + break; + + case s_res_HTTP: + STRICT_CHECK(ch != '/'); + UPDATE_STATE(s_res_http_major); + break; + + case s_res_http_major: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + UPDATE_STATE(s_res_http_dot); + break; + + case s_res_http_dot: + { + if (UNLIKELY(ch != '.')) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + UPDATE_STATE(s_res_http_minor); + break; + } + + case s_res_http_minor: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + UPDATE_STATE(s_res_http_end); + break; + + case s_res_http_end: + { + if (UNLIKELY(ch != ' ')) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + UPDATE_STATE(s_res_first_status_code); + break; + } + + case s_res_first_status_code: + { + if (!IS_NUM(ch)) { + if (ch == ' ') { + break; + } + + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + parser->status_code = ch - '0'; + UPDATE_STATE(s_res_status_code); + break; + } + + case s_res_status_code: + { + if (!IS_NUM(ch)) { + switch (ch) { + case ' ': + UPDATE_STATE(s_res_status_start); + break; + case CR: + case LF: + UPDATE_STATE(s_res_status_start); + REEXECUTE(); + break; + default: + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + break; + } + + parser->status_code *= 10; + parser->status_code += ch - '0'; + + if (UNLIKELY(parser->status_code > 999)) { + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + + break; + } + + case s_res_status_start: + { + MARK(status); + UPDATE_STATE(s_res_status); + parser->index = 0; + + if (ch == CR || ch == LF) + REEXECUTE(); + + break; + } + + case s_res_status: + if (ch == CR) { + UPDATE_STATE(s_res_line_almost_done); + CALLBACK_DATA(status); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_field_start); + CALLBACK_DATA(status); + break; + } + + break; + + case s_res_line_almost_done: + STRICT_CHECK(ch != LF); + UPDATE_STATE(s_header_field_start); + break; + + case s_start_req: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (UNLIKELY(!IS_ALPHA(ch))) { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + parser->method = (enum http_method) 0; + parser->index = 1; + switch (ch) { + case 'A': parser->method = HTTP_ACL; break; + case 'B': parser->method = HTTP_BIND; break; + case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break; + case 'D': parser->method = HTTP_DELETE; break; + case 'G': parser->method = HTTP_GET; break; + case 'H': parser->method = HTTP_HEAD; break; + case 'L': parser->method = HTTP_LOCK; /* or LINK */ break; + case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break; + case 'N': parser->method = HTTP_NOTIFY; break; + case 'O': parser->method = HTTP_OPTIONS; break; + case 'P': parser->method = HTTP_POST; + /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ + break; + case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break; + case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH, SOURCE */ break; + case 'T': parser->method = HTTP_TRACE; break; + case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break; + default: + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + UPDATE_STATE(s_req_method); + + CALLBACK_NOTIFY(message_begin); + + break; + } + + case s_req_method: + { + const char *matcher; + if (UNLIKELY(ch == '\0')) { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + matcher = method_strings[parser->method]; + if (ch == ' ' && matcher[parser->index] == '\0') { + UPDATE_STATE(s_req_spaces_before_url); + } else if (ch == matcher[parser->index]) { + ; /* nada */ + } else if ((ch >= 'A' && ch <= 'Z') || ch == '-') { + + switch (parser->method << 16 | parser->index << 8 | ch) { +#define XX(meth, pos, ch, new_meth) \ + case (HTTP_##meth << 16 | pos << 8 | ch): \ + parser->method = HTTP_##new_meth; break; + + XX(POST, 1, 'U', PUT) + XX(POST, 1, 'A', PATCH) + XX(POST, 1, 'R', PROPFIND) + XX(PUT, 2, 'R', PURGE) + XX(CONNECT, 1, 'H', CHECKOUT) + XX(CONNECT, 2, 'P', COPY) + XX(MKCOL, 1, 'O', MOVE) + XX(MKCOL, 1, 'E', MERGE) + XX(MKCOL, 1, '-', MSEARCH) + XX(MKCOL, 2, 'A', MKACTIVITY) + XX(MKCOL, 3, 'A', MKCALENDAR) + XX(SUBSCRIBE, 1, 'E', SEARCH) + XX(SUBSCRIBE, 1, 'O', SOURCE) + XX(REPORT, 2, 'B', REBIND) + XX(PROPFIND, 4, 'P', PROPPATCH) + XX(LOCK, 1, 'I', LINK) + XX(UNLOCK, 2, 'S', UNSUBSCRIBE) + XX(UNLOCK, 2, 'B', UNBIND) + XX(UNLOCK, 3, 'I', UNLINK) +#undef XX + default: + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + } else { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + ++parser->index; + break; + } + + case s_req_spaces_before_url: + { + if (ch == ' ') break; + + MARK(url); + if (parser->method == HTTP_CONNECT) { + UPDATE_STATE(s_req_server_start); + } + + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + + break; + } + + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + { + switch (ch) { + /* No whitespace allowed here */ + case ' ': + case CR: + case LF: + SET_ERRNO(HPE_INVALID_URL); + goto error; + default: + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + + break; + } + + case s_req_server: + case s_req_server_with_at: + case s_req_path: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + { + switch (ch) { + case ' ': + UPDATE_STATE(s_req_http_start); + CALLBACK_DATA(url); + break; + case CR: + case LF: + parser->http_major = 0; + parser->http_minor = 9; + UPDATE_STATE((ch == CR) ? + s_req_line_almost_done : + s_header_field_start); + CALLBACK_DATA(url); + break; + default: + UPDATE_STATE(parse_url_char(CURRENT_STATE(), ch)); + if (UNLIKELY(CURRENT_STATE() == s_dead)) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + break; + } + + case s_req_http_start: + switch (ch) { + case ' ': + break; + case 'H': + UPDATE_STATE(s_req_http_H); + break; + case 'I': + if (parser->method == HTTP_SOURCE) { + UPDATE_STATE(s_req_http_I); + break; + } + /* fall through */ + default: + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + break; + + case s_req_http_H: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_req_http_HT); + break; + + case s_req_http_HT: + STRICT_CHECK(ch != 'T'); + UPDATE_STATE(s_req_http_HTT); + break; + + case s_req_http_HTT: + STRICT_CHECK(ch != 'P'); + UPDATE_STATE(s_req_http_HTTP); + break; + + case s_req_http_I: + STRICT_CHECK(ch != 'C'); + UPDATE_STATE(s_req_http_IC); + break; + + case s_req_http_IC: + STRICT_CHECK(ch != 'E'); + UPDATE_STATE(s_req_http_HTTP); /* Treat "ICE" as "HTTP". */ + break; + + case s_req_http_HTTP: + STRICT_CHECK(ch != '/'); + UPDATE_STATE(s_req_http_major); + break; + + case s_req_http_major: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + UPDATE_STATE(s_req_http_dot); + break; + + case s_req_http_dot: + { + if (UNLIKELY(ch != '.')) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + UPDATE_STATE(s_req_http_minor); + break; + } + + case s_req_http_minor: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + UPDATE_STATE(s_req_http_end); + break; + + case s_req_http_end: + { + if (ch == CR) { + UPDATE_STATE(s_req_line_almost_done); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_field_start); + break; + } + + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + break; + } + + /* end of request line */ + case s_req_line_almost_done: + { + if (UNLIKELY(ch != LF)) { + SET_ERRNO(HPE_LF_EXPECTED); + goto error; + } + + UPDATE_STATE(s_header_field_start); + break; + } + + case s_header_field_start: + { + if (ch == CR) { + UPDATE_STATE(s_headers_almost_done); + break; + } + + if (ch == LF) { + /* they might be just sending \n instead of \r\n so this would be + * the second \n to denote the end of headers*/ + UPDATE_STATE(s_headers_almost_done); + REEXECUTE(); + } + + c = TOKEN(ch); + + if (UNLIKELY(!c)) { + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + MARK(header_field); + + parser->index = 0; + UPDATE_STATE(s_header_field); + + switch (c) { + case 'c': + parser->header_state = h_C; + break; + + case 'p': + parser->header_state = h_matching_proxy_connection; + break; + + case 't': + parser->header_state = h_matching_transfer_encoding; + break; + + case 'u': + parser->header_state = h_matching_upgrade; + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_field: + { + const char* start = p; + for (; p != data + len; p++) { + ch = *p; + c = TOKEN(ch); + + if (!c) + break; + + switch (parser->header_state) { + case h_general: { + size_t limit = data + len - p; + limit = MIN(limit, max_header_size); + while (p+1 < data + limit && TOKEN(p[1])) { + p++; + } + break; + } + + case h_C: + parser->index++; + parser->header_state = (c == 'o' ? h_CO : h_general); + break; + + case h_CO: + parser->index++; + parser->header_state = (c == 'n' ? h_CON : h_general); + break; + + case h_CON: + parser->index++; + switch (c) { + case 'n': + parser->header_state = h_matching_connection; + break; + case 't': + parser->header_state = h_matching_content_length; + break; + default: + parser->header_state = h_general; + break; + } + break; + + /* connection */ + + case h_matching_connection: + parser->index++; + if (parser->index > sizeof(CONNECTION)-1 + || c != CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* proxy-connection */ + + case h_matching_proxy_connection: + parser->index++; + if (parser->index > sizeof(PROXY_CONNECTION)-1 + || c != PROXY_CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(PROXY_CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* content-length */ + + case h_matching_content_length: + parser->index++; + if (parser->index > sizeof(CONTENT_LENGTH)-1 + || c != CONTENT_LENGTH[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONTENT_LENGTH)-2) { + parser->header_state = h_content_length; + } + break; + + /* transfer-encoding */ + + case h_matching_transfer_encoding: + parser->index++; + if (parser->index > sizeof(TRANSFER_ENCODING)-1 + || c != TRANSFER_ENCODING[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) { + parser->header_state = h_transfer_encoding; + } + break; + + /* upgrade */ + + case h_matching_upgrade: + parser->index++; + if (parser->index > sizeof(UPGRADE)-1 + || c != UPGRADE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(UPGRADE)-2) { + parser->header_state = h_upgrade; + } + break; + + case h_connection: + case h_content_length: + case h_transfer_encoding: + case h_upgrade: + if (ch != ' ') parser->header_state = h_general; + break; + + default: + assert(0 && "Unknown header_state"); + break; + } + } + + if (p == data + len) { + --p; + COUNT_HEADER_SIZE(p - start); + break; + } + + COUNT_HEADER_SIZE(p - start); + + if (ch == ':') { + UPDATE_STATE(s_header_value_discard_ws); + CALLBACK_DATA(header_field); + break; + } + + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + case s_header_value_discard_ws: + if (ch == ' ' || ch == '\t') break; + + if (ch == CR) { + UPDATE_STATE(s_header_value_discard_ws_almost_done); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_value_discard_lws); + break; + } + + /* fall through */ + + case s_header_value_start: + { + MARK(header_value); + + UPDATE_STATE(s_header_value); + parser->index = 0; + + c = LOWER(ch); + + switch (parser->header_state) { + case h_upgrade: + parser->flags |= F_UPGRADE; + parser->header_state = h_general; + break; + + case h_transfer_encoding: + /* looking for 'Transfer-Encoding: chunked' */ + if ('c' == c) { + parser->header_state = h_matching_transfer_encoding_chunked; + } else { + parser->header_state = h_general; + } + break; + + case h_content_length: + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + if (parser->flags & F_CONTENTLENGTH) { + SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + + parser->flags |= F_CONTENTLENGTH; + parser->content_length = ch - '0'; + parser->header_state = h_content_length_num; + break; + + /* when obsolete line folding is encountered for content length + * continue to the s_header_value state */ + case h_content_length_ws: + break; + + case h_connection: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + parser->header_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + parser->header_state = h_matching_connection_close; + } else if (c == 'u') { + parser->header_state = h_matching_connection_upgrade; + } else { + parser->header_state = h_matching_connection_token; + } + break; + + /* Multi-value `Connection` header */ + case h_matching_connection_token_start: + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_value: + { + const char* start = p; + enum header_states h_state = (enum header_states) parser->header_state; + for (; p != data + len; p++) { + ch = *p; + if (ch == CR) { + UPDATE_STATE(s_header_almost_done); + parser->header_state = h_state; + CALLBACK_DATA(header_value); + break; + } + + if (ch == LF) { + UPDATE_STATE(s_header_almost_done); + COUNT_HEADER_SIZE(p - start); + parser->header_state = h_state; + CALLBACK_DATA_NOADVANCE(header_value); + REEXECUTE(); + } + + if (!lenient && !IS_HEADER_CHAR(ch)) { + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + c = LOWER(ch); + + switch (h_state) { + case h_general: + { + const char* p_cr; + const char* p_lf; + size_t limit = data + len - p; + + limit = MIN(limit, max_header_size); + + p_cr = (const char*) memchr(p, CR, limit); + p_lf = (const char*) memchr(p, LF, limit); + if (p_cr != NULL) { + if (p_lf != NULL && p_cr >= p_lf) + p = p_lf; + else + p = p_cr; + } else if (UNLIKELY(p_lf != NULL)) { + p = p_lf; + } else { + p = data + len; + } + --p; + break; + } + + case h_connection: + case h_transfer_encoding: + assert(0 && "Shouldn't get here."); + break; + + case h_content_length: + if (ch == ' ') break; + h_state = h_content_length_num; + /* fall through */ + + case h_content_length_num: + { + uint64_t t; + + if (ch == ' ') { + h_state = h_content_length_ws; + break; + } + + if (UNLIKELY(!IS_NUM(ch))) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + t = parser->content_length; + t *= 10; + t += ch - '0'; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (UNLIKELY((ULLONG_MAX - 10) / 10 < parser->content_length)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + parser->content_length = t; + break; + } + + case h_content_length_ws: + if (ch == ' ') break; + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + + /* Transfer-Encoding: chunked */ + case h_matching_transfer_encoding_chunked: + parser->index++; + if (parser->index > sizeof(CHUNKED)-1 + || c != CHUNKED[parser->index]) { + h_state = h_general; + } else if (parser->index == sizeof(CHUNKED)-2) { + h_state = h_transfer_encoding_chunked; + } + break; + + case h_matching_connection_token_start: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + h_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + h_state = h_matching_connection_close; + } else if (c == 'u') { + h_state = h_matching_connection_upgrade; + } else if (STRICT_TOKEN(c)) { + h_state = h_matching_connection_token; + } else if (c == ' ' || c == '\t') { + /* Skip lws */ + } else { + h_state = h_general; + } + break; + + /* looking for 'Connection: keep-alive' */ + case h_matching_connection_keep_alive: + parser->index++; + if (parser->index > sizeof(KEEP_ALIVE)-1 + || c != KEEP_ALIVE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(KEEP_ALIVE)-2) { + h_state = h_connection_keep_alive; + } + break; + + /* looking for 'Connection: close' */ + case h_matching_connection_close: + parser->index++; + if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(CLOSE)-2) { + h_state = h_connection_close; + } + break; + + /* looking for 'Connection: upgrade' */ + case h_matching_connection_upgrade: + parser->index++; + if (parser->index > sizeof(UPGRADE) - 1 || + c != UPGRADE[parser->index]) { + h_state = h_matching_connection_token; + } else if (parser->index == sizeof(UPGRADE)-2) { + h_state = h_connection_upgrade; + } + break; + + case h_matching_connection_token: + if (ch == ',') { + h_state = h_matching_connection_token_start; + parser->index = 0; + } + break; + + case h_transfer_encoding_chunked: + if (ch != ' ') h_state = h_general; + break; + + case h_connection_keep_alive: + case h_connection_close: + case h_connection_upgrade: + if (ch == ',') { + if (h_state == h_connection_keep_alive) { + parser->flags |= F_CONNECTION_KEEP_ALIVE; + } else if (h_state == h_connection_close) { + parser->flags |= F_CONNECTION_CLOSE; + } else if (h_state == h_connection_upgrade) { + parser->flags |= F_CONNECTION_UPGRADE; + } + h_state = h_matching_connection_token_start; + parser->index = 0; + } else if (ch != ' ') { + h_state = h_matching_connection_token; + } + break; + + default: + UPDATE_STATE(s_header_value); + h_state = h_general; + break; + } + } + parser->header_state = h_state; + + if (p == data + len) + --p; + + COUNT_HEADER_SIZE(p - start); + break; + } + + case s_header_almost_done: + { + if (UNLIKELY(ch != LF)) { + SET_ERRNO(HPE_LF_EXPECTED); + goto error; + } + + UPDATE_STATE(s_header_value_lws); + break; + } + + case s_header_value_lws: + { + if (ch == ' ' || ch == '\t') { + if (parser->header_state == h_content_length_num) { + /* treat obsolete line folding as space */ + parser->header_state = h_content_length_ws; + } + UPDATE_STATE(s_header_value_start); + REEXECUTE(); + } + + /* finished the header */ + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + case h_connection_upgrade: + parser->flags |= F_CONNECTION_UPGRADE; + break; + default: + break; + } + + UPDATE_STATE(s_header_field_start); + REEXECUTE(); + } + + case s_header_value_discard_ws_almost_done: + { + STRICT_CHECK(ch != LF); + UPDATE_STATE(s_header_value_discard_lws); + break; + } + + case s_header_value_discard_lws: + { + if (ch == ' ' || ch == '\t') { + UPDATE_STATE(s_header_value_discard_ws); + break; + } else { + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_connection_upgrade: + parser->flags |= F_CONNECTION_UPGRADE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + case h_content_length: + /* do not allow empty content length */ + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + break; + default: + break; + } + + /* header value was empty */ + MARK(header_value); + UPDATE_STATE(s_header_field_start); + CALLBACK_DATA_NOADVANCE(header_value); + REEXECUTE(); + } + } + + case s_headers_almost_done: + { + STRICT_CHECK(ch != LF); + + if (parser->flags & F_TRAILING) { + /* End of a chunked request */ + UPDATE_STATE(s_message_done); + CALLBACK_NOTIFY_NOADVANCE(chunk_complete); + REEXECUTE(); + } + + /* Cannot use chunked encoding and a content-length header together + per the HTTP specification. */ + if ((parser->flags & F_CHUNKED) && + (parser->flags & F_CONTENTLENGTH)) { + SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + + UPDATE_STATE(s_headers_done); + + /* Set this here so that on_headers_complete() callbacks can see it */ + if ((parser->flags & F_UPGRADE) && + (parser->flags & F_CONNECTION_UPGRADE)) { + /* For responses, "Upgrade: foo" and "Connection: upgrade" are + * mandatory only when it is a 101 Switching Protocols response, + * otherwise it is purely informational, to announce support. + */ + parser->upgrade = + (parser->type == HTTP_REQUEST || parser->status_code == 101); + } else { + parser->upgrade = (parser->method == HTTP_CONNECT); + } + + /* Here we call the headers_complete callback. This is somewhat + * different than other callbacks because if the user returns 1, we + * will interpret that as saying that this message has no body. This + * is needed for the annoying case of recieving a response to a HEAD + * request. + * + * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so + * we have to simulate it by handling a change in errno below. + */ + if (settings->on_headers_complete) { + switch (settings->on_headers_complete(parser)) { + case 0: + break; + + case 2: + parser->upgrade = 1; + + /* fall through */ + case 1: + parser->flags |= F_SKIPBODY; + break; + + default: + SET_ERRNO(HPE_CB_headers_complete); + RETURN(p - data); /* Error */ + } + } + + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + RETURN(p - data); + } + + REEXECUTE(); + } + + case s_headers_done: + { + int hasBody; + STRICT_CHECK(ch != LF); + + parser->nread = 0; + nread = 0; + + hasBody = parser->flags & F_CHUNKED || + (parser->content_length > 0 && parser->content_length != ULLONG_MAX); + if (parser->upgrade && (parser->method == HTTP_CONNECT || + (parser->flags & F_SKIPBODY) || !hasBody)) { + /* Exit, the rest of the message is in a different protocol. */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + RETURN((p - data) + 1); + } + + if (parser->flags & F_SKIPBODY) { + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else if (parser->flags & F_CHUNKED) { + /* chunked encoding - ignore Content-Length header */ + UPDATE_STATE(s_chunk_size_start); + } else { + if (parser->content_length == 0) { + /* Content-Length header given but zero: Content-Length: 0\r\n */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else if (parser->content_length != ULLONG_MAX) { + /* Content-Length header given and non-zero */ + UPDATE_STATE(s_body_identity); + } else { + if (!http_message_needs_eof(parser)) { + /* Assume content-length 0 - read the next */ + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + } else { + /* Read body until EOF */ + UPDATE_STATE(s_body_identity_eof); + } + } + } + + break; + } + + case s_body_identity: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* The difference between advancing content_length and p is because + * the latter will automaticaly advance on the next loop iteration. + * Further, if content_length ends up at 0, we want to see the last + * byte again for our message complete callback. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + UPDATE_STATE(s_message_done); + + /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. + * + * The alternative to doing this is to wait for the next byte to + * trigger the data callback, just as in every other case. The + * problem with this is that this makes it difficult for the test + * harness to distinguish between complete-on-EOF and + * complete-on-length. It's not clear that this distinction is + * important for applications, but let's keep it for now. + */ + CALLBACK_DATA_(body, p - body_mark + 1, p - data); + REEXECUTE(); + } + + break; + } + + /* read until EOF */ + case s_body_identity_eof: + MARK(body); + p = data + len - 1; + + break; + + case s_message_done: + UPDATE_STATE(NEW_MESSAGE()); + CALLBACK_NOTIFY(message_complete); + if (parser->upgrade) { + /* Exit, the rest of the message is in a different protocol. */ + RETURN((p - data) + 1); + } + break; + + case s_chunk_size_start: + { + assert(nread == 1); + assert(parser->flags & F_CHUNKED); + + unhex_val = unhex[(unsigned char)ch]; + if (UNLIKELY(unhex_val == -1)) { + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + parser->content_length = unhex_val; + UPDATE_STATE(s_chunk_size); + break; + } + + case s_chunk_size: + { + uint64_t t; + + assert(parser->flags & F_CHUNKED); + + if (ch == CR) { + UPDATE_STATE(s_chunk_size_almost_done); + break; + } + + unhex_val = unhex[(unsigned char)ch]; + + if (unhex_val == -1) { + if (ch == ';' || ch == ' ') { + UPDATE_STATE(s_chunk_parameters); + break; + } + + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + t = parser->content_length; + t *= 16; + t += unhex_val; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (UNLIKELY((ULLONG_MAX - 16) / 16 < parser->content_length)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = t; + break; + } + + case s_chunk_parameters: + { + assert(parser->flags & F_CHUNKED); + /* just ignore this shit. TODO check for overflow */ + if (ch == CR) { + UPDATE_STATE(s_chunk_size_almost_done); + break; + } + break; + } + + case s_chunk_size_almost_done: + { + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + + parser->nread = 0; + nread = 0; + + if (parser->content_length == 0) { + parser->flags |= F_TRAILING; + UPDATE_STATE(s_header_field_start); + } else { + UPDATE_STATE(s_chunk_data); + } + CALLBACK_NOTIFY(chunk_header); + break; + } + + case s_chunk_data: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->flags & F_CHUNKED); + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* See the explanation in s_body_identity for why the content + * length and data pointers are managed this way. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + UPDATE_STATE(s_chunk_data_almost_done); + } + + break; + } + + case s_chunk_data_almost_done: + assert(parser->flags & F_CHUNKED); + assert(parser->content_length == 0); + STRICT_CHECK(ch != CR); + UPDATE_STATE(s_chunk_data_done); + CALLBACK_DATA(body); + break; + + case s_chunk_data_done: + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + parser->nread = 0; + nread = 0; + UPDATE_STATE(s_chunk_size_start); + CALLBACK_NOTIFY(chunk_complete); + break; + + default: + assert(0 && "unhandled state"); + SET_ERRNO(HPE_INVALID_INTERNAL_STATE); + goto error; + } + } + + /* Run callbacks for any marks that we have leftover after we ran out of + * bytes. There should be at most one of these set, so it's OK to invoke + * them in series (unset marks will not result in callbacks). + * + * We use the NOADVANCE() variety of callbacks here because 'p' has already + * overflowed 'data' and this allows us to correct for the off-by-one that + * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p' + * value that's in-bounds). + */ + + assert(((header_field_mark ? 1 : 0) + + (header_value_mark ? 1 : 0) + + (url_mark ? 1 : 0) + + (body_mark ? 1 : 0) + + (status_mark ? 1 : 0)) <= 1); + + CALLBACK_DATA_NOADVANCE(header_field); + CALLBACK_DATA_NOADVANCE(header_value); + CALLBACK_DATA_NOADVANCE(url); + CALLBACK_DATA_NOADVANCE(body); + CALLBACK_DATA_NOADVANCE(status); + + RETURN(len); + +error: + if (HTTP_PARSER_ERRNO(parser) == HPE_OK) { + SET_ERRNO(HPE_UNKNOWN); + } + + RETURN(p - data); +} + + +/* Does the parser need to see an EOF to find the end of the message? */ +int +http_message_needs_eof (const http_parser *parser) +{ + if (parser->type == HTTP_REQUEST) { + return 0; + } + + /* See RFC 2616 section 4.4 */ + if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 || /* Not Modified */ + parser->flags & F_SKIPBODY) { /* response to a HEAD request */ + return 0; + } + + if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) { + return 0; + } + + return 1; +} + + +int +http_should_keep_alive (const http_parser *parser) +{ + if (parser->http_major > 0 && parser->http_minor > 0) { + /* HTTP/1.1 */ + if (parser->flags & F_CONNECTION_CLOSE) { + return 0; + } + } else { + /* HTTP/1.0 or earlier */ + if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { + return 0; + } + } + + return !http_message_needs_eof(parser); +} + + +const char * +http_method_str (enum http_method m) +{ + return ELEM_AT(method_strings, m, ""); +} + +const char * +http_status_str (enum http_status s) +{ + switch (s) { +#define XX(num, name, string) case HTTP_STATUS_##name: return #string; + HTTP_STATUS_MAP(XX) +#undef XX + default: return ""; + } +} + +void +http_parser_init (http_parser *parser, enum http_parser_type t) +{ + void *data = parser->data; /* preserve application data */ + memset(parser, 0, sizeof(*parser)); + parser->data = data; + parser->type = t; + parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res)); + parser->http_errno = HPE_OK; +} + +void +http_parser_settings_init(http_parser_settings *settings) +{ + memset(settings, 0, sizeof(*settings)); +} + +const char * +http_errno_name(enum http_errno err) { + assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].name; +} + +const char * +http_errno_description(enum http_errno err) { + assert(((size_t) err) < ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].description; +} + +static enum http_host_state +http_parse_host_char(enum http_host_state s, const char ch) { + switch(s) { + case s_http_userinfo: + case s_http_userinfo_start: + if (ch == '@') { + return s_http_host_start; + } + + if (IS_USERINFO_CHAR(ch)) { + return s_http_userinfo; + } + break; + + case s_http_host_start: + if (ch == '[') { + return s_http_host_v6_start; + } + + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + break; + + case s_http_host: + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + /* fall through */ + case s_http_host_v6_end: + if (ch == ':') { + return s_http_host_port_start; + } + + break; + + case s_http_host_v6: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* fall through */ + case s_http_host_v6_start: + if (IS_HEX(ch) || ch == ':' || ch == '.') { + return s_http_host_v6; + } + + if (s == s_http_host_v6 && ch == '%') { + return s_http_host_v6_zone_start; + } + break; + + case s_http_host_v6_zone: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* fall through */ + case s_http_host_v6_zone_start: + /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */ + if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' || + ch == '~') { + return s_http_host_v6_zone; + } + break; + + case s_http_host_port: + case s_http_host_port_start: + if (IS_NUM(ch)) { + return s_http_host_port; + } + + break; + + default: + break; + } + return s_http_host_dead; +} + +static int +http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { + enum http_host_state s; + + const char *p; + size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; + + assert(u->field_set & (1 << UF_HOST)); + + u->field_data[UF_HOST].len = 0; + + s = found_at ? s_http_userinfo_start : s_http_host_start; + + for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) { + enum http_host_state new_s = http_parse_host_char(s, *p); + + if (new_s == s_http_host_dead) { + return 1; + } + + switch(new_s) { + case s_http_host: + if (s != s_http_host) { + u->field_data[UF_HOST].off = (uint16_t)(p - buf); + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_v6: + if (s != s_http_host_v6) { + u->field_data[UF_HOST].off = (uint16_t)(p - buf); + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + u->field_data[UF_HOST].len++; + break; + + case s_http_host_port: + if (s != s_http_host_port) { + u->field_data[UF_PORT].off = (uint16_t)(p - buf); + u->field_data[UF_PORT].len = 0; + u->field_set |= (1 << UF_PORT); + } + u->field_data[UF_PORT].len++; + break; + + case s_http_userinfo: + if (s != s_http_userinfo) { + u->field_data[UF_USERINFO].off = (uint16_t)(p - buf); + u->field_data[UF_USERINFO].len = 0; + u->field_set |= (1 << UF_USERINFO); + } + u->field_data[UF_USERINFO].len++; + break; + + default: + break; + } + s = new_s; + } + + /* Make sure we don't end somewhere unexpected */ + switch (s) { + case s_http_host_start: + case s_http_host_v6_start: + case s_http_host_v6: + case s_http_host_v6_zone_start: + case s_http_host_v6_zone: + case s_http_host_port_start: + case s_http_userinfo: + case s_http_userinfo_start: + return 1; + default: + break; + } + + return 0; +} + +void +http_parser_url_init(struct http_parser_url *u) { + memset(u, 0, sizeof(*u)); +} + +int +http_parser_parse_url(const char *buf, size_t buflen, int is_connect, + struct http_parser_url *u) +{ + enum state s; + const char *p; + enum http_parser_url_fields uf, old_uf; + int found_at = 0; + + if (buflen == 0) { + return 1; + } + + u->port = u->field_set = 0; + s = is_connect ? s_req_server_start : s_req_spaces_before_url; + old_uf = UF_MAX; + + for (p = buf; p < buf + buflen; p++) { + s = parse_url_char(s, *p); + + /* Figure out the next field that we're operating on */ + switch (s) { + case s_dead: + return 1; + + /* Skip delimeters */ + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_query_string_start: + case s_req_fragment_start: + continue; + + case s_req_schema: + uf = UF_SCHEMA; + break; + + case s_req_server_with_at: + found_at = 1; + + /* fall through */ + case s_req_server: + uf = UF_HOST; + break; + + case s_req_path: + uf = UF_PATH; + break; + + case s_req_query_string: + uf = UF_QUERY; + break; + + case s_req_fragment: + uf = UF_FRAGMENT; + break; + + default: + assert(!"Unexpected state"); + return 1; + } + + /* Nothing's changed; soldier on */ + if (uf == old_uf) { + u->field_data[uf].len++; + continue; + } + + u->field_data[uf].off = (uint16_t)(p - buf); + u->field_data[uf].len = 1; + + u->field_set |= (1 << uf); + old_uf = uf; + } + + /* host must be present if there is a schema */ + /* parsing http:///toto will fail */ + if ((u->field_set & (1 << UF_SCHEMA)) && + (u->field_set & (1 << UF_HOST)) == 0) { + return 1; + } + + if (u->field_set & (1 << UF_HOST)) { + if (http_parse_host(buf, u, found_at) != 0) { + return 1; + } + } + + /* CONNECT requests can only contain "hostname:port" */ + if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) { + return 1; + } + + if (u->field_set & (1 << UF_PORT)) { + uint16_t off; + uint16_t len; + const char* p; + const char* end; + unsigned long v; + + off = u->field_data[UF_PORT].off; + len = u->field_data[UF_PORT].len; + end = buf + off + len; + + /* NOTE: The characters are already validated and are in the [0-9] range */ + assert(off + len <= buflen && "Port number overflow"); + v = 0; + for (p = buf + off; p < end; p++) { + v *= 10; + v += *p - '0'; + + /* Ports have a max value of 2^16 */ + if (v > 0xffff) { + return 1; + } + } + + u->port = (uint16_t) v; + } + + return 0; +} + +void +http_parser_pause(http_parser *parser, int paused) { + /* Users should only be pausing/unpausing a parser that is not in an error + * state. In non-debug builds, there's not much that we can do about this + * other than ignore it. + */ + if (HTTP_PARSER_ERRNO(parser) == HPE_OK || + HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) { + uint32_t nread = parser->nread; /* used by the SET_ERRNO macro */ + SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK); + } else { + assert(0 && "Attempting to pause parser in error state"); + } +} + +int +http_body_is_final(const struct http_parser *parser) { + return parser->state == s_message_done; +} + +unsigned long +http_parser_version(void) { + return HTTP_PARSER_VERSION_MAJOR * 0x10000 | + HTTP_PARSER_VERSION_MINOR * 0x00100 | + HTTP_PARSER_VERSION_PATCH * 0x00001; +} + +void +http_parser_set_max_header_size(uint32_t size) { + max_header_size = size; +} diff --git a/src/3rdparty/http-parser/http_parser.h b/src/3rdparty/http-parser/http_parser.h new file mode 100644 index 00000000..880ed278 --- /dev/null +++ b/src/3rdparty/http-parser/http_parser.h @@ -0,0 +1,439 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef http_parser_h +#define http_parser_h +#ifdef __cplusplus +extern "C" { +#endif + +/* Also update SONAME in the Makefile whenever you change these. */ +#define HTTP_PARSER_VERSION_MAJOR 2 +#define HTTP_PARSER_VERSION_MINOR 9 +#define HTTP_PARSER_VERSION_PATCH 0 + +#include +#if defined(_WIN32) && !defined(__MINGW32__) && \ + (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) +#include +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run + * faster + */ +#ifndef HTTP_PARSER_STRICT +# define HTTP_PARSER_STRICT 1 +#endif + +/* Maximium header size allowed. If the macro is not defined + * before including this header then the default is used. To + * change the maximum header size, define the macro in the build + * environment (e.g. -DHTTP_MAX_HEADER_SIZE=). To remove + * the effective limit on the size of the header, define the macro + * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff) + */ +#ifndef HTTP_MAX_HEADER_SIZE +# define HTTP_MAX_HEADER_SIZE (80*1024) +#endif + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; + + +/* Callbacks should return non-zero to indicate an error. The parser will + * then halt execution. + * + * The one exception is on_headers_complete. In a HTTP_RESPONSE parser + * returning '1' from on_headers_complete will tell the parser that it + * should not expect a body. This is used when receiving a response to a + * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: + * chunked' headers that indicate the presence of a body. + * + * Returning `2` from on_headers_complete will tell parser that it should not + * expect neither a body nor any futher responses on this connection. This is + * useful for handling responses to a CONNECT request which may not contain + * `Upgrade` or `Connection: upgrade` headers. + * + * http_data_cb does not return data chunks. It will be called arbitrarily + * many times for each string. E.G. you might get 10 callbacks for "on_url" + * each providing just a few characters more data. + */ +typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); +typedef int (*http_cb) (http_parser*); + + +/* Status Codes */ +#define HTTP_STATUS_MAP(XX) \ + XX(100, CONTINUE, Continue) \ + XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \ + XX(102, PROCESSING, Processing) \ + XX(200, OK, OK) \ + XX(201, CREATED, Created) \ + XX(202, ACCEPTED, Accepted) \ + XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \ + XX(204, NO_CONTENT, No Content) \ + XX(205, RESET_CONTENT, Reset Content) \ + XX(206, PARTIAL_CONTENT, Partial Content) \ + XX(207, MULTI_STATUS, Multi-Status) \ + XX(208, ALREADY_REPORTED, Already Reported) \ + XX(226, IM_USED, IM Used) \ + XX(300, MULTIPLE_CHOICES, Multiple Choices) \ + XX(301, MOVED_PERMANENTLY, Moved Permanently) \ + XX(302, FOUND, Found) \ + XX(303, SEE_OTHER, See Other) \ + XX(304, NOT_MODIFIED, Not Modified) \ + XX(305, USE_PROXY, Use Proxy) \ + XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \ + XX(308, PERMANENT_REDIRECT, Permanent Redirect) \ + XX(400, BAD_REQUEST, Bad Request) \ + XX(401, UNAUTHORIZED, Unauthorized) \ + XX(402, PAYMENT_REQUIRED, Payment Required) \ + XX(403, FORBIDDEN, Forbidden) \ + XX(404, NOT_FOUND, Not Found) \ + XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \ + XX(406, NOT_ACCEPTABLE, Not Acceptable) \ + XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \ + XX(408, REQUEST_TIMEOUT, Request Timeout) \ + XX(409, CONFLICT, Conflict) \ + XX(410, GONE, Gone) \ + XX(411, LENGTH_REQUIRED, Length Required) \ + XX(412, PRECONDITION_FAILED, Precondition Failed) \ + XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \ + XX(414, URI_TOO_LONG, URI Too Long) \ + XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \ + XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \ + XX(417, EXPECTATION_FAILED, Expectation Failed) \ + XX(421, MISDIRECTED_REQUEST, Misdirected Request) \ + XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \ + XX(423, LOCKED, Locked) \ + XX(424, FAILED_DEPENDENCY, Failed Dependency) \ + XX(426, UPGRADE_REQUIRED, Upgrade Required) \ + XX(428, PRECONDITION_REQUIRED, Precondition Required) \ + XX(429, TOO_MANY_REQUESTS, Too Many Requests) \ + XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \ + XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \ + XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \ + XX(501, NOT_IMPLEMENTED, Not Implemented) \ + XX(502, BAD_GATEWAY, Bad Gateway) \ + XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \ + XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \ + XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \ + XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \ + XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \ + XX(508, LOOP_DETECTED, Loop Detected) \ + XX(510, NOT_EXTENDED, Not Extended) \ + XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \ + +enum http_status + { +#define XX(num, name, string) HTTP_STATUS_##name = num, + HTTP_STATUS_MAP(XX) +#undef XX + }; + + +/* Request Methods */ +#define HTTP_METHOD_MAP(XX) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + /* pathological */ \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + /* WebDAV */ \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + XX(16, BIND, BIND) \ + XX(17, REBIND, REBIND) \ + XX(18, UNBIND, UNBIND) \ + XX(19, ACL, ACL) \ + /* subversion */ \ + XX(20, REPORT, REPORT) \ + XX(21, MKACTIVITY, MKACTIVITY) \ + XX(22, CHECKOUT, CHECKOUT) \ + XX(23, MERGE, MERGE) \ + /* upnp */ \ + XX(24, MSEARCH, M-SEARCH) \ + XX(25, NOTIFY, NOTIFY) \ + XX(26, SUBSCRIBE, SUBSCRIBE) \ + XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ + /* RFC-5789 */ \ + XX(28, PATCH, PATCH) \ + XX(29, PURGE, PURGE) \ + /* CalDAV */ \ + XX(30, MKCALENDAR, MKCALENDAR) \ + /* RFC-2068, section 19.6.1.2 */ \ + XX(31, LINK, LINK) \ + XX(32, UNLINK, UNLINK) \ + /* icecast */ \ + XX(33, SOURCE, SOURCE) \ + +enum http_method + { +#define XX(num, name, string) HTTP_##name = num, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; + + +/* Flag values for http_parser.flags field */ +enum flags + { F_CHUNKED = 1 << 0 + , F_CONNECTION_KEEP_ALIVE = 1 << 1 + , F_CONNECTION_CLOSE = 1 << 2 + , F_CONNECTION_UPGRADE = 1 << 3 + , F_TRAILING = 1 << 4 + , F_UPGRADE = 1 << 5 + , F_SKIPBODY = 1 << 6 + , F_CONTENTLENGTH = 1 << 7 + }; + + +/* Map for errno-related constants + * + * The provided argument should be a macro that takes 2 arguments. + */ +#define HTTP_ERRNO_MAP(XX) \ + /* No error */ \ + XX(OK, "success") \ + \ + /* Callback-related errors */ \ + XX(CB_message_begin, "the on_message_begin callback failed") \ + XX(CB_url, "the on_url callback failed") \ + XX(CB_header_field, "the on_header_field callback failed") \ + XX(CB_header_value, "the on_header_value callback failed") \ + XX(CB_headers_complete, "the on_headers_complete callback failed") \ + XX(CB_body, "the on_body callback failed") \ + XX(CB_message_complete, "the on_message_complete callback failed") \ + XX(CB_status, "the on_status callback failed") \ + XX(CB_chunk_header, "the on_chunk_header callback failed") \ + XX(CB_chunk_complete, "the on_chunk_complete callback failed") \ + \ + /* Parsing-related errors */ \ + XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ + XX(HEADER_OVERFLOW, \ + "too many header bytes seen; overflow detected") \ + XX(CLOSED_CONNECTION, \ + "data received after completed connection: close message") \ + XX(INVALID_VERSION, "invalid HTTP version") \ + XX(INVALID_STATUS, "invalid HTTP status code") \ + XX(INVALID_METHOD, "invalid HTTP method") \ + XX(INVALID_URL, "invalid URL") \ + XX(INVALID_HOST, "invalid host") \ + XX(INVALID_PORT, "invalid port") \ + XX(INVALID_PATH, "invalid path") \ + XX(INVALID_QUERY_STRING, "invalid query string") \ + XX(INVALID_FRAGMENT, "invalid fragment") \ + XX(LF_EXPECTED, "LF character expected") \ + XX(INVALID_HEADER_TOKEN, "invalid character in header") \ + XX(INVALID_CONTENT_LENGTH, \ + "invalid character in content-length header") \ + XX(UNEXPECTED_CONTENT_LENGTH, \ + "unexpected content-length header") \ + XX(INVALID_CHUNK_SIZE, \ + "invalid character in chunk size header") \ + XX(INVALID_CONSTANT, "invalid constant string") \ + XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ + XX(STRICT, "strict mode assertion failed") \ + XX(PAUSED, "parser is paused") \ + XX(UNKNOWN, "an unknown error occurred") + + +/* Define HPE_* values for each errno value above */ +#define HTTP_ERRNO_GEN(n, s) HPE_##n, +enum http_errno { + HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) +}; +#undef HTTP_ERRNO_GEN + + +/* Get an http_errno value from an http_parser */ +#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) + + +struct http_parser { + /** PRIVATE **/ + unsigned int type : 2; /* enum http_parser_type */ + unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */ + unsigned int state : 7; /* enum state from http_parser.c */ + unsigned int header_state : 7; /* enum header_state from http_parser.c */ + unsigned int index : 7; /* index into current matcher */ + unsigned int lenient_http_headers : 1; + + uint32_t nread; /* # bytes read in various scenarios */ + uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ + + /** READ-ONLY **/ + unsigned short http_major; + unsigned short http_minor; + unsigned int status_code : 16; /* responses only */ + unsigned int method : 8; /* requests only */ + unsigned int http_errno : 7; + + /* 1 = Upgrade header was present and the parser has exited because of that. + * 0 = No upgrade header present. + * Should be checked when http_parser_execute() returns in addition to + * error checking. + */ + unsigned int upgrade : 1; + + /** PUBLIC **/ + void *data; /* A pointer to get hook to the "connection" or "socket" object */ +}; + + +struct http_parser_settings { + http_cb on_message_begin; + http_data_cb on_url; + http_data_cb on_status; + http_data_cb on_header_field; + http_data_cb on_header_value; + http_cb on_headers_complete; + http_data_cb on_body; + http_cb on_message_complete; + /* When on_chunk_header is called, the current chunk length is stored + * in parser->content_length. + */ + http_cb on_chunk_header; + http_cb on_chunk_complete; +}; + + +enum http_parser_url_fields + { UF_SCHEMA = 0 + , UF_HOST = 1 + , UF_PORT = 2 + , UF_PATH = 3 + , UF_QUERY = 4 + , UF_FRAGMENT = 5 + , UF_USERINFO = 6 + , UF_MAX = 7 + }; + + +/* Result structure for http_parser_parse_url(). + * + * Callers should index into field_data[] with UF_* values iff field_set + * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and + * because we probably have padding left over), we convert any port to + * a uint16_t. + */ +struct http_parser_url { + uint16_t field_set; /* Bitmask of (1 << UF_*) values */ + uint16_t port; /* Converted UF_PORT string */ + + struct { + uint16_t off; /* Offset into buffer in which field starts */ + uint16_t len; /* Length of run in buffer */ + } field_data[UF_MAX]; +}; + + +/* Returns the library version. Bits 16-23 contain the major version number, + * bits 8-15 the minor version number and bits 0-7 the patch level. + * Usage example: + * + * unsigned long version = http_parser_version(); + * unsigned major = (version >> 16) & 255; + * unsigned minor = (version >> 8) & 255; + * unsigned patch = version & 255; + * printf("http_parser v%u.%u.%u\n", major, minor, patch); + */ +unsigned long http_parser_version(void); + +void http_parser_init(http_parser *parser, enum http_parser_type type); + + +/* Initialize http_parser_settings members to 0 + */ +void http_parser_settings_init(http_parser_settings *settings); + + +/* Executes the parser. Returns number of parsed bytes. Sets + * `parser->http_errno` on error. */ +size_t http_parser_execute(http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len); + + +/* If http_should_keep_alive() in the on_headers_complete or + * on_message_complete callback returns 0, then this should be + * the last message on the connection. + * If you are the server, respond with the "Connection: close" header. + * If you are the client, close the connection. + */ +int http_should_keep_alive(const http_parser *parser); + +/* Returns a string version of the HTTP method. */ +const char *http_method_str(enum http_method m); + +/* Returns a string version of the HTTP status code. */ +const char *http_status_str(enum http_status s); + +/* Return a string name of the given error */ +const char *http_errno_name(enum http_errno err); + +/* Return a string description of the given error */ +const char *http_errno_description(enum http_errno err); + +/* Initialize all http_parser_url members to 0 */ +void http_parser_url_init(struct http_parser_url *u); + +/* Parse a URL; return nonzero on failure */ +int http_parser_parse_url(const char *buf, size_t buflen, + int is_connect, + struct http_parser_url *u); + +/* Pause or un-pause the parser; a nonzero value pauses */ +void http_parser_pause(http_parser *parser, int paused); + +/* Checks if this is the final chunk of the body. */ +int http_body_is_final(const http_parser *parser); + +/* Change the maximum header size provided at compile time. */ +void http_parser_set_max_header_size(uint32_t size); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/App.cpp b/src/App.cpp index db91bab5..38e335c2 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -31,11 +31,11 @@ #include "api/Api.h" #include "App.h" #include "base/io/Console.h" +#include "base/io/log/Log.h" #include "base/kernel/Signals.h" #include "common/cpu/Cpu.h" -#include "common/log/Log.h" #include "common/Platform.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "crypto/CryptoNight.h" #include "Mem.h" @@ -45,14 +45,8 @@ #include "workers/Workers.h" -#ifndef XMRIG_NO_HTTPD -# include "common/api/Httpd.h" -#endif - - xmrig::App::App(Process *process) : m_console(nullptr), - m_httpd(nullptr), m_signals(nullptr) { m_controller = new Controller(process); @@ -71,10 +65,6 @@ xmrig::App::~App() delete m_signals; delete m_console; delete m_controller; - -# ifndef XMRIG_NO_HTTPD - delete m_httpd; -# endif } @@ -98,24 +88,9 @@ int xmrig::App::exec() return 0; } -# ifndef XMRIG_NO_API - Api::start(m_controller); -# endif - -# ifndef XMRIG_NO_HTTPD - m_httpd = new Httpd( - m_controller->config()->apiPort(), - m_controller->config()->apiToken(), - m_controller->config()->isApiIPv6(), - m_controller->config()->isApiRestricted() - ); - - m_httpd->start(); -# endif - Workers::start(m_controller); - m_controller->network()->connect(); + m_controller->start(); const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); @@ -135,7 +110,7 @@ void xmrig::App::onConsoleCommand(char command) case 'p': case 'P': if (Workers::isEnabled()) { - LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume"); + LOG_INFO(YELLOW_BOLD("paused") ", press " MAGENTA_BOLD("r") " to resume"); Workers::setEnabled(false); } break; @@ -143,7 +118,7 @@ void xmrig::App::onConsoleCommand(char command) case 'r': case 'R': if (!Workers::isEnabled()) { - LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;32mresumed" : "resumed"); + LOG_INFO(GREEN_BOLD("resumed")); Workers::setEnabled(true); } break; @@ -185,14 +160,10 @@ void xmrig::App::onSignal(int signum) void xmrig::App::close() { -# ifndef XMRIG_NO_HTTPD - m_httpd->stop(); -# endif - m_signals->stop(); m_console->stop(); m_controller->stop(); Workers::stop(); - Log::release(); + Log::destroy(); } diff --git a/src/App.h b/src/App.h index 86f281e8..aa534aad 100644 --- a/src/App.h +++ b/src/App.h @@ -36,7 +36,6 @@ namespace xmrig { class Console; class Controller; -class Httpd; class Network; class Process; class Signals; @@ -60,7 +59,6 @@ private: Console *m_console; Controller *m_controller; - Httpd *m_httpd; Signals *m_signals; }; diff --git a/src/App_unix.cpp b/src/App_unix.cpp index 2b598c22..5149513c 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -30,8 +30,8 @@ #include "App.h" -#include "common/log/Log.h" -#include "core/Config.h" +#include "base/io/log/Log.h" +#include "core/config/Config.h" #include "core/Controller.h" diff --git a/src/App_win.cpp b/src/App_win.cpp index 57f57a6d..a41ed505 100644 --- a/src/App_win.cpp +++ b/src/App_win.cpp @@ -29,7 +29,7 @@ #include "App.h" #include "core/Controller.h" -#include "core/Config.h" +#include "core/config/Config.h" void xmrig::App::background() diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 833c200c..3506c2d1 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -28,13 +28,18 @@ #include -#include "common/log/Log.h" +#include "base/io/log/Log.h" #include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "Mem.h" +#if defined(__APPLE__) +# include +#endif + + void Mem::init(bool enabled) { m_enabled = enabled; diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 27c1348b..e7afb5d3 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -30,7 +30,7 @@ #include -#include "common/log/Log.h" +#include "base/io/log/Log.h" #include "common/utils/mm_malloc.h" #include "common/xmrig.h" #include "crypto/CryptoNight.h" @@ -67,11 +67,11 @@ static BOOL SetLockPagesPrivilege() { tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; - if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &(tp.Privileges[0].Luid)) != TRUE) { + if (LookupPrivilegeValue(nullptr, SE_LOCK_MEMORY_NAME, &(tp.Privileges[0].Luid)) != TRUE) { return FALSE; } - BOOL rc = AdjustTokenPrivileges(token, FALSE, (PTOKEN_PRIVILEGES) &tp, 0, NULL, NULL); + BOOL rc = AdjustTokenPrivileges(token, FALSE, (PTOKEN_PRIVILEGES) &tp, 0, nullptr, nullptr); if (rc != TRUE || GetLastError() != ERROR_SUCCESS) { return FALSE; } @@ -95,12 +95,12 @@ static LSA_UNICODE_STRING StringToLsaUnicodeString(LPCTSTR string) { static BOOL ObtainLockPagesPrivilege() { HANDLE token; - PTOKEN_USER user = NULL; + PTOKEN_USER user = nullptr; if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) == TRUE) { DWORD size = 0; - GetTokenInformation(token, TokenUser, NULL, 0, &size); + GetTokenInformation(token, TokenUser, nullptr, 0, &size); if (size) { user = (PTOKEN_USER) LocalAlloc(LPTR, size); } @@ -118,7 +118,7 @@ static BOOL ObtainLockPagesPrivilege() { ZeroMemory(&attributes, sizeof(attributes)); BOOL result = FALSE; - if (LsaOpenPolicy(NULL, &attributes, POLICY_ALL_ACCESS, &handle) == 0) { + if (LsaOpenPolicy(nullptr, &attributes, POLICY_ALL_ACCESS, &handle) == 0) { LSA_UNICODE_STRING str = StringToLsaUnicodeString(_T(SE_LOCK_MEMORY_NAME)); if (LsaAddAccountRights(handle, user->User.Sid, &str, 1) == 0) { @@ -187,7 +187,7 @@ void Mem::release(MemInfo &info) void *Mem::allocateExecutableMemory(size_t size) { - return VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); + return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); } diff --git a/src/Summary.cpp b/src/Summary.cpp index 78d624df..697e2373 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -28,10 +28,10 @@ #include +#include "base/io/log/Log.h" #include "base/net/stratum/Pool.h" #include "common/cpu/Cpu.h" -#include "common/log/Log.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "crypto/Asm.h" #include "Mem.h" @@ -41,62 +41,44 @@ #ifndef XMRIG_NO_ASM static const char *coloredAsmNames[] = { - "\x1B[1;31mnone\x1B[0m", + RED_BOLD("none"), "auto", - "\x1B[1;32mintel\x1B[0m", - "\x1B[1;32mryzen\x1B[0m", - "\x1B[1;32mbulldozer\x1B[0m" + GREEN_BOLD("intel"), + GREEN_BOLD("ryzen"), + GREEN_BOLD("bulldozer") }; -inline static const char *asmName(xmrig::Assembly assembly, bool colors) +inline static const char *asmName(xmrig::Assembly assembly) { - return colors ? coloredAsmNames[assembly] : xmrig::Asm::toString(assembly); + return coloredAsmNames[assembly]; } #endif -static void print_memory(xmrig::Config *config) { +static void print_memory(xmrig::Config *) { # ifdef _WIN32 - if (config->isColors()) { - xmrig::Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s", - "HUGE PAGES", Mem::isHugepagesAvailable() ? "\x1B[1;32mavailable" : "\x1B[01;31munavailable"); - } - else { - xmrig::Log::i()->text(" * %-13s%s", "HUGE PAGES", Mem::isHugepagesAvailable() ? "available" : "unavailable"); - } + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") "%s", + "HUGE PAGES", Mem::isHugepagesAvailable() ? GREEN_BOLD("available") : RED_BOLD("unavailable")); # endif } -static void print_cpu(xmrig::Config *config) +static void print_cpu(xmrig::Config *) { using namespace xmrig; - if (config->isColors()) { - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%d)") " %sx64 %sAES %sAVX2", - "CPU", - Cpu::info()->brand(), - Cpu::info()->sockets(), - Cpu::info()->isX64() ? "\x1B[1;32m" : "\x1B[1;31m-", - Cpu::info()->hasAES() ? "\x1B[1;32m" : "\x1B[1;31m-", - Cpu::info()->hasAVX2() ? "\x1B[1;32m" : "\x1B[1;31m-"); -# ifndef XMRIG_NO_LIBCPUID - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); -# endif - } - else { - Log::i()->text(" * %-13s%s (%d) %sx64 %sAES %sAVX2", - "CPU", - Cpu::info()->brand(), - Cpu::info()->sockets(), - Cpu::info()->isX64() ? "" : "-", - Cpu::info()->hasAES() ? "" : "-", - Cpu::info()->hasAVX2() ? "" : "-"); -# ifndef XMRIG_NO_LIBCPUID - Log::i()->text(" * %-13s%.1f MB/%.1f MB", "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); -# endif - } + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s (%d)") " %sx64 %sAES %sAVX2", + "CPU", + Cpu::info()->brand(), + Cpu::info()->sockets(), + Cpu::info()->isX64() ? GREEN_BOLD_S : RED_BOLD_S "-", + Cpu::info()->hasAES() ? GREEN_BOLD_S : RED_BOLD_S "-", + Cpu::info()->hasAVX2() ? GREEN_BOLD_S : RED_BOLD_S "-" + ); +# ifndef XMRIG_NO_LIBCPUID + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%.1f MB/%.1f MB"), "CPU L2/L3", Cpu::info()->L2() / 1024.0, Cpu::info()->L3() / 1024.0); +# endif } @@ -108,49 +90,48 @@ static void print_threads(xmrig::Config *config) snprintf(buf, sizeof buf, ", affinity=0x%" PRIX64, config->affinity()); } - xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, av=%d, %sdonate=%d%%") WHITE_BOLD("%s") - : " * %-13s%d, %s, av=%d, %sdonate=%d%%%s", - "THREADS", - config->threadsCount(), - config->algorithm().name(), - config->algoVariant(), - config->isColors() && config->pools().donateLevel() == 0 ? "\x1B[1;31m" : "", - config->pools().donateLevel(), - buf); + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, av=%d, %sdonate=%d%%") WHITE_BOLD("%s"), + "THREADS", + config->threadsCount(), + config->algorithm().shortName(), + config->algoVariant(), + config->pools().donateLevel() == 0 ? RED_BOLD_S : "", + config->pools().donateLevel(), + buf + ); } else { - xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, %sdonate=%d%%") - : " * %-13s%d, %s, %sdonate=%d%%", - "THREADS", - config->threadsCount(), - config->algorithm().name(), - config->isColors() && config->pools().donateLevel() == 0 ? "\x1B[1;31m" : "", - config->pools().donateLevel()); + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", %s, %sdonate=%d%%"), + "THREADS", + config->threadsCount(), + config->algorithm().shortName(), + config->pools().donateLevel() == 0 ? RED_BOLD_S : "", + config->pools().donateLevel() + ); } # ifndef XMRIG_NO_ASM if (config->assembly() == xmrig::ASM_AUTO) { const xmrig::Assembly assembly = xmrig::Cpu::info()->assembly(); - xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s") - : " * %-13sauto:%s", "ASSEMBLY", asmName(assembly, config->isColors())); + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s"), "ASSEMBLY", asmName(assembly)); } else { - xmrig::Log::i()->text(config->isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s") : " * %-13s%s", "ASSEMBLY", asmName(config->assembly(), config->isColors())); + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s"), "ASSEMBLY", asmName(config->assembly())); } # endif } -static void print_commands(xmrig::Config *config) +static void print_commands(xmrig::Config *) { - if (config->isColors()) { - xmrig::Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") + if (xmrig::Log::colors) { + xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("COMMANDS ") MAGENTA_BOLD("h") WHITE_BOLD("ashrate, ") MAGENTA_BOLD("p") WHITE_BOLD("ause, ") MAGENTA_BOLD("r") WHITE_BOLD("esume")); } else { - xmrig::Log::i()->text(" * COMMANDS 'h' hashrate, 'p' pause, 'r' resume"); + xmrig::Log::print(" * COMMANDS 'h' hashrate, 'p' pause, 'r' resume"); } } @@ -161,8 +142,7 @@ void Summary::print(xmrig::Controller *controller) print_memory(controller->config()); print_cpu(controller->config()); print_threads(controller->config()); - controller->config()->printPools(); - controller->config()->printAPI(); + controller->config()->pools().print(); print_commands(controller->config()); } diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 2abdf4be..fc82ac9c 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -22,52 +22,166 @@ * along with this program. If not, see . */ -#include + +#include +#ifndef _WIN32 +# include +#endif + + +#include "3rdparty/http-parser/http_parser.h" #include "api/Api.h" -#include "api/ApiRouter.h" -#include "common/api/HttpReply.h" -#include "common/api/HttpRequest.h" +#include "api/interfaces/IApiListener.h" +#include "api/requests/HttpApiRequest.h" +#include "api/v1/ApiRouter.h" +#include "base/tools/Buffer.h" +#include "common/crypto/keccak.h" +#include "core/config/Config.h" +#include "core/Controller.h" +#include "version.h" -ApiRouter *Api::m_router = nullptr; +#ifdef XMRIG_FEATURE_HTTP +# include "api/Httpd.h" +#endif -bool Api::start(xmrig::Controller *controller) +xmrig::Api::Api(Controller *controller) : + m_id(), + m_workerId(), + m_controller(controller), + m_httpd(nullptr) { - m_router = new ApiRouter(controller); + controller->addListener(this); - return true; + genId(m_controller->config()->apiId()); + + m_v1 = new ApiRouter(controller); + addListener(m_v1); } -void Api::release() +xmrig::Api::~Api() { - delete m_router; + delete m_v1; + +# ifdef XMRIG_FEATURE_HTTP + delete m_httpd; +# endif } -void Api::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) +void xmrig::Api::request(const HttpRequest &req) { - if (!m_router) { - reply.status = 500; + HttpApiRequest request(req, m_controller->config()->http().isRestricted()); + + exec(request); +} + + +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()); + } + + if (config->apiWorkerId() != previousConfig->apiWorkerId()) { + genWorkerId(config->apiWorkerId()); + } +} + + +void xmrig::Api::exec(IApiRequest &request) +{ + using namespace rapidjson; + + if (request.method() == IApiRequest::METHOD_GET && (request.url() == "/1/summary" || request.url() == "/api.json")) { + request.accept(); + request.reply().AddMember("id", StringRef(m_id), request.doc().GetAllocator()); + request.reply().AddMember("worker_id", StringRef(m_workerId), request.doc().GetAllocator());; + } + + for (IApiListener *listener : m_listeners) { + listener->onRequest(request); + + if (request.isDone()) { + return; + } + } + + request.done(request.isNew() ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK); +} + + +void xmrig::Api::genId(const String &id) +{ + memset(m_id, 0, sizeof(m_id)); + + if (id.size() > 0) { + strncpy(m_id, id.data(), sizeof(m_id) - 1); return; } - if (req.method() == xmrig::HttpRequest::Get) { - return m_router->get(req, reply); - } + uv_interface_address_t *interfaces; + int count = 0; - m_router->exec(req, reply); -} - - -void Api::tick(const xmrig::NetworkState &network) -{ - if (!m_router) { + if (uv_interface_addresses(&interfaces, &count) < 0) { return; } - m_router->tick(network); + 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(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); + } } diff --git a/src/api/Api.h b/src/api/Api.h index caee887a..d76e3105 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -26,32 +26,56 @@ #define XMRIG_API_H -#include +#include -class ApiRouter; -class Hashrate; +#include "base/kernel/interfaces/IControllerListener.h" namespace xmrig { - class Controller; - class HttpReply; - class HttpRequest; - class NetworkState; -} -class Api +class ApiRouter; +class Controller; +class Httpd; +class HttpRequest; +class IApiListener; +class IApiRequest; +class String; + + +class Api : public IControllerListener { public: - static bool start(xmrig::Controller *controller); - static void release(); + Api(Controller *controller); + ~Api() override; - static void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - static void tick(const xmrig::NetworkState &results); + inline const char *id() const { return m_id; } + inline const char *workerId() const { return m_workerId; } + inline void addListener(IApiListener *listener) { m_listeners.push_back(listener); } + + void request(const HttpRequest &req); + void start(); + void stop(); + +protected: + void onConfigChanged(Config *config, Config *previousConfig) override; private: - static ApiRouter *m_router; + void exec(IApiRequest &request); + void genId(const String &id); + void genWorkerId(const String &id); + + ApiRouter *m_v1; + char m_id[32]; + char m_workerId[128]; + Controller *m_controller; + Httpd *m_httpd; + std::vector m_listeners; }; + +} // namespace xmrig + + #endif /* XMRIG_API_H */ diff --git a/src/api/ApiRouter.cpp b/src/api/ApiRouter.cpp deleted file mode 100644 index 69ab153c..00000000 --- a/src/api/ApiRouter.cpp +++ /dev/null @@ -1,339 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - -#include -#include -#include - -#if _WIN32 -# include "winsock2.h" -#else -# include "unistd.h" -#endif - - -#include "api/ApiRouter.h" -#include "base/tools/Buffer.h" -#include "common/api/HttpReply.h" -#include "common/api/HttpRequest.h" -#include "common/cpu/Cpu.h" -#include "common/crypto/keccak.h" -#include "common/Platform.h" -#include "core/Config.h" -#include "core/Controller.h" -#include "interfaces/IThread.h" -#include "rapidjson/document.h" -#include "rapidjson/prettywriter.h" -#include "rapidjson/stringbuffer.h" -#include "version.h" -#include "workers/Hashrate.h" -#include "workers/Workers.h" - - -static inline double normalize(double d) -{ - if (!isnormal(d)) { - return 0.0; - } - - return floor(d * 100.0) / 100.0; -} - - -ApiRouter::ApiRouter(xmrig::Controller *controller) : - m_controller(controller) -{ - memset(m_workerId, 0, sizeof(m_workerId)); - - setWorkerId(controller->config()->apiWorkerId()); - genId(controller->config()->apiId()); -} - - -ApiRouter::~ApiRouter() -{ -} - - -void ApiRouter::ApiRouter::get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const -{ - rapidjson::Document doc; - - if (req.match("/1/config")) { - if (req.isRestricted()) { - reply.status = 403; - return; - } - - m_controller->config()->getJSON(doc); - - return finalize(reply, doc); - } - - if (req.match("/1/threads")) { - getThreads(doc); - - return finalize(reply, doc); - } - - doc.SetObject(); - - getIdentify(doc); - getMiner(doc); - getHashrate(doc); - getResults(doc); - getConnection(doc); - - return finalize(reply, doc); -} - - -void ApiRouter::exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) -{ - if (req.method() == xmrig::HttpRequest::Put && req.match("/1/config")) { - m_controller->config()->reload(req.body()); - return; - } - - reply.status = 404; -} - - -void ApiRouter::tick(const xmrig::NetworkState &network) -{ - m_network = network; -} - - -void ApiRouter::onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) -{ - updateWorkerId(config->apiWorkerId(), previousConfig->apiWorkerId()); -} - - -void ApiRouter::finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const -{ - rapidjson::StringBuffer buffer(nullptr, 4096); - rapidjson::PrettyWriter writer(buffer); - writer.SetMaxDecimalPlaces(10); - doc.Accept(writer); - - reply.status = 200; - reply.buf = strdup(buffer.GetString()); - reply.size = buffer.GetSize(); -} - - -void ApiRouter::genId(const char *id) -{ - memset(m_id, 0, sizeof(m_id)); - - if (id && strlen(id) > 0) { - strncpy(m_id, id, sizeof(m_id) - 1); - return; - } - - uv_interface_address_t *interfaces; - int count = 0; - - if (uv_interface_addresses(&interfaces, &count) < 0) { - return; - } - - 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(m_controller->config()->apiPort()); - - 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 ApiRouter::getConnection(rapidjson::Document &doc) const -{ - auto &allocator = doc.GetAllocator(); - - rapidjson::Value connection(rapidjson::kObjectType); - connection.AddMember("pool", rapidjson::StringRef(m_network.pool), allocator); - connection.AddMember("uptime", m_network.connectionTime(), allocator); - connection.AddMember("ping", m_network.latency(), allocator); - connection.AddMember("failures", m_network.failures, allocator); - connection.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator); - - doc.AddMember("connection", connection, allocator); -} - - -void ApiRouter::getHashrate(rapidjson::Document &doc) const -{ - auto &allocator = doc.GetAllocator(); - - rapidjson::Value hashrate(rapidjson::kObjectType); - rapidjson::Value total(rapidjson::kArrayType); - rapidjson::Value threads(rapidjson::kArrayType); - - const Hashrate *hr = Workers::hashrate(); - - total.PushBack(normalize(hr->calc(Hashrate::ShortInterval)), allocator); - total.PushBack(normalize(hr->calc(Hashrate::MediumInterval)), allocator); - total.PushBack(normalize(hr->calc(Hashrate::LargeInterval)), allocator); - - for (size_t i = 0; i < Workers::threads(); i++) { - rapidjson::Value thread(rapidjson::kArrayType); - thread.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); - thread.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); - thread.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); - - threads.PushBack(thread, allocator); - } - - hashrate.AddMember("total", total, allocator); - hashrate.AddMember("highest", normalize(hr->highest()), allocator); - hashrate.AddMember("threads", threads, allocator); - doc.AddMember("hashrate", hashrate, allocator); -} - - -void ApiRouter::getIdentify(rapidjson::Document &doc) const -{ - doc.AddMember("id", rapidjson::StringRef(m_id), doc.GetAllocator()); - doc.AddMember("worker_id", rapidjson::StringRef(m_workerId), doc.GetAllocator()); -} - - -void ApiRouter::getMiner(rapidjson::Document &doc) const -{ - using namespace xmrig; - auto &allocator = doc.GetAllocator(); - - rapidjson::Value cpu(rapidjson::kObjectType); - cpu.AddMember("brand", rapidjson::StringRef(Cpu::info()->brand()), allocator); - cpu.AddMember("aes", Cpu::info()->hasAES(), allocator); - cpu.AddMember("x64", Cpu::info()->isX64(), allocator); - cpu.AddMember("sockets", Cpu::info()->sockets(), allocator); - - doc.AddMember("version", APP_VERSION, allocator); - doc.AddMember("kind", APP_KIND, allocator); - doc.AddMember("ua", rapidjson::StringRef(Platform::userAgent()), allocator); - doc.AddMember("cpu", cpu, allocator); - doc.AddMember("algo", rapidjson::StringRef(m_controller->config()->algorithm().name()), allocator); - doc.AddMember("hugepages", Workers::hugePages() > 0, allocator); - doc.AddMember("donate_level", m_controller->config()->pools().donateLevel(), allocator); -} - - -void ApiRouter::getResults(rapidjson::Document &doc) const -{ - auto &allocator = doc.GetAllocator(); - - rapidjson::Value results(rapidjson::kObjectType); - - results.AddMember("diff_current", m_network.diff, allocator); - results.AddMember("shares_good", m_network.accepted, allocator); - results.AddMember("shares_total", m_network.accepted + m_network.rejected, allocator); - results.AddMember("avg_time", m_network.avgTime(), allocator); - results.AddMember("hashes_total", m_network.total, allocator); - - rapidjson::Value best(rapidjson::kArrayType); - for (size_t i = 0; i < m_network.topDiff.size(); ++i) { - best.PushBack(m_network.topDiff[i], allocator); - } - - results.AddMember("best", best, allocator); - results.AddMember("error_log", rapidjson::Value(rapidjson::kArrayType), allocator); - - doc.AddMember("results", results, allocator); -} - - -void ApiRouter::getThreads(rapidjson::Document &doc) const -{ - doc.SetObject(); - auto &allocator = doc.GetAllocator(); - const Hashrate *hr = Workers::hashrate(); - - Workers::threadsSummary(doc); - - const std::vector &threads = m_controller->config()->threads(); - rapidjson::Value list(rapidjson::kArrayType); - - size_t i = 0; - for (const xmrig::IThread *thread : threads) { - rapidjson::Value value = thread->toAPI(doc); - - rapidjson::Value hashrate(rapidjson::kArrayType); - hashrate.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); - hashrate.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); - hashrate.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); - - i++; - - value.AddMember("hashrate", hashrate, allocator); - list.PushBack(value, allocator); - } - - doc.AddMember("threads", list, allocator); -} - - -void ApiRouter::setWorkerId(const char *id) -{ - memset(m_workerId, 0, sizeof(m_workerId)); - - if (id && strlen(id) > 0) { - strncpy(m_workerId, id, sizeof(m_workerId) - 1); - } - else { - gethostname(m_workerId, sizeof(m_workerId) - 1); - } -} - - -void ApiRouter::updateWorkerId(const char *id, const char *previousId) -{ - if (id == previousId) { - return; - } - - if (id != nullptr && previousId != nullptr && strcmp(id, previousId) == 0) { - return; - } - - setWorkerId(id); -} diff --git a/src/api/ApiRouter.h b/src/api/ApiRouter.h deleted file mode 100644 index a92173ce..00000000 --- a/src/api/ApiRouter.h +++ /dev/null @@ -1,76 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - -#ifndef XMRIG_APIROUTER_H -#define XMRIG_APIROUTER_H - - -#include "api/NetworkState.h" -#include "common/interfaces/IControllerListener.h" -#include "rapidjson/fwd.h" - - -class Hashrate; - - -namespace xmrig { - class Controller; - class HttpReply; - class HttpRequest; -} - - -class ApiRouter : public xmrig::IControllerListener -{ -public: - ApiRouter(xmrig::Controller *controller); - ~ApiRouter() override; - - void get(const xmrig::HttpRequest &req, xmrig::HttpReply &reply) const; - void exec(const xmrig::HttpRequest &req, xmrig::HttpReply &reply); - - void tick(const xmrig::NetworkState &results); - -protected: - void onConfigChanged(xmrig::Config *config, xmrig::Config *previousConfig) override; - -private: - void finalize(xmrig::HttpReply &reply, rapidjson::Document &doc) const; - void genId(const char *id); - void getConnection(rapidjson::Document &doc) const; - void getHashrate(rapidjson::Document &doc) const; - void getIdentify(rapidjson::Document &doc) const; - void getMiner(rapidjson::Document &doc) const; - void getResults(rapidjson::Document &doc) const; - void getThreads(rapidjson::Document &doc) const; - void setWorkerId(const char *id); - void updateWorkerId(const char *id, const char *previousId); - - char m_id[32]; - char m_workerId[128]; - xmrig::NetworkState m_network; - xmrig::Controller *m_controller; -}; - -#endif /* XMRIG_APIROUTER_H */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp new file mode 100644 index 00000000..726f75e0 --- /dev/null +++ b/src/api/Httpd.cpp @@ -0,0 +1,193 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include "3rdparty/http-parser/http_parser.h" +#include "api/Api.h" +#include "api/Httpd.h" +#include "base/io/log/Log.h" +#include "base/net/http/HttpApiResponse.h" +#include "base/net/http/HttpRequest.h" +#include "base/net/http/HttpServer.h" +#include "base/net/tools/TcpServer.h" +#include "core/config/Config.h" +#include "core/Controller.h" + + +namespace xmrig { + +static const char *kAuthorization = "authorization"; +static const char *kContentType = "content-type"; + +#ifdef _WIN32 +static const char *favicon = nullptr; +static size_t faviconSize = 0; +#endif + +} // namespace xmrig + + +xmrig::Httpd::Httpd(Controller *controller) : + m_controller(controller), + m_http(nullptr), + m_server(nullptr), + m_port(0) +{ + controller->addListener(this); +} + + +xmrig::Httpd::~Httpd() +{ +} + + +bool xmrig::Httpd::start() +{ + const Http &config = m_controller->config()->http(); + + 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(rc); + +# ifdef _WIN32 + HRSRC src = FindResource(nullptr, MAKEINTRESOURCE(1), RT_ICON); + if (src != nullptr) { + HGLOBAL res = LoadResource(nullptr, src); + if (res != nullptr) { + favicon = static_cast(LockResource(res)); + faviconSize = SizeofResource(nullptr, src); + } + } +# endif + + 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(); +} + + +void xmrig::Httpd::onHttpRequest(const HttpRequest &req) +{ + if (req.method == HTTP_OPTIONS) { + return HttpApiResponse(req.id()).end(); + } + + if (req.method == HTTP_GET && req.url == "/favicon.ico") { +# ifdef _WIN32 + if (favicon != nullptr) { + HttpResponse response(req.id()); + response.setHeader("Content-Type", "image/x-icon"); + + return response.end(favicon, faviconSize); + } +# endif + + return HttpResponse(req.id(), 404).end(); + } + + if (req.method > 4) { + return HttpApiResponse(req.id(), HTTP_STATUS_METHOD_NOT_ALLOWED).end(); + } + + const int status = auth(req); + if (status != HTTP_STATUS_OK) { + return HttpApiResponse(req.id(), status).end(); + } + + if (req.method != HTTP_GET) { + if (m_controller->config()->http().isRestricted()) { + return HttpApiResponse(req.id(), HTTP_STATUS_FORBIDDEN).end(); + } + + if (!req.headers.count(kContentType) || req.headers.at(kContentType) != "application/json") { + return HttpApiResponse(req.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end(); + } + } + + m_controller->api()->request(req); +} + + +int xmrig::Httpd::auth(const HttpRequest &req) const +{ + const Http &config = m_controller->config()->http(); + + 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; + } + + return strncmp(config.token().data(), token.c_str() + 7, config.token().size()) == 0 ? HTTP_STATUS_OK : HTTP_STATUS_FORBIDDEN; +} diff --git a/src/common/api/Httpd.h b/src/api/Httpd.h similarity index 63% rename from src/common/api/Httpd.h rename to src/api/Httpd.h index f0b6ce87..99ee2e88 100644 --- a/src/common/api/Httpd.h +++ b/src/api/Httpd.h @@ -26,52 +26,41 @@ #define XMRIG_HTTPD_H -#include +#include -#include "base/kernel/interfaces/ITimerListener.h" - - -struct MHD_Connection; -struct MHD_Daemon; -struct MHD_Response; +#include "base/kernel/interfaces/IControllerListener.h" +#include "base/kernel/interfaces/IHttpListener.h" namespace xmrig { -class HttpRequest; -class Timer; +class Controller; +class HttpServer; +class TcpServer; -class Httpd : public ITimerListener +class Httpd : public IControllerListener, public IHttpListener { public: - Httpd(int port, const char *accessToken, bool IPv6, bool restricted); + Httpd(Controller *controller); ~Httpd() override; bool start(); void stop(); protected: - void onTimer(const Timer *) override { run(); } + void onConfigChanged(Config *config, Config *previousConfig) override; + void onHttpRequest(const HttpRequest &req) override; private: - constexpr static const int kIdleInterval = 200; - constexpr static const int kActiveInterval = 25; + int auth(const HttpRequest &req) const; - int process(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); - - bool m_idle; - bool m_IPv6; - bool m_restricted; - const char *m_accessToken; - const int m_port; - MHD_Daemon *m_daemon; - Timer *m_timer; + Controller *m_controller; + HttpServer *m_http; + TcpServer *m_server; + uint16_t m_port; }; diff --git a/src/core/ConfigCreator.h b/src/api/interfaces/IApiListener.h similarity index 78% rename from src/core/ConfigCreator.h rename to src/api/interfaces/IApiListener.h index 054eb78c..7897e375 100644 --- a/src/core/ConfigCreator.h +++ b/src/api/interfaces/IApiListener.h @@ -20,31 +20,26 @@ * along with this program. If not, see . */ -#ifndef __CONFIGCREATOR_H__ -#define __CONFIGCREATOR_H__ - - -#include "common/interfaces/IConfigCreator.h" -#include "core/Config.h" +#ifndef XMRIG_IAPILISTENER_H +#define XMRIG_IAPILISTENER_H namespace xmrig { -class IConfig; +class IApiRequest; -class ConfigCreator : public IConfigCreator +class IApiListener { public: - inline IConfig *create() const override - { - return new Config(); - } + virtual ~IApiListener() = default; + + virtual void onRequest(IApiRequest &request) = 0; }; } /* namespace xmrig */ -#endif // __CONFIGCREATOR_H__ +#endif // XMRIG_IAPILISTENER_H diff --git a/src/api/interfaces/IApiRequest.h b/src/api/interfaces/IApiRequest.h new file mode 100644 index 00000000..2c2f5634 --- /dev/null +++ b/src/api/interfaces/IApiRequest.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * 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 . + */ + +#ifndef XMRIG_IAPIREQUEST_H +#define XMRIG_IAPIREQUEST_H + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class String; + + +class IApiRequest +{ +public: + enum Method { + METHOD_DELETE, + METHOD_GET, + METHOD_HEAD, + METHOD_POST, + METHOD_PUT + }; + + + enum Source { + SOURCE_HTTP + }; + + + virtual ~IApiRequest() = default; + + virtual bool isDone() const = 0; + virtual bool isNew() const = 0; + virtual bool isRestricted() const = 0; + virtual const rapidjson::Value &json() const = 0; + virtual const String &url() const = 0; + virtual Method method() const = 0; + virtual rapidjson::Document &doc() = 0; + virtual rapidjson::Value &reply() = 0; + virtual Source source() const = 0; + virtual void accept() = 0; + virtual void done(int status) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // XMRIG_IAPIREQUEST_H diff --git a/src/common/api/HttpReply.h b/src/api/requests/ApiRequest.cpp similarity index 75% rename from src/common/api/HttpReply.h rename to src/api/requests/ApiRequest.cpp index 6a6cb802..c092a334 100644 --- a/src/common/api/HttpReply.h +++ b/src/api/requests/ApiRequest.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -22,32 +22,18 @@ * along with this program. If not, see . */ -#ifndef __HTTPREPLY_H__ -#define __HTTPREPLY_H__ + +#include "api/requests/ApiRequest.h" -#include - - -namespace xmrig { - - -class HttpReply +xmrig::ApiRequest::ApiRequest(Source source, bool restricted) : + m_restricted(restricted), + m_source(source), + m_state(STATE_NEW) { -public: - HttpReply() : - buf(nullptr), - status(200), - size(0) - {} - - char *buf; - int status; - size_t size; -}; +} -} /* namespace xmrig */ - - -#endif /* __HTTPREPLY_H__ */ +xmrig::ApiRequest::~ApiRequest() +{ +} diff --git a/src/api/requests/ApiRequest.h b/src/api/requests/ApiRequest.h new file mode 100644 index 00000000..1754aa9c --- /dev/null +++ b/src/api/requests/ApiRequest.h @@ -0,0 +1,67 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_APIREQUEST_H +#define XMRIG_APIREQUEST_H + + +#include "api/interfaces/IApiRequest.h" + + +namespace xmrig { + + +class ApiRequest : public IApiRequest +{ +public: + ApiRequest(Source source, bool restricted); + ~ApiRequest() override; + +protected: + inline bool isDone() const override { return m_state == STATE_DONE; } + inline bool isNew() const override { return m_state == STATE_NEW; } + inline bool isRestricted() const override { return m_restricted; } + inline Source source() const override { return m_source; } + inline void accept() override { m_state = STATE_ACCEPTED; } + inline void done(int) override { m_state = STATE_DONE; } + +private: + enum State { + STATE_NEW, + STATE_ACCEPTED, + STATE_DONE + }; + + bool m_restricted; + Source m_source; + State m_state; +}; + + +} // namespace xmrig + + +#endif // XMRIG_APIREQUEST_H + diff --git a/src/common/log/BasicLog.cpp b/src/api/requests/HttpApiRequest.cpp similarity index 52% rename from src/common/log/BasicLog.cpp rename to src/api/requests/HttpApiRequest.cpp index b048dd6e..a61dde8d 100644 --- a/src/common/log/BasicLog.cpp +++ b/src/api/requests/HttpApiRequest.cpp @@ -23,68 +23,54 @@ */ -#include -#include -#include -#include -#include - -#ifdef WIN32 -# include -# include -#endif +#include "api/requests/HttpApiRequest.h" +#include "base/net/http/HttpRequest.h" +#include "rapidjson/error/en.h" -#include "common/log/BasicLog.h" -#include "common/log/Log.h" - - -xmrig::BasicLog::BasicLog() +xmrig::HttpApiRequest::HttpApiRequest(const HttpRequest &req, bool restricted) : + ApiRequest(SOURCE_HTTP, restricted), + m_parsed(false), + m_req(req), + m_res(req.id()), + m_url(req.url.c_str()) { } -void xmrig::BasicLog::message(Level level, const char* fmt, va_list args) +const rapidjson::Value &xmrig::HttpApiRequest::json() const { - time_t now = time(nullptr); - tm stime; - -# ifdef _WIN32 - localtime_s(&stime, &now); -# else - localtime_r(&now, &stime); -# endif - - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec, - Log::colorByLevel(level, false), - fmt, - Log::endl(false) - ); - - print(args); + return m_body; } -void xmrig::BasicLog::text(const char* fmt, va_list args) +xmrig::IApiRequest::Method xmrig::HttpApiRequest::method() const { - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(false)); - - print(args); + return static_cast(m_req.method); } -void xmrig::BasicLog::print(va_list args) +void xmrig::HttpApiRequest::accept() { - if (vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args) <= 0) { - return; + using namespace rapidjson; + + ApiRequest::accept(); + + if (!m_parsed && !m_req.body.empty()) { + m_parsed = true; + m_body.Parse(m_req.body.c_str()); + + if (m_body.HasParseError()) { + reply().AddMember("error", StringRef(GetParseError_En(m_body.GetParseError())), doc().GetAllocator());; + } } - - fputs(m_buf, stdout); - fflush(stdout); +} + + +void xmrig::HttpApiRequest::done(int status) +{ + ApiRequest::done(status); + + m_res.setStatus(status); + m_res.end(); } diff --git a/src/api/requests/HttpApiRequest.h b/src/api/requests/HttpApiRequest.h new file mode 100644 index 00000000..26f26f39 --- /dev/null +++ b/src/api/requests/HttpApiRequest.h @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTPAPIREQUEST_H +#define XMRIG_HTTPAPIREQUEST_H + + +#include "api/requests/ApiRequest.h" +#include "base/net/http/HttpApiResponse.h" +#include "base/tools/String.h" + + +namespace xmrig { + + +class HttpRequest; + + +class HttpApiRequest : public ApiRequest +{ +public: + HttpApiRequest(const HttpRequest &req, bool restricted); + +protected: + inline rapidjson::Document &doc() override { return m_res.doc(); } + inline rapidjson::Value &reply() override { return m_res.doc(); } + inline const String &url() const override { return m_url; } + + const rapidjson::Value &json() const override; + Method method() const override; + void accept() override; + void done(int status) override; + +private: + bool m_parsed; + const HttpRequest &m_req; + HttpApiResponse m_res; + rapidjson::Document m_body; + String m_url; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPAPIREQUEST_H + diff --git a/src/api/v1/ApiRouter.cpp b/src/api/v1/ApiRouter.cpp new file mode 100644 index 00000000..cef4b7fc --- /dev/null +++ b/src/api/v1/ApiRouter.cpp @@ -0,0 +1,178 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#include +#include +#include + + +#include "api/interfaces/IApiRequest.h" +#include "api/v1/ApiRouter.h" +#include "common/cpu/Cpu.h" +#include "common/Platform.h" +#include "core/config/Config.h" +#include "core/Controller.h" +#include "interfaces/IThread.h" +#include "rapidjson/document.h" +#include "version.h" +#include "workers/Hashrate.h" +#include "workers/Workers.h" + + +static inline double normalize(double d) +{ + if (!isnormal(d)) { + return 0.0; + } + + return floor(d * 100.0) / 100.0; +} + + +xmrig::ApiRouter::ApiRouter(xmrig::Controller *controller) : + m_controller(controller) +{ +} + + +xmrig::ApiRouter::~ApiRouter() +{ +} + + +void xmrig::ApiRouter::onRequest(IApiRequest &request) +{ + if (request.method() == IApiRequest::METHOD_GET) { + if (request.url() == "/1/summary" || request.url() == "/api.json") { + request.accept(); + getMiner(request.reply(), request.doc()); + getHashrate(request.reply(), request.doc()); + } + else if (request.url() == "/1/threads") { + request.accept(); + getThreads(request.reply(), request.doc()); + } + else if (request.url() == "/1/config") { + if (request.isRestricted()) { + return request.done(403); + } + + request.accept(); + m_controller->config()->getJSON(request.doc()); + } + } + else if (request.method() == IApiRequest::METHOD_PUT || request.method() == IApiRequest::METHOD_POST) { + if (request.url() == "/1/config") { + request.accept(); + + if (!m_controller->config()->reload(request.json())) { + return request.done(400); + } + + request.done(204); + } + } +} + + +void xmrig::ApiRouter::getHashrate(rapidjson::Value &reply, rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value hashrate(kObjectType); + Value total(kArrayType); + Value threads(kArrayType); + + const Hashrate *hr = Workers::hashrate(); + + total.PushBack(normalize(hr->calc(Hashrate::ShortInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::MediumInterval)), allocator); + total.PushBack(normalize(hr->calc(Hashrate::LargeInterval)), allocator); + + for (size_t i = 0; i < Workers::threads(); i++) { + Value thread(kArrayType); + thread.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); + thread.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); + + threads.PushBack(thread, allocator); + } + + hashrate.AddMember("total", total, allocator); + hashrate.AddMember("highest", normalize(hr->highest()), allocator); + hashrate.AddMember("threads", threads, allocator); + reply.AddMember("hashrate", hashrate, allocator); +} + + +void xmrig::ApiRouter::getMiner(rapidjson::Value &reply, rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value cpu(kObjectType); + cpu.AddMember("brand", StringRef(Cpu::info()->brand()), allocator); + cpu.AddMember("aes", Cpu::info()->hasAES(), allocator); + cpu.AddMember("x64", Cpu::info()->isX64(), allocator); + cpu.AddMember("sockets", Cpu::info()->sockets(), allocator); + + reply.AddMember("version", APP_VERSION, allocator); + reply.AddMember("kind", APP_KIND, allocator); + reply.AddMember("ua", StringRef(Platform::userAgent()), allocator); + reply.AddMember("cpu", cpu, allocator); + reply.AddMember("algo", StringRef(m_controller->config()->algorithm().name()), allocator); + reply.AddMember("hugepages", Workers::hugePages() > 0, allocator); + reply.AddMember("donate_level", m_controller->config()->pools().donateLevel(), allocator); +} + + +void xmrig::ApiRouter::getThreads(rapidjson::Value &reply, rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + const Hashrate *hr = Workers::hashrate(); + + Workers::threadsSummary(doc); + + const std::vector &threads = m_controller->config()->threads(); + Value list(kArrayType); + + size_t i = 0; + for (const xmrig::IThread *thread : threads) { + Value value = thread->toAPI(doc); + + Value hashrate(kArrayType); + hashrate.PushBack(normalize(hr->calc(i, Hashrate::ShortInterval)), allocator); + hashrate.PushBack(normalize(hr->calc(i, Hashrate::MediumInterval)), allocator); + hashrate.PushBack(normalize(hr->calc(i, Hashrate::LargeInterval)), allocator); + + i++; + + value.AddMember("hashrate", hashrate, allocator); + list.PushBack(value, allocator); + } + + reply.AddMember("threads", list, allocator); +} diff --git a/src/api/v1/ApiRouter.h b/src/api/v1/ApiRouter.h new file mode 100644 index 00000000..9bb99244 --- /dev/null +++ b/src/api/v1/ApiRouter.h @@ -0,0 +1,63 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_APIROUTER_H +#define XMRIG_APIROUTER_H + + +#include "api/interfaces/IApiListener.h" +#include "rapidjson/fwd.h" + + +class Hashrate; + + +namespace xmrig { + + +class Controller; + + +class ApiRouter : public xmrig::IApiListener +{ +public: + ApiRouter(Controller *controller); + ~ApiRouter() override; + +protected: + void onRequest(IApiRequest &request) override; + +private: + void getHashrate(rapidjson::Value &reply, rapidjson::Document &doc) const; + void getMiner(rapidjson::Value &reply, rapidjson::Document &doc) const; + void getThreads(rapidjson::Value &reply, rapidjson::Document &doc) const; + + Controller *m_controller; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_APIROUTER_H */ diff --git a/src/base/base.cmake b/src/base/base.cmake index 08d86805..5f32f425 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -1,13 +1,18 @@ set(HEADERS_BASE src/base/io/Console.h src/base/io/Json.h + src/base/io/log/backends/ConsoleLog.h + src/base/io/log/backends/FileLog.h + src/base/io/log/Log.h src/base/io/Watcher.h src/base/kernel/Entry.h src/base/kernel/interfaces/IClientListener.h src/base/kernel/interfaces/IConfigListener.h src/base/kernel/interfaces/IConsoleListener.h + src/base/kernel/interfaces/IControllerListener.h src/base/kernel/interfaces/IDnsListener.h src/base/kernel/interfaces/ILineListener.h + src/base/kernel/interfaces/ILogBackend.h src/base/kernel/interfaces/ISignalListener.h src/base/kernel/interfaces/IStrategy.h src/base/kernel/interfaces/IStrategyListener.h @@ -17,6 +22,7 @@ set(HEADERS_BASE src/base/kernel/Signals.h src/base/net/dns/Dns.h src/base/net/dns/DnsRecord.h + src/base/net/http/Http.h src/base/net/stratum/Client.h src/base/net/stratum/Job.h src/base/net/stratum/Pool.h @@ -37,12 +43,16 @@ set(HEADERS_BASE set(SOURCES_BASE src/base/io/Console.cpp src/base/io/Json.cpp + src/base/io/log/backends/ConsoleLog.cpp + src/base/io/log/backends/FileLog.cpp + src/base/io/log/Log.cpp src/base/io/Watcher.cpp src/base/kernel/Entry.cpp src/base/kernel/Process.cpp src/base/kernel/Signals.cpp src/base/net/dns/Dns.cpp src/base/net/dns/DnsRecord.cpp + src/base/net/http/Http.cpp src/base/net/stratum/Client.cpp src/base/net/stratum/Job.cpp src/base/net/stratum/Pool.cpp @@ -61,3 +71,46 @@ if (WIN32) else() set(SOURCES_OS src/base/io/Json_unix.cpp) endif() + + +if (NOT WIN32) + CHECK_INCLUDE_FILE (syslog.h HAVE_SYSLOG_H) + if (HAVE_SYSLOG_H) + add_definitions(/DHAVE_SYSLOG_H) + set(SOURCES_SYSLOG src/base/io/log/backends/SysLog.h src/base/io/log/backends/SysLog.cpp) + endif() +endif() + + +if (WITH_HTTPD) + set(HEADERS_BASE_HTTP + src/3rdparty/http-parser/http_parser.h + src/base/kernel/interfaces/IHttpListener.h + src/base/kernel/interfaces/ITcpServerListener.h + src/base/net/http/HttpApiResponse.h + src/base/net/http/HttpContext.h + src/base/net/http/HttpRequest.h + src/base/net/http/HttpResponse.h + src/base/net/http/HttpServer.h + src/base/net/tools/TcpServer.h + ) + + set(SOURCES_BASE_HTTP + src/3rdparty/http-parser/http_parser.c + src/base/net/http/HttpApiResponse.cpp + src/base/net/http/HttpContext.cpp + src/base/net/http/HttpResponse.cpp + src/base/net/http/HttpServer.cpp + src/base/net/tools/TcpServer.cpp + ) + + add_definitions(/DXMRIG_FEATURE_HTTP) + add_definitions(/DXMRIG_FEATURE_API) +else() + set(HEADERS_BASE_HTTP "") + set(SOURCES_BASE_HTTP "") + remove_definitions(/DXMRIG_FEATURE_HTTP) + remove_definitions(/DXMRIG_FEATURE_API) +endif() + +add_definitions(/DXMRIG_DEPRECATED) diff --git a/src/base/io/log/Log.cpp b/src/base/io/log/Log.cpp new file mode 100644 index 00000000..fe5aa205 --- /dev/null +++ b/src/base/io/log/Log.cpp @@ -0,0 +1,247 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifdef WIN32 +# include +# include +#endif + + +#include +#include +#include +#include +#include +#include + + +#include "base/io/log/Log.h" +#include "base/kernel/interfaces/ILogBackend.h" + + +namespace xmrig { + + +static const char *colors_map[] = { + RED_BOLD_S, // EMERG + RED_BOLD_S, // ALERT + RED_BOLD_S, // CRIT + RED_S, // ERR + YELLOW_S, // WARNING + WHITE_BOLD_S, // NOTICE + nullptr, // INFO +# ifdef WIN32 + BLACK_BOLD_S // DEBUG +# else + BRIGHT_BLACK_S // DEBUG +# endif +}; + + + +class LogPrivate +{ +public: + inline LogPrivate() : + m_buf() + { + uv_mutex_init(&m_mutex); + } + + + inline ~LogPrivate() + { + uv_mutex_destroy(&m_mutex); + + for (ILogBackend *backend : m_backends) { + delete backend; + } + } + + + inline void add(ILogBackend *backend) { m_backends.push_back(backend); } + + + void print(Log::Level level, const char *fmt, va_list args) + { + size_t size = 0; + size_t offset = 0; + + lock(); + timestamp(level, size, offset); + color(level, size); + + const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args); + if (rc < 0) { + return unlock(); + } + + size += std::min(static_cast(rc), sizeof (m_buf) - offset - 32); + endl(size); + + std::string txt(m_buf); + size_t i; + while ((i = txt.find(CSI)) != std::string::npos) { + txt.erase(i, txt.find('m', i) - i + 1); + } + + if (!m_backends.empty()) { + for (ILogBackend *backend : m_backends) { + backend->print(level, m_buf, offset, size, true); + backend->print(level, txt.c_str(), offset, txt.size(), false); + } + } + else { + fputs(txt.c_str(), stdout); + fflush(stdout); + } + + unlock(); + } + + +private: + inline void lock() { uv_mutex_lock(&m_mutex); } + inline void unlock() { uv_mutex_unlock(&m_mutex); } + + + inline void timestamp(Log::Level level, size_t &size, size_t &offset) + { + if (level == Log::NONE) { + return; + } + + time_t now = time(nullptr); + tm stime; + +# ifdef _WIN32 + localtime_s(&stime, &now); +# else + localtime_r(&now, &stime); +# endif + + const int rc = snprintf(m_buf, sizeof(m_buf) - 1, "[%d-%02d-%02d %02d:%02d:%02d] ", + stime.tm_year + 1900, + stime.tm_mon + 1, + stime.tm_mday, + stime.tm_hour, + stime.tm_min, + stime.tm_sec + ); + + if (rc > 0) { + size = offset = static_cast(rc); + } + } + + + inline void color(Log::Level level, size_t &size) + { + if (level == Log::NONE) { + return; + } + + const char *color = colors_map[level]; + if (color == nullptr) { + return; + } + + const size_t s = strlen(color); + memcpy(m_buf + size, color, s); + + size += s; + } + + + inline void endl(size_t &size) + { +# ifdef _WIN32 + memcpy(m_buf + size, CLEAR "\r\n", 7); + size += 6; +# else + memcpy(m_buf + size, CLEAR "\n", 6); + size += 5; +# endif + } + + + char m_buf[4096]; + std::vector m_backends; + uv_mutex_t m_mutex; +}; + + +bool Log::colors = true; +LogPrivate *Log::d = new LogPrivate(); + + +} /* namespace xmrig */ + + + +void xmrig::Log::add(ILogBackend *backend) +{ + if (d) { + d->add(backend); + } +} + + +void xmrig::Log::destroy() +{ + delete d; + d = nullptr; +} + + +void xmrig::Log::print(const char *fmt, ...) +{ + if (!d) { + return; + } + + va_list args; + va_start(args, fmt); + + d->print(NONE, fmt, args); + + va_end(args); +} + + +void xmrig::Log::print(Level level, const char *fmt, ...) +{ + if (!d) { + return; + } + + va_list args; + va_start(args, fmt); + + d->print(level, fmt, args); + + va_end(args); +} diff --git a/src/base/io/log/Log.h b/src/base/io/log/Log.h new file mode 100644 index 00000000..a14ffded --- /dev/null +++ b/src/base/io/log/Log.h @@ -0,0 +1,129 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_LOG_H +#define XMRIG_LOG_H + + +namespace xmrig { + + +class ILogBackend; +class LogPrivate; + + +class Log +{ +public: + enum Level : int { + NONE = -1, + EMERG, // system is unusable + ALERT, // action must be taken immediately + CRIT, // critical conditions + ERR, // error conditions + WARNING, // warning conditions + NOTICE, // normal but significant condition + INFO, // informational + DEBUG, // debug-level messages + }; + + static void add(ILogBackend *backend); + static void destroy(); + static void print(const char *fmt, ...); + static void print(Level level, const char *fmt, ...); + + static bool colors; + +private: + static LogPrivate *d; +}; + + +#define CSI "\x1B[" // Control Sequence Introducer (ANSI spec name) +#define CLEAR CSI "0m" // all attributes off +#define BRIGHT_BLACK_S CSI "0;90m" // somewhat MD.GRAY +#define BLACK_S CSI "0;30m" +#define BLACK_BOLD_S CSI "1;30m" // another name for GRAY +#define RED_S CSI "0;31m" +#define RED_BOLD_S CSI "1;31m" +#define GREEN_S CSI "0;32m" +#define GREEN_BOLD_S CSI "1;32m" +#define YELLOW_S CSI "0;33m" +#define YELLOW_BOLD_S CSI "1;33m" +#define BLUE_S CSI "0;34m" +#define BLUE_BOLD_S CSI "1;34m" +#define MAGENTA_S CSI "0;35m" +#define MAGENTA_BOLD_S CSI "1;35m" +#define CYAN_S CSI "0;36m" +#define CYAN_BOLD_S CSI "1;36m" +#define WHITE_S CSI "0;37m" // another name for LT.GRAY +#define WHITE_BOLD_S CSI "1;37m" // actually white + +//color wrappings +#define BLACK(x) BLACK_S x CLEAR +#define BLACK_BOLD(x) BLACK_BOLD_S x CLEAR +#define RED(x) RED_S x CLEAR +#define RED_BOLD(x) RED_BOLD_S x CLEAR +#define GREEN(x) GREEN_S x CLEAR +#define GREEN_BOLD(x) GREEN_BOLD_S x CLEAR +#define YELLOW(x) YELLOW_S x CLEAR +#define YELLOW_BOLD(x) YELLOW_BOLD_S x CLEAR +#define BLUE(x) BLUE_S x CLEAR +#define BLUE_BOLD(x) BLUE_BOLD_S x CLEAR +#define MAGENTA(x) MAGENTA_S x CLEAR +#define MAGENTA_BOLD(x) MAGENTA_BOLD_S x CLEAR +#define CYAN(x) CYAN_S x CLEAR +#define CYAN_BOLD(x) CYAN_BOLD_S x CLEAR +#define WHITE(x) WHITE_S x CLEAR +#define WHITE_BOLD(x) WHITE_BOLD_S x CLEAR + + +#define LOG_EMERG(x, ...) xmrig::Log::print(xmrig::Log::EMERG, x, ##__VA_ARGS__) +#define LOG_ALERT(x, ...) xmrig::Log::print(xmrig::Log::ALERT, x, ##__VA_ARGS__) +#define LOG_CRIT(x, ...) xmrig::Log::print(xmrig::Log::CRIT, x, ##__VA_ARGS__) +#define LOG_ERR(x, ...) xmrig::Log::print(xmrig::Log::ERR, x, ##__VA_ARGS__) +#define LOG_WARN(x, ...) xmrig::Log::print(xmrig::Log::WARNING, x, ##__VA_ARGS__) +#define LOG_NOTICE(x, ...) xmrig::Log::print(xmrig::Log::NOTICE, x, ##__VA_ARGS__) +#define LOG_INFO(x, ...) xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__) + +#ifdef APP_DEBUG +# define LOG_DEBUG(x, ...) xmrig::Log::print(xmrig::Log::DEBUG, x, ##__VA_ARGS__) +#else +# define LOG_DEBUG(x, ...) +#endif + +#if defined(APP_DEBUG) || defined(APP_DEVEL) +# define LOG_DEBUG_ERR(x, ...) xmrig::Log::print(xmrig::Log::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG_WARN(x, ...) xmrig::Log::print(xmrig::Log::WARNING, x, ##__VA_ARGS__) +#else +# define LOG_DEBUG_ERR(x, ...) +# define LOG_DEBUG_WARN(x, ...) +#endif + + +} /* namespace xmrig */ + + +#endif /* XMRIG_LOG_H */ diff --git a/src/common/log/ConsoleLog.cpp b/src/base/io/log/backends/ConsoleLog.cpp similarity index 64% rename from src/common/log/ConsoleLog.cpp rename to src/base/io/log/backends/ConsoleLog.cpp index e0ac01ea..a5b6c1a7 100644 --- a/src/common/log/ConsoleLog.cpp +++ b/src/base/io/log/backends/ConsoleLog.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -23,21 +24,12 @@ */ -#include #include -#include -#include -#include - -#ifdef WIN32 -# include -# include -#endif #include "base/tools/Handle.h" -#include "common/log/ConsoleLog.h" -#include "common/log/Log.h" +#include "base/io/log/backends/ConsoleLog.h" +#include "base/io/log/Log.h" xmrig::ConsoleLog::ConsoleLog() : @@ -51,8 +43,7 @@ xmrig::ConsoleLog::ConsoleLog() : } uv_tty_set_mode(m_tty, UV_TTY_MODE_NORMAL); - m_uvBuf.base = m_buf; - m_stream = reinterpret_cast(m_tty); + m_stream = reinterpret_cast(m_tty); # ifdef WIN32 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); @@ -73,38 +64,25 @@ xmrig::ConsoleLog::~ConsoleLog() } -void xmrig::ConsoleLog::message(Level level, const char* fmt, va_list args) +void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors) { - time_t now = time(nullptr); - tm stime; + if (Log::colors != colors) { + return; + } # ifdef _WIN32 - localtime_s(&stime, &now); + uv_buf_t buf = uv_buf_init(const_cast(line), static_cast(size)); # else - localtime_r(&now, &stime); + uv_buf_t buf = uv_buf_init(const_cast(line), size); # endif - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec, - Log::colorByLevel(level, Log::colors), - fmt, - Log::endl(Log::colors) - ); - - print(args); -} - - -void xmrig::ConsoleLog::text(const char* fmt, va_list args) -{ - snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s", fmt, Log::endl(Log::colors)); - - print(args); + if (!isWritable()) { + fputs(line, stdout); + fflush(stdout); + } + else { + uv_try_write(m_stream, &buf, 1); + } } @@ -117,20 +95,3 @@ bool xmrig::ConsoleLog::isWritable() const const uv_handle_type type = uv_guess_handle(1); return type == UV_TTY || type == UV_NAMED_PIPE; } - - -void xmrig::ConsoleLog::print(va_list args) -{ - m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args); - if (m_uvBuf.len <= 0) { - return; - } - - if (!isWritable()) { - fputs(m_buf, stdout); - fflush(stdout); - } - else { - uv_try_write(m_stream, &m_uvBuf, 1); - } -} diff --git a/src/common/log/ConsoleLog.h b/src/base/io/log/backends/ConsoleLog.h similarity index 84% rename from src/common/log/ConsoleLog.h rename to src/base/io/log/backends/ConsoleLog.h index 73a701e6..90e4fa14 100644 --- a/src/common/log/ConsoleLog.h +++ b/src/base/io/log/backends/ConsoleLog.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -26,10 +27,11 @@ #define XMRIG_CONSOLELOG_H -#include +typedef struct uv_stream_s uv_stream_t; +typedef struct uv_tty_s uv_tty_t; -#include "common/interfaces/ILogBackend.h" +#include "base/kernel/interfaces/ILogBackend.h" namespace xmrig { @@ -42,16 +44,11 @@ public: ~ConsoleLog() override; protected: - void message(Level level, const char *fmt, va_list args) override; - void text(const char *fmt, va_list args) override; + void print(int level, const char *line, size_t offset, size_t size, bool colors) override; private: bool isWritable() const; - void print(va_list args); - char m_buf[kBufferSize]; - char m_fmt[256]; - uv_buf_t m_uvBuf; uv_stream_t *m_stream; uv_tty_t *m_tty; }; diff --git a/src/common/log/FileLog.cpp b/src/base/io/log/backends/FileLog.cpp similarity index 63% rename from src/common/log/FileLog.cpp rename to src/base/io/log/backends/FileLog.cpp index 71eeb18c..1ff01637 100644 --- a/src/common/log/FileLog.cpp +++ b/src/base/io/log/backends/FileLog.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -23,15 +24,11 @@ */ -#include -#include -#include #include -#include +#include -#include "common/log/FileLog.h" -#include "common/log/Log.h" +#include "base/io/log/backends/FileLog.h" xmrig::FileLog::FileLog(const char *fileName) @@ -42,43 +39,22 @@ xmrig::FileLog::FileLog(const char *fileName) } -void xmrig::FileLog::message(Level level, const char* fmt, va_list args) +void xmrig::FileLog::print(int, const char *line, size_t, size_t size, bool colors) { - if (m_file < 0) { + if (m_file < 0 || colors) { return; } - time_t now = time(nullptr); - tm stime; - # ifdef _WIN32 - localtime_s(&stime, &now); + uv_buf_t buf = uv_buf_init(strdup(line), static_cast(size)); # else - localtime_r(&now, &stime); + uv_buf_t buf = uv_buf_init(strdup(line), size); # endif - snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s", - stime.tm_year + 1900, - stime.tm_mon + 1, - stime.tm_mday, - stime.tm_hour, - stime.tm_min, - stime.tm_sec, - Log::colorByLevel(level, Log::colors), - fmt, - Log::endl(Log::colors) - ); + uv_fs_t *req = new uv_fs_t; + req->data = buf.base; - char *buf = new char[kBufferSize]; - const int size = vsnprintf(buf, kBufferSize - 1, m_fmt, args); - - write(buf, size); -} - - -void xmrig::FileLog::text(const char* fmt, va_list args) -{ - message(INFO, fmt, args); + uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, FileLog::onWrite); } @@ -89,13 +65,3 @@ void xmrig::FileLog::onWrite(uv_fs_t *req) uv_fs_req_cleanup(req); delete req; } - - -void xmrig::FileLog::write(char *data, size_t size) -{ - uv_buf_t buf = uv_buf_init(data, (unsigned int) size); - uv_fs_t *req = new uv_fs_t; - req->data = buf.base; - - uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, FileLog::onWrite); -} diff --git a/src/common/log/FileLog.h b/src/base/io/log/backends/FileLog.h similarity index 85% rename from src/common/log/FileLog.h rename to src/base/io/log/backends/FileLog.h index da80acb2..188a99aa 100644 --- a/src/common/log/FileLog.h +++ b/src/base/io/log/backends/FileLog.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -26,10 +27,10 @@ #define XMRIG_FILELOG_H -#include +typedef struct uv_fs_s uv_fs_t; -#include "common/interfaces/ILogBackend.h" +#include "base/kernel/interfaces/ILogBackend.h" namespace xmrig { @@ -40,15 +41,13 @@ class FileLog : public ILogBackend public: FileLog(const char *fileName); - void message(Level level, const char* fmt, va_list args) override; - void text(const char* fmt, va_list args) override; + +protected: + void print(int level, const char *line, size_t offset, size_t size, bool colors) override; private: static void onWrite(uv_fs_t *req); - void write(char *data, size_t size); - - char m_fmt[256]; int m_file; }; diff --git a/src/common/log/SysLog.cpp b/src/base/io/log/backends/SysLog.cpp similarity index 79% rename from src/common/log/SysLog.cpp rename to src/base/io/log/backends/SysLog.cpp index 1a8ffbd2..e66f2e35 100644 --- a/src/common/log/SysLog.cpp +++ b/src/base/io/log/backends/SysLog.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -26,7 +27,7 @@ #include -#include "common/log/SysLog.h" +#include "base/io/log/backends/SysLog.h" #include "version.h" @@ -36,13 +37,17 @@ xmrig::SysLog::SysLog() } -void xmrig::SysLog::message(Level level, const char *fmt, va_list args) +xmrig::SysLog::~SysLog() { - vsyslog(static_cast(level), fmt, args); + closelog(); } -void xmrig::SysLog::text(const char *fmt, va_list args) +void xmrig::SysLog::print(int level, const char *line, size_t offset, size_t, bool colors) { - vsyslog(LOG_INFO, fmt, args); + if (colors) { + return; + } + + syslog(level == -1 ? LOG_INFO : level, "%s", line + offset); } diff --git a/src/common/log/SysLog.h b/src/base/io/log/backends/SysLog.h similarity index 85% rename from src/common/log/SysLog.h rename to src/base/io/log/backends/SysLog.h index 3e64cf8b..0e8d0f8e 100644 --- a/src/common/log/SysLog.h +++ b/src/base/io/log/backends/SysLog.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -26,7 +27,7 @@ #define XMRIG_SYSLOG_H -#include "common/interfaces/ILogBackend.h" +#include "base/kernel/interfaces/ILogBackend.h" namespace xmrig { @@ -36,9 +37,10 @@ class SysLog : public ILogBackend { public: SysLog(); + ~SysLog(); - void message(Level level, const char *fmt, va_list args) override; - void text(const char *fmt, va_list args) override; +protected: + void print(int level, const char *line, size_t offset, size_t size, bool colors) override; }; diff --git a/src/base/kernel/Entry.cpp b/src/base/kernel/Entry.cpp index 84c4c971..1834cb82 100644 --- a/src/base/kernel/Entry.cpp +++ b/src/base/kernel/Entry.cpp @@ -27,19 +27,14 @@ #include -#ifndef XMRIG_NO_HTTPD -# include -#endif - - -#ifndef XMRIG_NO_TLS +#ifdef XMRIG_FEATURE_TLS # include #endif #include "base/kernel/Entry.h" #include "base/kernel/Process.h" -#include "core/usage.h" +#include "core/config/usage.h" #include "version.h" @@ -73,11 +68,7 @@ static int showVersion() printf("\nlibuv/%s\n", uv_version_string()); -# ifndef XMRIG_NO_HTTPD - printf("microhttpd/%s\n", MHD_get_version()); -# endif - -# if !defined(XMRIG_NO_TLS) && defined(OPENSSL_VERSION_TEXT) +# if defined(XMRIG_FEATURE_TLS) && defined(OPENSSL_VERSION_TEXT) { constexpr const char *v = OPENSSL_VERSION_TEXT + 8; printf("OpenSSL/%.*s\n", static_cast(strchr(v, ' ') - v), v); diff --git a/src/common/interfaces/IControllerListener.h b/src/base/kernel/interfaces/IControllerListener.h similarity index 100% rename from src/common/interfaces/IControllerListener.h rename to src/base/kernel/interfaces/IControllerListener.h diff --git a/src/common/interfaces/IConfigCreator.h b/src/base/kernel/interfaces/IHttpListener.h similarity index 83% rename from src/common/interfaces/IConfigCreator.h rename to src/base/kernel/interfaces/IHttpListener.h index c2e0e62e..d4d905ca 100644 --- a/src/common/interfaces/IConfigCreator.h +++ b/src/base/kernel/interfaces/IHttpListener.h @@ -22,26 +22,27 @@ * along with this program. If not, see . */ -#ifndef XMRIG_ICONFIGCREATOR_H -#define XMRIG_ICONFIGCREATOR_H +#ifndef XMRIG_IHTTPLISTENER_H +#define XMRIG_IHTTPLISTENER_H namespace xmrig { -class IConfig; +class HttpRequest; +class HttpResponse; -class IConfigCreator +class IHttpListener { public: - virtual ~IConfigCreator() = default; + virtual ~IHttpListener() = default; - virtual IConfig *create() const = 0; + virtual void onHttpRequest(const HttpRequest &req) = 0; }; } /* namespace xmrig */ -#endif // XMRIG_ICONFIGCREATOR_H +#endif // XMRIG_IHTTPLISTENER_H diff --git a/src/common/interfaces/ILogBackend.h b/src/base/kernel/interfaces/ILogBackend.h similarity index 79% rename from src/common/interfaces/ILogBackend.h rename to src/base/kernel/interfaces/ILogBackend.h index 2299fc67..ef18da88 100644 --- a/src/common/interfaces/ILogBackend.h +++ b/src/base/kernel/interfaces/ILogBackend.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2019 Spudz76 * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * @@ -36,24 +37,9 @@ namespace xmrig { class ILogBackend { public: - enum Level { - ERR, - WARNING, - NOTICE, - INFO, - DEBUG - }; - -# ifdef APP_DEBUG - constexpr static const size_t kBufferSize = 1024; -# else - constexpr static const size_t kBufferSize = 512; -# endif - virtual ~ILogBackend() = default; - virtual void message(Level level, const char* fmt, va_list args) = 0; - virtual void text(const char* fmt, va_list args) = 0; + virtual void print(int level, const char *line, size_t offset, size_t size, bool colors) = 0; }; diff --git a/src/common/log/BasicLog.h b/src/base/kernel/interfaces/ITcpServerListener.h similarity index 75% rename from src/common/log/BasicLog.h rename to src/base/kernel/interfaces/ITcpServerListener.h index bddbe07c..3cb1576e 100644 --- a/src/common/log/BasicLog.h +++ b/src/base/kernel/interfaces/ITcpServerListener.h @@ -22,36 +22,32 @@ * along with this program. If not, see . */ -#ifndef XMRIG_BASICLOG_H -#define XMRIG_BASICLOG_H +#ifndef XMRIG_ITCPSERVERLISTENER_H +#define XMRIG_ITCPSERVERLISTENER_H -#include +#include -#include "common/interfaces/ILogBackend.h" +typedef struct uv_stream_s uv_stream_t; namespace xmrig { -class BasicLog : public ILogBackend +class String; + + +class ITcpServerListener { public: - BasicLog(); + virtual ~ITcpServerListener() = default; - void message(Level level, const char *fmt, va_list args) override; - void text(const char *fmt, va_list args) override; - -private: - bool isWritable() const; - void print(va_list args); - - char m_buf[kBufferSize]; - char m_fmt[256]; + virtual void onConnection(uv_stream_t *stream, uint16_t port) = 0; }; } /* namespace xmrig */ -#endif /* XMRIG_BASICLOG_H */ + +#endif // XMRIG_ITCPSERVERLISTENER_H diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index f2402124..f57278c4 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -37,7 +37,8 @@ namespace xmrig { xmrig::Dns::Dns(IDnsListener *listener) : m_hints(), m_listener(listener), - m_status(0) + m_status(0), + m_resolver(nullptr) { m_key = m_storage.add(this); @@ -54,7 +55,7 @@ xmrig::Dns::~Dns() { m_storage.release(m_key); - Handle::close(m_resolver); + delete m_resolver; } diff --git a/src/base/net/http/Http.cpp b/src/base/net/http/Http.cpp new file mode 100644 index 00000000..597260cf --- /dev/null +++ b/src/base/net/http/Http.cpp @@ -0,0 +1,99 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include "3rdparty/rapidjson/document.h" +#include "base/io/Json.h" +#include "base/net/http/Http.h" + + +namespace xmrig { + +static const char *kEnabled = "enabled"; +static const char *kHost = "host"; +static const char *kLocalhost = "127.0.0.1"; +static const char *kPort = "port"; +static const char *kRestricted = "restricted"; +static const char *kToken = "access-token"; + +} + + +xmrig::Http::Http() : + m_enabled(false), + m_restricted(true), + m_host(kLocalhost), + m_port(0) +{ +} + + +bool xmrig::Http::isEqual(const Http &other) const +{ + return other.m_enabled == m_enabled && + other.m_restricted == m_restricted && + other.m_host == m_host && + other.m_token == m_token && + other.m_port == m_port; +} + + +rapidjson::Value xmrig::Http::toJSON(rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value obj(kObjectType); + + obj.AddMember(StringRef(kEnabled), m_enabled, allocator); + obj.AddMember(StringRef(kHost), m_host.toJSON(), allocator); + obj.AddMember(StringRef(kPort), m_port, allocator); + obj.AddMember(StringRef(kToken), m_token.toJSON(), allocator); + obj.AddMember(StringRef(kRestricted), m_restricted, allocator); + + return obj; +} + + +void xmrig::Http::load(const rapidjson::Value &http) +{ + if (!http.IsObject()) { + return; + } + + m_enabled = Json::getBool(http, kEnabled); + m_restricted = Json::getBool(http, kRestricted, true); + m_host = Json::getString(http, kHost, kLocalhost); + m_token = Json::getString(http, kToken); + + setPort(Json::getInt(http, kPort)); +} + + +void xmrig::Http::setPort(int port) +{ + if (port >= 0 && port <= 65536) { + m_port = static_cast(port); + } +} diff --git a/src/base/net/http/Http.h b/src/base/net/http/Http.h new file mode 100644 index 00000000..21eb581a --- /dev/null +++ b/src/base/net/http/Http.h @@ -0,0 +1,73 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTP_H +#define XMRIG_HTTP_H + + +#include "base/tools/String.h" + + +namespace xmrig { + + +class Http +{ +public: + Http(); + + inline bool isAuthRequired() const { return m_restricted == false || !m_token.isNull(); } + inline bool isEnabled() const { return m_enabled; } + inline bool isRestricted() const { return m_restricted; } + inline const String &host() const { return m_host; } + inline const String &token() const { return m_token; } + inline uint16_t port() const { return m_port; } + inline void setEnabled(bool enabled) { m_enabled = enabled; } + inline void setHost(const char *host) { m_host = host; } + inline void setRestricted(bool restricted) { m_restricted = restricted; } + inline void setToken(const char *token) { m_token = token; } + + inline bool operator!=(const Http &other) const { return !isEqual(other); } + inline bool operator==(const Http &other) const { return isEqual(other); } + + bool isEqual(const Http &other) const; + rapidjson::Value toJSON(rapidjson::Document &doc) const; + void load(const rapidjson::Value &http); + void setPort(int port); + +private: + bool m_enabled; + bool m_restricted; + String m_host; + String m_token; + uint16_t m_port; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTP_H + diff --git a/src/base/net/http/HttpApiResponse.cpp b/src/base/net/http/HttpApiResponse.cpp new file mode 100644 index 00000000..bf91445a --- /dev/null +++ b/src/base/net/http/HttpApiResponse.cpp @@ -0,0 +1,86 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include "3rdparty/http-parser/http_parser.h" +#include "base/net/http/HttpApiResponse.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/stringbuffer.h" + + +namespace xmrig { + +static const char *kError = "error"; +static const char *kStatus = "status"; + +} // namespace xmrig + + +xmrig::HttpApiResponse::HttpApiResponse(uint64_t id) : + HttpResponse(id), + m_doc(rapidjson::kObjectType) +{ +} + + +xmrig::HttpApiResponse::HttpApiResponse(uint64_t id, int status) : + HttpResponse(id), + m_doc(rapidjson::kObjectType) +{ + setStatus(status); +} + + +void xmrig::HttpApiResponse::end() +{ + using namespace rapidjson; + + setHeader("Access-Control-Allow-Origin", "*"); + setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type"); + + if (statusCode() >= 400) { + if (!m_doc.HasMember(kStatus)) { + m_doc.AddMember(StringRef(kStatus), statusCode(), m_doc.GetAllocator()); + } + + if (!m_doc.HasMember(kError)) { + m_doc.AddMember(StringRef(kError), StringRef(http_status_str(static_cast(statusCode()))), m_doc.GetAllocator()); + } + } + + if (!m_doc.MemberCount()) { + return HttpResponse::end(); + } + + setHeader("Content-Type", "application/json"); + + StringBuffer buffer(nullptr, 4096); + PrettyWriter writer(buffer); + writer.SetMaxDecimalPlaces(10); + m_doc.Accept(writer); + + HttpResponse::end(buffer.GetString(), buffer.GetSize()); +} diff --git a/src/common/api/HttpBody.h b/src/base/net/http/HttpApiResponse.h similarity index 65% rename from src/common/api/HttpBody.h rename to src/base/net/http/HttpApiResponse.h index 0b143fb7..bbaf132e 100644 --- a/src/common/api/HttpBody.h +++ b/src/base/net/http/HttpApiResponse.h @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * 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 @@ -22,48 +23,35 @@ * along with this program. If not, see . */ -#ifndef __HTTPBODY_H__ -#define __HTTPBODY_H__ + +#ifndef XMRIG_HTTPAPIRESPONSE_H +#define XMRIG_HTTPAPIRESPONSE_H -#include +#include "base/net/http/HttpResponse.h" +#include "rapidjson/document.h" namespace xmrig { -class HttpBody +class HttpApiResponse : public HttpResponse { public: - inline HttpBody() : - m_pos(0) - {} + HttpApiResponse(uint64_t id); + HttpApiResponse(uint64_t id, int status); + inline rapidjson::Document &doc() { return m_doc; } - 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; } + void end(); private: - char m_data[32768]; - size_t m_pos; + rapidjson::Document m_doc; }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* __HTTPBODY_H__ */ +#endif // XMRIG_HTTPAPIRESPONSE_H + diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp new file mode 100644 index 00000000..de32f34d --- /dev/null +++ b/src/base/net/http/HttpContext.cpp @@ -0,0 +1,193 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include +#include + + +#include "3rdparty/http-parser/http_parser.h" +#include "base/kernel/interfaces/IHttpListener.h" +#include "base/net/http/HttpContext.h" + + +namespace xmrig { + +static uint64_t SEQUENCE = 0; +std::map HttpContext::m_storage; + +} // namespace xmrig + + +xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) : + HttpRequest(SEQUENCE++), + listener(listener), + connect(nullptr), + m_wasHeaderValue(false) +{ + m_storage[id()] = this; + + parser = new http_parser; + tcp = new uv_tcp_t; + + uv_tcp_init(uv_default_loop(), tcp); + http_parser_init(parser, static_cast(parser_type)); + + parser->data = tcp->data = this; +} + + +xmrig::HttpContext::~HttpContext() +{ + delete connect; + delete tcp; + delete parser; +} + + +void xmrig::HttpContext::close() +{ + auto it = m_storage.find(id()); + if (it != m_storage.end()) { + m_storage.erase(it); + } + + if (!uv_is_closing(handle())) { + uv_close(handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast(handle->data); }); + } +} + + +xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id) +{ + if (m_storage.count(id) == 0) { + return nullptr; + } + + return m_storage[id]; +} + + +void xmrig::HttpContext::attach(http_parser_settings *settings) +{ + if (settings->on_message_complete != nullptr) { + return; + } + + settings->on_message_begin = nullptr; + settings->on_status = nullptr; + settings->on_chunk_header = nullptr; + settings->on_chunk_complete = nullptr; + + settings->on_url = [](http_parser *parser, const char *at, size_t length) -> int + { + static_cast(parser->data)->url = std::string(at, length); + return 0; + }; + + settings->on_header_field = onHeaderField; + settings->on_header_value = onHeaderValue; + + settings->on_headers_complete = [](http_parser* parser) -> int { + HttpContext *ctx = static_cast(parser->data); + ctx->method = parser->method; + + if (!ctx->m_lastHeaderField.empty()) { + ctx->setHeader(); + } + + return 0; + }; + + settings->on_body = [](http_parser *parser, const char *at, size_t len) -> int + { + static_cast(parser->data)->body += std::string(at, len); + + return 0; + }; + + settings->on_message_complete = [](http_parser *parser) -> int + { + const HttpContext *ctx = reinterpret_cast(parser->data); + ctx->listener->onHttpRequest(*ctx); + + return 0; + }; +} + + +void xmrig::HttpContext::closeAll() +{ + for (auto kv : m_storage) { + if (!uv_is_closing(kv.second->handle())) { + uv_close(kv.second->handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast(handle->data); }); + } + } +} + + +int xmrig::HttpContext::onHeaderField(http_parser *parser, const char *at, size_t length) +{ + HttpContext *ctx = static_cast(parser->data); + + if (ctx->m_wasHeaderValue) { + if (!ctx->m_lastHeaderField.empty()) { + ctx->setHeader(); + } + + ctx->m_lastHeaderField = std::string(at, length); + ctx->m_wasHeaderValue = false; + } else { + ctx->m_lastHeaderField += std::string(at, length); + } + + return 0; +} + + +int xmrig::HttpContext::onHeaderValue(http_parser *parser, const char *at, size_t length) +{ + HttpContext *ctx = static_cast(parser->data); + + if (!ctx->m_wasHeaderValue) { + ctx->m_lastHeaderValue = std::string(at, length); + ctx->m_wasHeaderValue = true; + } else { + ctx->m_lastHeaderValue += std::string(at, length); + } + + return 0; +} + + +void xmrig::HttpContext::setHeader() +{ + std::transform(m_lastHeaderField.begin(), m_lastHeaderField.end(), m_lastHeaderField.begin(), ::tolower); + headers.insert({ m_lastHeaderField, m_lastHeaderValue }); + + m_lastHeaderField.clear(); + m_lastHeaderValue.clear(); +} + diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h new file mode 100644 index 00000000..3b2dead3 --- /dev/null +++ b/src/base/net/http/HttpContext.h @@ -0,0 +1,86 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTPCONTEXT_H +#define XMRIG_HTTPCONTEXT_H + + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; +typedef struct uv_connect_s uv_connect_t; +typedef struct uv_handle_s uv_handle_t; +typedef struct uv_stream_s uv_stream_t; +typedef struct uv_tcp_s uv_tcp_t; + + +#include "base/net/http/HttpRequest.h" + + +namespace xmrig { + + +class IHttpListener; + + +class HttpContext : public HttpRequest +{ +public: + HttpContext(int parser_type, IHttpListener *listener); + ~HttpContext(); + + inline uv_stream_t *stream() const { return reinterpret_cast(tcp); } + inline uv_handle_t *handle() const { return reinterpret_cast(tcp); } + + void close(); + + static HttpContext *get(uint64_t id); + static void attach(http_parser_settings *settings); + static void closeAll(); + + http_parser *parser; + IHttpListener *listener; + uv_connect_t *connect; + uv_tcp_t *tcp; + +private: + static int onHeaderField(http_parser *parser, const char *at, size_t length); + static int onHeaderValue(http_parser *parser, const char *at, size_t length); + + void setHeader(); + + bool m_wasHeaderValue; + std::string m_lastHeaderField; + std::string m_lastHeaderValue; + + static std::map m_storage; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPCONTEXT_H + diff --git a/src/base/net/http/HttpRequest.h b/src/base/net/http/HttpRequest.h new file mode 100644 index 00000000..b97f0b8d --- /dev/null +++ b/src/base/net/http/HttpRequest.h @@ -0,0 +1,60 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTPREQUEST_H +#define XMRIG_HTTPREQUEST_H + + +#include +#include +#include + + +namespace xmrig { + + +class HttpRequest +{ +public: + inline HttpRequest(uint64_t id) : method(0), m_id(id) {} + + inline uint64_t id() const { return m_id; } + + int method; + std::map headers; + std::string body; + std::string url; + +private: + const uint64_t m_id; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPREQUEST_H + diff --git a/src/base/net/http/HttpResponse.cpp b/src/base/net/http/HttpResponse.cpp new file mode 100644 index 00000000..3b88e29b --- /dev/null +++ b/src/base/net/http/HttpResponse.cpp @@ -0,0 +1,139 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include +#include +#include + + +#include "3rdparty/http-parser/http_parser.h" +#include "base/io/log/Log.h" +#include "base/net/http/HttpContext.h" +#include "base/net/http/HttpResponse.h" + + +namespace xmrig { + +static const char *kCRLF = "\r\n"; +static const char *kUserAgent = "user-agent"; + +} // namespace xmrig + + +xmrig::HttpResponse::HttpResponse(uint64_t id, int statusCode) : + m_id(id), + m_statusCode(statusCode) +{ +} + + +bool xmrig::HttpResponse::isAlive() const +{ + HttpContext *ctx = HttpContext::get(m_id); + + return ctx && uv_is_writable(ctx->stream()); +} + + +void xmrig::HttpResponse::end(const char *data, size_t size) +{ + if (!isAlive()) { + return; + } + + if (data && !size) { + size = strlen(data); + } + + if (size) { + setHeader("Content-Length", std::to_string(size)); + } + + setHeader("Connection", "close"); + + std::stringstream ss; + ss << "HTTP/1.1 " << statusCode() << " " << http_status_str(static_cast(statusCode())) << kCRLF; + + for (auto &header : m_headers) { + ss << header.first << ": " << header.second << kCRLF; + } + + ss << kCRLF; + const std::string header = ss.str(); + + uv_buf_t bufs[2]; + bufs[0].base = const_cast(header.c_str()); + +# ifdef _WIN32 + bufs[0].len = static_cast(header.size()); +# else + bufs[0].len = header.size(); +# endif + + if (data) { + bufs[1].base = const_cast(data); + +# ifdef _WIN32 + bufs[1].len = static_cast(size); +# else + bufs[1].len = size; +# endif + } + + HttpContext *ctx = HttpContext::get(m_id); + +# ifndef APP_DEBUG + if (statusCode() >= 400) +# endif + { + const bool err = statusCode() >= 400; + char ip[46] = {}; + sockaddr_storage addr = {}; + int aSize = sizeof(addr); + + uv_tcp_getpeername(ctx->tcp, reinterpret_cast(&addr), &aSize); + if (reinterpret_cast(&addr)->sin_family == AF_INET6) { + uv_ip6_name(reinterpret_cast(&addr), ip, 45); + } + else { + uv_ip4_name(reinterpret_cast(&addr), ip, 16); + } + + Log::print(err ? Log::ERR : Log::INFO, CYAN("%s ") CLEAR MAGENTA_BOLD("%s") WHITE_BOLD(" %s ") CSI "1;%dm%d " CLEAR WHITE_BOLD("%zu ") BLACK_BOLD("\"%s\""), + ip, + http_method_str(static_cast(ctx->method)), + ctx->url.c_str(), + err ? 31 : 32, + statusCode(), + header.size() + size, + ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr + ); + } + + uv_try_write(ctx->stream(), bufs, data ? 2 : 1); + + ctx->close(); +} diff --git a/src/base/net/http/HttpResponse.h b/src/base/net/http/HttpResponse.h new file mode 100644 index 00000000..c2bc9001 --- /dev/null +++ b/src/base/net/http/HttpResponse.h @@ -0,0 +1,61 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTPRESPONSE_H +#define XMRIG_HTTPRESPONSE_H + + +#include +#include + + +namespace xmrig { + + +class HttpResponse +{ +public: + HttpResponse(uint64_t id, int statusCode = 200); + + inline int statusCode() const { return m_statusCode; } + inline void setHeader(const std::string &key, const std::string &value) { m_headers.insert({ key, value }); } + inline void setStatus(int code) { m_statusCode = code; } + + bool isAlive() const; + void end(const char *data = nullptr, size_t size = 0); + +private: + const uint64_t m_id; + int m_statusCode; + std::map m_headers; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPRESPONSE_H + diff --git a/src/base/net/http/HttpServer.cpp b/src/base/net/http/HttpServer.cpp new file mode 100644 index 00000000..c6e02e44 --- /dev/null +++ b/src/base/net/http/HttpServer.cpp @@ -0,0 +1,91 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include +#include + + +#include "3rdparty/http-parser/http_parser.h" +#include "base/kernel/interfaces/IHttpListener.h" +#include "base/net/http/HttpContext.h" +#include "base/net/http/HttpResponse.h" +#include "base/net/http/HttpServer.h" + + +namespace xmrig { + +static http_parser_settings http_settings; + +} // namespace xmrig + + +xmrig::HttpServer::HttpServer(IHttpListener *listener) : + m_listener(listener) +{ + HttpContext::attach(&http_settings); +} + + +xmrig::HttpServer::~HttpServer() +{ + HttpContext::closeAll(); +} + + +void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) +{ + HttpContext *ctx = new HttpContext(HTTP_REQUEST, m_listener); + uv_accept(stream, ctx->stream()); + + uv_read_start(ctx->stream(), + [](uv_handle_t *, size_t suggested_size, uv_buf_t *buf) + { + buf->base = new char[suggested_size]; + +# ifdef _WIN32 + buf->len = static_cast(suggested_size); +# else + buf->len = suggested_size; +# endif + }, + [](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) + { + HttpContext *ctx = static_cast(tcp->data); + + if (nread >= 0) { + const size_t size = static_cast(nread); + const size_t parsed = http_parser_execute(ctx->parser, &http_settings, buf->base, size); + + if (parsed < size) { + ctx->close(); + } + } else { + ctx->close(); + } + + delete [] buf->base; + }); +} diff --git a/src/base/net/http/HttpServer.h b/src/base/net/http/HttpServer.h new file mode 100644 index 00000000..45f3843d --- /dev/null +++ b/src/base/net/http/HttpServer.h @@ -0,0 +1,62 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2014-2019 heapwolf + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#ifndef XMRIG_HTTPSERVER_H +#define XMRIG_HTTPSERVER_H + + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; + + +#include "base/kernel/interfaces/ITcpServerListener.h" + + +namespace xmrig { + + +class IHttpListener; + + +class HttpServer : public ITcpServerListener +{ +public: + HttpServer(IHttpListener *listener); + ~HttpServer() override; + +protected: + void onConnection(uv_stream_t *stream, uint16_t port) override; + +private: + IHttpListener *m_listener; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPSERVER_H + diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index db6f2663..667dd448 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -30,19 +30,19 @@ #include -#ifndef XMRIG_NO_TLS +#ifdef XMRIG_FEATURE_TLS # include # include # include "base/net/stratum/Tls.h" #endif +#include "base/io/log/Log.h" #include "base/kernel/interfaces/IClientListener.h" #include "base/net/dns/Dns.h" #include "base/net/stratum/Client.h" #include "base/tools/Buffer.h" #include "base/tools/Chrono.h" -#include "common/log/Log.h" #include "net/JobResult.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" @@ -107,7 +107,7 @@ xmrig::Client::~Client() void xmrig::Client::connect() { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (m_pool.isTLS()) { m_tls = new Tls(this); } @@ -184,7 +184,7 @@ bool xmrig::Client::disconnect() bool xmrig::Client::isTLS() const { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS return m_pool.isTLS() && m_tls; # else return false; @@ -194,7 +194,7 @@ bool xmrig::Client::isTLS() const const char *xmrig::Client::tlsFingerprint() const { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (isTLS() && m_pool.fingerprint() == nullptr) { return m_tls->fingerprint(); } @@ -206,7 +206,7 @@ const char *xmrig::Client::tlsFingerprint() const const char *xmrig::Client::tlsVersion() const { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (isTLS()) { return m_tls->version(); } @@ -435,7 +435,7 @@ bool xmrig::Client::parseLogin(const rapidjson::Value &result, int *code) bool xmrig::Client::send(BIO *bio) { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS uv_buf_t buf; buf.len = BIO_get_mem_data(bio, &buf.base); @@ -543,7 +543,7 @@ int64_t xmrig::Client::send(size_t size) { LOG_DEBUG("[%s] send (%d bytes): \"%s\"", url(), size, m_sendBuf); -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (isTLS()) { if (!m_tls->send(m_sendBuf, size)) { return -1; @@ -597,7 +597,7 @@ void xmrig::Client::connect(sockaddr *addr) void xmrig::Client::handshake() { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (isTLS()) { m_expire = Chrono::steadyMSecs() + kResponseTimeout; @@ -661,7 +661,7 @@ void xmrig::Client::onClose() m_socket = nullptr; setState(UnconnectedState); -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (m_tls) { delete m_tls; m_tls = nullptr; @@ -858,7 +858,7 @@ void xmrig::Client::read(ssize_t nread) m_recvBuf.nread(size); -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS if (isTLS()) { LOG_DEBUG("[%s] TLS received (%d bytes)", url(), static_cast(nread)); diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index c0ea8a93..65b44a5b 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -75,7 +75,7 @@ public: constexpr static int kResponseTimeout = 20 * 1000; -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS constexpr static int kInputBufferSize = 1024 * 16; # else constexpr static int kInputBufferSize = 1024 * 2; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index c5f16f4f..2c48ab0b 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -35,7 +35,7 @@ #ifdef APP_DEBUG -# include "common/log/Log.h" +# include "base/io/log/Log.h" #endif @@ -178,7 +178,7 @@ bool xmrig::Pool::isCompatible(const Algorithm &algorithm) const bool xmrig::Pool::isEnabled() const { -# ifdef XMRIG_NO_TLS +# ifndef XMRIG_FEATURE_TLS if (isTLS()) { return false; } diff --git a/src/base/net/stratum/Pools.cpp b/src/base/net/stratum/Pools.cpp index fc132dd5..63102c00 100644 --- a/src/base/net/stratum/Pools.cpp +++ b/src/base/net/stratum/Pools.cpp @@ -23,10 +23,10 @@ */ +#include "base/io/log/Log.h" #include "base/net/stratum/Pools.h" #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" -#include "common/log/Log.h" #include "donate.h" #include "rapidjson/document.h" @@ -161,25 +161,12 @@ void xmrig::Pools::print() const { size_t i = 1; for (const Pool &pool : m_data) { - if (Log::colors) { - const int color = pool.isEnabled() ? (pool.isTLS() ? 32 : 36) : 31; - - Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m variant " WHITE_BOLD("%s"), - i, - color, - pool.url().data(), - pool.algorithm().variantName() - ); - } - else { - Log::i()->text(" * POOL #%-7zu%s%s variant=%s %s", - i, - pool.isEnabled() ? "" : "-", - pool.url().data(), - pool.algorithm().variantName(), - pool.isTLS() ? "TLS" : "" - ); - } + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") CSI "1;%dm%s" CLEAR " variant " WHITE_BOLD("%s"), + i, + (pool.isEnabled() ? (pool.isTLS() ? 32 : 36) : 31), + pool.url().data(), + pool.algorithm().variantName() + ); i++; } diff --git a/src/base/net/stratum/Tls.cpp b/src/base/net/stratum/Tls.cpp index d92d7e54..8916579b 100644 --- a/src/base/net/stratum/Tls.cpp +++ b/src/base/net/stratum/Tls.cpp @@ -27,10 +27,10 @@ #include +#include "base/io/log/Log.h" #include "base/net/stratum/Client.h" #include "base/net/stratum/Tls.h" #include "base/tools/Buffer.h" -#include "common/log/Log.h" #ifdef _MSC_VER diff --git a/src/base/net/tools/TcpServer.cpp b/src/base/net/tools/TcpServer.cpp new file mode 100644 index 00000000..e5e6e4e1 --- /dev/null +++ b/src/base/net/tools/TcpServer.cpp @@ -0,0 +1,102 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + + +#include "base/kernel/interfaces/ITcpServerListener.h" +#include "base/net/tools/TcpServer.h" +#include "base/tools/Handle.h" +#include "base/tools/String.h" + + +static const xmrig::String kLocalHost("127.0.0.1"); + + +xmrig::TcpServer::TcpServer(const String &host, uint16_t port, ITcpServerListener *listener) : + m_host(host.isNull() ? kLocalHost : host), + m_version(0), + m_listener(listener), + m_addr(), + m_port(port) +{ + m_tcp = new uv_tcp_t; + uv_tcp_init(uv_default_loop(), m_tcp); + m_tcp->data = this; + + uv_tcp_nodelay(m_tcp, 1); + + if (m_host.contains(":") && uv_ip6_addr(m_host.data(), m_port, reinterpret_cast(&m_addr)) == 0) { + m_version = 6; + } + else if (uv_ip4_addr(m_host.data(), m_port, reinterpret_cast(&m_addr)) == 0) { + m_version = 4; + } +} + + +xmrig::TcpServer::~TcpServer() +{ + Handle::close(m_tcp); +} + + +int xmrig::TcpServer::bind() +{ + if (!m_version) { + return UV_EAI_ADDRFAMILY; + } + + uv_tcp_bind(m_tcp, reinterpret_cast(&m_addr), 0); + + const int rc = uv_listen(reinterpret_cast(m_tcp), 511, TcpServer::onConnection); + if (rc != 0) { + return rc; + } + + if (!m_port) { + sockaddr_storage storage = {}; + int size = sizeof(storage); + + uv_tcp_getsockname(m_tcp, reinterpret_cast(&storage), &size); + + m_port = ntohs(reinterpret_cast(&storage)->sin_port); + } + + return m_port; +} + + +void xmrig::TcpServer::create(uv_stream_t *stream, int status) +{ + if (status < 0) { + return; + } + + m_listener->onConnection(stream, m_port); +} + + +void xmrig::TcpServer::onConnection(uv_stream_t *stream, int status) +{ + static_cast(stream->data)->create(stream, status); +} diff --git a/src/base/net/tools/TcpServer.h b/src/base/net/tools/TcpServer.h new file mode 100644 index 00000000..fc29fa2d --- /dev/null +++ b/src/base/net/tools/TcpServer.h @@ -0,0 +1,64 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_TCPSERVER_H +#define XMRIG_TCPSERVER_H + + +#include + + +namespace xmrig { + + +class ITcpServerListener; +class String; + + +class TcpServer +{ +public: + TcpServer(const String &host, uint16_t port, ITcpServerListener *listener); + ~TcpServer(); + + int bind(); + +private: + void create(uv_stream_t *stream, int status); + + static void onConnection(uv_stream_t *stream, int status); + + const String &m_host; + int m_version; + ITcpServerListener *m_listener; + sockaddr_storage m_addr; + uint16_t m_port; + uv_tcp_t *m_tcp; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_TCPSERVER_H */ diff --git a/src/base/tools/Handle.h b/src/base/tools/Handle.h index 288c32de..1a7d08f1 100644 --- a/src/base/tools/Handle.h +++ b/src/base/tools/Handle.h @@ -76,17 +76,6 @@ inline void Handle::close(uv_signal_t *handle) } -template<> -inline void Handle::close(uv_getaddrinfo_t *handle) -{ - if (handle) { - uv_cancel(reinterpret_cast(handle)); - - delete handle; - } -} - - template<> inline void Handle::close(uv_fs_event_t *handle) { diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index 6bd0ac80..a74f1978 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -27,7 +27,7 @@ #include -#ifndef XMRIG_NO_TLS +#ifdef XMRIG_FEATURE_TLS # include # include #endif @@ -41,7 +41,7 @@ xmrig::String Platform::m_userAgent; void Platform::init(const char *userAgent) { -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS SSL_library_init(); SSL_load_error_strings(); ERR_load_BIO_strings(); diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 9e9b772d..f2363cd0 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -29,7 +29,7 @@ #include -#include "log/Log.h" +#include "base/io/log/Log.h" #include "Platform.h" #include "version.h" diff --git a/src/common/api/HttpRequest.cpp b/src/common/api/HttpRequest.cpp deleted file mode 100644 index 6898a385..00000000 --- a/src/common/api/HttpRequest.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * 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 . - */ - - -#include -#include - -#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(*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, Content-Type"); - - 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; -} diff --git a/src/common/api/HttpRequest.h b/src/common/api/HttpRequest.h deleted file mode 100644 index f6ff9a40..00000000 --- a/src/common/api/HttpRequest.h +++ /dev/null @@ -1,84 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , - * - * - * 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 . - */ - -#ifndef __HTTPREQUEST_H__ -#define __HTTPREQUEST_H__ - - -#include - - -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__ */ diff --git a/src/common/api/Httpd.cpp b/src/common/api/Httpd.cpp deleted file mode 100644 index 7b5e8578..00000000 --- a/src/common/api/Httpd.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - - -#include -#include - - -#include "api/Api.h" -#include "base/tools/Handle.h" -#include "base/tools/Timer.h" -#include "common/api/Httpd.h" -#include "common/api/HttpReply.h" -#include "common/api/HttpRequest.h" -#include "common/log/Log.h" - - -xmrig::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) -{ - m_timer = new Timer(this); -} - - -xmrig::Httpd::~Httpd() -{ - stop(); - - if (m_daemon) { - MHD_stop_daemon(m_daemon); - } - - delete m_accessToken; -} - - -bool xmrig::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; - } - -# if MHD_VERSION >= 0x00093900 - m_timer->start(kIdleInterval, kIdleInterval); -# else - m_timer->start(kActiveInterval, kActiveInterval); -# endif - - return true; -} - - -void xmrig::Httpd::stop() -{ - delete m_timer; - m_timer = nullptr; -} - - -int xmrig::Httpd::process(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 xmrig::Httpd::run() -{ - MHD_run(m_daemon); - -# if MHD_VERSION >= 0x00093900 - const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); - if (m_idle && info->num_connections) { - m_timer->setRepeat(kActiveInterval); - m_idle = false; - } - else if (!m_idle && !info->num_connections) { - m_timer->setRepeat(kIdleInterval); - m_idle = true; - } -# endif -} - - -int xmrig::Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *, const char *uploadData, size_t *uploadSize, void **con_cls) -{ - 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(cls)->process(req); -} diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 94bbd59f..b0b716c1 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -30,12 +30,7 @@ #include -#ifndef XMRIG_NO_HTTPD -# include -#endif - - -#ifndef XMRIG_NO_TLS +#ifdef XMRIG_FEATURE_TLS # include #endif @@ -55,8 +50,8 @@ #include "base/io/Json.h" +#include "base/io/log/Log.h" #include "common/config/CommonConfig.h" -#include "common/log/Log.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -66,46 +61,18 @@ xmrig::CommonConfig::CommonConfig() : m_algorithm(CRYPTONIGHT, VARIANT_AUTO), m_adjusted(false), - m_apiIPv6(false), - m_apiRestricted(true), m_autoSave(true), m_background(false), m_dryRun(false), m_syslog(false), + m_upgrade(false), m_watch(true), - m_apiPort(0), m_printTime(60), m_state(NoneState) { } -bool xmrig::CommonConfig::isColors() const -{ - return Log::colors; -} - - -void xmrig::CommonConfig::printAPI() -{ -# ifndef XMRIG_NO_API - if (apiPort() == 0) { - return; - } - - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN("%s:") CYAN_BOLD("%d") - : " * %-13s%s:%d", - "API BIND", isApiIPv6() ? "[::]" : "0.0.0.0", apiPort()); -# endif -} - - -void xmrig::CommonConfig::printPools() -{ - m_pools.print(); -} - - void xmrig::CommonConfig::printVersions() { char buf[256] = { 0 }; @@ -118,9 +85,7 @@ void xmrig::CommonConfig::printVersions() snprintf(buf, sizeof buf, "MSVC/%d", MSVC_VERSION); # endif - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" %s") - : " * %-13s%s/%s %s", - "ABOUT", APP_NAME, APP_VERSION, buf); + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%s/%s") WHITE_BOLD(" %s"), "ABOUT", APP_NAME, APP_VERSION, buf); # if defined(XMRIG_AMD_PROJECT) # if CL_VERSION_2_0 @@ -141,25 +106,19 @@ void xmrig::CommonConfig::printVersions() # else memset(buf, 0, 16); -# if !defined(XMRIG_NO_HTTPD) || !defined(XMRIG_NO_TLS) +# if defined(XMRIG_FEATURE_HTTP) || defined(XMRIG_FEATURE_TLS) int length = 0; # endif # endif -# if !defined(XMRIG_NO_TLS) && defined(OPENSSL_VERSION_TEXT) +# if defined(XMRIG_FEATURE_TLS) && defined(OPENSSL_VERSION_TEXT) { constexpr const char *v = OPENSSL_VERSION_TEXT + 8; length += snprintf(buf + length, (sizeof buf) - length, "OpenSSL/%.*s ", static_cast(strchr(v, ' ') - v), v); } # endif -# ifndef XMRIG_NO_HTTPD - length += snprintf(buf + length, (sizeof buf) - length, "microhttpd/%s ", MHD_get_version()); -# endif - - Log::i()->text(isColors() ? GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s") - : " * %-13slibuv/%s %s", - "LIBS", uv_version_string(), buf); + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13slibuv/%s %s"), "LIBS", uv_version_string(), buf); } @@ -240,12 +199,23 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) m_watch = enable; break; - case ApiIPv6Key: /* ipv6 */ - m_apiIPv6 = enable; +# ifdef XMRIG_DEPRECATED + case ApiIPv6Key: /* --api-ipv6 */ break; - case ApiRestrictedKey: /* restricted */ - m_apiRestricted = enable; + case ApiRestrictedKey: /* --api-no-restricted */ + fputs("option \"--api-no-restricted\" deprecated, use \"--http-no-restricted\" instead.\n", stderr); + fflush(stdout); + m_http.setRestricted(enable); + break; +# endif + + case HttpRestrictedKey: /* --http-no-restricted */ + m_http.setRestricted(enable); + break; + + case HttpEnabledKey: /* --http-enabled */ + m_http.setEnabled(enable); break; case DryRunKey: /* --dry-run */ @@ -301,8 +271,20 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) m_logFile = arg; break; +# ifdef XMRIG_DEPRECATED case ApiAccessTokenKey: /* --api-access-token */ - m_apiToken = arg; + fputs("option \"--api-access-token\" deprecated, use \"--http-access-token\" instead.\n", stderr); + fflush(stdout); + m_http.setToken(arg); + break; +# endif + + case HttpAccessTokenKey: /* --http-access-token */ + m_http.setToken(arg); + break; + + case HttpHostKey: /* --http-host */ + m_http.setHost(arg); break; case ApiWorkerIdKey: /* --api-worker-id */ @@ -319,32 +301,33 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) case RetriesKey: /* --retries */ case RetryPauseKey: /* --retry-pause */ - case ApiPort: /* --api-port */ case PrintTimeKey: /* --print-time */ - return parseUint64(key, strtol(arg, nullptr, 10)); + case HttpPort: /* --http-port */ +# ifdef XMRIG_DEPRECATED + case ApiPort: /* --api-port */ +# endif + return parseUint64(key, static_cast(strtol(arg, nullptr, 10))); - case BackgroundKey: /* --background */ - case SyslogKey: /* --syslog */ - case KeepAliveKey: /* --keepalive */ - case NicehashKey: /* --nicehash */ - case TlsKey: /* --tls */ - case ApiIPv6Key: /* --api-ipv6 */ - case DryRunKey: /* --dry-run */ + case BackgroundKey: /* --background */ + case SyslogKey: /* --syslog */ + case KeepAliveKey: /* --keepalive */ + case NicehashKey: /* --nicehash */ + case TlsKey: /* --tls */ + case DryRunKey: /* --dry-run */ + case HttpEnabledKey: /* --http-enabled */ return parseBoolean(key, true); - case ColorKey: /* --no-color */ - case WatchKey: /* --no-watch */ + case ColorKey: /* --no-color */ + case WatchKey: /* --no-watch */ + case HttpRestrictedKey: /* --http-no-restricted */ +# ifdef XMRIG_DEPRECATED case ApiRestrictedKey: /* --api-no-restricted */ + case ApiIPv6Key: /* --api-ipv6 */ +# endif return parseBoolean(key, false); case DonateLevelKey: /* --donate-level */ -# ifdef XMRIG_PROXY_PROJECT - if (strncmp(arg, "minemonero.pro", 14) == 0) { - m_donateLevel = 0; - return true; - } -# endif - return parseUint64(key, strtol(arg, nullptr, 10)); + return parseUint64(key, static_cast(strtol(arg, nullptr, 10))); default: break; @@ -360,12 +343,27 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg) } -void xmrig::CommonConfig::parseJSON(const rapidjson::Document &doc) +void xmrig::CommonConfig::parseJSON(const rapidjson::Value &json) { - const rapidjson::Value &pools = doc["pools"]; + const rapidjson::Value &pools = json["pools"]; if (pools.IsArray()) { m_pools.load(pools); } + +# ifdef XMRIG_DEPRECATED + const rapidjson::Value &api = json["api"]; + if (api.IsObject() && api.HasMember("port")) { + m_upgrade = true; + m_http.load(api); + m_http.setEnabled(Json::getUint(api, "port") > 0); + m_http.setHost("0.0.0.0"); + } + else { + m_http.load(json["http"]); + } +# else + m_http.load(doc["http"]); +# endif } @@ -402,10 +400,16 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) m_pools.setProxyDonate(arg); break; +# ifdef XMRIG_DEPRECATED case ApiPort: /* --api-port */ - if (arg > 0 && arg <= 65536) { - m_apiPort = arg; - } + fputs("option \"--api-port\" deprecated, use \"--http-port\" instead.\n", stderr); + fflush(stdout); + m_http.setPort(arg); + break; +# endif + + case HttpPort: /* --http-port */ + m_http.setPort(arg); break; case PrintTimeKey: /* --print-time */ diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index e11069da..55f36d0d 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -26,8 +26,8 @@ #define XMRIG_COMMONCONFIG_H +#include "base/net/http/Http.h" #include "base/net/stratum/Pools.h" -#include "base/tools/String.h" #include "common/interfaces/IConfig.h" #include "common/xmrig.h" @@ -40,19 +40,16 @@ class CommonConfig : public IConfig public: CommonConfig(); - inline bool isApiIPv6() const { return m_apiIPv6; } - inline bool isApiRestricted() const { return m_apiRestricted; } inline bool isAutoSave() const { return m_autoSave; } inline bool isBackground() const { return m_background; } inline bool isDryRun() const { return m_dryRun; } inline bool isSyslog() const { return m_syslog; } - inline const char *apiId() const { return m_apiId.data(); } - inline const char *apiToken() const { return m_apiToken.data(); } - inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } + inline const String &apiId() const { return m_apiId; } + inline const String &apiWorkerId() const { return m_apiWorkerId; } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } + inline const Http &http() const { return m_http; } inline const Pools &pools() const { return m_pools; } - inline int apiPort() const { return m_apiPort; } inline int printTime() const { return m_printTime; } inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } @@ -61,9 +58,6 @@ public: bool save() override; - bool isColors() const; - void printAPI(); - void printPools(); void printVersions(); protected: @@ -77,24 +71,22 @@ protected: bool parseBoolean(int key, bool enable) override; bool parseString(int key, const char *arg) override; bool parseUint64(int key, uint64_t arg) override; - void parseJSON(const rapidjson::Document &doc) override; + void parseJSON(const rapidjson::Value &json) override; void setFileName(const char *fileName) override; Algorithm m_algorithm; bool m_adjusted; - bool m_apiIPv6; - bool m_apiRestricted; bool m_autoSave; bool m_background; bool m_dryRun; bool m_syslog; + bool m_upgrade; bool m_watch; - int m_apiPort; + Http m_http; int m_printTime; Pools m_pools; State m_state; String m_apiId; - String m_apiToken; String m_apiWorkerId; String m_fileName; String m_logFile; diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index 26742e5f..2b7f50ce 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -29,16 +29,6 @@ #include -#ifndef XMRIG_NO_HTTPD -# include -#endif - - -#ifndef XMRIG_NO_TLS -# include -#endif - - #include "base/io/Json.h" #include "base/kernel/interfaces/IConfigListener.h" #include "base/kernel/Process.h" @@ -46,21 +36,24 @@ #include "common/config/ConfigWatcher.h" #include "common/interfaces/IConfig.h" #include "common/Platform.h" -#include "core/ConfigCreator.h" -#include "core/ConfigLoader_platform.h" +#include "core/config/Config.h" +#include "core/config/ConfigLoader_platform.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/fwd.h" #ifdef XMRIG_FEATURE_EMBEDDED_CONFIG -# include "core/ConfigLoader_default.h" +# include "core/config/ConfigLoader_default.h" #endif -xmrig::ConfigWatcher *xmrig::ConfigLoader::m_watcher = nullptr; -xmrig::IConfigCreator *xmrig::ConfigLoader::m_creator = nullptr; -xmrig::IConfigListener *xmrig::ConfigLoader::m_listener = nullptr; +namespace xmrig { + +ConfigWatcher *ConfigLoader::m_watcher = nullptr; +IConfigListener *ConfigLoader::m_listener = nullptr; + +} // namespace xmrig #ifndef ARRAY_SIZE @@ -95,28 +88,28 @@ bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const char *json) } -bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson::Document &doc) +bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson::Value &json) { for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) { - parseJSON(config, &config_options[i], doc); + parseJSON(config, &config_options[i], json); } - const rapidjson::Value &api = doc["api"]; + const rapidjson::Value &api = json["api"]; if (api.IsObject()) { for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { parseJSON(config, &api_options[i], api); } } - config->parseJSON(doc); + config->parseJSON(json); return config->finalize(); } -bool xmrig::ConfigLoader::reload(xmrig::IConfig *oldConfig, const char *json) +bool xmrig::ConfigLoader::reload(xmrig::IConfig *oldConfig, const rapidjson::Value &json) { - xmrig::IConfig *config = m_creator->create(); + IConfig *config = Config::create(); if (!loadFromJSON(config, json)) { delete config; @@ -145,17 +138,16 @@ bool xmrig::ConfigLoader::watch(IConfig *config) assert(m_watcher == nullptr); - m_watcher = new xmrig::ConfigWatcher(config->fileName(), m_creator, m_listener); + m_watcher = new ConfigWatcher(config->fileName(), m_listener); return true; } -xmrig::IConfig *xmrig::ConfigLoader::load(Process *process, IConfigCreator *creator, IConfigListener *listener) +xmrig::IConfig *xmrig::ConfigLoader::load(Process *process, IConfigListener *listener) { - m_creator = creator; m_listener = listener; - xmrig::IConfig *config = m_creator->create(); + IConfig *config = Config::create(); int key; int argc = process->arguments().argc(); char **argv = process->arguments().argv(); @@ -181,7 +173,7 @@ xmrig::IConfig *xmrig::ConfigLoader::load(Process *process, IConfigCreator *crea if (!config->finalize()) { delete config; - config = m_creator->create(); + config = Config::create(); loadFromFile(config, process->location(Process::ExeLocation, "config.json")); } @@ -189,7 +181,7 @@ xmrig::IConfig *xmrig::ConfigLoader::load(Process *process, IConfigCreator *crea if (!config->finalize()) { delete config; - config = m_creator->create(); + config = Config::create(); loadFromJSON(config, default_config); } # endif @@ -213,10 +205,8 @@ xmrig::IConfig *xmrig::ConfigLoader::load(Process *process, IConfigCreator *crea void xmrig::ConfigLoader::release() { delete m_watcher; - delete m_creator; m_watcher = nullptr; - m_creator = nullptr; } @@ -239,7 +229,7 @@ bool xmrig::ConfigLoader::getJSON(const char *fileName, rapidjson::Document &doc bool xmrig::ConfigLoader::parseArg(xmrig::IConfig *config, int key, const char *arg) { - if (key == xmrig::IConfig::ConfigKey) { + if (key == IConfig::ConfigKey) { return loadFromFile(config, arg); } diff --git a/src/common/config/ConfigLoader.h b/src/common/config/ConfigLoader.h index 99d6143f..f03de711 100644 --- a/src/common/config/ConfigLoader.h +++ b/src/common/config/ConfigLoader.h @@ -39,7 +39,6 @@ namespace xmrig { class ConfigWatcher; -class IConfigCreator; class IConfigListener; class IConfig; class Process; @@ -50,10 +49,10 @@ class ConfigLoader public: static bool loadFromFile(IConfig *config, const char *fileName); static bool loadFromJSON(IConfig *config, const char *json); - static bool loadFromJSON(IConfig *config, const rapidjson::Document &doc); - static bool reload(IConfig *oldConfig, const char *json); + static bool loadFromJSON(IConfig *config, const rapidjson::Value &json); + static bool reload(IConfig *oldConfig, const rapidjson::Value &json); static bool watch(IConfig *config); - static IConfig *load(Process *process, IConfigCreator *creator, IConfigListener *listener); + static IConfig *load(Process *process, IConfigListener *listener); static void release(); private: @@ -62,11 +61,11 @@ private: static void parseJSON(IConfig *config, const struct option *option, const rapidjson::Value &object); static ConfigWatcher *m_watcher; - static IConfigCreator *m_creator; static IConfigListener *m_listener; }; } /* namespace xmrig */ + #endif /* XMRIG_CONFIGLOADER_H */ diff --git a/src/common/config/ConfigWatcher.cpp b/src/common/config/ConfigWatcher.cpp index 1e35bc9b..1c70547e 100644 --- a/src/common/config/ConfigWatcher.cpp +++ b/src/common/config/ConfigWatcher.cpp @@ -23,16 +23,15 @@ */ +#include "base/io/log/Log.h" #include "base/io/Watcher.h" #include "base/kernel/interfaces/IConfigListener.h" #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" -#include "common/log/Log.h" -#include "core/ConfigCreator.h" +#include "core/config/Config.h" -xmrig::ConfigWatcher::ConfigWatcher(const String &path, IConfigCreator *creator, IConfigListener *listener) : - m_creator(creator), +xmrig::ConfigWatcher::ConfigWatcher(const String &path, IConfigListener *listener) : m_listener(listener) { m_watcher = new Watcher(path, this); @@ -50,7 +49,7 @@ void xmrig::ConfigWatcher::onFileChanged(const String &fileName) { LOG_WARN("\"%s\" was changed, reloading configuration", fileName.data()); - IConfig *config = m_creator->create(); + IConfig *config = Config::create(); ConfigLoader::loadFromFile(config, fileName); if (!config->finalize()) { diff --git a/src/common/config/ConfigWatcher.h b/src/common/config/ConfigWatcher.h index c2c3ee29..77b41cf5 100644 --- a/src/common/config/ConfigWatcher.h +++ b/src/common/config/ConfigWatcher.h @@ -37,7 +37,6 @@ struct option; namespace xmrig { -class IConfigCreator; class IConfigListener; class Watcher; @@ -45,14 +44,13 @@ class Watcher; class ConfigWatcher : public IWatcherListener { public: - ConfigWatcher(const String &path, IConfigCreator *creator, IConfigListener *listener); + ConfigWatcher(const String &path, IConfigListener *listener); ~ConfigWatcher() override; protected: void onFileChanged(const String &fileName) override; private: - IConfigCreator *m_creator; IConfigListener *m_listener; Watcher *m_watcher; }; diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index e898c24c..071b3478 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -41,94 +41,102 @@ class IConfig public: enum Keys { // common - AlgorithmKey = 'a', - ApiAccessTokenKey = 4001, - ApiIPv6Key = 4003, - ApiPort = 4000, - ApiRestrictedKey = 4004, - ApiWorkerIdKey = 4002, - ApiIdKey = 4005, - BackgroundKey = 'B', - ColorKey = 1002, - ConfigKey = 'c', - DonateLevelKey = 1003, - KeepAliveKey = 'k', - LogFileKey = 'l', - PasswordKey = 'p', - RetriesKey = 'r', - RetryPauseKey = 'R', - RigIdKey = 1012, - SyslogKey = 'S', - UrlKey = 'o', - UserAgentKey = 1008, - UserKey = 'u', - UserpassKey = 'O', - VariantKey = 1010, - VerboseKey = 1100, - WatchKey = 1105, - TlsKey = 1013, - FingerprintKey = 1014, - AutoSaveKey = 1016, - ProxyDonateKey = 1017, + AlgorithmKey = 'a', + ApiWorkerIdKey = 4002, + ApiIdKey = 4005, + HttpPort = 4100, + HttpAccessTokenKey = 4101, + HttpRestrictedKey = 4104, + HttpEnabledKey = 4106, + HttpHostKey = 4107, + BackgroundKey = 'B', + ColorKey = 1002, + ConfigKey = 'c', + DonateLevelKey = 1003, + KeepAliveKey = 'k', + LogFileKey = 'l', + PasswordKey = 'p', + RetriesKey = 'r', + RetryPauseKey = 'R', + RigIdKey = 1012, + SyslogKey = 'S', + UrlKey = 'o', + UserAgentKey = 1008, + UserKey = 'u', + UserpassKey = 'O', + VariantKey = 1010, + VerboseKey = 1100, + WatchKey = 1105, + TlsKey = 1013, + FingerprintKey = 1014, + AutoSaveKey = 1016, + ProxyDonateKey = 1017, + +# ifdef XMRIG_DEPRECATED + ApiPort = 4000, + ApiAccessTokenKey = 4001, + ApiIPv6Key = 4003, + ApiRestrictedKey = 4004, +# endif // xmrig common - CPUPriorityKey = 1021, - NicehashKey = 1006, - PrintTimeKey = 1007, + CPUPriorityKey = 1021, + NicehashKey = 1006, + PrintTimeKey = 1007, // xmrig cpu - AVKey = 'v', - CPUAffinityKey = 1020, - DryRunKey = 5000, - HugePagesKey = 1009, - MaxCPUUsageKey = 1004, - SafeKey = 1005, - ThreadsKey = 't', - HardwareAESKey = 1011, - AssemblyKey = 1015, + AVKey = 'v', + CPUAffinityKey = 1020, + DryRunKey = 5000, + HugePagesKey = 1009, + MaxCPUUsageKey = 1004, + SafeKey = 1005, + ThreadsKey = 't', + HardwareAESKey = 1011, + AssemblyKey = 1015, // xmrig amd - OclPlatformKey = 1400, - OclAffinityKey = 1401, - OclDevicesKey = 1402, - OclLaunchKey = 1403, - OclCacheKey = 1404, - OclPrintKey = 1405, - OclLoaderKey = 1406, - OclSridedIndexKey = 1407, - OclMemChunkKey = 1408, - OclUnrollKey = 1409, - OclCompModeKey = 1410, + OclPlatformKey = 1400, + OclAffinityKey = 1401, + OclDevicesKey = 1402, + OclLaunchKey = 1403, + OclCacheKey = 1404, + OclPrintKey = 1405, + OclLoaderKey = 1406, + OclSridedIndexKey = 1407, + OclMemChunkKey = 1408, + OclUnrollKey = 1409, + OclCompModeKey = 1410, // xmrig-proxy - AccessLogFileKey = 'A', - BindKey = 'b', - CoinKey = 1104, - CustomDiffKey = 1102, - DebugKey = 1101, - ModeKey = 'm', - PoolCoinKey = 'C', - ReuseTimeoutKey = 1106, - WorkersKey = 1103, - WorkersAdvKey = 1107, - TlsBindKey = 1108, - TlsCertKey = 1109, - TlsCertKeyKey = 1110, - TlsDHparamKey = 1111, - TlsCiphersKey = 1112, - TlsCipherSuitesKey = 1113, - TlsProtocolsKey = 1114, - AlgoExtKey = 1115, - ProxyPasswordKey = 1116, + AccessLogFileKey = 'A', + BindKey = 'b', + CoinKey = 1104, + CustomDiffKey = 1102, + DebugKey = 1101, + ModeKey = 'm', + PoolCoinKey = 'C', + ReuseTimeoutKey = 1106, + WorkersKey = 1103, + WorkersAdvKey = 1107, + TlsBindKey = 1108, + TlsCertKey = 1109, + TlsCertKeyKey = 1110, + TlsDHparamKey = 1111, + TlsCiphersKey = 1112, + TlsCipherSuitesKey = 1113, + TlsProtocolsKey = 1114, + AlgoExtKey = 1115, + ProxyPasswordKey = 1116, // xmrig nvidia - CudaMaxThreadsKey = 1200, - CudaBFactorKey = 1201, - CudaBSleepKey = 1202, - CudaDevicesKey = 1203, - CudaLaunchKey = 1204, - CudaAffinityKey = 1205, - CudaMaxUsageKey = 1206, + CudaMaxThreadsKey = 1200, + CudaBFactorKey = 1201, + CudaBSleepKey = 1202, + CudaDevicesKey = 1203, + CudaLaunchKey = 1204, + CudaAffinityKey = 1205, + CudaMaxUsageKey = 1206, }; virtual ~IConfig() = default; @@ -142,7 +150,7 @@ public: virtual const Algorithm &algorithm() const = 0; virtual const String &fileName() const = 0; virtual void getJSON(rapidjson::Document &doc) const = 0; - virtual void parseJSON(const rapidjson::Document &doc) = 0; + virtual void parseJSON(const rapidjson::Value &json) = 0; virtual void setFileName(const char *fileName) = 0; }; diff --git a/src/common/log/Log.cpp b/src/common/log/Log.cpp deleted file mode 100644 index 4ef1e14a..00000000 --- a/src/common/log/Log.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - - -#include -#include -#include -#include -#include - - -#include "common/interfaces/ILogBackend.h" -#include "common/log/BasicLog.h" -#include "common/log/Log.h" - - -namespace xmrig { - -Log *Log::m_self = nullptr; -bool Log::colors = true; - - -static const char *color[5] = { - "\x1B[0;31m", /* ERR */ - "\x1B[0;33m", /* WARNING */ - "\x1B[1;37m", /* NOTICE */ - "", /* INFO */ -# ifdef WIN32 - "\x1B[1;30m" /* DEBUG */ -# else - "\x1B[90m" /* DEBUG */ -# endif -}; - -} /* namespace xmrig */ - - -void xmrig::Log::message(ILogBackend::Level level, const char* fmt, ...) -{ - uv_mutex_lock(&m_mutex); - - va_list args; - va_list copy; - va_start(args, fmt); - - for (ILogBackend *backend : m_backends) { - va_copy(copy, args); - backend->message(level, fmt, copy); - va_end(copy); - } - - va_end(args); - - uv_mutex_unlock(&m_mutex); -} - - -void xmrig::Log::text(const char* fmt, ...) -{ - uv_mutex_lock(&m_mutex); - - va_list args; - va_list copy; - va_start(args, fmt); - - for (ILogBackend *backend : m_backends) { - va_copy(copy, args); - backend->text(fmt, copy); - va_end(copy); - } - - va_end(args); - - uv_mutex_unlock(&m_mutex); -} - - -const char *xmrig::Log::colorByLevel(ILogBackend::Level level, bool isColors) -{ - if (!isColors) { - return ""; - } - - return color[level]; -} - - -const char *xmrig::Log::endl(bool isColors) -{ -# ifdef _WIN32 - return isColors ? "\x1B[0m\r\n" : "\r\n"; -# else - return isColors ? "\x1B[0m\n" : "\n"; -# endif -} - - -void xmrig::Log::defaultInit() -{ - m_self = new Log(); - - add(new BasicLog()); -} - - -xmrig::Log::~Log() -{ - m_self = nullptr; - - for (auto backend : m_backends) { - delete backend; - } -} diff --git a/src/common/log/Log.h b/src/common/log/Log.h deleted file mode 100644 index 9a0ba2a9..00000000 --- a/src/common/log/Log.h +++ /dev/null @@ -1,111 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - -#ifndef XMRIG_LOG_H -#define XMRIG_LOG_H - - -#include -#include -#include - - -#include "common/interfaces/ILogBackend.h" - - -namespace xmrig { - - -class Log -{ -public: - static inline Log* i() { if (!m_self) { defaultInit(); } return m_self; } - static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); } - static inline void init() { if (!m_self) { new Log(); } } - static inline void release() { delete m_self; } - - void message(ILogBackend::Level level, const char* fmt, ...); - void text(const char* fmt, ...); - - static const char *colorByLevel(ILogBackend::Level level, bool isColors = true); - static const char *endl(bool isColors = true); - static void defaultInit(); - - static bool colors; - -private: - inline Log() { - assert(m_self == nullptr); - - uv_mutex_init(&m_mutex); - - m_self = this; - } - - ~Log(); - - static Log *m_self; - std::vector m_backends; - uv_mutex_t m_mutex; -}; - - -} /* namespace xmrig */ - - -#define RED_BOLD(x) "\x1B[1;31m" x "\x1B[0m" -#define RED(x) "\x1B[0;31m" x "\x1B[0m" -#define GREEN_BOLD(x) "\x1B[1;32m" x "\x1B[0m" -#define GREEN(x) "\x1B[0;32m" x "\x1B[0m" -#define YELLOW(x) "\x1B[0;33m" x "\x1B[0m" -#define YELLOW_BOLD(x) "\x1B[1;33m" x "\x1B[0m" -#define MAGENTA_BOLD(x) "\x1B[1;35m" x "\x1B[0m" -#define MAGENTA(x) "\x1B[0;35m" x "\x1B[0m" -#define CYAN_BOLD(x) "\x1B[1;36m" x "\x1B[0m" -#define CYAN(x) "\x1B[0;36m" x "\x1B[0m" -#define WHITE_BOLD(x) "\x1B[1;37m" x "\x1B[0m" -#define WHITE(x) "\x1B[0;37m" x "\x1B[0m" -#define GRAY(x) "\x1B[1;30m" x "\x1B[0m" - - -#define LOG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__) -#define LOG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__) -#define LOG_NOTICE(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::NOTICE, x, ##__VA_ARGS__) -#define LOG_INFO(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::INFO, x, ##__VA_ARGS__) - -#ifdef APP_DEBUG -# define LOG_DEBUG(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::DEBUG, x, ##__VA_ARGS__) -#else -# define LOG_DEBUG(x, ...) -#endif - -#if defined(APP_DEBUG) || defined(APP_DEVEL) -# define LOG_DEBUG_ERR(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::ERR, x, ##__VA_ARGS__) -# define LOG_DEBUG_WARN(x, ...) xmrig::Log::i()->message(xmrig::ILogBackend::WARNING, x, ##__VA_ARGS__) -#else -# define LOG_DEBUG_ERR(x, ...) -# define LOG_DEBUG_WARN(x, ...) -#endif - -#endif /* XMRIG_LOG_H */ diff --git a/src/config.json b/src/config.json index 08b912a0..dbb980d0 100644 --- a/src/config.json +++ b/src/config.json @@ -1,11 +1,14 @@ { "algo": "cryptonight", "api": { + "id": null, + "worker-id": null + }, + "http": { + "enabled": false, + "host": "127.0.0.1", "port": 0, "access-token": null, - "id": null, - "worker-id": null, - "ipv6": false, "restricted": true }, "asm": true, @@ -30,6 +33,7 @@ "nicehash": false, "keepalive": false, "variant": -1, + "enabled": true, "tls": false, "tls-fingerprint": null } @@ -40,5 +44,6 @@ "safe": false, "threads": null, "user-agent": null, + "syslog": false, "watch": true } \ No newline at end of file diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index cfd132dd..32507e90 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -26,20 +26,25 @@ #include +#include "base/io/log/backends/ConsoleLog.h" +#include "base/io/log/backends/FileLog.h" +#include "base/io/log/Log.h" +#include "base/kernel/interfaces/IControllerListener.h" #include "common/config/ConfigLoader.h" #include "common/cpu/Cpu.h" -#include "common/interfaces/IControllerListener.h" -#include "common/log/ConsoleLog.h" -#include "common/log/FileLog.h" -#include "common/log/Log.h" #include "common/Platform.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "net/Network.h" #ifdef HAVE_SYSLOG_H -# include "common/log/SysLog.h" +# include "base/io/log/backends/SysLog.h" +#endif + + +#ifdef XMRIG_FEATURE_API +# include "api/Api.h" #endif @@ -47,6 +52,7 @@ class xmrig::ControllerPrivate { public: inline ControllerPrivate(Process *process) : + api(nullptr), config(nullptr), network(nullptr), process(process) @@ -55,11 +61,16 @@ public: inline ~ControllerPrivate() { +# ifdef XMRIG_FEATURE_API + delete api; +# endif + delete network; delete config; } + Api *api; Config *config; Network *network; Process *process; @@ -79,6 +90,14 @@ xmrig::Controller::~Controller() } +xmrig::Api *xmrig::Controller::api() const +{ + assert(d_ptr->api != nullptr); + + return d_ptr->api; +} + + bool xmrig::Controller::isReady() const { return d_ptr->config && d_ptr->network; @@ -102,7 +121,10 @@ int xmrig::Controller::init() return 1; } - Log::init(); +# ifdef XMRIG_FEATURE_API + d_ptr->api = new Api(this); +# endif + Platform::init(config()->userAgent()); Platform::setProcessPriority(d_ptr->config->priority()); @@ -158,7 +180,7 @@ void xmrig::Controller::onNewConfig(IConfig *config) Config *previousConfig = d_ptr->config; d_ptr->config = static_cast(config); - for (xmrig::IControllerListener *listener : d_ptr->listeners) { + for (IControllerListener *listener : d_ptr->listeners) { listener->onConfigChanged(d_ptr->config, previousConfig); } @@ -166,8 +188,24 @@ void xmrig::Controller::onNewConfig(IConfig *config) } +void xmrig::Controller::start() +{ + network()->connect(); + +# ifdef XMRIG_FEATURE_API + api()->start(); +# endif + + save(); +} + + void xmrig::Controller::stop() { +# ifdef XMRIG_FEATURE_API + api()->stop(); +# endif + ConfigLoader::release(); delete d_ptr->network; diff --git a/src/core/Controller.h b/src/core/Controller.h index 7c74b53c..c1ca3da3 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -29,12 +29,10 @@ #include "base/kernel/interfaces/IConfigListener.h" -class StatsData; - - namespace xmrig { +class Api; class Config; class ControllerPrivate; class IControllerListener; @@ -48,12 +46,14 @@ public: Controller(Process *process); ~Controller() override; + Api *api() const; bool isReady() const; Config *config() const; int init(); Network *network() const; void addListener(IControllerListener *listener); void save(); + void start(); void stop(); protected: diff --git a/src/core/Config.cpp b/src/core/config/Config.cpp similarity index 88% rename from src/core/Config.cpp rename to src/core/config/Config.cpp index d566af26..2b706d74 100644 --- a/src/core/Config.cpp +++ b/src/core/config/Config.cpp @@ -27,10 +27,10 @@ #include +#include "base/io/log/Log.h" #include "common/config/ConfigLoader.h" #include "common/cpu/Cpu.h" -#include "core/Config.h" -#include "core/ConfigCreator.h" +#include "core/config/Config.h" #include "crypto/Asm.h" #include "crypto/CryptoNight_constants.h" #include "rapidjson/document.h" @@ -55,7 +55,7 @@ xmrig::Config::Config() : xmrig::CommonConfig(), } -bool xmrig::Config::reload(const char *json) +bool xmrig::Config::reload(const rapidjson::Value &json) { return xmrig::ConfigLoader::reload(this, json); } @@ -72,13 +72,10 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("algo", StringRef(algorithm().name()), allocator); Value api(kObjectType); - api.AddMember("port", apiPort(), allocator); - api.AddMember("access-token", apiToken() ? Value(StringRef(apiToken())).Move() : Value(kNullType).Move(), allocator); - api.AddMember("id", apiId() ? Value(StringRef(apiId())).Move() : Value(kNullType).Move(), allocator); - api.AddMember("worker-id", apiWorkerId() ? Value(StringRef(apiWorkerId())).Move() : Value(kNullType).Move(), allocator); - api.AddMember("ipv6", isApiIPv6(), allocator); - api.AddMember("restricted", isApiRestricted(), allocator); + api.AddMember("id", m_apiId.toJSON(), allocator); + api.AddMember("worker-id", m_apiWorkerId.toJSON(), allocator); doc.AddMember("api", api, allocator); + doc.AddMember("http", m_http.toJSON(doc), allocator); # ifndef XMRIG_NO_ASM doc.AddMember("asm", Asm::toJSON(m_assembly), allocator); @@ -87,7 +84,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("autosave", isAutoSave(), allocator); doc.AddMember("av", algoVariant(), allocator); doc.AddMember("background", isBackground(), allocator); - doc.AddMember("colors", isColors(), allocator); + doc.AddMember("colors", Log::colors, allocator); if (affinity() != -1L) { snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity()); @@ -123,19 +120,15 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("threads", threadsCount(), allocator); } - doc.AddMember("user-agent", userAgent() ? Value(StringRef(userAgent())).Move() : Value(kNullType).Move(), allocator); - -# ifdef HAVE_SYSLOG_H - doc.AddMember("syslog", isSyslog(), allocator); -# endif - - doc.AddMember("watch", m_watch, allocator); + doc.AddMember("user-agent", m_userAgent.toJSON(), allocator); + doc.AddMember("syslog", isSyslog(), allocator); + doc.AddMember("watch", m_watch, allocator); } xmrig::Config *xmrig::Config::load(Process *process, IConfigListener *listener) { - return static_cast(ConfigLoader::load(process, new ConfigCreator(), listener)); + return static_cast(ConfigLoader::load(process, listener)); } @@ -272,7 +265,7 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) switch (key) { case CPUAffinityKey: /* --cpu-affinity */ if (arg) { - m_threads.mask = arg; + m_threads.mask = static_cast(arg); } break; @@ -284,11 +277,11 @@ bool xmrig::Config::parseUint64(int key, uint64_t arg) } -void xmrig::Config::parseJSON(const rapidjson::Document &doc) +void xmrig::Config::parseJSON(const rapidjson::Value &json) { - CommonConfig::parseJSON(doc); + CommonConfig::parseJSON(json); - const rapidjson::Value &threads = doc["threads"]; + const rapidjson::Value &threads = json["threads"]; if (threads.IsArray()) { for (const rapidjson::Value &value : threads.GetArray()) { diff --git a/src/core/Config.h b/src/core/config/Config.h similarity index 92% rename from src/core/Config.h rename to src/core/config/Config.h index d2e8c166..2a8ca11d 100644 --- a/src/core/Config.h +++ b/src/core/config/Config.h @@ -67,7 +67,7 @@ public: Config(); - bool reload(const char *json); + bool reload(const rapidjson::Value &json); void getJSON(rapidjson::Document &doc) const override; @@ -75,11 +75,12 @@ public: inline AlgoVariant algoVariant() const { return m_algoVariant; } inline Assembly assembly() const { return m_assembly; } inline bool isHugePages() const { return m_hugePages; } - inline bool isShouldSave() const { return m_shouldSave && isAutoSave(); } + inline bool isShouldSave() const { return (m_shouldSave || m_upgrade) && isAutoSave(); } inline const std::vector &threads() const { return m_threads.list; } inline int priority() const { return m_priority; } - inline int threadsCount() const { return m_threads.list.size(); } + inline int threadsCount() const { return static_cast(m_threads.list.size()); } inline int64_t affinity() const { return m_threads.mask; } + inline static IConfig *create() { return new Config(); } inline ThreadsMode threadsMode() const { return m_threads.mode; } static Config *load(Process *process, IConfigListener *listener); @@ -89,7 +90,7 @@ protected: bool parseBoolean(int key, bool enable) override; bool parseString(int key, const char *arg) override; bool parseUint64(int key, uint64_t arg) override; - void parseJSON(const rapidjson::Document &doc) override; + void parseJSON(const rapidjson::Value &json) override; private: bool parseInt(int key, int arg); diff --git a/src/core/ConfigLoader_default.h b/src/core/config/ConfigLoader_default.h similarity index 92% rename from src/core/ConfigLoader_default.h rename to src/core/config/ConfigLoader_default.h index 8fd0502b..9ef81fd2 100644 --- a/src/core/ConfigLoader_default.h +++ b/src/core/config/ConfigLoader_default.h @@ -35,11 +35,14 @@ R"===( { "algo": "cryptonight", "api": { + "id": null, + "worker-id": null + }, + "http": { + "enabled": false, + "host": "127.0.0.1", "port": 0, "access-token": null, - "id": null, - "worker-id": null, - "ipv6": false, "restricted": true }, "asm": true, @@ -50,6 +53,7 @@ R"===( "cpu-affinity": null, "cpu-priority": null, "donate-level": 5, + "donate-over-proxy": 1, "huge-pages": true, "hw-aes": null, "log-file": null, @@ -63,6 +67,7 @@ R"===( "nicehash": false, "keepalive": false, "variant": -1, + "enabled": true, "tls": false, "tls-fingerprint": null } @@ -73,7 +78,8 @@ R"===( "safe": false, "threads": null, "user-agent": null, - "watch": false + "syslog": false, + "watch": true } )==="; #endif diff --git a/src/core/ConfigLoader_platform.h b/src/core/config/ConfigLoader_platform.h similarity index 50% rename from src/core/ConfigLoader_platform.h rename to src/core/config/ConfigLoader_platform.h index a13983cd..5026888d 100644 --- a/src/core/ConfigLoader_platform.h +++ b/src/core/config/ConfigLoader_platform.h @@ -44,45 +44,54 @@ static char const short_options[] = "a:c:kBp:Px:r:R:s:t:T:o:u:O:v:l:S"; static struct option const options[] = { - { "algo", 1, nullptr, IConfig::AlgorithmKey }, - { "api-access-token", 1, nullptr, IConfig::ApiAccessTokenKey }, - { "api-port", 1, nullptr, IConfig::ApiPort }, - { "api-worker-id", 1, nullptr, IConfig::ApiWorkerIdKey }, - { "api-id", 1, nullptr, IConfig::ApiIdKey }, - { "api-ipv6", 0, nullptr, IConfig::ApiIPv6Key }, - { "api-no-restricted", 0, nullptr, IConfig::ApiRestrictedKey }, - { "av", 1, nullptr, IConfig::AVKey }, - { "background", 0, nullptr, IConfig::BackgroundKey }, - { "config", 1, nullptr, IConfig::ConfigKey }, - { "cpu-affinity", 1, nullptr, IConfig::CPUAffinityKey }, - { "cpu-priority", 1, nullptr, IConfig::CPUPriorityKey }, - { "donate-level", 1, nullptr, IConfig::DonateLevelKey }, - { "donate-over-proxy", 1, nullptr, IConfig::ProxyDonateKey }, - { "dry-run", 0, nullptr, IConfig::DryRunKey }, - { "keepalive", 0, nullptr, IConfig::KeepAliveKey }, - { "log-file", 1, nullptr, IConfig::LogFileKey }, - { "max-cpu-usage", 1, nullptr, IConfig::MaxCPUUsageKey }, - { "nicehash", 0, nullptr, IConfig::NicehashKey }, - { "no-color", 0, nullptr, IConfig::ColorKey }, - { "no-watch", 0, nullptr, IConfig::WatchKey }, - { "no-huge-pages", 0, nullptr, IConfig::HugePagesKey }, - { "variant", 1, nullptr, IConfig::VariantKey }, - { "pass", 1, nullptr, IConfig::PasswordKey }, - { "print-time", 1, nullptr, IConfig::PrintTimeKey }, - { "retries", 1, nullptr, IConfig::RetriesKey }, - { "retry-pause", 1, nullptr, IConfig::RetryPauseKey }, - { "safe", 0, nullptr, IConfig::SafeKey }, - { "syslog", 0, nullptr, IConfig::SyslogKey }, - { "threads", 1, nullptr, IConfig::ThreadsKey }, - { "url", 1, nullptr, IConfig::UrlKey }, - { "user", 1, nullptr, IConfig::UserKey }, - { "user-agent", 1, nullptr, IConfig::UserAgentKey }, - { "userpass", 1, nullptr, IConfig::UserpassKey }, - { "rig-id", 1, nullptr, IConfig::RigIdKey }, - { "tls", 0, nullptr, IConfig::TlsKey }, - { "tls-fingerprint", 1, nullptr, IConfig::FingerprintKey }, - { "asm", 1, nullptr, IConfig::AssemblyKey }, - { nullptr, 0, nullptr, 0 } + { "algo", 1, nullptr, IConfig::AlgorithmKey }, + { "api-worker-id", 1, nullptr, IConfig::ApiWorkerIdKey }, + { "api-id", 1, nullptr, IConfig::ApiIdKey }, + { "http-enabled", 0, nullptr, IConfig::HttpEnabledKey }, + { "http-host", 1, nullptr, IConfig::HttpHostKey }, + { "http-access-token", 1, nullptr, IConfig::HttpAccessTokenKey }, + { "http-port", 1, nullptr, IConfig::HttpPort }, + { "http-no-restricted", 0, nullptr, IConfig::HttpRestrictedKey }, + { "av", 1, nullptr, IConfig::AVKey }, + { "background", 0, nullptr, IConfig::BackgroundKey }, + { "config", 1, nullptr, IConfig::ConfigKey }, + { "cpu-affinity", 1, nullptr, IConfig::CPUAffinityKey }, + { "cpu-priority", 1, nullptr, IConfig::CPUPriorityKey }, + { "donate-level", 1, nullptr, IConfig::DonateLevelKey }, + { "donate-over-proxy", 1, nullptr, IConfig::ProxyDonateKey }, + { "dry-run", 0, nullptr, IConfig::DryRunKey }, + { "keepalive", 0, nullptr, IConfig::KeepAliveKey }, + { "log-file", 1, nullptr, IConfig::LogFileKey }, + { "max-cpu-usage", 1, nullptr, IConfig::MaxCPUUsageKey }, + { "nicehash", 0, nullptr, IConfig::NicehashKey }, + { "no-color", 0, nullptr, IConfig::ColorKey }, + { "no-watch", 0, nullptr, IConfig::WatchKey }, + { "no-huge-pages", 0, nullptr, IConfig::HugePagesKey }, + { "variant", 1, nullptr, IConfig::VariantKey }, + { "pass", 1, nullptr, IConfig::PasswordKey }, + { "print-time", 1, nullptr, IConfig::PrintTimeKey }, + { "retries", 1, nullptr, IConfig::RetriesKey }, + { "retry-pause", 1, nullptr, IConfig::RetryPauseKey }, + { "safe", 0, nullptr, IConfig::SafeKey }, + { "syslog", 0, nullptr, IConfig::SyslogKey }, + { "threads", 1, nullptr, IConfig::ThreadsKey }, + { "url", 1, nullptr, IConfig::UrlKey }, + { "user", 1, nullptr, IConfig::UserKey }, + { "user-agent", 1, nullptr, IConfig::UserAgentKey }, + { "userpass", 1, nullptr, IConfig::UserpassKey }, + { "rig-id", 1, nullptr, IConfig::RigIdKey }, + { "tls", 0, nullptr, IConfig::TlsKey }, + { "tls-fingerprint", 1, nullptr, IConfig::FingerprintKey }, + { "asm", 1, nullptr, IConfig::AssemblyKey }, + +# ifdef XMRIG_DEPRECATED + { "api-port", 1, nullptr, IConfig::ApiPort }, + { "api-access-token", 1, nullptr, IConfig::ApiAccessTokenKey }, + { "api-no-restricted", 0, nullptr, IConfig::ApiRestrictedKey }, + { "api-ipv6", 0, nullptr, IConfig::ApiIPv6Key }, +# endif + + { nullptr, 0, nullptr, 0 } }; @@ -115,11 +124,7 @@ static struct option const config_options[] = { static struct option const api_options[] = { - { "port", 1, nullptr, IConfig::ApiPort }, - { "access-token", 1, nullptr, IConfig::ApiAccessTokenKey }, { "worker-id", 1, nullptr, IConfig::ApiWorkerIdKey }, - { "ipv6", 0, nullptr, IConfig::ApiIPv6Key }, - { "restricted", 0, nullptr, IConfig::ApiRestrictedKey }, { "id", 1, nullptr, IConfig::ApiIdKey }, { nullptr, 0, nullptr, 0 } }; diff --git a/src/core/config/usage.h b/src/core/config/usage.h new file mode 100644 index 00000000..430a60c7 --- /dev/null +++ b/src/core/config/usage.h @@ -0,0 +1,96 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_USAGE_H +#define XMRIG_USAGE_H + + +#include "version.h" + + +namespace xmrig { + + +static char const usage[] = "\ +Usage: " APP_ID " [OPTIONS]\n\ +Options:\n\ + -a, --algo=ALGO specify the algorithm to use\n\ + cryptonight\n" +#ifndef XMRIG_NO_AEON +"\ + cryptonight-lite\n" +#endif +#ifndef XMRIG_NO_SUMO +"\ + cryptonight-heavy\n" +#endif +"\ + -o, --url=URL URL of mining server\n\ + -O, --userpass=U:P username:password pair for mining server\n\ + -u, --user=USERNAME username for mining server\n\ + -p, --pass=PASSWORD password for mining server\n\ + --rig-id=ID rig identifier for pool-side statistics (needs pool support)\n\ + -t, --threads=N number of miner threads\n\ + -v, --av=N algorithm variation, 0 auto select\n\ + -k, --keepalive send keepalived packet for prevent timeout (needs pool support)\n\ + --nicehash enable nicehash.com support\n\ + --tls enable SSL/TLS support (needs pool support)\n\ + --tls-fingerprint=F pool TLS certificate fingerprint, if set enable strict certificate pinning\n\ + -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ + -R, --retry-pause=N time to pause between retries (default: 5)\n\ + --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ + --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ + --no-huge-pages disable huge pages support\n\ + --no-color disable colored output\n\ + --variant algorithm PoW variant\n\ + --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ + --user-agent set custom user-agent string for pool\n\ + -B, --background run the miner in the background\n\ + -c, --config=FILE load a JSON-format configuration file\n\ + -l, --log-file=FILE log all output to a file\n" +# ifdef HAVE_SYSLOG_H +"\ + -S, --syslog use system log for output messages\n" +# endif +"\ + --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ + --safe safe adjust threads and av settings for current CPU\n\ + --asm=ASM ASM code for cn/2, possible values: auto, none, intel, ryzen, bulldozer.\n\ + --print-time=N print hashrate report every N seconds\n\ + --api-worker-id=ID custom worker-id for API\n\ + --api-id=ID custom instance ID for API\n\ + --http-enabled enable HTTP API\n\ + --http-host=HOST bind host for HTTP API (by default 127.0.0.1)\n\ + --http-port=N bind port for HTTP API\n\ + --http-access-token=T access token for HTTP API\n\ + --http-no-restricted enable full remote access to HTTP API (only if access token set)\n\ + --dry-run test configuration and exit\n\ + -h, --help display this help and exit\n\ + -V, --version output version information and exit\n\ +"; + + +} /* namespace xmrig */ + +#endif /* XMRIG_USAGE_H */ diff --git a/src/core/usage.h b/src/core/usage.h deleted file mode 100644 index 0d5c4781..00000000 --- a/src/core/usage.h +++ /dev/null @@ -1,95 +0,0 @@ -/* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , - * - * 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 . - */ - -#ifndef XMRIG_USAGE_H -#define XMRIG_USAGE_H - - -#include "version.h" - - -namespace xmrig { - - -static char const usage[] = "\ -Usage: " APP_ID " [OPTIONS]\n\ -Options:\n\ - -a, --algo=ALGO specify the algorithm to use\n\ - cryptonight\n" -#ifndef XMRIG_NO_AEON -"\ - cryptonight-lite\n" -#endif -#ifndef XMRIG_NO_SUMO -"\ - cryptonight-heavy\n" -#endif -"\ - -o, --url=URL URL of mining server\n\ - -O, --userpass=U:P username:password pair for mining server\n\ - -u, --user=USERNAME username for mining server\n\ - -p, --pass=PASSWORD password for mining server\n\ - --rig-id=ID rig identifier for pool-side statistics (needs pool support)\n\ - -t, --threads=N number of miner threads\n\ - -v, --av=N algorithm variation, 0 auto select\n\ - -k, --keepalive send keepalived packet for prevent timeout (needs pool support)\n\ - --nicehash enable nicehash.com support\n\ - --tls enable SSL/TLS support (needs pool support)\n\ - --tls-fingerprint=F pool TLS certificate fingerprint, if set enable strict certificate pinning\n\ - -r, --retries=N number of times to retry before switch to backup server (default: 5)\n\ - -R, --retry-pause=N time to pause between retries (default: 5)\n\ - --cpu-affinity set process affinity to CPU core(s), mask 0x3 for cores 0 and 1\n\ - --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ - --no-huge-pages disable huge pages support\n\ - --no-color disable colored output\n\ - --variant algorithm PoW variant\n\ - --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ - --user-agent set custom user-agent string for pool\n\ - -B, --background run the miner in the background\n\ - -c, --config=FILE load a JSON-format configuration file\n\ - -l, --log-file=FILE log all output to a file\n" -# ifdef HAVE_SYSLOG_H -"\ - -S, --syslog use system log for output messages\n" -# endif -"\ - --max-cpu-usage=N maximum CPU usage for automatic threads mode (default 75)\n\ - --safe safe adjust threads and av settings for current CPU\n\ - --asm=ASM ASM code for cn/2, possible values: auto, none, intel, ryzen, bulldozer.\n\ - --print-time=N print hashrate report every N seconds\n\ - --api-port=N port for the miner API\n\ - --api-access-token=T access token for API\n\ - --api-worker-id=ID custom worker-id for API\n\ - --api-id=ID custom instance ID for API\n\ - --api-ipv6 enable IPv6 support for API\n\ - --api-no-restricted enable full remote access (only if API token set)\n\ - --dry-run test configuration and exit\n\ - -h, --help display this help and exit\n\ - -V, --version output version information and exit\n\ -"; - - -} /* namespace xmrig */ - -#endif /* XMRIG_USAGE_H */ diff --git a/src/crypto/cn_gpu_avx.cpp b/src/crypto/cn_gpu_avx.cpp index 3dc7cacb..9f801c80 100644 --- a/src/crypto/cn_gpu_avx.cpp +++ b/src/crypto/cn_gpu_avx.cpp @@ -30,6 +30,12 @@ # include # define __restrict__ __restrict #endif +#ifndef _mm256_bslli_epi128 + #define _mm256_bslli_epi128(a, count) _mm256_slli_si256((a), (count)) +#endif +#ifndef _mm256_bsrli_epi128 + #define _mm256_bsrli_epi128(a, count) _mm256_srli_si256((a), (count)) +#endif inline void prep_dv_avx(__m256i* idx, __m256i& v, __m256& n01) { diff --git a/src/interfaces/IThread.h b/src/interfaces/IThread.h index 3a8708e6..e74b5bca 100644 --- a/src/interfaces/IThread.h +++ b/src/interfaces/IThread.h @@ -51,7 +51,7 @@ public: PentaWay }; - virtual ~IThread() {} + virtual ~IThread() = default; virtual Algo algorithm() const = 0; virtual int priority() const = 0; @@ -61,7 +61,7 @@ public: virtual size_t index() const = 0; virtual Type type() const = 0; -# ifndef XMRIG_NO_API +# ifdef XMRIG_FEATURE_API virtual rapidjson::Value toAPI(rapidjson::Document &doc) const = 0; # endif diff --git a/src/net/Network.cpp b/src/net/Network.cpp index f50e3f0a..5de840ea 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -32,18 +32,25 @@ #include "api/Api.h" +#include "base/io/log/Log.h" #include "base/net/stratum/Client.h" #include "base/net/stratum/SubmitResult.h" #include "base/tools/Chrono.h" #include "base/tools/Timer.h" -#include "common/log/Log.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" +#include "rapidjson/document.h" #include "workers/Workers.h" +#ifdef XMRIG_FEATURE_API +# include "api/Api.h" +# include "api/interfaces/IApiRequest.h" +#endif + + xmrig::Network::Network(Controller *controller) : m_donate(nullptr), m_timer(nullptr) @@ -51,6 +58,10 @@ xmrig::Network::Network(Controller *controller) : Workers::setListener(this); controller->addListener(this); +# ifdef XMRIG_FEATURE_API + controller->api()->addListener(this); +# endif + const Pools &pools = controller->config()->pools(); m_strategy = pools.createStrategy(this); @@ -90,13 +101,12 @@ void xmrig::Network::onActive(IStrategy *strategy, Client *client) m_state.setPool(client->host(), client->port(), client->ip()); const char *tlsVersion = client->tlsVersion(); - LOG_INFO(isColors() ? WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " \x1B[1;30m%s " - : "use pool %s:%d %s %s", + LOG_INFO(WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " " BLACK_BOLD("%s"), client->host(), client->port(), tlsVersion ? tlsVersion : "", client->ip()); const char *fingerprint = client->tlsFingerprint(); if (fingerprint != nullptr) { - LOG_INFO("%sfingerprint (SHA-256): \"%s\"", isColors() ? "\x1B[1;30m" : "", fingerprint); + LOG_INFO(BLACK_BOLD("fingerprint (SHA-256): \"%s\""), fingerprint); } } @@ -153,39 +163,42 @@ void xmrig::Network::onPause(IStrategy *strategy) } +void xmrig::Network::onRequest(IApiRequest &request) +{ +# ifdef XMRIG_FEATURE_API + if (request.method() == IApiRequest::METHOD_GET && (request.url() == "/1/summary" || request.url() == "/api.json")) { + request.accept(); + + getResults(request.reply(), request.doc()); + getConnection(request.reply(), request.doc()); + } +# endif +} + + void xmrig::Network::onResultAccepted(IStrategy *, Client *, const SubmitResult &result, const char *error) { m_state.add(result, error); if (error) { - LOG_INFO(isColors() ? "\x1B[1;31mrejected\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[1;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[1;30m(%" PRIu64 " ms)" - : "rejected (%" PRId64 "/%" PRId64 ") diff %u \"%s\" (%" PRIu64 " ms)", + LOG_INFO(RED_BOLD("rejected") " (%" PRId64 "/%" PRId64 ") diff " WHITE_BOLD("%u") " " RED("\"%s\"") " " BLACK_BOLD("(%" PRIu64 " ms)"), m_state.accepted, m_state.rejected, result.diff, error, result.elapsed); } else { - LOG_INFO(isColors() ? "\x1B[1;32maccepted\x1B[0m (%" PRId64 "/%" PRId64 ") diff \x1B[1;37m%u\x1B[0m \x1B[1;30m(%" PRIu64 " ms)" - : "accepted (%" PRId64 "/%" PRId64 ") diff %u (%" PRIu64 " ms)", + LOG_INFO(GREEN_BOLD("accepted") " (%" PRId64 "/%" PRId64 ") diff " WHITE_BOLD("%u") " " BLACK_BOLD("(%" PRIu64 " ms)"), m_state.accepted, m_state.rejected, result.diff, result.elapsed); } } -bool xmrig::Network::isColors() const -{ - return Log::colors; -} - - void xmrig::Network::setJob(Client *client, const Job &job, bool donate) { if (job.height()) { - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") " height " WHITE_BOLD("%" PRIu64) - : "new job from %s:%d diff %d algo %s height %" PRIu64, + LOG_INFO(MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") " height " WHITE_BOLD("%" PRIu64), client->host(), client->port(), job.diff(), job.algorithm().shortName(), job.height()); } else { - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") - : "new job from %s:%d diff %d algo %s", + LOG_INFO(MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s"), client->host(), client->port(), job.diff(), job.algorithm().shortName()); } @@ -207,8 +220,47 @@ void xmrig::Network::tick() if (m_donate) { m_donate->tick(now); } - -# ifndef XMRIG_NO_API - Api::tick(m_state); -# endif } + + +#ifdef XMRIG_FEATURE_API +void xmrig::Network::getConnection(rapidjson::Value &reply, rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value connection(kObjectType); + connection.AddMember("pool", StringRef(m_state.pool), allocator); + connection.AddMember("uptime", m_state.connectionTime(), allocator); + connection.AddMember("ping", m_state.latency(), allocator); + connection.AddMember("failures", m_state.failures, allocator); + connection.AddMember("error_log", Value(kArrayType), allocator); + + reply.AddMember("connection", connection, allocator); +} + + +void xmrig::Network::getResults(rapidjson::Value &reply, rapidjson::Document &doc) const +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value results(kObjectType); + + results.AddMember("diff_current", m_state.diff, allocator); + results.AddMember("shares_good", m_state.accepted, allocator); + results.AddMember("shares_total", m_state.accepted + m_state.rejected, allocator); + results.AddMember("avg_time", m_state.avgTime(), allocator); + results.AddMember("hashes_total", m_state.total, allocator); + + Value best(kArrayType); + for (size_t i = 0; i < m_state.topDiff.size(); ++i) { + best.PushBack(m_state.topDiff[i], allocator); + } + + results.AddMember("best", best, allocator); + results.AddMember("error_log", Value(kArrayType), allocator); + + reply.AddMember("results", results, allocator); +} +#endif diff --git a/src/net/Network.h b/src/net/Network.h index ac74384e..09e7c815 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -29,11 +29,13 @@ #include -#include "api/NetworkState.h" +#include "api/interfaces/IApiListener.h" +#include "base/kernel/interfaces/IControllerListener.h" #include "base/kernel/interfaces/IStrategyListener.h" #include "base/kernel/interfaces/ITimerListener.h" -#include "common/interfaces/IControllerListener.h" #include "interfaces/IJobResultListener.h" +#include "net/NetworkState.h" +#include "rapidjson/fwd.h" namespace xmrig { @@ -43,7 +45,7 @@ class Controller; class IStrategy; -class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener +class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener, public IApiListener { public: Network(Controller *controller); @@ -61,15 +63,20 @@ protected: void onJob(IStrategy *strategy, Client *client, const Job &job) override; void onJobResult(const JobResult &result) override; void onPause(IStrategy *strategy) override; + void onRequest(IApiRequest &request) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; private: constexpr static int kTickInterval = 1 * 1000; - bool isColors() const; void setJob(Client *client, const Job &job, bool donate); void tick(); +# ifdef XMRIG_FEATURE_API + void getConnection(rapidjson::Value &reply, rapidjson::Document &doc) const; + void getResults(rapidjson::Value &reply, rapidjson::Document &doc) const; +# endif + IStrategy *m_donate; IStrategy *m_strategy; NetworkState m_state; diff --git a/src/api/NetworkState.cpp b/src/net/NetworkState.cpp similarity index 98% rename from src/api/NetworkState.cpp rename to src/net/NetworkState.cpp index 8a541eaa..df248602 100644 --- a/src/api/NetworkState.cpp +++ b/src/net/NetworkState.cpp @@ -29,9 +29,9 @@ #include -#include "api/NetworkState.h" #include "base/net/stratum/SubmitResult.h" #include "base/tools/Chrono.h" +#include "net/NetworkState.h" xmrig::NetworkState::NetworkState() : diff --git a/src/api/NetworkState.h b/src/net/NetworkState.h similarity index 100% rename from src/api/NetworkState.h rename to src/net/NetworkState.h diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index adfb9a99..9275bde9 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -35,7 +35,7 @@ #include "common/crypto/keccak.h" #include "common/Platform.h" #include "common/xmrig.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" diff --git a/src/version.h b/src/version.h index 2c6eb6ef..117f1d6a 100644 --- a/src/version.h +++ b/src/version.h @@ -28,7 +28,7 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.15.0-beta" +#define APP_VERSION "2.15.1-evo" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2019 xmrig.com" @@ -36,7 +36,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 15 -#define APP_VER_PATCH 0 +#define APP_VER_PATCH 1 #ifdef _MSC_VER # if (_MSC_VER >= 1910) diff --git a/src/workers/CpuThread.cpp b/src/workers/CpuThread.cpp index 6548b461..2481162c 100644 --- a/src/workers/CpuThread.cpp +++ b/src/workers/CpuThread.cpp @@ -25,8 +25,8 @@ #include +#include "base/io/log/Log.h" #include "common/cpu/Cpu.h" -#include "common/log/Log.h" #include "crypto/Asm.h" #include "Mem.h" #include "rapidjson/document.h" @@ -706,7 +706,7 @@ void xmrig::CpuThread::print() const #endif -#ifndef XMRIG_NO_API +#ifdef XMRIG_FEATURE_API rapidjson::Value xmrig::CpuThread::toAPI(rapidjson::Document &doc) const { using namespace rapidjson; diff --git a/src/workers/CpuThread.h b/src/workers/CpuThread.h index 05d4a066..2af421be 100644 --- a/src/workers/CpuThread.h +++ b/src/workers/CpuThread.h @@ -90,7 +90,7 @@ protected: void print() const override; # endif -# ifndef XMRIG_NO_API +# ifdef XMRIG_FEATURE_API rapidjson::Value toAPI(rapidjson::Document &doc) const override; # endif diff --git a/src/workers/Hashrate.cpp b/src/workers/Hashrate.cpp index 591b719a..568817cd 100644 --- a/src/workers/Hashrate.cpp +++ b/src/workers/Hashrate.cpp @@ -30,9 +30,9 @@ #include +#include "base/io/log/Log.h" #include "base/tools/Handle.h" -#include "common/log/Log.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "workers/Hashrate.h" @@ -51,8 +51,7 @@ inline static const char *format(double h, char *buf, size_t size) Hashrate::Hashrate(size_t threads, xmrig::Controller *controller) : m_highest(0.0), m_threads(threads), - m_timer(nullptr), - m_controller(controller) + m_timer(nullptr) { m_counts = new uint64_t*[threads]; m_timestamps = new uint64_t*[threads]; @@ -163,8 +162,7 @@ void Hashrate::print() const char num3[8] = { 0 }; char num4[8] = { 0 }; - LOG_INFO(m_controller->config()->isColors() ? WHITE_BOLD("speed") " 10s/60s/15m " CYAN_BOLD("%s") CYAN(" %s %s ") CYAN_BOLD("H/s") " max " CYAN_BOLD("%s H/s") - : "speed 10s/60s/15m %s %s %s H/s max %s H/s", + LOG_INFO(WHITE_BOLD("speed") " 10s/60s/15m " CYAN_BOLD("%s") CYAN(" %s %s ") CYAN_BOLD("H/s") " max " CYAN_BOLD("%s H/s"), format(calc(ShortInterval), num1, sizeof(num1)), format(calc(MediumInterval), num2, sizeof(num2)), format(calc(LargeInterval), num3, sizeof(num3)), diff --git a/src/workers/Hashrate.h b/src/workers/Hashrate.h index 673c6a51..d27b289e 100644 --- a/src/workers/Hashrate.h +++ b/src/workers/Hashrate.h @@ -69,7 +69,6 @@ private: uint64_t** m_counts; uint64_t** m_timestamps; uv_timer_t *m_timer; - xmrig::Controller *m_controller; }; diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index b3c4882b..8b9e0881 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -28,7 +28,6 @@ #include "crypto/CryptoNight_test.h" -#include "common/log/Log.h" #include "workers/CpuThread.h" #include "workers/MultiWorker.h" #include "workers/Workers.h" diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 5426762e..53990135 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -28,9 +28,9 @@ #include "api/Api.h" +#include "base/io/log/Log.h" #include "base/tools/Handle.h" -#include "common/log/Log.h" -#include "core/Config.h" +#include "core/config/Config.h" #include "core/Controller.h" #include "crypto/CryptoNight_constants.h" #include "interfaces/IJobResultListener.h" @@ -99,16 +99,15 @@ void Workers::printHashrate(bool detail) } if (detail) { - const bool isColors = m_controller->config()->isColors(); char num1[8] = { 0 }; char num2[8] = { 0 }; char num3[8] = { 0 }; - xmrig::Log::i()->text("%s| THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |", isColors ? "\x1B[1;37m" : ""); + xmrig::Log::print(WHITE_BOLD_S "| THREAD | AFFINITY | 10s H/s | 60s H/s | 15m H/s |"); size_t i = 0; for (const xmrig::IThread *thread : m_controller->config()->threads()) { - xmrig::Log::i()->text("| %6zu | %8" PRId64 " | %7s | %7s | %7s |", + xmrig::Log::print("| %6zu | %8" PRId64 " | %7s | %7s | %7s |", thread->index(), thread->affinity(), Hashrate::format(m_hashrate->calc(thread->index(), Hashrate::ShortInterval), num1, sizeof num1), @@ -178,7 +177,6 @@ void Workers::start(xmrig::Controller *controller) const std::vector &threads = controller->config()->threads(); m_status.algo = controller->config()->algorithm().algo(); - m_status.colors = controller->config()->isColors(); m_status.threads = threads.size(); for (const xmrig::IThread *thread : threads) { @@ -209,8 +207,6 @@ void Workers::start(xmrig::Controller *controller) m_workers.push_back(handle); handle->start(Workers::onReady); } - - controller->save(); } @@ -239,7 +235,7 @@ void Workers::submit(const xmrig::JobResult &result) } -#ifndef XMRIG_NO_API +#ifdef XMRIG_FEATURE_API void Workers::threadsSummary(rapidjson::Document &doc) { uv_mutex_lock(&m_mutex); @@ -302,7 +298,7 @@ void Workers::onReady(void *arg) } -void Workers::onResult(uv_async_t *handle) +void Workers::onResult(uv_async_t *) { std::list results; @@ -321,7 +317,7 @@ void Workers::onResult(uv_async_t *handle) } -void Workers::onTick(uv_timer_t *handle) +void Workers::onTick(uv_timer_t *) { for (ThreadHandle *handle : m_workers) { if (!handle->worker()) { @@ -350,16 +346,10 @@ void Workers::start(IWorker *worker) const double percent = (double) m_status.hugePages / m_status.pages * 100.0; const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1024; - if (m_status.colors) { - LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "", - m_status.threads, m_status.ways, - (m_status.hugePages == m_status.pages ? "\x1B[1;32m" : (m_status.hugePages == 0 ? "\x1B[1;31m" : "\x1B[1;33m")), - m_status.hugePages, m_status.pages, percent, memory); - } - else { - LOG_INFO("READY (CPU) threads %zu(%zu) huge pages %zu/%zu %1.0f%% memory %zu KB", - m_status.threads, m_status.ways, m_status.hugePages, m_status.pages, percent, memory); - } + LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "", + m_status.threads, m_status.ways, + (m_status.hugePages == m_status.pages ? GREEN_BOLD_S : (m_status.hugePages == 0 ? RED_BOLD_S : YELLOW_BOLD_S)), + m_status.hugePages, m_status.pages, percent, memory); } uv_mutex_unlock(&m_mutex); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 23b0a27c..5b084fc2 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -68,7 +68,7 @@ public: static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } static inline void setListener(xmrig::IJobResultListener *listener) { m_listener = listener; } -# ifndef XMRIG_NO_API +# ifdef XMRIG_FEATURE_API static void threadsSummary(rapidjson::Document &doc); # endif @@ -82,7 +82,6 @@ private: { public: inline LaunchStatus() : - colors(true), hugePages(0), pages(0), started(0), @@ -91,7 +90,6 @@ private: algo(xmrig::CRYPTONIGHT) {} - bool colors; size_t hugePages; size_t pages; size_t started;