22#include "util/Mutex.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "util/log/Logger.hpp"
25#include "web/dosguard/DOSGuardInterface.hpp"
26#include "web/dosguard/WeightsInterface.hpp"
27#include "web/dosguard/WhitelistHandlerInterface.hpp"
29#include <boost/asio.hpp>
30#include <boost/iterator/transform_iterator.hpp>
31#include <boost/json/object.hpp>
32#include <boost/system/error_code.hpp>
38#include <unordered_map>
39#include <unordered_set>
41namespace web::dosguard {
53 std::uint32_t transferredByte = 0;
54 std::uint32_t requestsCount = 0;
58 std::unordered_map<std::string, ClientState> ipState;
59 std::unordered_map<std::string, std::uint32_t> ipConnCount;
63 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
64 std::reference_wrapper<WeightsInterface const> weights_;
66 std::uint32_t
const maxFetches_;
67 std::uint32_t
const maxConnCount_;
68 std::uint32_t
const maxRequestCount_;
93 isWhiteListed(std::string_view
const ip)
const noexcept override;
103 isOk(std::string
const& ip)
const noexcept override;
111 increment(std::string
const& ip)
noexcept override;
119 decrement(std::string
const& ip)
noexcept override;
133 [[maybe_unused]]
bool
134 add(std::string
const& ip, uint32_t numObjects)
noexcept override;
148 [[maybe_unused]]
bool
149 request(std::string
const& ip, boost::json::object
const&
request)
override;
155 clear()
noexcept override;
158 [[nodiscard]]
static std::unordered_set<std::string>
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:96
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:46
A simple denial of service guard used for rate limiting.
Definition DOSGuard.hpp:48
bool isWhiteListed(std::string_view const ip) const noexcept override
Check whether an ip address is in the whitelist or not.
Definition DOSGuard.cpp:57
void clear() noexcept override
Instantly clears all fetch counters added by.
Definition DOSGuard.cpp:142
bool request(std::string const &ip, boost::json::object const &request) override
Adds one request for the given ip address.
Definition DOSGuard.cpp:126
void increment(std::string const &ip) noexcept override
Increment connection count for the given ip address.
Definition DOSGuard.cpp:91
bool isOk(std::string const &ip) const noexcept override
Check whether an ip address is currently rate limited or not.
Definition DOSGuard.cpp:63
void decrement(std::string const &ip) noexcept override
Decrement connection count for the given ip address.
Definition DOSGuard.cpp:100
DOSGuard(util::config::ClioConfigDefinition const &config, WhitelistHandlerInterface const &whitelistHandler, WeightsInterface const &weights)
Constructs a new DOS guard.
Definition DOSGuard.cpp:43
bool add(std::string const &ip, uint32_t numObjects) noexcept override
Adds numObjects of usage for the given ip address.
Definition DOSGuard.cpp:112
Interface for determining request weights in DOS protection.
Definition WeightsInterface.hpp:34
Interface for a whitelist handler.
Definition WhitelistHandlerInterface.hpp:29