Skip to content

Commit

Permalink
Renamed log levels, hoping that helps windows somehow?
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Feb 6, 2024
1 parent b3a2cc7 commit f132bbd
Show file tree
Hide file tree
Showing 43 changed files with 375 additions and 399 deletions.
44 changes: 25 additions & 19 deletions component/chromium_ipfs_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

#include <base/logging.h>

#include <ipfs_client/crypto/openssl_signature_verifier.h>
#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/ipns_record.h>


using Self = ipfs::ChromiumIpfsContext;

void Self::SetupHttp(network::mojom::URLLoaderFactory& lf) {
Expand Down Expand Up @@ -76,26 +76,32 @@ auto Self::ParseJson(std::string_view j_str) const
}
return {};
}
unsigned int Self::GetGatewayRate(std::string_view prefix) {
return rates_.GetRate(prefix);
}
void Self::SetGatewayRate(std::string_view prefix, unsigned int new_rate) {
rates_.SetRate(prefix, new_rate);
}
auto Self::GetGateway(std::size_t index) const -> std::optional<GatewaySpec> {
auto [gw, r] = rates_.at(index);
if (gw) {
return GatewaySpec{*gw, r};
}
return std::nullopt;
}
void Self ::AddGateway(std::string_view prefix) {
rates_.SetRate(prefix, 120U);
}
// unsigned int Self::GetGatewayRate(std::string_view prefix) {
// return rates_.GetGatewayRate(prefix);
// }
// void Self::SetGatewayRate(std::string_view prefix, unsigned int new_rate) {
// rates_.SetGatewayRate(prefix, new_rate);
// }
// auto Self::GetGateway(std::size_t index) const -> std::optional<GatewaySpec>
// {
// auto [gw, r] = rates_.at(index);
// if (gw) {
// return GatewaySpec{*gw, r};
// }
// return std::nullopt;
// }
// void Self ::AddGateway(std::string_view prefix) {
// rates_.SetGatewayRate(prefix, 120U);
// }

