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
25namespace web::dosguard {
26
34 struct ClientState {
35 std::uint32_t transferredByte = 0;
36 std::uint32_t requestsCount = 0;
37 };
38
39 struct State {
40 std::unordered_map<std::string, ClientState> ipState;
41 std::unordered_map<std::string, std::uint32_t> ipConnCount;
42 };
44
45 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
46 std::reference_wrapper<WeightsInterface const> weights_;
47
48 std::uint32_t const maxFetches_;
49 std::uint32_t const maxConnCount_;
50 std::uint32_t const maxRequestCount_;
51 util::Logger log_{"RPC"};
52
53public:
63 WhitelistHandlerInterface const& whitelistHandler,
64 WeightsInterface const& weights
65 );
66
74 [[nodiscard]] bool
75 isWhiteListed(std::string_view const ip) const noexcept override;
76
84 [[nodiscard]] bool
85 isOk(std::string const& ip) const noexcept override;
86
92 void
93 increment(std::string const& ip) noexcept override;
94
100 void
101 decrement(std::string const& ip) noexcept override;
102
115 [[maybe_unused]] bool
116 add(std::string const& ip, uint32_t numObjects) noexcept override;
117
130 [[maybe_unused]] bool
131 request(std::string const& ip, boost::json::object const& request) override;
132
136 void
137 clear() noexcept override;
138
139private:
140 [[nodiscard]] static std::unordered_set<std::string>
141 getWhitelist(util::config::ClioConfigDefinition const& config);
142};
143
144} // namespace web::dosguard
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
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 add(std::string const&, uint32_t).
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
Denial of service protection: rate limiting, IP whitelisting and request weighting.
Definition DOSGuard.cpp:22