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

This commit is contained in:
XMRig 2020-03-18 20:09:11 +07:00
parent 92a258f142
commit 5b610e4dfe
No known key found for this signature in database
GPG key ID: 446A53638BE94409
38 changed files with 1601 additions and 178 deletions

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,18 +67,27 @@ 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);
bool tls = false;
# ifdef XMRIG_FEATURE_TLS
m_http = new HttpsServer(m_httpListener);
tls = 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();
Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") BLUE_BOLD("%s:%d") " " RED_BOLD("%s"),
Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CSI "1;%dm%s:%d" " " RED_BOLD("%s"),
"HTTP API",
tls ? 32 : 36,
config.host().data(),
rc < 0 ? config.port() : rc,
rc < 0 ? uv_strerror(rc) : ""
@ -143,7 +155,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
};