Self::ChromiumIpfsContext(InterRequestState& state, PrefService* prefs)
: rates_{prefs} {
Self::ChromiumIpfsContext(InterRequestState& state, PrefService* prefs) {
with(std::make_unique<ChromiumIpfsGatewayConfig>(prefs));
with(std::make_unique<ChromiumDnsTxtLookup>(state));
using K = crypto::SigningKeyType;
with(K::RSA,
std::make_unique<crypto::OpensslSignatureVerifier>(EVP_PKEY_RSA));
with(K::Ed25519,
std::make_unique<crypto::OpensslSignatureVerifier>(EVP_PKEY_ED25519));
}
Self::~ChromiumIpfsContext() noexcept {
LOG(WARNING) << "API dtor - are all URIs loaded?";
Expand Down
7 changes: 0 additions & 7 deletions component/chromium_ipfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class IpfsRequest;
class NetworkRequestor;

class ChromiumIpfsContext final : public ContextApi {
GatewayRates rates_;

std::string MimeType(std::string extension,
std::string_view content,
std::string const& url) const override;
Expand All @@ -39,11 +37,6 @@ class ChromiumIpfsContext final : public ContextApi {
std::unique_ptr<DagCborValue> ParseCbor(ByteView) const override;
std::unique_ptr<DagJsonValue> ParseJson(std::string_view) const override;

std::optional<GatewaySpec> GetGateway(std::size_t index) const override;
unsigned int GetGatewayRate(std::string_view) override;
void SetGatewayRate(std::string_view, unsigned int) override;
void AddGateway(std::string_view) override;

public:
ChromiumIpfsContext(InterRequestState&, PrefService* prefs);
~ChromiumIpfsContext() noexcept override;
Expand Down
5 changes: 2 additions & 3 deletions component/inter_request_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <base/logging.h>
#include "content/public/browser/browser_context.h"

#include <ipfs_client/gateways.h>
#include <ipfs_client/gw/default_requestor.h>
#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/response.h>
#include "ipfs_client/ctx/default_gateways.h"

using Self = ipfs::InterRequestState;

Expand Down Expand Up @@ -51,8 +51,7 @@ auto Self::cache() -> std::shared_ptr<CacheRequestor>& {
}
auto Self::orchestrator() -> Orchestrator& {
if (!orc_) {
auto rtor =
gw::default_requestor(Gateways::DefaultGateways(), cache(), api());
auto rtor = gw::default_requestor(cache(), api());
orc_ = std::make_shared<Orchestrator>(rtor, api());
}
return *orc_;
Expand Down
2 changes: 1 addition & 1 deletion component/inter_request_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "cache_requestor.h"

#include "ipfs_client/gateways.h"
#include "ipfs_client/ctx/default_gateways.h"
#include "ipfs_client/ipns_names.h"
#include "ipfs_client/orchestrator.h"

Expand Down
2 changes: 1 addition & 1 deletion component/ipfs_url_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "chromium_ipfs_context.h"
#include "inter_request_state.h"

#include "ipfs_client/gateways.h"
#include "ipfs_client/ctx/default_gateways.h"
#include "ipfs_client/ipfs_request.h"

#include "base/debug/stack_trace.h"
Expand Down
39 changes: 27 additions & 12 deletions component/preferences.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include "preferences.h"

#include <ipfs_client/gateways.h>
#include <ipfs_client/ctx/default_gateways.h>
#include <ipfs_client/ctx/transitory_gateway_config.h>

#include <base/logging.h>
#include <components/prefs/pref_registry_simple.h>
#include <components/prefs/pref_service.h>

#include <ipfs_client/gateways.h>

namespace {
std::string const kRateLimit{"ipfs.gateways.rate_limits"};
}

void ipfs::RegisterPreferences(PrefRegistrySimple* service) {
ctx::TransitoryGatewayConfig cfg;
ctx::LoadStaticGatewayList(cfg);
base::Value::Dict vals;
for (auto& gw : Gateways::DefaultGateways()) {
vals.Set(gw.prefix, static_cast<int>(gw.rate));
for (auto i = 0UL; i < 999UL; ++i) {
auto gw = cfg.GetGateway(i);
if (!gw) {
break;
}
vals.Set(gw->prefix, static_cast<int>(gw->rate));
}
VLOG(2) << "Registering ipfs.gateways preference with a default value "
"that contains "
Expand All @@ -25,8 +30,8 @@ void ipfs::RegisterPreferences(PrefRegistrySimple* service) {
}
service->RegisterDictionaryPref(kRateLimit, std::move(vals));
}
using Rates = ipfs::GatewayRates;
Rates::GatewayRates(PrefService* prefs) : prefs_{prefs} {
using Self = ipfs::ChromiumIpfsGatewayConfig;
Self::ChromiumIpfsGatewayConfig(PrefService* prefs) : prefs_{prefs} {
if (prefs) {
last_ = prefs->GetDict(kRateLimit).Clone();
for (auto [k, v] : last_) {
Expand All @@ -40,7 +45,7 @@ Rates::GatewayRates(PrefService* prefs) : prefs_{prefs} {
}
}

std::pair<std::string const*, unsigned> Rates::at(std::size_t index) const {
std::pair<std::string const*, unsigned> Self::at(std::size_t index) const {
if (index >= curr_.size()) {
return {nullptr, 0U};
}
Expand All @@ -49,11 +54,11 @@ std::pair<std::string const*, unsigned> Rates::at(std::size_t index) const {
auto v = static_cast<unsigned>(std::max(0, it->second.GetInt()));
return {p_k, v};
}
unsigned Rates::GetRate(std::string_view k) const {
unsigned Self::GetGatewayRate(std::string_view k) {
auto i = std::max(0, curr_.FindInt(k).value_or(0));
return static_cast<unsigned>(i);
}
void Rates::SetRate(std::string_view k, unsigned val) {
void Self::SetGatewayRate(std::string_view k, unsigned val) {
auto i = static_cast<int>(std::min(val, static_cast<unsigned>(INT_MAX)));
auto old = curr_.contains(k);
curr_.Set(k, i);
Expand All @@ -70,15 +75,25 @@ void Rates::SetRate(std::string_view k, unsigned val) {
}
}
}
std::size_t Rates::delta() const {
void Self::AddGateway(std::string_view k) {
SetGatewayRate(k, 120);
}
auto Self::GetGateway(std::size_t index) const -> std::optional<GatewaySpec> {
auto [k, r] = at(index);
if (k) {
return GatewaySpec{*k, r};
}
return std::nullopt;
}
std::size_t Self::delta() const {
std::size_t rv = 0;
for (auto [k, v] : curr_) {
auto d = std::abs(v.GetInt() - last_.FindInt(k).value_or(0));
rv += static_cast<std::size_t>(d);
}
return rv;
}
void Rates::save() {
void Self::save() {
// Should be called on UI thread
changes = 0;
last_ = curr_.Clone();
Expand Down
13 changes: 8 additions & 5 deletions component/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

#include <base/memory/raw_ptr.h>
#include <base/values.h>
#include <ipfs_client/gateways.h>

#include <ipfs_client/ctx/gateway_config.h>

#include <ranges>

Expand All @@ -14,7 +15,7 @@ class PrefService;

namespace ipfs {
COMPONENT_EXPORT(IPFS) void RegisterPreferences(PrefRegistrySimple*);
class GatewayRates {
class ChromiumIpfsGatewayConfig final : public ipfs::ctx::GatewayConfig {
raw_ptr<PrefService> prefs_;
base::Value::Dict last_;
base::Value::Dict curr_;
Expand All @@ -25,10 +26,12 @@ class GatewayRates {
std::size_t delta() const;

public:
GatewayRates(PrefService*);
ChromiumIpfsGatewayConfig(PrefService*);

unsigned GetRate(std::string_view) const;
void SetRate(std::string_view, unsigned);
unsigned GetGatewayRate(std::string_view) override;
void SetGatewayRate(std::string_view, unsigned) override;
std::optional<GatewaySpec> GetGateway(std::size_t index) const override;
void AddGateway(std::string_view) override;
std::pair<std::string const*, unsigned> at(std::size_t index) const;
};
}
Expand Down
1 change: 1 addition & 0 deletions library/BUILD.gn.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if (enable_ipfs) {
]
public_deps = [
"//third_party/abseil-cpp:absl",
"//third_party/boringssl",
"//base",
]
deps = [
Expand Down
22 changes: 11 additions & 11 deletions library/include/ipfs_client/context_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "crypto/signature_verifier.h"
#include "crypto/signing_key_type.h"
#include "ctx/dns_txt_lookup.h"
#include "ctx/gateway_config.h"
#include "ctx/http_api.h"
#include "dag_cbor_value.h"
#include "gateway_spec.h"
#include "http_request_description.h"
#include "ipns_cbor_entry.h"
#include "multi_hash.h"
Expand Down Expand Up @@ -36,13 +36,18 @@ class DagJsonValue;
*/
class ContextApi : public std::enable_shared_from_this<ContextApi> {
public:
using SigningKeyType = ::ipfs::crypto::SigningKeyType;

ContextApi();
virtual ~ContextApi() noexcept {}

ctx::HttpApi& http();
ctx::DnsTxtLookup& dns_txt();
ctx::GatewayConfig& gw_cfg();
ContextApi& with(std::unique_ptr<ctx::HttpApi>);
ContextApi& with(std::unique_ptr<ctx::DnsTxtLookup>);
ContextApi& with(std::unique_ptr<ctx::GatewayConfig>);
ContextApi& with(SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>);

/*!
* \brief Determine a mime type for a given file.
Expand All @@ -67,26 +72,21 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {

using IpnsCborEntry = ::ipfs::IpnsCborEntry;

using SigningKeyType = ::ipfs::crypto::SigningKeyType;
using ByteView = ::ipfs::ByteView;
virtual bool VerifyKeySignature(SigningKeyType,
ByteView signature,
ByteView data,
ByteView key_bytes) const;
bool VerifyKeySignature(SigningKeyType,
ByteView signature,
ByteView data,
ByteView key_bytes) const;

std::optional<std::vector<Byte>> Hash(HashType, ByteView data);

virtual std::optional<GatewaySpec> GetGateway(std::size_t index) const = 0;
virtual unsigned GetGatewayRate(std::string_view) = 0;
virtual void SetGatewayRate(std::string_view, unsigned);
virtual void AddGateway(std::string_view) = 0;

protected:
std::unordered_map<HashType, std::unique_ptr<crypto::Hasher>> hashers_;
std::unordered_map<SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>>
verifiers_;
std::unique_ptr<ctx::HttpApi> http_api_;
std::unique_ptr<ctx::DnsTxtLookup> dns_txt_;
std::unique_ptr<ctx::GatewayConfig> gateway_config_;
};

} // namespace ipfs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef IPFS_CHROMIUM_OPENSSL_SIGNATURE_VERIFIER_H
#define IPFS_CHROMIUM_OPENSSL_SIGNATURE_VERIFIER_H

#include <ipfs_client/crypto/signature_verifier.h>
#include "signature_verifier.h"

#if __has_include(<openssl/evp.h>)
#include <openssl/evp.h>
Expand Down
2 changes: 1 addition & 1 deletion library/include/ipfs_client/crypto/signature_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ipfs::crypto {
class SignatureVerifier {
public:
virtual ~SignatureVerifier() noexcept {}

using ByteView = ipfs::ByteView;
virtual bool VerifySignature(ByteView signature,
ByteView data,
ByteView key) = 0;
Expand Down
12 changes: 12 additions & 0 deletions library/include/ipfs_client/ctx/default_gateways.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CHROMIUM_IPFS_GATEWAYS_H_
#define CHROMIUM_IPFS_GATEWAYS_H_

namespace ipfs::ctx {
class GatewayConfig;

bool LoadGatewaysFromEnvironmentVariable(GatewayConfig&);
void LoadStaticGatewayList(GatewayConfig&);

} // namespace ipfs

#endif // CHROMIUM_IPFS_GATEWAYS_H_
21 changes: 21 additions & 0 deletions library/include/ipfs_client/ctx/gateway_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef IPFS_CHROMIUM_GATEWAY_CONFIG_H
#define IPFS_CHROMIUM_GATEWAY_CONFIG_H

#include <ipfs_client/gateway_spec.h>

#include <cstdint>
#include <optional>
#include <string_view>

namespace ipfs::ctx {
class GatewayConfig {
public:
virtual ~GatewayConfig() noexcept {}
virtual std::optional<GatewaySpec> GetGateway(std::size_t index) const = 0;
virtual unsigned GetGatewayRate(std::string_view) = 0;
virtual void SetGatewayRate(std::string_view, unsigned) = 0;
virtual void AddGateway(std::string_view) = 0;
};
} // namespace ipfs::ctx

#endif // IPFS_CHROMIUM_GATEWAY_CONFIG_H
Loading

0 comments on commit f132bbd

Please sign in to comment.