Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some IPFS DOH Fixes #14631

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion components/services/ipfs/ipfs_service_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/re2/src/re2/re2.h"
Expand Down Expand Up @@ -57,8 +58,13 @@ bool UpdateConfigJSON(const std::string& source,

if (config->doh_server_url) {
base::Value::Dict dns_resolvers;
dns_resolvers.Set(".", *config->doh_server_url);
std::string doh_url = *(config->doh_server_url);
// Kubo doesn't support RFC-8484 DOH url format
base::ReplaceSubstringsAfterOffset(&doh_url, 0, "{?dns}", "");
dns_resolvers.Set(".", std::move(doh_url));
dict->SetByDottedPath("DNS.Resolvers", std::move(dns_resolvers));
} else {
dict->RemoveByDottedPath("DNS.Resolvers");
}

base::Value::List list;
Expand Down
63 changes: 63 additions & 0 deletions components/services/ipfs/ipfs_service_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ TEST_F(IPFSServiceUtils, UpdateConfigJSONTest) {
EXPECT_EQ(updated, "");
}

TEST_F(IPFSServiceUtils, DNSResolversRemove) {
std::string updated;
{
std::string json = R"({})";

auto config = ipfs::mojom::IpfsConfig::New(
base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort",
"APIPort", "SwarmPort", "StorageSize",
"https://cloudflare.com/dns-query");

std::string expect =
"{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\","
"\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":"
"[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\""
"]},\"DNS\":{\"Resolvers\":{\".\":\"https://cloudflare.com/"
"dns-query\"}},"
"\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":"
"\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\","
"\"HighWater\":40,\"LowWater\":20}}}";

EXPECT_TRUE(UpdateConfigJSON(json, config.get(), &updated));
EXPECT_EQ(updated, expect);
}

std::string json = updated;

auto config = ipfs::mojom::IpfsConfig::New(
base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort",
"APIPort", "SwarmPort", "StorageSize", absl::nullopt);

std::string expect =
"{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\","
"\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":"
"[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\""
"]},"
"\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":"
"\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\","
"\"HighWater\":40,\"LowWater\":20}}}";

EXPECT_TRUE(UpdateConfigJSON(json, config.get(), &updated));
ASSERT_EQ(updated, expect);
}

TEST_F(IPFSServiceUtils, DNSResolversUpdate) {
std::string json = R"({})";
std::string updated;
Expand All @@ -85,6 +128,26 @@ TEST_F(IPFSServiceUtils, DNSResolversUpdate) {
EXPECT_EQ(updated, expect);
}

TEST_F(IPFSServiceUtils, DNSResolversUpdate_DnsHasRFC8484Template) {
std::string json = R"({})";
std::string updated;
auto config = ipfs::mojom::IpfsConfig::New(
base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort",
"APIPort", "SwarmPort", "StorageSize",
"https://cloudflare.com/dns-query{?dns}");

std::string expect =
"{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\","
"\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":"
"[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\""
"]},\"DNS\":{\"Resolvers\":{\".\":\"https://cloudflare.com/dns-query\"}},"
"\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":"
"\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\","
"\"HighWater\":40,\"LowWater\":20}}}";
ASSERT_TRUE(UpdateConfigJSON(json, config.get(), &updated));
EXPECT_EQ(updated, expect);
}

TEST_F(IPFSServiceUtils, GetVerisonFromNodeFilename) {
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.9.0-rc1_windows-amd64"),
Expand Down