Update HTTP

This commit is contained in:
XMRig 2020-12-03 10:48:57 +07:00
parent 63bd45c397
commit 86795aa5b7
No known key found for this signature in database
GPG key ID: 446A53638BE94409
11 changed files with 65 additions and 79 deletions

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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<IHttpListener> &listener, int type) void xmrig::fetch(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type, uint64_t rpcId)
{ {
# ifdef APP_DEBUG # ifdef APP_DEBUG
LOG_DEBUG(CYAN("http%s://%s:%u ") MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD(" body: ") CYAN_BOLD("%zu") BLACK_BOLD(" bytes"), 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<IHttpListener> &listen
HttpClient *client; HttpClient *client;
# ifdef XMRIG_FEATURE_TLS # ifdef XMRIG_FEATURE_TLS
if (req.tls) { if (req.tls) {
client = new HttpsClient(std::move(req), listener); client = new HttpsClient(tag, std::move(req), listener);
} }
else else
# endif # endif
{ {
client = new HttpClient(std::move(req), listener); client = new HttpClient(tag, std::move(req), listener);
} }
client->userType = type; client->userType = type;
client->rpcId = rpcId;
client->connect(); client->connect();
} }

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -58,10 +58,11 @@ public:
String host; String host;
String path; String path;
uint16_t port = 0; uint16_t port = 0;
uint64_t timeout = 0;
}; };
void fetch(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type = 0); void fetch(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener, int type = 0, uint64_t rpcId = 0);
} // namespace xmrig } // namespace xmrig

View file

