Update DNS subsystem.

This commit is contained in:
XMRig
2022-07-13 17:45:18 +07:00
parent 07d53fb77e
commit d8f39ebef3
12 changed files with 159 additions and 104 deletions

View File

@ -21,7 +21,6 @@ set(HEADERS_BASE
src/base/kernel/Base.h
src/base/kernel/config/BaseConfig.h
src/base/kernel/config/BaseTransform.h
src/base/kernel/private/Title.h
src/base/kernel/constants.h
src/base/kernel/Entry.h
src/base/kernel/interfaces/IAsyncListener.h
@ -43,10 +42,11 @@ set(HEADERS_BASE
src/base/kernel/interfaces/IWatcherListener.h
src/base/kernel/Lib.h
src/base/kernel/OS.h
src/base/kernel/private/DnsConfig.h
src/base/kernel/private/Title.h
src/base/kernel/Process.h
src/base/kernel/Versions.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
@ -111,14 +111,14 @@ set(SOURCES_BASE
src/base/kernel/Base.cpp
src/base/kernel/config/BaseConfig.cpp
src/base/kernel/config/BaseTransform.cpp
src/base/kernel/private/Title.cpp
src/base/kernel/Entry.cpp
src/base/kernel/Lib.cpp
src/base/kernel/OS.cpp
src/base/kernel/private/DnsConfig.cpp
src/base/kernel/private/Title.cpp
src/base/kernel/Process.cpp
src/base/kernel/Versions.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

View File

@ -30,9 +30,9 @@
#include "base/io/log/Log.h"
#include "base/io/log/Tags.h"
#include "base/kernel/interfaces/IJsonReader.h"
#include "base/kernel/private/DnsConfig.h"
#include "base/kernel/Process.h"
#include "base/kernel/Versions.h"
#include "base/net/dns/Dns.h"
#include "version.h"
@ -114,7 +114,7 @@ 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));
DnsConfig::set(reader.getObject(DnsConfig::kField));
return m_pools.active() > 0;
}

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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
@ -31,8 +31,8 @@
#include "base/io/log/Log.h"
#include "base/kernel/config/BaseConfig.h"
#include "base/kernel/interfaces/IConfig.h"
#include "base/kernel/private/DnsConfig.h"
#include "base/kernel/Process.h"
#include "base/net/dns/DnsConfig.h"
#include "base/net/stratum/Pool.h"
#include "base/net/stratum/Pools.h"
#include "base/tools/Arguments.h"

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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

View File

@ -0,0 +1,107 @@
/* XMRig
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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/kernel/private/DnsConfig.h"
#include "3rdparty/rapidjson/document.h"
#include "base/io/json/Json.h"
#include "base/tools/Arguments.h"
#ifdef APP_DEBUG
# include "base/io/log/Log.h"
# include "base/kernel/Config.h"
#endif
#include <algorithm>
namespace xmrig {
const char *DnsConfig::kField = "dns";
const char *DnsConfig::kIPv6 = "ipv6";
const char *DnsConfig::kTTL = "ttl";
#ifndef XMRIG_FEATURE_EVENTS
static DnsConfig config;
#endif
} // namespace xmrig
xmrig::DnsConfig::DnsConfig(const Arguments &arguments) :
m_ipv6(arguments.contains("--dns-ipv6")),
m_ttl(std::max(arguments.value("--dns-ttl").toUint(kDefaultTTL), 1U))
{
}
xmrig::DnsConfig::DnsConfig(const rapidjson::Value &value, const DnsConfig &current)
{
m_ipv6 = Json::getBool(value, kIPv6, current.m_ipv6);
m_ttl = std::max(Json::getUint(value, kTTL, current.m_ttl), 1U);
}
bool xmrig::DnsConfig::isEqual(const DnsConfig &other) const
{
return other.m_ipv6 == m_ipv6 && other.m_ttl == m_ttl;
}
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 ? Value(m_ipv6) : Value(kNullType), allocator);
obj.AddMember(StringRef(kTTL), m_ttl == kDefaultTTL ? Value(kNullType) : Value(m_ttl), allocator);
return obj;
}
void xmrig::DnsConfig::print() const
{
# ifdef APP_DEBUG
LOG_DEBUG("%s " MAGENTA_BOLD("DNS")
MAGENTA("<ipv6=") CYAN("%d")
MAGENTA(", ttl=") CYAN("%u")
MAGENTA(">"),
Config::tag(), m_ipv6, m_ttl);
# endif
}
#ifndef XMRIG_FEATURE_EVENTS
const xmrig::DnsConfig &xmrig::DnsConfig::current()
{
return config;
}
void xmrig::DnsConfig::set(const rapidjson::Value &value)
{
config = { value, config };
}
#endif

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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
@ -26,6 +26,9 @@
namespace xmrig {
class Arguments;
class DnsConfig
{
public:
@ -33,22 +36,35 @@ public:
static const char *kIPv6;
static const char *kTTL;
static constexpr uint32_t kDefaultTTL = 30U;
DnsConfig() = default;
DnsConfig(const rapidjson::Value &value);
DnsConfig(const Arguments &arguments);
DnsConfig(const rapidjson::Value &value, const DnsConfig &current);
inline bool isIPv6() const { return m_ipv6; }
inline uint32_t ttl() const { return m_ttl * 1000U; }
inline bool isIPv6() const { return m_ipv6; }
inline uint32_t ttl() const { return m_ttl * 1000U; }
inline bool operator!=(const DnsConfig &other) const { return !isEqual(other); }
inline bool operator==(const DnsConfig &other) const { return isEqual(other); }
bool isEqual(const DnsConfig &other) const;
rapidjson::Value toJSON(rapidjson::Document &doc) const;
void print() const;
static const DnsConfig &current();
# ifndef XMRIG_FEATURE_EVENTS
static void set(const rapidjson::Value &value);
# endif
private:
bool m_ipv6 = false;
uint32_t m_ttl = 30U;
uint32_t m_ttl = kDefaultTTL;
};
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_DNSCONFIG_H */
#endif // XMRIG_DNSCONFIG_H

