Added DNS config.

This commit is contained in:
XMRig 2021-03-20 00:09:59 +07:00
parent 3e41bdc552
commit 2c8f7f692c
No known key found for this signature in database
GPG key ID: 446A53638BE94409
9 changed files with 130 additions and 4 deletions

View file

@ -44,6 +44,7 @@ set(HEADERS_BASE
src/base/kernel/Platform.h src/base/kernel/Platform.h
src/base/kernel/Process.h src/base/kernel/Process.h
src/base/net/dns/Dns.h src/base/net/dns/Dns.h
src/base/net/dns/DnsConfig.h
src/base/net/dns/DnsRecord.h src/base/net/dns/DnsRecord.h
src/base/net/dns/DnsRecords.h src/base/net/dns/DnsRecords.h
src/base/net/dns/DnsRequest.h src/base/net/dns/DnsRequest.h
@ -103,6 +104,7 @@ set(SOURCES_BASE
src/base/kernel/Platform.cpp src/base/kernel/Platform.cpp
src/base/kernel/Process.cpp src/base/kernel/Process.cpp
src/base/net/dns/Dns.cpp src/base/net/dns/Dns.cpp
src/base/net/dns/DnsConfig.cpp
src/base/net/dns/DnsRecord.cpp src/base/net/dns/DnsRecord.cpp
src/base/net/dns/DnsRecords.cpp src/base/net/dns/DnsRecords.cpp
src/base/net/dns/DnsUvBackend.cpp src/base/net/dns/DnsUvBackend.cpp

View file

@ -23,6 +23,7 @@
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/io/log/Tags.h" #include "base/io/log/Tags.h"
#include "base/kernel/interfaces/IJsonReader.h" #include "base/kernel/interfaces/IJsonReader.h"
#include "base/net/dns/Dns.h"
#include "version.h" #include "version.h"
@ -105,6 +106,8 @@ bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName)
m_http.load(reader.getObject(kHttp)); m_http.load(reader.getObject(kHttp));
m_pools.load(reader); m_pools.load(reader);
Dns::set(reader.getObject(DnsConfig::kField));
return m_pools.active() > 0; return m_pools.active() > 0;
} }

View file

@ -24,6 +24,7 @@
namespace xmrig { namespace xmrig {
DnsConfig Dns::m_config;
std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends; std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends;
@ -36,5 +37,5 @@ std::shared_ptr<xmrig::DnsRequest> xmrig::Dns::resolve(const String &host, IDnsL
m_backends.insert({ host, std::make_shared<DnsUvBackend>() }); m_backends.insert({ host, std::make_shared<DnsUvBackend>() });
} }
return m_backends.at(host)->resolve(host, listener, ttl); return m_backends.at(host)->resolve(host, listener, ttl == 0 ? m_config.ttl() : ttl);
} }

View file

@ -20,6 +20,7 @@
#define XMRIG_DNS_H #define XMRIG_DNS_H
#include "base/net/dns/DnsConfig.h"
#include "base/tools/String.h" #include "base/tools/String.h"
@ -30,6 +31,7 @@
namespace xmrig { namespace xmrig {
class DnsConfig;
class DnsRequest; class DnsRequest;
class IDnsBackend; class IDnsBackend;
class IDnsListener; class IDnsListener;
@ -38,9 +40,13 @@ class IDnsListener;
class Dns class Dns
{ {
public: public:
static std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl = 60000); inline static const DnsConfig &config() { return m_config; }
inline static void set(const DnsConfig &config) { m_config = config; }
static std::shared_ptr<DnsRequest> resolve(const String &host, IDnsListener *listener, uint64_t ttl = 0);
private: private:
static DnsConfig m_config;
static std::map<String, std::shared_ptr<IDnsBackend> > m_backends; static std::map<String, std::shared_ptr<IDnsBackend> > m_backends;
}; };

View file

@ -0,0 +1,57 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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/dns/DnsConfig.h"
#include "3rdparty/rapidjson/document.h"
#include "base/io/json/Json.h"
#include <algorithm>
namespace xmrig {
const char *DnsConfig::kField = "dns";
const char *DnsConfig::kIPv6 = "ipv6";
const char *DnsConfig::kTTL = "ttl";
} // namespace xmrig
xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value)
{
m_ipv6 = Json::getBool(value, kIPv6, m_ipv6);
m_ttl = std::max(Json::getUint(value, kTTL, m_ttl), 1U);
}
rapidjson::Value xmrig::DnsConfig::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember(StringRef(kIPv6), m_ipv6, allocator);
obj.AddMember(StringRef(kTTL), m_ttl, allocator);
return obj;
}

