Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
DOSGuard.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
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"
28
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>
33
34#include <cstdint>
35#include <functional>
36#include <string>
37#include <string_view>
38#include <unordered_map>
39#include <unordered_set>
40
41namespace web::dosguard {
42
52 struct ClientState {
53 std::uint32_t transferredByte = 0;
54 std::uint32_t requestsCount = 0;
55 };
56
57 struct State {
58 std::unordered_map<std::string, ClientState> ipState;
59 std::unordered_map<std::string, std::uint32_t> ipConnCount;
60 };
62
63 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
64 std::reference_wrapper<WeightsInterface const> weights_;
65
66 std::uint32_t const maxFetches_;
67 std::uint32_t const maxConnCount_;
68 std::uint32_t const maxRequestCount_;
69 util::Logger log_{"RPC"};
70
71public:
81 WhitelistHandlerInterface const& whitelistHandler,
82 WeightsInterface const& weights
83 );
84
92 [[nodiscard]] bool
93 isWhiteListed(std::string_view const ip) const noexcept override;
94
102 [[nodiscard]] bool
103 isOk(std::string const& ip) const noexcept override;
104
110 void
111 increment(std::string const& ip) noexcept override;
112
118 void
119 decrement(std::string const& ip) noexcept override;
120
133 [[maybe_unused]] bool
134 add(std::string const& ip, uint32_t numObjects) noexcept override;
135
148 [[maybe_unused]] bool
149 request(std::string const& ip, boost::json::object const& request) override;
150
154 void
155 clear() noexcept override;
156
157private:
158 [[nodiscard]] static std::unordered_set<std::string>
159 getWhitelist(util::config::ClioConfigDefinition const& config);
160};
161
162} // namespace web::dosguard
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