diff --git a/cmake/OpenSSL.cmake b/cmake/OpenSSL.cmake index a7f49b7..3f26dbd 100644 --- a/cmake/OpenSSL.cmake +++ b/cmake/OpenSSL.cmake @@ -16,6 +16,8 @@ if (WITH_TLS) set(TLS_SOURCES src/base/net/stratum/Tls.cpp src/base/net/stratum/Tls.h + src/base/net/tls/ServerTls.cpp + src/base/net/tls/ServerTls.h src/base/net/tls/TlsConfig.cpp src/base/net/tls/TlsConfig.h src/base/net/tls/TlsContext.cpp @@ -29,7 +31,14 @@ if (WITH_TLS) include_directories(${OPENSSL_INCLUDE_DIR}) if (WITH_HTTP) - set(TLS_SOURCES ${TLS_SOURCES} src/base/net/http/HttpsClient.h src/base/net/http/HttpsClient.cpp) + set(TLS_SOURCES ${TLS_SOURCES} + src/base/net/https/HttpsClient.cpp + src/base/net/https/HttpsClient.h + src/base/net/https/HttpsContext.cpp + src/base/net/https/HttpsContext.h + src/base/net/https/HttpsServer.cpp + src/base/net/https/HttpsServer.h + ) endif() else() message(FATAL_ERROR "OpenSSL NOT found: use `-DWITH_TLS=OFF` to build without TLS support") @@ -41,5 +50,12 @@ else() set(OPENSSL_LIBRARIES "") remove_definitions(/DXMRIG_FEATURE_TLS) + if (WITH_HTTP) + set(TLS_SOURCES ${TLS_SOURCES} + src/base/net/http/HttpServer.cpp + src/base/net/http/HttpServer.h + ) + endif() + set(CMAKE_PROJECT_NAME "${CMAKE_PROJECT_NAME}-notls") endif() diff --git a/src/base/api/Httpd.cpp b/src/base/api/Httpd.cpp index 37db1ad..b5bf74e 100644 --- a/src/base/api/Httpd.cpp +++ b/src/base/api/Httpd.cpp @@ -29,12 +29,18 @@ #include "base/io/log/Log.h" #include "base/net/http/HttpApiResponse.h" #include "base/net/http/HttpData.h" -#include "base/net/http/HttpServer.h" #include "base/net/tools/TcpServer.h" #include "core/config/Config.h" #include "core/Controller.h" +#ifdef XMRIG_FEATURE_TLS +# include "base/net/https/HttpsServer.h" +#else +# include "base/net/http/HttpServer.h" +#endif + + namespace xmrig { static const char *kAuthorization = "authorization"; @@ -48,10 +54,7 @@ static size_t faviconSize = 0; xmrig::Httpd::Httpd(Base *base) : - m_base(base), - m_http(nullptr), - m_server(nullptr), - m_port(0) + m_base(base) { m_httpListener = std::make_shared(this); @@ -64,13 +67,19 @@ xmrig::Httpd::~Httpd() = default; bool xmrig::Httpd::start() { - const Http &config = m_base->config()->http(); + const auto &config = m_base->config()->http(); if (!config.isEnabled()) { return true; } - m_http = new HttpServer(m_httpListener); +# ifdef XMRIG_FEATURE_TLS + m_http = new HttpsServer(m_httpListener); + m_http->setTls(m_base->config()->tls()); +# else + m_http = new HttpServer(m_httpListener); +# endif + m_server = new TcpServer(config.host(), config.port(), m_http); const int rc = m_server->bind(); @@ -143,7 +152,7 @@ void xmrig::Httpd::onHttpData(const HttpData &data) } # endif - return HttpResponse(data.id(), 404).end(); + return HttpResponse(data.id(), HTTP_STATUS_NOT_FOUND).end(); } if (data.method > 4) { diff --git a/src/base/api/Httpd.h b/src/base/api/Httpd.h index 25d8857..462a067 100644 --- a/src/base/api/Httpd.h +++ b/src/base/api/Httpd.h @@ -40,6 +40,7 @@ namespace xmrig { class Base; class HttpServer; +class HttpsServer; class TcpServer; @@ -61,11 +62,16 @@ protected: private: int auth(const HttpData &req) const; - Base *m_base; - HttpServer *m_http; + const Base *m_base; std::shared_ptr m_httpListener; - TcpServer *m_server; - uint16_t m_port; + TcpServer *m_server = nullptr; + uint16_t m_port = 0; + +# ifdef XMRIG_FEATURE_TLS + HttpsServer *m_http = nullptr; +# else + HttpServer *m_http = nullptr; +# endif }; diff --git a/src/base/base.cmake b/src/base/base.cmake index f2a1438..fe30bd9 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -157,7 +157,6 @@ if (WITH_HTTP) src/base/net/http/HttpContext.h src/base/net/http/HttpData.h src/base/net/http/HttpResponse.h - src/base/net/http/HttpServer.h src/base/net/stratum/DaemonClient.h src/base/net/stratum/SelfSelectClient.h src/base/net/tools/TcpServer.h @@ -176,7 +175,6 @@ if (WITH_HTTP) src/base/net/http/HttpData.cpp src/base/net/http/HttpListener.cpp src/base/net/http/HttpResponse.cpp - src/base/net/http/HttpServer.cpp src/base/net/stratum/DaemonClient.cpp src/base/net/stratum/SelfSelectClient.cpp src/base/net/tools/TcpServer.cpp diff --git a/src/base/net/http/Fetch.cpp b/src/base/net/http/Fetch.cpp index 361769e..a0eb2b5 100644 --- a/src/base/net/http/Fetch.cpp +++ b/src/base/net/http/Fetch.cpp @@ -27,7 +27,7 @@ #ifdef XMRIG_FEATURE_TLS -# include "base/net/http/HttpsClient.h" +# include "base/net/https/HttpsClient.h" #endif diff --git a/src/base/net/http/HttpClient.cpp b/src/base/net/http/HttpClient.cpp index b684dda..70c6282 100644 --- a/src/base/net/http/HttpClient.cpp +++ b/src/base/net/http/HttpClient.cpp @@ -29,7 +29,6 @@ #include "base/io/log/Log.h" #include "base/kernel/Platform.h" #include "base/net/dns/Dns.h" -#include "base/tools/Baton.h" #include @@ -41,31 +40,6 @@ namespace xmrig { static const char *kCRLF = "\r\n"; -class HttpClientWriteBaton : public Baton -{ -public: - inline HttpClientWriteBaton(const std::string &header, std::string &&body) : - m_body(std::move(body)), - m_header(header) - { - m_bufs[0] = uv_buf_init(const_cast(m_header.c_str()), m_header.size()); - m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast(m_body.c_str()), m_body.size()); - } - - void write(uv_stream_t *stream) - { - uv_write(&req, stream, m_bufs, nbufs(), [](uv_write_t *req, int) { delete reinterpret_cast(req->data); }); - } - -private: - inline size_t nbufs() const { return m_bufs[1].len > 0 ? 2 : 1; } - - std::string m_body; - std::string m_header; - uv_buf_t m_bufs[2]{}; -}; - - } // namespace xmrig @@ -137,7 +111,8 @@ void xmrig::HttpClient::handshake() headers.clear(); - write(ss.str()); + body.insert(0, ss.str()); + end(std::move(body)); } @@ -149,13 +124,6 @@ void xmrig::HttpClient::read(const char *data, size_t size) } -void xmrig::HttpClient::write(const std::string &header) -{ - auto baton = new HttpClientWriteBaton(header, std::move(body)); - baton->write(stream()); -} - - void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) { auto client = static_cast(req->data); diff --git a/src/base/net/http/HttpClient.h b/src/base/net/http/HttpClient.h index b54882a..acfe15e 100644 --- a/src/base/net/http/HttpClient.h +++ b/src/base/net/http/HttpClient.h @@ -59,7 +59,6 @@ protected: virtual void handshake(); virtual void read(const char *data, size_t size); - virtual void write(const std::string &header); protected: inline const FetchRequest &req() const { return m_req; } diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index 66a25d6..e2d301b 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -27,6 +27,7 @@ #include "base/net/http/HttpContext.h" #include "3rdparty/http-parser/http_parser.h" #include "base/kernel/interfaces/IHttpListener.h" +#include "base/tools/Baton.h" #include "base/tools/Chrono.h" @@ -42,6 +43,26 @@ static std::map storage; static uint64_t SEQUENCE = 0; +class HttpWriteBaton : public Baton +{ +public: + inline HttpWriteBaton(std::string &&body) : + m_body(std::move(body)) + { + m_buf = uv_buf_init(const_cast(m_body.c_str()), m_body.size()); + } + + void write(uv_stream_t *stream) + { + uv_write(&req, stream, &m_buf, 1, [](uv_write_t *req, int) { delete reinterpret_cast(req->data); }); + } + +private: + std::string m_body; + uv_buf_t m_buf{}; +}; + + } // namespace xmrig @@ -75,6 +96,17 @@ xmrig::HttpContext::~HttpContext() } +void xmrig::HttpContext::end(std::string &&data) +{ + if (uv_is_writable(stream()) != 1) { + return; + } + + auto baton = new HttpWriteBaton(std::move(data)); + baton->write(stream()); +} + + bool xmrig::HttpContext::isRequest() const { return m_parser->type == HTTP_REQUEST; diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h index 784bb7e..9a52784 100644 --- a/src/base/net/http/HttpContext.h +++ b/src/base/net/http/HttpContext.h @@ -65,6 +65,8 @@ public: inline const char *tlsVersion() const override { return nullptr; } inline uint16_t port() const override { return 0; } + void end(std::string &&data) override; + bool isRequest() const override; size_t parse(const char *data, size_t size); std::string ip() const override; diff --git a/src/base/net/http/HttpData.h b/src/base/net/http/HttpData.h index cd85263..e2231dd 100644 --- a/src/base/net/http/HttpData.h +++ b/src/base/net/http/HttpData.h @@ -59,6 +59,7 @@ public: virtual const char *tlsVersion() const = 0; virtual std::string ip() const = 0; virtual uint16_t port() const = 0; + virtual void end(std::string &&data) = 0; int method = 0; int status = 0; diff --git a/src/base/net/http/HttpResponse.cpp b/src/base/net/http/HttpResponse.cpp index ebd0bcf..376a19b 100644 --- a/src/base/net/http/HttpResponse.cpp +++ b/src/base/net/http/HttpResponse.cpp @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2014-2019 heapwolf - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -28,8 +28,6 @@ #include "3rdparty/http-parser/http_parser.h" #include "base/io/log/Log.h" #include "base/net/http/HttpContext.h" -#include "base/tools/Baton.h" -#include "base/tools/Object.h" #include @@ -45,52 +43,6 @@ static const char *kCRLF = "\r\n"; static const char *kUserAgent = "user-agent"; -class WriteBaton : public Baton -{ -public: - XMRIG_DISABLE_COPY_MOVE_DEFAULT(WriteBaton) - - inline WriteBaton(const std::stringstream &ss, const char *data, size_t size, HttpContext *ctx) : - m_ctx(ctx), - m_header(ss.str()) - { - bufs[0].len = m_header.size(); - bufs[0].base = const_cast(m_header.c_str()); - - if (data) { - bufs[1].len = size; - bufs[1].base = new char[size]; - memcpy(bufs[1].base, data, size); - } - else { - bufs[1].base = nullptr; - bufs[1].len = 0; - } - } - - - inline ~WriteBaton() - { - if (count() == 2) { - delete [] bufs[1].base; - } - - m_ctx->close(); - } - - - inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; } - inline size_t size() const { return bufs[0].len + bufs[1].len; } - inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast(req->data); } - - - uv_buf_t bufs[2]{}; - -private: - HttpContext *m_ctx; - std::string m_header; -}; - } // namespace xmrig @@ -134,8 +86,8 @@ void xmrig::HttpResponse::end(const char *data, size_t size) ss << kCRLF; - auto ctx = HttpContext::get(m_id); - auto baton = new WriteBaton(ss, data, size, ctx); + auto ctx = HttpContext::get(m_id); + std::string body = data ? (ss.str() + std::string(data, size)) : ss.str(); # ifndef APP_DEBUG if (statusCode() >= 400) @@ -149,11 +101,11 @@ void xmrig::HttpResponse::end(const char *data, size_t size) ctx->url.c_str(), err ? 31 : 32, statusCode(), - baton->size(), + body.size(), ctx->elapsed(), ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr ); } - uv_write(&baton->req, ctx->stream(), baton->bufs, baton->count(), WriteBaton::onWrite); + ctx->end(std::move(body)); } diff --git a/src/base/net/http/HttpResponse.h b/src/base/net/http/HttpResponse.h index c2bc900..1891ad7 100644 --- a/src/base/net/http/HttpResponse.h +++ b/src/base/net/http/HttpResponse.h @@ -6,8 +6,8 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2014-2019 heapwolf - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 diff --git a/src/base/net/http/HttpServer.cpp b/src/base/net/http/HttpServer.cpp index 4af39c2..db8c70f 100644 --- a/src/base/net/http/HttpServer.cpp +++ b/src/base/net/http/HttpServer.cpp @@ -28,11 +28,11 @@ #include +#include "base/net/http/HttpServer.h" #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" xmrig::HttpServer::HttpServer(const std::shared_ptr &listener) : @@ -56,12 +56,7 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) [](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 + buf->len = suggested_size; }, [](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) { diff --git a/src/base/net/http/HttpsClient.cpp b/src/base/net/https/HttpsClient.cpp similarity index 83% rename from src/base/net/http/HttpsClient.cpp rename to src/base/net/https/HttpsClient.cpp index 96162e9..3923097 100644 --- a/src/base/net/http/HttpsClient.cpp +++ b/src/base/net/https/HttpsClient.cpp @@ -29,7 +29,7 @@ #include -#include "base/net/http/HttpsClient.h" +#include "base/net/https/HttpsClient.h" #include "base/io/log/Log.h" #include "base/tools/Buffer.h" @@ -49,8 +49,8 @@ xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr 0) { - HttpClient::read(m_buf, static_cast(bytes_read)); + static char buf[16384]{}; + + int rc = 0; + while ((rc = SSL_read(m_ssl, buf, sizeof(buf))) > 0) { + HttpClient::read(buf, static_cast(rc)); + } + + if (rc == 0) { + close(UV_EOF); } } -void xmrig::HttpsClient::write(const std::string &header) +void xmrig::HttpsClient::end(std::string &&data) { - SSL_write(m_ssl, (header + body).c_str(), header.size() + body.size()); - body.clear(); + const std::string body = std::move(data); + SSL_write(m_ssl, body.data(), body.size()); flush(); } @@ -184,21 +190,15 @@ bool xmrig::HttpsClient::verifyFingerprint(X509 *cert) void xmrig::HttpsClient::flush() { - uv_buf_t buf; - buf.len = BIO_get_mem_data(m_writeBio, &buf.base); - - if (buf.len == 0) { + if (uv_is_writable(stream()) != 1) { return; } - bool result = false; - if (uv_is_writable(stream())) { - result = uv_try_write(stream(), &buf, 1) == static_cast(buf.len); + char *data = nullptr; + const size_t size = BIO_get_mem_data(m_write, &data); + std::string body(data, size); - if (!result) { - close(UV_EIO); - } - } + (void) BIO_reset(m_write); - (void) BIO_reset(m_writeBio); + HttpContext::end(std::move(body)); } diff --git a/src/base/net/http/HttpsClient.h b/src/base/net/https/HttpsClient.h similarity index 92% rename from src/base/net/http/HttpsClient.h rename to src/base/net/https/HttpsClient.h index afac6e0..02f1a39 100644 --- a/src/base/net/http/HttpsClient.h +++ b/src/base/net/https/HttpsClient.h @@ -55,17 +55,17 @@ public: protected: void handshake() override; void read(const char *data, size_t size) override; - void write(const std::string &header) override; private: + void end(std::string &&data) override; + bool verify(X509 *cert); bool verifyFingerprint(X509 *cert); void flush(); - BIO *m_readBio = nullptr; - BIO *m_writeBio = nullptr; + BIO *m_read = nullptr; + BIO *m_write = nullptr; bool m_ready = false; - char m_buf[1024 * 2]{}; char m_fingerprint[32 * 2 + 8]{}; SSL *m_ssl = nullptr; SSL_CTX *m_ctx = nullptr; diff --git a/src/base/net/https/HttpsContext.cpp b/src/base/net/https/HttpsContext.cpp new file mode 100644 index 0000000..c2d6f14 --- /dev/null +++ b/src/base/net/https/HttpsContext.cpp @@ -0,0 +1,97 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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/net/https/HttpsContext.h" +#include "3rdparty/http-parser/http_parser.h" +#include "base/net/tls/TlsContext.h" + + +#include +#include + + +xmrig::HttpsContext::HttpsContext(TlsContext *tls, const std::weak_ptr &listener) : + HttpContext(HTTP_REQUEST, listener), + ServerTls(tls ? tls->ctx() : nullptr) +{ + if (!tls) { + m_mode = TLS_OFF; + } +} + + +xmrig::HttpsContext::~HttpsContext() = default; + + +void xmrig::HttpsContext::append(char *data, size_t size) +{ + if (m_mode == TLS_AUTO) { + m_mode = isTLS(data, size) ? TLS_ON : TLS_OFF; + } + + if (m_mode == TLS_ON) { + read(data, size); + } + else { + parse(data, size); + } +} + + +bool xmrig::HttpsContext::write(BIO *bio) +{ + if (uv_is_writable(stream()) != 1) { + return false; + } + + char *data = nullptr; + const size_t size = BIO_get_mem_data(bio, &data); + std::string body(data, size); + + (void) BIO_reset(bio); + + HttpContext::end(std::move(body)); + + return true; +} + + +void xmrig::HttpsContext::parse(char *data, size_t size) +{ + if (HttpContext::parse(data, size) < size) { + close(); + } +} + + +void xmrig::HttpsContext::shutdown() +{ + close(); +} + + +void xmrig::HttpsContext::end(std::string &&data) +{ + if (m_mode == TLS_ON) { + send(data.data(), data.size()); + } + else { + HttpContext::end(std::move(data)); + } +} diff --git a/src/base/net/https/HttpsContext.h b/src/base/net/https/HttpsContext.h new file mode 100644 index 0000000..1abc61e --- /dev/null +++ b/src/base/net/https/HttpsContext.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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_HTTPSCONTEXT_H +#define XMRIG_HTTPSCONTEXT_H + + +using BIO = struct bio_st; +using SSL = struct ssl_st; + + +#include "base/net/http/HttpContext.h" +#include "base/net/tls/ServerTls.h" + + +namespace xmrig { + + +class TlsContext; + + +class HttpsContext : public HttpContext, public ServerTls +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsContext) + + HttpsContext(TlsContext *tls, const std::weak_ptr &listener); + ~HttpsContext() override; + + void append(char *data, size_t size); + +protected: + // ServerTls + bool write(BIO *bio) override; + void parse(char *data, size_t size) override; + void shutdown() override; + + // HttpContext + void end(std::string &&data) override; + +private: + enum TlsMode : uint32_t { + TLS_AUTO, + TLS_OFF, + TLS_ON + }; + + TlsMode m_mode = TLS_AUTO; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPSCONTEXT_H + diff --git a/src/base/net/https/HttpsServer.cpp b/src/base/net/https/HttpsServer.cpp new file mode 100644 index 0000000..7ca778b --- /dev/null +++ b/src/base/net/https/HttpsServer.cpp @@ -0,0 +1,92 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 "base/net/https/HttpsServer.h" +#include "3rdparty/http-parser/http_parser.h" +#include "base/kernel/interfaces/IHttpListener.h" +#include "base/net/http/HttpResponse.h" +#include "base/net/https/HttpsContext.h" +#include "base/net/tls/TlsConfig.h" +#include "base/net/tls/TlsContext.h" + + +xmrig::HttpsServer::HttpsServer(const std::shared_ptr &listener) : + m_listener(listener) +{ +} + + +xmrig::HttpsServer::~HttpsServer() +{ + HttpContext::closeAll(); + + delete m_tls; +} + + +bool xmrig::HttpsServer::setTls(const TlsConfig &config) +{ + if (!config.isValid()) { + return false; + } + + auto tls = new TlsContext(); + if (!tls->load(config)) { + delete tls; + + return false; + } + + m_tls = tls; + + return true; +} + + +void xmrig::HttpsServer::onConnection(uv_stream_t *stream, uint16_t) +{ + auto ctx = new HttpsContext(m_tls, 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]; + buf->len = suggested_size; + }, + onRead); +} + + +void xmrig::HttpsServer::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) +{ + auto ctx = static_cast(stream->data); + if (nread >= 0) { + ctx->append(buf->base, static_cast(nread)); + } + else { + ctx->close(); + } + + delete [] buf->base; +} diff --git a/src/base/net/https/HttpsServer.h b/src/base/net/https/HttpsServer.h new file mode 100644 index 0000000..7f56762 --- /dev/null +++ b/src/base/net/https/HttpsServer.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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_HTTPSSERVER_H +#define XMRIG_HTTPSSERVER_H + + +using uv_tcp_t = struct uv_tcp_s; + +struct http_parser; +struct http_parser_settings; +struct uv_buf_t; + + +#include "base/kernel/interfaces/ITcpServerListener.h" +#include "base/tools/Object.h" + + +#include + + +namespace xmrig { + + +class IHttpListener; +class TlsContext; +class TlsConfig; + + +class HttpsServer : public ITcpServerListener +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsServer) + + HttpsServer(const std::shared_ptr &listener); + ~HttpsServer() override; + + bool setTls(const TlsConfig &config); + +protected: + void onConnection(uv_stream_t *stream, uint16_t port) override; + +private: + static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); + + std::weak_ptr m_listener; + TlsContext *m_tls = nullptr; + uv_tcp_t *m_tcp = nullptr; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPSSERVER_H + diff --git a/src/base/net/tls/ServerTls.cpp b/src/base/net/tls/ServerTls.cpp new file mode 100644 index 0000000..e76d586 --- /dev/null +++ b/src/base/net/tls/ServerTls.cpp @@ -0,0 +1,103 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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/net/tls/ServerTls.h" + + +#include +#include +#include + + +xmrig::ServerTls::ServerTls(SSL_CTX *ctx) : + m_ctx(ctx) +{ +} + + +xmrig::ServerTls::~ServerTls() +{ + if (m_ssl) { + SSL_free(m_ssl); + } +} + + +bool xmrig::ServerTls::isTLS(const char *data, size_t size) +{ + static const uint8_t test[3] = { 0x16, 0x03, 0x01 }; + + return size >= sizeof(test) && memcmp(data, test, sizeof(test)) == 0; +} + + +bool xmrig::ServerTls::send(const char *data, size_t size) +{ + SSL_write(m_ssl, data, size); + + return write(m_write); +} + + +void xmrig::ServerTls::read(const char *data, size_t size) +{ + if (!m_ssl) { + m_ssl = SSL_new(m_ctx); + + m_write = BIO_new(BIO_s_mem()); + m_read = BIO_new(BIO_s_mem()); + + SSL_set_accept_state(m_ssl); + SSL_set_bio(m_ssl, m_read, m_write); + } + + + BIO_write(m_read, data, size); + + if (!SSL_is_init_finished(m_ssl)) { + const int rc = SSL_do_handshake(m_ssl); + + if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { + write(m_write); + } else if (rc == 1) { + write(m_write); + + m_ready = true; + read(); + } + else { + shutdown(); + } + + return; + } + + read(); +} + + +void xmrig::ServerTls::read() +{ + static char buf[16384]{}; + + int bytes_read = 0; + while ((bytes_read = SSL_read(m_ssl, buf, sizeof(buf))) > 0) { + parse(buf, bytes_read); + } +} diff --git a/src/base/net/tls/ServerTls.h b/src/base/net/tls/ServerTls.h new file mode 100644 index 0000000..892bed2 --- /dev/null +++ b/src/base/net/tls/ServerTls.h @@ -0,0 +1,68 @@ +/* XMRig + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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_SERVERTLS_H +#define XMRIG_SERVERTLS_H + + +using BIO = struct bio_st; +using SSL = struct ssl_st; +using SSL_CTX = struct ssl_ctx_st; + + + +#include "base/tools/Object.h" + + +namespace xmrig { + + +class ServerTls +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(ServerTls) + + ServerTls(SSL_CTX *ctx); + virtual ~ServerTls(); + + static bool isTLS(const char *data, size_t size); + + bool send(const char *data, size_t size); + void read(const char *data, size_t size); + +protected: + virtual bool write(BIO *bio) = 0; + virtual void parse(char *data, size_t size) = 0; + virtual void shutdown() = 0; + +private: + void read(); + + BIO *m_read = nullptr; + BIO *m_write = nullptr; + bool m_ready = false; + SSL *m_ssl = nullptr; + SSL_CTX *m_ctx; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_SERVERTLS_H */ diff --git a/src/base/net/tools/TcpServer.cpp b/src/base/net/tools/TcpServer.cpp index e5e6e4e..06d4033 100644 --- a/src/base/net/tools/TcpServer.cpp +++ b/src/base/net/tools/TcpServer.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -34,11 +34,11 @@ 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) { + assert(m_listener != nullptr); + m_tcp = new uv_tcp_t; uv_tcp_init(uv_default_loop(), m_tcp); m_tcp->data = this; diff --git a/src/base/net/tools/TcpServer.h b/src/base/net/tools/TcpServer.h index fc29fa2..de464d3 100644 --- a/src/base/net/tools/TcpServer.h +++ b/src/base/net/tools/TcpServer.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 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 @@ -29,6 +29,9 @@ #include +#include "base/tools/Object.h" + + namespace xmrig { @@ -39,6 +42,8 @@ class String; class TcpServer { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(TcpServer) + TcpServer(const String &host, uint16_t port, ITcpServerListener *listener); ~TcpServer(); @@ -50,9 +55,9 @@ private: static void onConnection(uv_stream_t *stream, int status); const String &m_host; - int m_version; + int m_version = 0; ITcpServerListener *m_listener; - sockaddr_storage m_addr; + sockaddr_storage m_addr{}; uint16_t m_port; uv_tcp_t *m_tcp; }; diff --git a/src/proxy/Miner.cpp b/src/proxy/Miner.cpp index b5d0b63..75b098c 100644 --- a/src/proxy/Miner.cpp +++ b/src/proxy/Miner.cpp @@ -46,6 +46,7 @@ #ifdef XMRIG_FEATURE_TLS # include "base/net/tls/TlsContext.h" # include "proxy/tls/MinerTls.h" +# include #endif @@ -573,7 +574,6 @@ void xmrig::Miner::startTLS() # ifdef XMRIG_FEATURE_TLS if (m_tlsCtx && (m_strictTls || *m_recvBuf.base != '{')) { m_tls = new Tls(m_tlsCtx->ctx(), this); - m_tls->accept(); } # endif } diff --git a/src/proxy/tls/MinerTls.cpp b/src/proxy/tls/MinerTls.cpp index f064b33..0837fc8 100644 --- a/src/proxy/tls/MinerTls.cpp +++ b/src/proxy/tls/MinerTls.cpp @@ -24,100 +24,29 @@ */ -#include - - #include "proxy/tls/MinerTls.h" xmrig::Miner::Tls::Tls(SSL_CTX *ctx, Miner *miner) : - m_miner(miner), - m_ctx(ctx) + ServerTls(ctx), + m_miner(miner) { - m_writeBio = BIO_new(BIO_s_mem()); - m_readBio = BIO_new(BIO_s_mem()); } -xmrig::Miner::Tls::~Tls() +bool xmrig::Miner::Tls::write(BIO *bio) { - if (m_ssl) { - SSL_free(m_ssl); - } + return m_miner->send(bio); } -bool xmrig::Miner::Tls::accept() +void xmrig::Miner::Tls::parse(char *data, size_t size) { - m_ssl = SSL_new(m_ctx); - assert(m_ssl != nullptr); - - if (!m_ssl) { - return false; - } - - SSL_set_accept_state(m_ssl); - SSL_set_bio(m_ssl, m_readBio, m_writeBio); - - return send(); + m_miner->parse(data, size); } -bool xmrig::Miner::Tls::send(const char *data, size_t size) +void xmrig::Miner::Tls::shutdown() { - SSL_write(m_ssl, data, size); - - return send(); -} - - -const char *xmrig::Miner::Tls::fingerprint() const -{ - return m_ready ? m_fingerprint : nullptr; -} - - -const char *xmrig::Miner::Tls::version() const -{ - return m_ready ? SSL_get_version(m_ssl) : nullptr; -} - - -void xmrig::Miner::Tls::read(const char *data, size_t size) -{ - BIO_write(m_readBio, data, size); - - if (!SSL_is_init_finished(m_ssl)) { - const int rc = SSL_do_handshake(m_ssl); - - if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) { - send(); - } else if (rc == 1) { - m_ready = true; - send(); - read(); - } - else { - m_miner->close(); - } - - return; - } - - read(); -} - - -bool xmrig::Miner::Tls::send() -{ - return m_miner->send(m_writeBio); -} - - -void xmrig::Miner::Tls::read() -{ - int bytes_read = 0; - while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) { - m_miner->parse(m_buf, bytes_read); - } + m_miner->close(); } diff --git a/src/proxy/tls/MinerTls.h b/src/proxy/tls/MinerTls.h index 1392a45..4e054e2 100644 --- a/src/proxy/tls/MinerTls.h +++ b/src/proxy/tls/MinerTls.h @@ -26,40 +26,22 @@ #define XMRIG_MINER_TLS_H -#include - - -#include "base/tools/Object.h" +#include "base/net/tls/ServerTls.h" #include "proxy/Miner.h" -class xmrig::Miner::Tls +class xmrig::Miner::Tls : public ServerTls { public: - XMRIG_DISABLE_COPY_MOVE_DEFAULT(Tls) - Tls(SSL_CTX *ctx, Miner *miner); - ~Tls(); - bool accept(); - bool send(const char *data, size_t size); - const char *fingerprint() const; - const char *version() const; - void read(const char *data, size_t size); +protected: + bool write(BIO *bio) override; + void parse(char *data, size_t size) override; + void shutdown() override; private: - bool send(); - bool verify(X509 *cert); - void read(); - - BIO *m_readBio; - BIO *m_writeBio; - bool m_ready = false; - char m_buf[1024 * 1]{}; - char m_fingerprint[32 * 2 + 8]{}; Miner *m_miner; - SSL *m_ssl = nullptr; - SSL_CTX *m_ctx; };