View file

@ -0,0 +1,54 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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_DNSCONFIG_H
#define XMRIG_DNSCONFIG_H
#include "3rdparty/rapidjson/fwd.h"
namespace xmrig {
class DnsConfig
{
public:
static const char *kField;
static const char *kIPv6;
static const char *kTTL;
DnsConfig() = default;
DnsConfig(const rapidjson::Value &object);
inline bool isIPv6() const { return m_ipv6; }
inline uint32_t ttl() const { return m_ttl * 1000U; }
rapidjson::Value toJSON(rapidjson::Document &doc) const;
private:
bool m_ipv6 = false;
uint32_t m_ttl = 30U;
};
} /* namespace xmrig */
#endif /* XMRIG_DNSCONFIG_H */

View file

@ -21,6 +21,7 @@
#include "base/net/dns/DnsRecords.h" #include "base/net/dns/DnsRecords.h"
#include "base/net/dns/Dns.h"
const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const
@ -34,7 +35,7 @@ const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const
const size_t ipv4 = m_ipv4.size(); const size_t ipv4 = m_ipv4.size();
const size_t ipv6 = m_ipv6.size(); const size_t ipv6 = m_ipv6.size();
if (ipv6 && (prefered == DnsRecord::AAAA || !ipv4)) { if (ipv6 && (prefered == DnsRecord::AAAA || Dns::config().isIPv6() || !ipv4)) {
return m_ipv6[ipv6 == 1 ? 0 : static_cast<size_t>(rand()) % ipv6]; return m_ipv6[ipv6 == 1 ? 0 : static_cast<size_t>(rand()) % ipv6];
} }

View file

@ -31,7 +31,7 @@ class DnsRecords
public: public:
inline bool isEmpty() const { return m_ipv4.empty() && m_ipv6.empty(); } inline bool isEmpty() const { return m_ipv4.empty() && m_ipv6.empty(); }
const DnsRecord &get(DnsRecord::Type prefered = DnsRecord::A) const; const DnsRecord &get(DnsRecord::Type prefered = DnsRecord::Unknown) const;
size_t count(DnsRecord::Type type = DnsRecord::Unknown) const; size_t count(DnsRecord::Type type = DnsRecord::Unknown) const;
void clear(); void clear();
void parse(addrinfo *res); void parse(addrinfo *res);

View file

@ -28,6 +28,7 @@
#include "backend/cpu/Cpu.h" #include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/kernel/interfaces/IJsonReader.h" #include "base/kernel/interfaces/IJsonReader.h"
#include "base/net/dns/Dns.h"
#include "crypto/common/Assembly.h" #include "crypto/common/Assembly.h"
@ -295,6 +296,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
doc.AddMember(StringRef(kTls), m_tls.toJSON(doc), allocator); doc.AddMember(StringRef(kTls), m_tls.toJSON(doc), allocator);
# endif # endif
doc.AddMember(StringRef(DnsConfig::kField), Dns::config().toJSON(doc), allocator);
doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator); doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator);
doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator); doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator);
doc.AddMember(StringRef(kWatch), m_watch, allocator); doc.AddMember(StringRef(kWatch), m_watch, allocator);