View File

@ -16,16 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/net/dns/Dns.h"
#include "base/kernel/private/DnsConfig.h"
#include "base/net/dns/DnsUvBackend.h"
namespace xmrig {
DnsConfig Dns::m_config;
std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends;
static std::map<String, std::shared_ptr<IDnsBackend> > backends;
} // namespace xmrig
@ -33,9 +32,9 @@ std::map<String, std::shared_ptr<IDnsBackend> > Dns::m_backends;
std::shared_ptr<xmrig::DnsRequest> xmrig::Dns::resolve(const String &host, IDnsListener *listener, uint64_t ttl)
{
if (m_backends.find(host) == m_backends.end()) {
m_backends.insert({ host, std::make_shared<DnsUvBackend>() });
if (backends.find(host) == backends.end()) {
backends.insert({ host, std::make_shared<DnsUvBackend>() });
}
return m_backends.at(host)->resolve(host, listener, ttl == 0 ? m_config.ttl() : ttl);
return backends.at(host)->resolve(host, listener, ttl == 0 ? DnsConfig::current().ttl() : ttl);
}

View File

@ -20,38 +20,26 @@
#define XMRIG_DNS_H
#include "base/net/dns/DnsConfig.h"
#include "base/tools/String.h"
#include <map>
#include <cstdint>
#include <memory>
namespace xmrig {
class DnsConfig;
class DnsRequest;
class IDnsBackend;
class IDnsListener;
class String;
class Dns
{
public:
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:
static DnsConfig m_config;
static std::map<String, std::shared_ptr<IDnsBackend> > m_backends;
};
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_DNS_H */
#endif // XMRIG_DNS_H

View File

@ -1,56 +0,0 @@
/* 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

@ -20,7 +20,7 @@
#include "base/net/dns/DnsRecords.h"
#include "base/net/dns/Dns.h"
#include "base/kernel/private/DnsConfig.h"
const xmrig::DnsRecord &xmrig::DnsRecords::get(DnsRecord::Type prefered) const
@ -34,7 +34,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 || Dns::config().isIPv6() || !ipv4)) {
if (ipv6 && (prefered == DnsRecord::AAAA || DnsConfig::current().isIPv6() || !ipv4)) {
return m_ipv6[ipv6 == 1 ? 0 : static_cast<size_t>(rand()) % ipv6]; // NOLINT(concurrency-mt-unsafe, cert-msc30-c, cert-msc50-cpp)
}

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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
@ -27,6 +27,7 @@
#include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h"
#include "base/kernel/interfaces/IJsonReader.h"
#include "base/kernel/private/DnsConfig.h"
#include "base/net/dns/Dns.h"
#include "crypto/common/Assembly.h"
@ -299,7 +300,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(DnsConfig::kField), DnsConfig::current().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);

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 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