Reuse VoidLibrary rate limiting and export stable C ABI

This commit is contained in:
faumaray committed 2026-07-29 22:40:35 +03:00
1 parent 2d132d2806
commit c85bf13244
7 files changed
+188 -107

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
name: Independent component release
on:
push:
tags: [v*]
schedule:
- cron: '17 2 * * *'
workflow_dispatch:
jobs:
publish:
runs-on: echoion-release-serial
steps:
- name: Check out component
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
with:
fetch-depth: 0
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
persist-credentials: false
- name: Check out pinned public release tools
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
with:
repository: Void/Echoion
ref: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
path: .echoion-release-tools
fetch-depth: 0
ssh-key: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
persist-credentials: false
- name: Build, publish, and merge feed transaction
shell: bash
env:
AWS_ACCESS_KEY_ID: ${{ secrets.COMPONENT_OBJECT_STORAGE_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.COMPONENT_OBJECT_STORAGE_SECRET_KEY }}
AWS_DEFAULT_REGION: ${{ vars.COMPONENT_OBJECT_STORAGE_REGION }}
ECHOION_COMPONENT_SSH_KEY: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
ECHOION_DISTRIBUTION_BASE_URL: ${{ vars.ECHOION_DISTRIBUTION_BASE_URL }}
ECHOION_FEED_BASE_URL: ${{ vars.ECHOION_FEED_BASE_URL }}
ECHOION_FEED_COMMIT_SIGNING_KEY: ${{ secrets.RELEASE_FEED_GIT_SIGNING_KEY }}
ECHOION_FEED_PRIVATE_KEY: ${{ secrets.RELEASE_FEED_ED25519_PRIVATE_KEY }}
ECHOION_FEED_REPOSITORY: ssh://gitea@git.faumaray.ru/Void/Echoion.git
ECHOION_FEED_SIGNING_KEY_ID: ${{ vars.ECHOION_FEED_SIGNING_KEY_ID }}
ECHOION_OBJECT_STORAGE_BUCKET: ${{ vars.COMPONENT_OBJECT_STORAGE_BUCKET }}
ECHOION_OBJECT_STORAGE_ENDPOINT: ${{ vars.COMPONENT_OBJECT_STORAGE_ENDPOINT }}
ECHOION_OBJECT_STORAGE_PREFIX: ${{ vars.COMPONENT_OBJECT_STORAGE_PREFIX }}
ECHOION_RELEASE_TAG_ALLOWED_SIGNERS: ${{ secrets.RELEASE_TAG_ALLOWED_SIGNERS }}
GITEA_API_URL: ${{ gitea.api_url }}
GITEA_EVENT_NAME: ${{ gitea.event_name }}
GITEA_REF_NAME: ${{ gitea.ref_name }}
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
GITEA_SHA: ${{ gitea.sha }}
GITEA_REPOSITORY: ${{ gitea.repository }}
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN }}
RELEASE_TOOLS_REF: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
run: |
set -euo pipefail
[[ "$RELEASE_TOOLS_REF" =~ ^[0-9a-f]{40}$ ]]
test "$(git -C .echoion-release-tools rev-parse HEAD)" = "$RELEASE_TOOLS_REF"
.echoion-release-tools/scripts/release-component.sh component.release.json
+6 -2
View File
@@ -1,8 +1,11 @@
cmake_minimum_required(VERSION 4.0.0)
if(NOT DEFINED ECHOION_PRODUCT_VERSION)
set(ECHOION_PRODUCT_VERSION 2.0.0)
set(ECHOION_PRODUCT_VERSION 0.0.0)
endif()
project(EchoionWeb VERSION "${ECHOION_PRODUCT_VERSION}" LANGUAGES CXX)
if(NOT DEFINED ECHOION_COMPONENT_VERSION)
set(ECHOION_COMPONENT_VERSION "${ECHOION_PRODUCT_VERSION}")
endif()
project(EchoionWeb VERSION "${ECHOION_COMPONENT_VERSION}" LANGUAGES CXX)
include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -73,6 +76,7 @@ target_link_libraries(EchoionWeb
Void::Data
Void::Log
Void::Async
Void::Resilience
PRIVATE
Threads::Threads
Boost::beast
+3 -2
View File
@@ -1,14 +1,15 @@
{
"schemaVersion": 1,
"contractId": "echoion.module.web",
"descriptor": "Void::Plugin::ModuleDescriptor",
"descriptor": "echoion_module_descriptor_v1",
"querySymbol": "echoion_module_abi_query",
"moduleId": "EchoionWeb",
"activation": "restart-required",
"dependencies": [
{
"moduleId": "EchoionApi",
"versionRange": "^2.0.0",
"abiHash": "sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3"
"abiHash": "sha256:d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06c6c113"
}
]
}
+9
View File
@@ -0,0 +1,9 @@
{
"schemaVersion": 1,
"componentId": "EchoionWeb",
"productCompatibility": ">=2.0.0 <3.0.0",
"publishNightly": true,
"cmakeOptions": {
"ECHOION_BUILD_WEB": "ON"
}
}
+14 -25
View File
@@ -1,5 +1,7 @@
#pragma once
#include <Void/Resilience/KeyedRateLimiter.hpp>
#include <Void/Resilience/RateLimiter.hpp>
#include <boost/asio/ip/address.hpp>
#include <chrono>
@@ -10,7 +12,6 @@
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace Echoion::Web::Security
@@ -23,8 +24,8 @@ namespace Echoion::Web::Security
CidrBlock(asio::ip::address network, unsigned int prefixLength);
[[nodiscard]] bool contains(
const asio::ip::address& address) const noexcept;
[[nodiscard]] bool
contains(const asio::ip::address &address) const noexcept;
private:
@@ -34,16 +35,17 @@ namespace Echoion::Web::Security
using ParsedCidrs = std::expected<std::vector<CidrBlock>, std::string>;
[[nodiscard]] ParsedCidrs parseTrustedProxyCidrs(
std::span<const std::string> cidrs);
[[nodiscard]] ParsedCidrs
parseTrustedProxyCidrs(std::span<const std::string> cidrs);
[[nodiscard]] std::string effectiveClientAddress(
const asio::ip::address& immediatePeer,
std::optional<std::string_view> forwardedFor,
std::span<const CidrBlock> trustedProxies);
[[nodiscard]] std::string
effectiveClientAddress(const asio::ip::address &immediatePeer,
std::optional<std::string_view> forwardedFor,
std::span<const CidrBlock> trustedProxies);
[[nodiscard]] bool validShortSlug(std::string_view slug) noexcept;
[[nodiscard]] bool validRedirectLocation(std::string_view location) noexcept;
[[nodiscard]] bool
validRedirectLocation(std::string_view location) noexcept;
class ShortLinkRateLimiter
{
@@ -72,25 +74,12 @@ namespace Echoion::Web::Security
private:
struct TokenBucket {
double tokens = 0.0;
TimePoint updated{};
bool initialized = false;
};
struct PeerBucket {
TokenBucket bucket;
TimePoint lastSeen{};
};
static void refill(TokenBucket& bucket, double rate, double burst,
TimePoint now) noexcept;
void evictIdle(TimePoint now, bool force);
Config config_;
mutable std::mutex mutex_;
TokenBucket global_;
std::unordered_map<std::string, PeerBucket> peers_;
Void::Resilience::TokenBucket global_;
Void::Resilience::KeyedRateLimiter<std::string> peers_;
TimePoint nextSweep_{};
bool sweepInitialized_ = false;
};
+28 -59
View File
@@ -18,7 +18,7 @@ namespace Echoion::Web::Security
constexpr std::size_t MAX_REDIRECT_LENGTH = 8192;
asio::ip::address
normalizeMappedAddress(const asio::ip::address& address)
normalizeMappedAddress(const asio::ip::address &address)
{
if (address.is_v6() && address.to_v6().is_v4_mapped()) {
return asio::ip::make_address_v4(asio::ip::v4_mapped,
@@ -27,12 +27,12 @@ namespace Echoion::Web::Security
return address;
}
bool isTrusted(const asio::ip::address& address,
bool isTrusted(const asio::ip::address &address,
std::span<const CidrBlock>
trustedProxies) noexcept
{
return std::ranges::any_of(trustedProxies,
[&address](const CidrBlock& network) {
[&address](const CidrBlock &network) {
return network.contains(address);
});
}
@@ -56,7 +56,7 @@ namespace Echoion::Web::Security
{
}
bool CidrBlock::contains(const asio::ip::address& address) const noexcept
bool CidrBlock::contains(const asio::ip::address &address) const noexcept
{
asio::ip::address candidate = address;
if (network_.is_v4() && candidate.is_v6()) {
@@ -73,7 +73,7 @@ namespace Echoion::Web::Security
return false;
}
const auto matches = [this](const auto& lhs, const auto& rhs) {
const auto matches = [this](const auto &lhs, const auto &rhs) {
for (std::size_t index = 0; index < lhs.size(); ++index) {
const auto consumed = static_cast<unsigned int>(index * 8);
const auto remaining =
@@ -145,7 +145,7 @@ namespace Echoion::Web::Security
return parsed;
}
std::string effectiveClientAddress(const asio::ip::address& immediatePeer,
std::string effectiveClientAddress(const asio::ip::address &immediatePeer,
std::optional<std::string_view>
forwardedFor,
std::span<const CidrBlock>
@@ -213,7 +213,7 @@ namespace Echoion::Web::Security
if (!parsed) {
return false;
}
const auto& url = *parsed;
const auto &url = *parsed;
if ((url.scheme_id() != boost::urls::scheme::http &&
url.scheme_id() != boost::urls::scheme::https) ||
!url.has_authority() || url.has_userinfo() ||
@@ -240,7 +240,11 @@ namespace Echoion::Web::Security
{
}
ShortLinkRateLimiter::ShortLinkRateLimiter(Config config) : config_(config)
ShortLinkRateLimiter::ShortLinkRateLimiter(Config config)
: config_(config), global_(config.globalBurst, config.globalRate),
peers_(
config.peerBurst, config.peerRate, [] { return Clock::now(); },
config.maxPeers)
{
if (!std::isfinite(config_.globalRate) || config_.globalRate < 0.0 ||
!std::isfinite(config_.globalBurst) || config_.globalBurst < 1.0 ||
@@ -252,7 +256,6 @@ namespace Echoion::Web::Security
throw std::invalid_argument(
"invalid short-link rate limiter settings");
}
peers_.reserve(config_.maxPeers);
}
bool ShortLinkRateLimiter::allow(std::string_view peer)
@@ -264,44 +267,36 @@ namespace Echoion::Web::Security
{
std::lock_guard lock(mutex_);
evictIdle(now, false);
refill(global_, config_.globalRate, config_.globalBurst, now);
if (global_.tokens < 1.0) {
if (global_.available_at(now) < 1.0) {
return false;
}
const std::string peerKey{peer};
auto found = peers_.find(peerKey);
if (found == peers_.end()) {
if (peers_.size() >= config_.maxPeers) {
evictIdle(now, true);
}
if (peers_.size() >= config_.maxPeers) {
return false;
}
found =
peers_
.emplace(peerKey, PeerBucket{.bucket = {}, .lastSeen = now})
.first;
auto available = peers_.remaining_at(peerKey, now);
if (!available) {
evictIdle(now, true);
available = peers_.remaining_at(peerKey, now);
}
found->second.lastSeen = now;
refill(found->second.bucket, config_.peerRate, config_.peerBurst, now);
if (found->second.bucket.tokens < 1.0) {
if (!available || *available < 1.0) {
return false;
}
// Commit both token deductions together. A peer-local rejection never
// consumes global capacity and cannot starve unrelated clients.
global_.tokens -= 1.0;
found->second.bucket.tokens -= 1.0;
const bool peerAllowed = peers_.allow_at(peerKey, now);
const bool globalAllowed = global_.try_acquire_at(now);
if (!peerAllowed || !globalAllowed) {
throw std::logic_error{
"rate limiter transaction became inconsistent"};
}
return true;
}
void ShortLinkRateLimiter::reset()
{
std::lock_guard lock(mutex_);
global_ = {};
peers_.clear();
global_.reset();
peers_.reset();
nextSweep_ = {};
sweepInitialized_ = false;
}
@@ -309,24 +304,7 @@ namespace Echoion::Web::Security
std::size_t ShortLinkRateLimiter::trackedPeerCount() const
{
std::lock_guard lock(mutex_);
return peers_.size();
}
void ShortLinkRateLimiter::refill(TokenBucket& bucket, double rate,
double burst, TimePoint now) noexcept
{
if (!bucket.initialized) {
bucket.tokens = burst;
bucket.updated = now;
bucket.initialized = true;
return;
}
const auto elapsed =
now > bucket.updated
? std::chrono::duration<double>(now - bucket.updated).count()
: 0.0;
bucket.tokens = std::min(burst, bucket.tokens + elapsed * rate);
bucket.updated = now;
return peers_.tracked();
}
void ShortLinkRateLimiter::evictIdle(TimePoint now, bool force)
@@ -334,16 +312,7 @@ namespace Echoion::Web::Security
if (!force && sweepInitialized_ && now < nextSweep_) {
return;
}
for (auto peer = peers_.begin(); peer != peers_.end();) {
const bool expired =
now >= peer->second.lastSeen &&
now - peer->second.lastSeen >= config_.idleTimeout;
if (expired) {
peer = peers_.erase(peer);
} else {
++peer;
}
}
(void)peers_.cleanup_at(now, config_.idleTimeout);
nextSweep_ = now + config_.sweepInterval;
sweepInitialized_ = true;
}
+72 -19
View File
@@ -1,5 +1,6 @@
#include <echoion/api/module/Module.hpp>
#include <echoion/api/module/LegacyModuleAbi.hpp>
#include <iterator>
#include <memory>
#include <string_view>
@@ -10,6 +11,7 @@ namespace Echoion::Web
class WebModule final : public Void::Plugin::IModule
{
public:
[[nodiscard]] std::string_view name() const noexcept override
{
return "EchoionWeb";
@@ -22,41 +24,92 @@ namespace Echoion::Web
}
} // namespace
const Void::Plugin::ModuleDescriptor& webModuleDescriptor()
const Void::Plugin::ModuleDescriptor &webModuleDescriptor()
{
static const Void::Plugin::ModuleDescriptor descriptor{
.module_id = "EchoionWeb",
.component_version = {
.major = ECHOION_COMPONENT_VERSION_MAJOR,
.minor = ECHOION_COMPONENT_VERSION_MINOR,
.patch = ECHOION_COMPONENT_VERSION_PATCH,
.pre_release = {},
.build = {},
},
.abi_hash =
"sha256:858dfd3a19804e42a63fa8694a70ebe07ee90485a886df1ac07cf91bb5d34316",
.component_version =
{
.major = ECHOION_COMPONENT_VERSION_MAJOR,
.minor = ECHOION_COMPONENT_VERSION_MINOR,
.patch = ECHOION_COMPONENT_VERSION_PATCH,
.pre_release = {},
.build = {},
},
.abi_hash = "sha256:"
"e1f826ddb06caadc394af7a3db27af32789a7e580cde6ee516edd0"
"ebda2a5c9f",
.abi_contract = Void::Json::object({
{"schemaVersion", 1},
{"contractId", "echoion.module.web"},
{"descriptor", "Void::Plugin::ModuleDescriptor"},
{"descriptor", "echoion_module_descriptor_v1"},
}),
.dependencies = {
.dependencies =
{
.module_id = "EchoionApi",
.version_range = "^2.0.0",
.abi_hash = "sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3",
{
.module_id = "EchoionApi",
.version_range = "^2.0.0",
.abi_hash =
"sha256:"
"d50293d345a97e52f9844088bdd42277f566bc815a21b763"
"6d3e053f06c6c113",
},
},
},
.activation_policy =
Void::Plugin::ActivationPolicy::RestartRequired,
.create = createWebModule,
};
return descriptor;
}
const echoion_module_descriptor_v1 &webModuleAbiDescriptor()
{
using Bridge = Api::Module::LegacyModuleAbi<createWebModule>;
static const echoion_module_dependency_v1 dependencies[]{
{
.struct_size = sizeof(echoion_module_dependency_v1),
.component_id = Api::Module::Detail::stringView("EchoionApi"),
.version_range =
Api::Module::Detail::stringView(">=2.0.0 <3.0.0"),
.abi_hash = Api::Module::Detail::stringView(
"sha256:"
"d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06"
"c6c11"
"3"),
.optional = 0,
.reserved_bytes = {},
},
};
static const echoion_module_descriptor_v1 descriptor{
.struct_size = sizeof(echoion_module_descriptor_v1),
.abi_version = ECHOION_MODULE_ABI_VERSION,
.component_id = Api::Module::Detail::stringView("EchoionWeb"),
.version_major = ECHOION_COMPONENT_VERSION_MAJOR,
.version_minor = ECHOION_COMPONENT_VERSION_MINOR,
.version_patch = ECHOION_COMPONENT_VERSION_PATCH,
.version_prerelease = {},
.abi_hash = Api::Module::Detail::stringView(
"sha256:"
"e1f826ddb06caadc394af7a3db27af32789a7e580cde6ee516edd0ebda2a5c"
"9f"),
.dependencies = dependencies,
.dependency_count = std::size(dependencies),
.activation_policy = ECHOION_ACTIVATION_RESTART_REQUIRED_V1,
.api = &Bridge::api,
.reserved = {},
};
return descriptor;
}
} // namespace Echoion::Web
extern "C" ECHOION_MODULE_EXPORT const Void::Plugin::ModuleDescriptor*
echoion_module_query()
extern "C" ECHOION_MODULE_EXPORT const Void::Plugin::ModuleDescriptor *
echoion_module_query()
{
return &Echoion::Web::webModuleDescriptor();
}
extern "C" ECHOION_MODULE_ABI_EXPORT const echoion_module_descriptor_v1 *
echoion_module_abi_query()
{
return &Echoion::Web::webModuleAbiDescriptor();
}