From 86795aa5b7905e890321a00845322b795b9bfbfc Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 3 Dec 2020 10:48:57 +0700 Subject: [PATCH] Update HTTP --- src/base/net/http/Fetch.cpp | 11 ++--- src/base/net/http/Fetch.h | 7 ++-- src/base/net/http/HttpClient.cpp | 42 ++++++++++--------- src/base/net/http/HttpClient.h | 25 ++++++----- src/base/net/http/HttpListener.cpp | 4 +- src/base/net/http/HttpListener.h | 13 ++---- src/base/net/https/HttpsClient.cpp | 16 +++---- src/base/net/https/HttpsClient.h | 14 ++----- src/base/net/stratum/DaemonClient.cpp | 4 +- src/base/net/stratum/SelfSelectClient.cpp | 2 +- .../net/stratum/benchmark/BenchClient.cpp | 6 +-- 11 files changed, 65 insertions(+), 79 deletions(-) diff --git a/src/base/net/http/Fetch.cpp b/src/base/net/http/Fetch.cpp index 4d18b71d0..84ff715c5 100644 --- a/src/base/net/http/Fetch.cpp +++ b/src/base/net/http/Fetch.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 @@ -95,7 +95,7 @@ void xmrig::FetchRequest::setBody(const rapidjson::Value &value) } -void xmrig::fetch(FetchRequest &&req, const std::weak_ptr &listener, int type) +void xmrig::fetch(const char *tag, FetchRequest &&req, const std::weak_ptr &listener, int type, uint64_t rpcId) { # ifdef APP_DEBUG LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD(" body: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"), @@ -109,14 +109,15 @@ void xmrig::fetch(FetchRequest &&req, const std::weak_ptr &listen HttpClient *client; # ifdef XMRIG_FEATURE_TLS if (req.tls) { - client = new HttpsClient(std::move(req), listener); + client = new HttpsClient(tag, std::move(req), listener); } else # endif { - client = new HttpClient(std::move(req), listener); + client = new HttpClient(tag, std::move(req), listener); } client->userType = type; + client->rpcId = rpcId; client->connect(); } diff --git a/src/base/net/http/Fetch.h b/src/base/net/http/Fetch.h index b6fbf487d..bdab46ba4 100644 --- a/src/base/net/http/Fetch.h +++ b/src/base/net/http/Fetch.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 @@ -58,10 +58,11 @@ public: String host; String path; uint16_t port = 0; + uint64_t timeout = 0; }; -void fetch(FetchRequest &&req, const std::weak_ptr &listener, int type = 0); +void fetch(const char *tag, FetchRequest &&req, const std::weak_ptr &listener, int type = 0, uint64_t rpcId = 0); } // namespace xmrig diff --git a/src/base/net/http/HttpClient.cpp b/src/base/net/http/HttpClient.cpp index 92ca6fac5..320f8ee45 100644 --- a/src/base/net/http/HttpClient.cpp +++ b/src/base/net/http/HttpClient.cpp @@ -1,13 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 @@ -30,6 +24,7 @@ #include "base/kernel/Platform.h" #include "base/net/dns/Dns.h" #include "base/net/tools/NetBuffer.h" +#include "base/tools/Timer.h" #include @@ -44,21 +39,20 @@ static const char *kCRLF = "\r\n"; } // namespace xmrig -xmrig::HttpClient::HttpClient(FetchRequest &&req, const std::weak_ptr &listener) : +xmrig::HttpClient::HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener) : HttpContext(HTTP_RESPONSE, listener), + m_tag(tag), m_req(std::move(req)) { method = m_req.method; url = std::move(m_req.path); body = std::move(m_req.body); headers = std::move(m_req.headers); - m_dns = new Dns(this); -} + m_dns = std::make_shared(this); - -xmrig::HttpClient::~HttpClient() -{ - delete m_dns; + if (m_req.timeout) { + m_timer = std::make_shared(this, m_req.timeout, 0); + } } @@ -74,7 +68,7 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status) if (status < 0 && dns.isEmpty()) { if (!isQuiet()) { - LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), port(), uv_strerror(status)); + LOG_ERR("%s " RED("DNS error: ") RED_BOLD("\"%s\""), tag(), uv_strerror(status)); } return; @@ -91,6 +85,12 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status) } +void xmrig::HttpClient::onTimer(const Timer *) +{ + close(UV_ETIMEDOUT); +} + + void xmrig::HttpClient::handshake() { headers.insert({ "Host", host() }); @@ -135,8 +135,12 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) } if (status < 0) { + if (status == UV_ECANCELED) { + status = UV_ETIMEDOUT; + } + if (!client->isQuiet()) { - LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(status)); + LOG_ERR("%s " RED("connect error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(status)); } return client->close(status); @@ -151,7 +155,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status) client->read(buf->base, static_cast(nread)); } else { if (!client->isQuiet() && nread != UV_EOF) { - LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(static_cast(nread))); + LOG_ERR("%s " RED("read error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(static_cast(nread))); } client->close(static_cast(nread)); diff --git a/src/base/net/http/HttpClient.h b/src/base/net/http/HttpClient.h index acfe15e2a..2b9a314d0 100644 --- a/src/base/net/http/HttpClient.h +++ b/src/base/net/http/HttpClient.h @@ -1,13 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 +23,7 @@ #include "base/kernel/interfaces/IDnsListener.h" +#include "base/kernel/interfaces/ITimerListener.h" #include "base/net/http/Fetch.h" #include "base/net/http/HttpContext.h" #include "base/tools/Object.h" @@ -40,22 +35,24 @@ namespace xmrig { class String; -class HttpClient : public HttpContext, public IDnsListener +class HttpClient : public HttpContext, public IDnsListener, public ITimerListener { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient); - HttpClient(FetchRequest &&req, const std::weak_ptr &listener); - ~HttpClient() override; + HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener); + ~HttpClient() override = default; inline bool isQuiet() const { return m_req.quiet; } inline const char *host() const override { return m_req.host; } + inline const char *tag() const { return m_tag; } inline uint16_t port() const override { return m_req.port; } bool connect(); protected: void onResolved(const Dns &dns, int status) override; + void onTimer(const Timer *timer) override; virtual void handshake(); virtual void read(const char *data, size_t size); @@ -66,8 +63,10 @@ protected: private: static void onConnect(uv_connect_t *req, int status); - Dns *m_dns; + const char *m_tag; FetchRequest m_req; + std::shared_ptr m_dns; + std::shared_ptr m_timer; }; diff --git a/src/base/net/http/HttpListener.cpp b/src/base/net/http/HttpListener.cpp index 077184fcb..ba9130ed9 100644 --- a/src/base/net/http/HttpListener.cpp +++ b/src/base/net/http/HttpListener.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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/HttpListener.h b/src/base/net/http/HttpListener.h index 4f982c4df..0e7264217 100644 --- a/src/base/net/http/HttpListener.h +++ b/src/base/net/http/HttpListener.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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,20 +29,13 @@ namespace xmrig { class HttpListener : public IHttpListener { public: - inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : -# ifdef APP_DEBUG - m_tag(tag), -# endif - m_listener(listener) - {} + inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : m_tag(tag), m_listener(listener) {} protected: void onHttpData(const HttpData &data) override; private: -# ifdef APP_DEBUG const char *m_tag; -# endif IHttpListener *m_listener; }; diff --git a/src/base/net/https/HttpsClient.cpp b/src/base/net/https/HttpsClient.cpp index 555ac778f..8b4149170 100644 --- a/src/base/net/https/HttpsClient.cpp +++ b/src/base/net/https/HttpsClient.cpp @@ -1,13 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 @@ -39,8 +33,8 @@ #endif -xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr &listener) : - HttpClient(std::move(req), listener) +xmrig::HttpsClient::HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener) : + HttpClient(tag, std::move(req), listener) { m_ctx = SSL_CTX_new(SSLv23_method()); assert(m_ctx != nullptr); diff --git a/src/base/net/https/HttpsClient.h b/src/base/net/https/HttpsClient.h index eeeec7473..bd0c65ffd 100644 --- a/src/base/net/https/HttpsClient.h +++ b/src/base/net/https/HttpsClient.h @@ -1,13 +1,7 @@ /* 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-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2014-2019 heapwolf + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 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 @@ -46,7 +40,7 @@ class HttpsClient : public HttpClient public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient) - HttpsClient(FetchRequest &&req, const std::weak_ptr &listener); + HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr &listener); ~HttpsClient() override; const char *tlsFingerprint() const override; diff --git a/src/base/net/stratum/DaemonClient.cpp b/src/base/net/stratum/DaemonClient.cpp index f39d08e78..8b4d0fb69 100644 --- a/src/base/net/stratum/DaemonClient.cpp +++ b/src/base/net/stratum/DaemonClient.cpp @@ -330,7 +330,7 @@ int64_t xmrig::DaemonClient::getBlockTemplate() int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc) { FetchRequest req(HTTP_POST, m_pool.host(), m_pool.port(), kJsonRPC, doc, m_pool.isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); return m_sequence++; } @@ -357,7 +357,7 @@ void xmrig::DaemonClient::retry() void xmrig::DaemonClient::send(const char *path) { FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } diff --git a/src/base/net/stratum/SelfSelectClient.cpp b/src/base/net/stratum/SelfSelectClient.cpp index 8ad48b65c..fc4cea6e0 100644 --- a/src/base/net/stratum/SelfSelectClient.cpp +++ b/src/base/net/stratum/SelfSelectClient.cpp @@ -154,7 +154,7 @@ void xmrig::SelfSelectClient::getBlockTemplate() JsonRequest::create(doc, m_sequence++, "getblocktemplate", params); FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet()); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } diff --git a/src/base/net/stratum/benchmark/BenchClient.cpp b/src/base/net/stratum/benchmark/BenchClient.cpp index 739342538..beac2f197 100644 --- a/src/base/net/stratum/benchmark/BenchClient.cpp +++ b/src/base/net/stratum/benchmark/BenchClient.cpp @@ -322,7 +322,7 @@ void xmrig::BenchClient::send(Request request) case GET_BENCH: { FetchRequest req(HTTP_GET, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } break; @@ -336,7 +336,7 @@ void xmrig::BenchClient::send(Request request) doc.AddMember("cpu", Cpu::toJSON(doc), allocator); FetchRequest req(HTTP_POST, m_ip, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } break; @@ -375,6 +375,6 @@ void xmrig::BenchClient::update(const rapidjson::Value &body) FetchRequest req(HTTP_PATCH, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true); req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)}); - fetch(std::move(req), m_httpListener); + fetch(tag(), std::move(req), m_httpListener); } #endif