Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
DOSGuard.hpp
1#pragma once
2
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"
9
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>
14
15#include <cstdint>
16#include <functional>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <unordered_set>
21
22namespace web::dosguard {
23
33 struct ClientState {
34 std::uint32_t transferredByte = 0;
35 std::uint32_t requestsCount = 0;
36 };
37
38 struct State {
39 std::unordered_map<std::string, ClientState> ipState;
40 std::unordered_map<std::string, std::uint32_t> ipConnCount;
41 };
43
44 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
45 std::reference_wrapper<WeightsInterface const> weights_;
46
47 std::uint32_t const maxFetches_;
48 std::uint32_t const maxConnCount_;
49 std::uint32_t const maxRequestCount_;
50 util::Logger log_{"RPC"};
51
52public:
62 WhitelistHandlerInterface const& whitelistHandler,
63 WeightsInterface const& weights
64 );
65
73 [[nodiscard]] bool
74 isWhiteListed(std::string_view const ip) const noexcept override;
75
83 [[nodiscard]] bool
84 isOk(std::string const& ip) const noexcept override;
85
91 void
92 increment(std::string const& ip) noexcept override;
93
99 void
100 decrement(std::string const& ip) noexcept override;
101
114 [[maybe_unused]] bool
115 add(std::string const& ip, uint32_t numObjects) noexcept override;
116
129 [[maybe_unused]] bool
130 request(std::string const& ip, boost::json::object const& request) override;
131
135 void
136 clear() noexcept override;
137
138private:
139 [[nodiscard]] static std::unordered_set<std::string>
140 getWhitelist(util::config::ClioConfigDefinition const& config);
141};
142
143} // namespace web::dosguard
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