diff --git a/src/base/base.cmake b/src/base/base.cmake index 14cc7a5d4..8c48fe752 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -44,6 +44,7 @@ set(HEADERS_BASE src/base/kernel/Platform.h src/base/kernel/Process.h src/base/net/dns/Dns.h + src/base/net/dns/DnsConfig.h src/base/net/dns/DnsRecord.h src/base/net/dns/DnsRecords.h src/base/net/dns/DnsRequest.h @@ -103,6 +104,7 @@ set(SOURCES_BASE src/base/kernel/Platform.cpp src/base/kernel/Process.cpp src/base/net/dns/Dns.cpp + src/base/net/dns/DnsConfig.cpp src/base/net/dns/DnsRecord.cpp src/base/net/dns/DnsRecords.cpp src/base/net/dns/DnsUvBackend.cpp diff --git a/src/base/kernel/config/BaseConfig.cpp b/src/base/kernel/config/BaseConfig.cpp index 1f2b09cb2..37982999e 100644 --- a/src/base/kernel/config/BaseConfig.cpp +++ b/src/base/kernel/config/BaseConfig.cpp @@ -23,6 +23,7 @@ #include "base/io/log/Log.h" #include "base/io/log/Tags.h" #include "base/kernel/interfaces/IJsonReader.h" +#include "base/net/dns/Dns.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_pools.load(reader); + Dns::set(reader.getObject(DnsConfig::kField)); + return m_pools.active() > 0; } diff --git a/src/base/net/dns/Dns.cpp b/src/base/net/dns/Dns.cpp index cb336f283..85590bb7d 100644 --- a/src/base/net/dns/Dns.cpp +++ b/src/base/net/dns/Dns.cpp @@ -24,6 +24,7 @@ namespace xmrig { +DnsConfig Dns::m_config; std::map > Dns::m_backends; @@ -36,5 +37,5 @@ std::shared_ptr xmrig::Dns::resolve(const String &host, IDnsL m_backends.insert({ host, std::make_shared() }); } - return m_backends.at(host)->resolve(host, listener, ttl); + return m_backends.at(host)->resolve(host, listener, ttl == 0 ? m_config.ttl() : ttl); } diff --git a/src/base/net/dns/Dns.h b/src/base/net/dns/Dns.h index b2a2dba09..cf054390f 100644 --- a/src/base/net/dns/Dns.h +++ b/src/base/net/dns/Dns.h @@ -20,6 +20,7 @@ #define XMRIG_DNS_H +#include "base/net/dns/DnsConfig.h" #include "base/tools/String.h" @@ -30,6 +31,7 @@ namespace xmrig { +class DnsConfig; class DnsRequest; class IDnsBackend; class IDnsListener; @@ -38,9 +40,13 @@ class IDnsListener; class Dns { public: - static std::shared_ptr 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 resolve(const String &host, IDnsListener *listener, uint64_t ttl = 0); private: + static DnsConfig m_config; static std::map > m_backends; }; diff --git a/src/base/net/dns/DnsConfig.cpp b/src/base/net/dns/DnsConfig.cpp new file mode 100644 index 000000000..c2446c2a6 --- /dev/null +++ b/src/base/net/dns/DnsConfig.cpp @@ -0,0 +1,57 @@ +/* XMRig + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 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 + * 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 . + */ + + +#include "base/net/dns/DnsConfig.h" +#include "3rdparty/rapidjson/document.h" +#include "base/io/json/Json.h" + + +#include + + +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; +} diff --git a/src/base/net/dns/DnsConfig.h b/src/base/net/dns/DnsConfig.h new file mode 100644 index 000000000..d42c8ad8b --- /dev/null +++ b/src/base/net/dns/DnsConfig.h @@ -0,0 +1,54 @@ +/* XMRig + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 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 + * 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 . + */ + +#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 */ diff --git a/src/base/net/dns/DnsRecords.cpp b/src/base/net/dns/DnsRecords.cpp index 583d4814a..fb232f276 100644 --- a/src/base/net/dns/DnsRecords.cpp +++ b/src/base/net/dns/DnsRecords.cpp @@ -21,6 +21,7 @@ #include "base/net/dns/DnsRecords.h" +#include "base/net/dns/Dns.h" 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 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(rand()) % ipv6]; } diff --git a/src/base/net/dns/DnsRecords.h b/src/base/net/dns/DnsRecords.h index e59966a28..cfa192179 100644 --- a/src/base/net/dns/DnsRecords.h +++ b/src/base/net/dns/DnsRecords.h @@ -31,7 +31,7 @@ class DnsRecords public: 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; void clear(); void parse(addrinfo *res); diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 2b27ae58d..3924123f0 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -28,6 +28,7 @@ #include "backend/cpu/Cpu.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IJsonReader.h" +#include "base/net/dns/Dns.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); # endif + doc.AddMember(StringRef(DnsConfig::kField), Dns::config().toJSON(doc), allocator); doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator); doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator); doc.AddMember(StringRef(kWatch), m_watch, allocator);