@ -1,13 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* 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 2014-2019 heapwolf <https://github.com/heapwolf>
* 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 * 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 * it under the terms of the GNU General Public License as published by
@ -30,6 +24,7 @@
#include "base/kernel/Platform.h" #include "base/kernel/Platform.h"
#include "base/net/dns/Dns.h" #include "base/net/dns/Dns.h"
#include "base/net/tools/NetBuffer.h" #include "base/net/tools/NetBuffer.h"
#include "base/tools/Timer.h"
#include <sstream> #include <sstream>
@ -44,21 +39,20 @@ static const char *kCRLF = "\r\n";
} // namespace xmrig } // namespace xmrig
xmrig::HttpClient::HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) : xmrig::HttpClient::HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpContext(HTTP_RESPONSE, listener), HttpContext(HTTP_RESPONSE, listener),
m_tag(tag),
m_req(std::move(req)) m_req(std::move(req))
{ {
method = m_req.method; method = m_req.method;
url = std::move(m_req.path); url = std::move(m_req.path);
body = std::move(m_req.body); body = std::move(m_req.body);
headers = std::move(m_req.headers); headers = std::move(m_req.headers);
m_dns = new Dns(this); m_dns = std::make_shared<Dns>(this);
}
if (m_req.timeout) {
xmrig::HttpClient::~HttpClient() m_timer = std::make_shared<Timer>(this, m_req.timeout, 0);
{ }
delete m_dns;
} }
@ -74,7 +68,7 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
if (status < 0 && dns.isEmpty()) { if (status < 0 && dns.isEmpty()) {
if (!isQuiet()) { 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; 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() void xmrig::HttpClient::handshake()
{ {
headers.insert({ "Host", host() }); headers.insert({ "Host", host() });
@ -135,8 +135,12 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
} }
if (status < 0) { if (status < 0) {
if (status == UV_ECANCELED) {
status = UV_ETIMEDOUT;
}
if (!client->isQuiet()) { 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); return client->close(status);
@ -151,7 +155,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
client->read(buf->base, static_cast<size_t>(nread)); client->read(buf->base, static_cast<size_t>(nread));
} else { } else {
if (!client->isQuiet() && nread != UV_EOF) { if (!client->isQuiet() && nread != UV_EOF) {
LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->port(), uv_strerror(static_cast<int>(nread))); LOG_ERR("%s " RED("read error: ") RED_BOLD("\"%s\""), client->tag(), uv_strerror(static_cast<int>(nread)));
} }
client->close(static_cast<int>(nread)); client->close(static_cast<int>(nread));

View file

@ -1,13 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* 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 2014-2019 heapwolf <https://github.com/heapwolf>
* 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 * 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 * 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/IDnsListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/net/http/Fetch.h" #include "base/net/http/Fetch.h"
#include "base/net/http/HttpContext.h" #include "base/net/http/HttpContext.h"
#include "base/tools/Object.h" #include "base/tools/Object.h"
@ -40,22 +35,24 @@ namespace xmrig {
class String; class String;
class HttpClient : public HttpContext, public IDnsListener class HttpClient : public HttpContext, public IDnsListener, public ITimerListener
{ {
public: public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient); XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpClient);
HttpClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener); HttpClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpClient() override; ~HttpClient() override = default;
inline bool isQuiet() const { return m_req.quiet; } inline bool isQuiet() const { return m_req.quiet; }
inline const char *host() const override { return m_req.host; } 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; } inline uint16_t port() const override { return m_req.port; }
bool connect(); bool connect();
protected: protected:
void onResolved(const Dns &dns, int status) override; void onResolved(const Dns &dns, int status) override;
void onTimer(const Timer *timer) override;
virtual void handshake(); virtual void handshake();
virtual void read(const char *data, size_t size); virtual void read(const char *data, size_t size);
@ -66,8 +63,10 @@ protected:
private: private:
static void onConnect(uv_connect_t *req, int status); static void onConnect(uv_connect_t *req, int status);
Dns *m_dns; const char *m_tag;
FetchRequest m_req; FetchRequest m_req;
std::shared_ptr<Dns> m_dns;
std::shared_ptr<Timer> m_timer;
}; };

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -29,20 +29,13 @@ namespace xmrig {
class HttpListener : public IHttpListener class HttpListener : public IHttpListener
{ {
public: public:
inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : inline HttpListener(IHttpListener *listener, const char *tag = nullptr) : m_tag(tag), m_listener(listener) {}
# ifdef APP_DEBUG
m_tag(tag),
# endif
m_listener(listener)
{}
protected: protected:
void onHttpData(const HttpData &data) override; void onHttpData(const HttpData &data) override;
private: private:
# ifdef APP_DEBUG
const char *m_tag; const char *m_tag;
# endif
IHttpListener *m_listener; IHttpListener *m_listener;
}; };

View file

@ -1,13 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* 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 2014-2019 heapwolf <https://github.com/heapwolf>
* 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 * 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 * it under the terms of the GNU General Public License as published by
@ -39,8 +33,8 @@
#endif #endif
xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) : xmrig::HttpsClient::HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpClient(std::move(req), listener) HttpClient(tag, std::move(req), listener)
{ {
m_ctx = SSL_CTX_new(SSLv23_method()); m_ctx = SSL_CTX_new(SSLv23_method());
assert(m_ctx != nullptr); assert(m_ctx != nullptr);

View file

@ -1,13 +1,7 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* 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 2014-2019 heapwolf <https://github.com/heapwolf>
* 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 * 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 * it under the terms of the GNU General Public License as published by
@ -46,7 +40,7 @@ class HttpsClient : public HttpClient
public: public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient) XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient)
HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener); HttpsClient(const char *tag, FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpsClient() override; ~HttpsClient() override;
const char *tlsFingerprint() const override; const char *tlsFingerprint() const override;

View file

@ -330,7 +330,7 @@ int64_t xmrig::DaemonClient::getBlockTemplate()
int64_t xmrig::DaemonClient::rpcSend(const rapidjson::Document &doc) 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()); 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++; return m_sequence++;
} }
@ -357,7 +357,7 @@ void xmrig::DaemonClient::retry()
void xmrig::DaemonClient::send(const char *path) void xmrig::DaemonClient::send(const char *path)
{ {
FetchRequest req(HTTP_GET, m_pool.host(), m_pool.port(), path, m_pool.isTLS(), isQuiet()); 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);
} }

View file

@ -154,7 +154,7 @@ void xmrig::SelfSelectClient::getBlockTemplate()
JsonRequest::create(doc, m_sequence++, "getblocktemplate", params); JsonRequest::create(doc, m_sequence++, "getblocktemplate", params);
FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet()); 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);
} }

View file

@ -322,7 +322,7 @@ void xmrig::BenchClient::send(Request request)
case GET_BENCH: case GET_BENCH:
{ {
FetchRequest req(HTTP_GET, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true); 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; break;
@ -336,7 +336,7 @@ void xmrig::BenchClient::send(Request request)
doc.AddMember("cpu", Cpu::toJSON(doc), allocator); doc.AddMember("cpu", Cpu::toJSON(doc), allocator);
FetchRequest req(HTTP_POST, m_ip, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true); 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; 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); 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)}); req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)});
fetch(std::move(req), m_httpListener); fetch(tag(), std::move(req), m_httpListener);
} }
#endif #endif