Skip to content

Commit

Permalink
Remove DNS.Resolvers from IPNS config if DOH is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Aug 15, 2022
1 parent b33b19b commit 060f63c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions browser/extensions/api/ipfs_api.cc
Expand Up @@ -167,8 +167,10 @@ void IpfsAddIpfsPeerFunction::OnConfigLoaded(const std::string& peer,
return Respond(Error("Could not obtain IPFS service"));
}
auto config_path = ipfs_service->GetConfigFilePath();

auto write_callback =
base::BindOnce(&WriteFileOnFileThread, config_path, new_config);

base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()}, std::move(write_callback),
base::BindOnce(&IpfsAddIpfsPeerFunction::OnConfigUpdated,
Expand Down
2 changes: 2 additions & 0 deletions components/services/ipfs/ipfs_service_utils.cc
Expand Up @@ -59,6 +59,8 @@ bool UpdateConfigJSON(const std::string& source,
base::Value::Dict dns_resolvers;
dns_resolvers.Set(".", *config->doh_server_url);
dict->SetByDottedPath("DNS.Resolvers", std::move(dns_resolvers));
} else {
dict->RemoveByDottedPath("DNS.Resolvers");
}

base::Value::List list;
Expand Down
43 changes: 43 additions & 0 deletions components/services/ipfs/ipfs_service_utils_unittest.cc
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 Down

0 comments on commit 060f63c

Please sign in to comment.