Skip to content

Commit

Permalink
add Cloudflare DNS for faster resolution
Browse files Browse the repository at this point in the history
For me, Google DNS takes about 40 seconds to resolve a domain name.
  • Loading branch information
ZTzTopia committed Feb 23, 2024
1 parent 8c76ce9 commit c096212
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static const std::map<std::string, ConfigStorage> config_defaults{
{ "server.address", "www.growtopia1.com" },
{ "client.game_version", "4.35" },
{ "client.protocol", 192 },
{ "client.dnsServer", "cloudflare" },
{ "extension.ignore", std::vector<std::string>{ "0xdeadbeef" } },
{ "log.printMessage", true },
{ "log.printGameUpdatePacket", false },
Expand Down
20 changes: 17 additions & 3 deletions src/extension/web_server/web_server_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,23 @@ class WebServerExtension final : public IWebServerExtension {

Result resolve_domain_name(const std::string& domain_name)
{
static httplib::Client cli{ "https://dns.google" };
std::string host{
core_->get_config().get("client.dnsServer") == "cloudflare"
? "cloudflare-dns.com"
: "dns.google"
};
std::string path{
core_->get_config().get("client.dnsServer") == "cloudflare"
? "/dns-query"
: "/resolve"
};

httplib::Headers headers = {
{ "Accept", "application/dns-json" }
};

httplib::Result res{ cli.Get(std::format("/resolve?name={}&type=A", domain_name)) };
static httplib::Client cli{ std::format("https://{}", host) };
httplib::Result res{ cli.Get(std::format("{}?name={}&type=A", path, domain_name), headers) };
if (!validate_server_response(res)) {
return { DomainResolverStatus::ServerFail, {} };
}
Expand Down Expand Up @@ -171,7 +185,7 @@ class WebServerExtension final : public IWebServerExtension {

if (status != DomainResolverStatus::NoError) {
spdlog::error(
"Error occurred while resolving {} ip address. Dns server returned {}",
"Error occurred while resolving {} ip address. DNS server returned {}",
host,
magic_enum::enum_name(status)
);
Expand Down

0 comments on commit c096212

Please sign in to comment.