Added TLS support for API and many other TLS related changes.

This commit is contained in:
XMRig
2020-03-18 03:58:20 +07:00
parent 7f13fe1081
commit f8b6623b4c
26 changed files with 651 additions and 253 deletions

View File

@@ -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()

View File

@@ -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<HttpListener>(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) {

View File

@@ -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<IHttpListener> 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
};

View File

@@ -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

View File

@@ -27,7 +27,7 @@
#ifdef XMRIG_FEATURE_TLS
# include "base/net/http/HttpsClient.h"
# include "base/net/https/HttpsClient.h"
#endif

View File

@@ -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 <sstream>
@@ -41,31 +40,6 @@ namespace xmrig {
static const char *kCRLF = "\r\n";
class HttpClientWriteBaton : public Baton<uv_write_t>
{
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<char *>(m_header.c_str()), m_header.size());
m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast<char *>(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<HttpClientWriteBaton *>(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<HttpClient *>(req->data);

View File

@@ -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; }

View File

@@ -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<uint64_t, HttpContext *> storage;
static uint64_t SEQUENCE = 0;
class HttpWriteBaton : public Baton<uv_write_t>
{
public:
inline HttpWriteBaton(std::string &&body) :
m_body(std::move(body))
{
m_buf = uv_buf_init(const_cast<char *>(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<HttpWriteBaton *>(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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -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 <cinttypes>
@@ -45,52 +43,6 @@ static const char *kCRLF = "\r\n";
static const char *kUserAgent = "user-agent";
class WriteBaton : public Baton<uv_write_t>
{
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<char *>(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<WriteBaton *>(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));
}

View File

@@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@@ -28,11 +28,11 @@
#include <uv.h>
#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<IHttpListener> &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<unsigned int>(suggested_size);
# else
buf->len = suggested_size;
# endif
buf->len = suggested_size;
},
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
{

View File

@@ -29,7 +29,7 @@
#include <uv.h>
#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<IHttpLis
return;
}
m_writeBio = BIO_new(BIO_s_mem());
m_readBio = BIO_new(BIO_s_mem());
m_write = BIO_new(BIO_s_mem());
m_read = BIO_new(BIO_s_mem());
SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
}
@@ -89,7 +89,7 @@ void xmrig::HttpsClient::handshake()
}
SSL_set_connect_state(m_ssl);
SSL_set_bio(m_ssl, m_readBio, m_writeBio);
SSL_set_bio(m_ssl, m_read, m_write);
SSL_set_tlsext_host_name(m_ssl, host());
SSL_do_handshake(m_ssl);
@@ -100,7 +100,7 @@ void xmrig::HttpsClient::handshake()
void xmrig::HttpsClient::read(const char *data, size_t size)
{
BIO_write(m_readBio, data, size);
BIO_write(m_read, data, size);
if (!SSL_is_init_finished(m_ssl)) {
const int rc = SSL_connect(m_ssl);
@@ -123,17 +123,23 @@ void xmrig::HttpsClient::read(const char *data, size_t size)
return;
}
int bytes_read = 0;
while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) {
HttpClient::read(m_buf, static_cast<size_t>(bytes_read));
static char buf[16384]{};
int rc = 0;
while ((rc = SSL_read(m_ssl, buf, sizeof(buf))) > 0) {
HttpClient::read(buf, static_cast<size_t>(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<int>(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));
}

View File

@@ -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;

View File

@@ -0,0 +1,97 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/https/HttpsContext.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/net/tls/TlsContext.h"
#include <openssl/bio.h>
#include <uv.h>
xmrig::HttpsContext::HttpsContext(TlsContext *tls, const std::weak_ptr<IHttpListener> &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));
}
}

View File

@@ -0,0 +1,72 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef 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<IHttpListener> &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

View File

@@ -0,0 +1,92 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <functional>
#include <uv.h>
#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<IHttpListener> &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<HttpsContext*>(stream->data);
if (nread >= 0) {
ctx->append(buf->base, static_cast<size_t>(nread));
}
else {
ctx->close();
}
delete [] buf->base;
}

View File

@@ -0,0 +1,72 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef 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 <memory>
namespace xmrig {
class IHttpListener;
class TlsContext;
class TlsConfig;
class HttpsServer : public ITcpServerListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsServer)
HttpsServer(const std::shared_ptr<IHttpListener> &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<IHttpListener> m_listener;
TlsContext *m_tls = nullptr;
uv_tcp_t *m_tcp = nullptr;
};
} // namespace xmrig
#endif // XMRIG_HTTPSSERVER_H

View File

@@ -0,0 +1,103 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/tls/ServerTls.h"
#include <cassert>
#include <cstring>
#include <openssl/ssl.h>
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);
}
}

View File

@@ -0,0 +1,68 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef 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 */

View File

@@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -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;

View File

@@ -5,8 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -29,6 +29,9 @@
#include <uv.h>
#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;
};

View File

@@ -46,6 +46,7 @@
#ifdef XMRIG_FEATURE_TLS
# include "base/net/tls/TlsContext.h"
# include "proxy/tls/MinerTls.h"
# include <openssl/bio.h>
#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
}

View File

@@ -24,100 +24,29 @@
*/
#include <cassert>
#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();
}

View File

@@ -26,40 +26,22 @@
#define XMRIG_MINER_TLS_H
#include <openssl/ssl.h>
#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;
};