1 parent
7a0446b573
commit
3632f2cd43
27 files changed
+3332
-97
No files matched your search
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <expected>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Web::Security
|
||||
{
|
||||
namespace asio = boost::asio;
|
||||
|
||||
class CidrBlock
|
||||
{
|
||||
public:
|
||||
|
||||
CidrBlock(asio::ip::address network, unsigned int prefixLength);
|
||||
|
||||
[[nodiscard]] bool contains(
|
||||
const asio::ip::address& address) const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
asio::ip::address network_;
|
||||
unsigned int prefixLength_ = 0;
|
||||
};
|
||||
|
||||
using ParsedCidrs = std::expected<std::vector<CidrBlock>, std::string>;
|
||||
|
||||
[[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]] bool validShortSlug(std::string_view slug) noexcept;
|
||||
[[nodiscard]] bool validRedirectLocation(std::string_view location) noexcept;
|
||||
|
||||
class ShortLinkRateLimiter
|
||||
{
|
||||
public:
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using TimePoint = Clock::time_point;
|
||||
|
||||
struct Config {
|
||||
double globalRate = 200.0;
|
||||
double globalBurst = 400.0;
|
||||
double peerRate = 10.0;
|
||||
double peerBurst = 20.0;
|
||||
std::size_t maxPeers = 4096;
|
||||
Clock::duration idleTimeout = std::chrono::minutes(10);
|
||||
Clock::duration sweepInterval = std::chrono::minutes(1);
|
||||
};
|
||||
|
||||
ShortLinkRateLimiter();
|
||||
explicit ShortLinkRateLimiter(Config config);
|
||||
|
||||
[[nodiscard]] bool allow(std::string_view peer);
|
||||
[[nodiscard]] bool allow(std::string_view peer, TimePoint now);
|
||||
void reset();
|
||||
[[nodiscard]] std::size_t trackedPeerCount() const;
|
||||
|
||||
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_;
|
||||
TimePoint nextSweep_{};
|
||||
bool sweepInitialized_ = false;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Web::Security
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "echoion/web/HttpSecurity.hpp"
|
||||
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
@@ -7,13 +9,16 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Web
|
||||
{
|
||||
@@ -43,6 +48,7 @@ namespace Echoion::Web
|
||||
// Server-wide switch for the Now Playing video preview,
|
||||
// surfaced to the browser via /config.json.
|
||||
bool nowPlayingVideo = true;
|
||||
std::vector<std::string> trustedProxyCidrs;
|
||||
};
|
||||
|
||||
HttpServer();
|
||||
@@ -52,6 +58,12 @@ namespace Echoion::Web
|
||||
void stop();
|
||||
[[nodiscard]] bool isRunning() const { return running_; }
|
||||
|
||||
using TrustedProxyUpdate = std::expected<void, std::string>;
|
||||
[[nodiscard]] static TrustedProxyUpdate validateTrustedProxyCidrs(
|
||||
const std::vector<std::string>& cidrs);
|
||||
[[nodiscard]] TrustedProxyUpdate setTrustedProxyCidrs(
|
||||
const std::vector<std::string>& cidrs);
|
||||
|
||||
// A user's avatar image, served at GET /avatar/<uid>. Installed
|
||||
// by Core (which owns the TeamSpeak plugin handle); best-effort —
|
||||
// nullopt yields a 404 and the UI falls back to initials. It may be
|
||||
@@ -79,6 +91,25 @@ namespace Echoion::Web
|
||||
dashboardProvider_ = std::move(provider);
|
||||
}
|
||||
|
||||
enum class ShortLinkStatus {
|
||||
found,
|
||||
notFound,
|
||||
gone,
|
||||
rateLimited,
|
||||
unavailable,
|
||||
};
|
||||
struct ShortLinkResolution {
|
||||
ShortLinkStatus status = ShortLinkStatus::notFound;
|
||||
std::string location;
|
||||
};
|
||||
using ShortLinkResolver =
|
||||
std::function<ShortLinkResolution(std::string_view key)>;
|
||||
void setShortLinkResolver(ShortLinkResolver resolver)
|
||||
{
|
||||
std::lock_guard lock(shortLinkResolverMutex_);
|
||||
shortLinkResolver_ = std::move(resolver);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void acceptLoop();
|
||||
@@ -96,7 +127,12 @@ namespace Echoion::Web
|
||||
// Serves one request; returns true when the connection should be
|
||||
// kept open for the next request (HTTP keep-alive).
|
||||
bool serveHttp(asio::ip::tcp::socket& socket,
|
||||
const http::request<http::string_body>& req);
|
||||
const http::request<http::string_body>& req,
|
||||
std::string_view clientAddress);
|
||||
[[nodiscard]] std::string clientAddress(
|
||||
const asio::ip::tcp::socket& socket,
|
||||
const http::request<http::string_body>& req) const;
|
||||
[[nodiscard]] bool allowShortLinkRequest(std::string_view peer);
|
||||
|
||||
Config config_;
|
||||
asio::io_context ioContext_;
|
||||
@@ -126,6 +162,11 @@ namespace Echoion::Web
|
||||
std::mutex avatarProviderMutex_;
|
||||
DashboardProvider dashboardProvider_;
|
||||
std::mutex dashboardProviderMutex_;
|
||||
ShortLinkResolver shortLinkResolver_;
|
||||
std::mutex shortLinkResolverMutex_;
|
||||
mutable std::shared_mutex trustedProxyMutex_;
|
||||
std::vector<Security::CidrBlock> trustedProxyNetworks_;
|
||||
Security::ShortLinkRateLimiter shortLinkRateLimiter_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Web
|
||||
Reference in new issue
Block a user