Code cleanup.
This commit is contained in:
parent
b0dda2b5b3
commit
5486300db7
5 changed files with 39 additions and 52 deletions
|
@ -5,8 +5,8 @@
|
||||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* 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
|
||||||
|
@ -50,13 +50,14 @@ xmrig::DnsRecord::DnsRecord(const addrinfo *addr) :
|
||||||
sockaddr *xmrig::DnsRecord::addr(uint16_t port) const
|
sockaddr *xmrig::DnsRecord::addr(uint16_t port) const
|
||||||
{
|
{
|
||||||
if (m_type == A) {
|
if (m_type == A) {
|
||||||
sockaddr_in *addr = new sockaddr_in();
|
auto addr = new sockaddr_in();
|
||||||
uv_ip4_addr(m_ip.data(), port, addr);
|
uv_ip4_addr(m_ip.data(), port, addr);
|
||||||
|
|
||||||
return reinterpret_cast<sockaddr *>(addr);
|
return reinterpret_cast<sockaddr *>(addr);
|
||||||
}
|
}
|
||||||
else if (m_type == AAAA) {
|
|
||||||
sockaddr_in6 *addr = new sockaddr_in6();
|
if (m_type == AAAA) {
|
||||||
|
auto addr = new sockaddr_in6();
|
||||||
uv_ip6_addr(m_ip.data(), port, addr);
|
uv_ip6_addr(m_ip.data(), port, addr);
|
||||||
|
|
||||||
return reinterpret_cast<sockaddr *>(addr);
|
return reinterpret_cast<sockaddr *>(addr);
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
* 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
|
||||||
|
@ -45,7 +45,7 @@ public:
|
||||||
AAAA
|
AAAA
|
||||||
};
|
};
|
||||||
|
|
||||||
inline DnsRecord() : m_type(Unknown) {}
|
DnsRecord() = default;
|
||||||
DnsRecord(const addrinfo *addr);
|
DnsRecord(const addrinfo *addr);
|
||||||
|
|
||||||
sockaddr *addr(uint16_t port = 0) const;
|
sockaddr *addr(uint16_t port = 0) const;
|
||||||
|
@ -55,7 +55,7 @@ public:
|
||||||
inline Type type() const { return m_type; }
|
inline Type type() const { return m_type; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type m_type;
|
Type m_type = Unknown;
|
||||||
String m_ip;
|
String m_ip;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -24,53 +24,45 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <sstream>
|
#include "base/net/http/HttpClient.h"
|
||||||
|
|
||||||
|
|
||||||
#include "3rdparty/http-parser/http_parser.h"
|
#include "3rdparty/http-parser/http_parser.h"
|
||||||
#include "base/io/log/Log.h"
|
#include "base/io/log/Log.h"
|
||||||
#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/http/HttpClient.h"
|
|
||||||
#include "base/tools/Baton.h"
|
#include "base/tools/Baton.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
static const char *kCRLF = "\r\n";
|
static const char *kCRLF = "\r\n";
|
||||||
|
|
||||||
|
|
||||||
class ClientWriteBaton : public Baton<uv_write_t>
|
class HttpClientWriteBaton : public Baton<uv_write_t>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline ClientWriteBaton(const std::string &header, std::string &&body) :
|
inline HttpClientWriteBaton(const std::string &header, std::string &&body) :
|
||||||
m_body(std::move(body)),
|
m_body(std::move(body)),
|
||||||
m_header(header)
|
m_header(header)
|
||||||
{
|
{
|
||||||
bufs[0].len = m_header.size();
|
m_bufs[0] = uv_buf_init(const_cast<char *>(m_header.c_str()), m_header.size());
|
||||||
bufs[0].base = const_cast<char *>(m_header.c_str());
|
m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast<char *>(m_body.c_str()), m_body.size());
|
||||||
|
|
||||||
if (!m_body.empty()) {
|
|
||||||
bufs[1].len = m_body.size();
|
|
||||||
bufs[1].base = const_cast<char *>(m_body.c_str());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bufs[1].base = nullptr;
|
|
||||||
bufs[1].len = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write(uv_stream_t *stream)
|
||||||
inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; }
|
{
|
||||||
inline size_t size() const { return bufs[0].len + bufs[1].len; }
|
uv_write(&req, stream, m_bufs, nbufs(), [](uv_write_t *req, int) { delete reinterpret_cast<HttpClientWriteBaton *>(req->data); });
|
||||||
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<ClientWriteBaton *>(req->data); }
|
}
|
||||||
|
|
||||||
|
|
||||||
uv_buf_t bufs[2]{};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
inline size_t nbufs() const { return m_bufs[1].len > 0 ? 2 : 1; }
|
||||||
|
|
||||||
std::string m_body;
|
std::string m_body;
|
||||||
std::string m_header;
|
std::string m_header;
|
||||||
|
uv_buf_t m_bufs[2]{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -129,6 +121,8 @@ void xmrig::HttpClient::onResolved(const Dns &dns, int status)
|
||||||
req->data = this;
|
req->data = this;
|
||||||
|
|
||||||
uv_tcp_connect(req, m_tcp, addr, onConnect);
|
uv_tcp_connect(req, m_tcp, addr, onConnect);
|
||||||
|
|
||||||
|
delete addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -167,8 +161,8 @@ void xmrig::HttpClient::read(const char *data, size_t size)
|
||||||
|
|
||||||
void xmrig::HttpClient::write(const std::string &header)
|
void xmrig::HttpClient::write(const std::string &header)
|
||||||
{
|
{
|
||||||
auto baton = new ClientWriteBaton(header, std::move(body));
|
auto baton = new HttpClientWriteBaton(header, std::move(body));
|
||||||
uv_write(&baton->req, stream(), baton->bufs, baton->count(), ClientWriteBaton::onWrite);
|
baton->write(stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -194,12 +188,7 @@ void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
|
||||||
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
|
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
|
||||||
{
|
{
|
||||||
buf->base = new char[suggested_size];
|
buf->base = new char[suggested_size];
|
||||||
|
|
||||||
# ifdef _WIN32
|
|
||||||
buf->len = static_cast<unsigned int>(suggested_size);
|
|
||||||
# else
|
|
||||||
buf->len = suggested_size;
|
buf->len = suggested_size;
|
||||||
# endif
|
|
||||||
},
|
},
|
||||||
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
|
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,8 +29,8 @@
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
|
|
||||||
|
|
||||||
#include "base/io/log/Log.h"
|
|
||||||
#include "base/net/http/HttpsClient.h"
|
#include "base/net/http/HttpsClient.h"
|
||||||
|
#include "base/io/log/Log.h"
|
||||||
#include "base/tools/Buffer.h"
|
#include "base/tools/Buffer.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,9 +41,6 @@
|
||||||
|
|
||||||
xmrig::HttpsClient::HttpsClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size, const String &fingerprint) :
|
xmrig::HttpsClient::HttpsClient(int method, const String &url, const std::weak_ptr<IHttpListener> &listener, const char *data, size_t size, const String &fingerprint) :
|
||||||
HttpClient(method, url, listener, data, size),
|
HttpClient(method, url, listener, data, size),
|
||||||
m_ready(false),
|
|
||||||
m_buf(),
|
|
||||||
m_ssl(nullptr),
|
|
||||||
m_fp(fingerprint)
|
m_fp(fingerprint)
|
||||||
{
|
{
|
||||||
m_ctx = SSL_CTX_new(SSLv23_method());
|
m_ctx = SSL_CTX_new(SSLv23_method());
|
||||||
|
|
|
@ -62,13 +62,13 @@ private:
|
||||||
bool verifyFingerprint(X509 *cert);
|
bool verifyFingerprint(X509 *cert);
|
||||||
void flush();
|
void flush();
|
||||||
|
|
||||||
BIO *m_readBio;
|
BIO *m_readBio = nullptr;
|
||||||
BIO *m_writeBio;
|
BIO *m_writeBio = nullptr;
|
||||||
bool m_ready;
|
bool m_ready = false;
|
||||||
char m_buf[1024 * 2];
|
char m_buf[1024 * 2]{};
|
||||||
char m_fingerprint[32 * 2 + 8];
|
char m_fingerprint[32 * 2 + 8]{};
|
||||||
SSL *m_ssl;
|
SSL *m_ssl = nullptr;
|
||||||
SSL_CTX *m_ctx;
|
SSL_CTX *m_ctx = nullptr;
|
||||||
String m_fp;
|
String m_fp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue