3#include "util/Mutex.hpp"
4#include "util/config/ConfigDefinition.hpp"
5#include "util/log/Logger.hpp"
6#include "web/dosguard/DOSGuardInterface.hpp"
7#include "web/dosguard/WeightsInterface.hpp"
8#include "web/dosguard/WhitelistHandlerInterface.hpp"
10#include <boost/asio.hpp>
11#include <boost/iterator/transform_iterator.hpp>
12#include <boost/json/object.hpp>
13#include <boost/system/error_code.hpp>
19#include <unordered_map>
20#include <unordered_set>
22namespace web::dosguard {
34 std::uint32_t transferredByte = 0;
35 std::uint32_t requestsCount = 0;
39 std::unordered_map<std::string, ClientState> ipState;
40 std::unordered_map<std::string, std::uint32_t> ipConnCount;
44 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
45 std::reference_wrapper<WeightsInterface const> weights_;
47 std::uint32_t
const maxFetches_;
48 std::uint32_t
const maxConnCount_;
49 std::uint32_t
const maxRequestCount_;
74 isWhiteListed(std::string_view
const ip)
const noexcept override;
84 isOk(std::string
const& ip)
const noexcept override;
92 increment(std::string
const& ip)
noexcept override;
100 decrement(std::string
const& ip)
noexcept override;
114 [[maybe_unused]]
bool
115 add(std::string
const& ip, uint32_t numObjects)
noexcept override;
129 [[maybe_unused]]
bool
130 request(std::string
const& ip, boost::json::object
const&
request)
override;
136 clear()
noexcept override;
139 [[nodiscard]]
static std::unordered_set<std::string>
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:82
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:27
bool isWhiteListed(std::string_view const ip) const noexcept override
Check whether an ip address is in the whitelist or not.
Definition DOSGuard.cpp:38
void clear() noexcept override
Instantly clears all fetch counters added by.
Definition DOSGuard.cpp:124
bool request(std::string const &ip, boost::json::object const &request) override
Adds one request for the given ip address.
Definition DOSGuard.cpp:108
void increment(std::string const &ip) noexcept override
Increment connection count for the given ip address.
Definition DOSGuard.cpp:73
bool isOk(std::string const &ip) const noexcept override
Check whether an ip address is currently rate limited or not.
Definition DOSGuard.cpp:44
void decrement(std::string const &ip) noexcept override
Decrement connection count for the given ip address.
Definition DOSGuard.cpp:82
DOSGuard(util::config::ClioConfigDefinition const &config, WhitelistHandlerInterface const &whitelistHandler, WeightsInterface const &weights)
Constructs a new DOS guard.
Definition DOSGuard.cpp:24
bool add(std::string const &ip, uint32_t numObjects) noexcept override
Adds numObjects of usage for the given ip address.
Definition DOSGuard.cpp:94
Interface for determining request weights in DOS protection.
Definition WeightsInterface.hpp:15
Interface for a whitelist handler.
Definition WhitelistHandlerInterface.hpp:10