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/log/Logger.hpp"
24#include "util/newconfig/ConfigDefinition.hpp"
25#include "web/dosguard/DOSGuardInterface.hpp"
26#include "web/dosguard/WhitelistHandlerInterface.hpp"
27
28#include <boost/asio.hpp>
29#include <boost/iterator/transform_iterator.hpp>
30#include <boost/system/error_code.hpp>
31
32#include <cstdint>
33#include <functional>
34#include <string>
35#include <string_view>
36#include <unordered_map>
37#include <unordered_set>
38
39namespace web::dosguard {
40
50 struct ClientState {
51 std::uint32_t transferedByte = 0;
52 std::uint32_t requestsCount = 0;
53 };
54
55 struct State {
56 std::unordered_map<std::string, ClientState> ipState;
57 std::unordered_map<std::string, std::uint32_t> ipConnCount;
58 };
60
61 std::reference_wrapper<WhitelistHandlerInterface const> whitelistHandler_;
62
63 std::uint32_t const maxFetches_;
64 std::uint32_t const maxConnCount_;
65 std::uint32_t const maxRequestCount_;
66 util::Logger log_{"RPC"};
67
68public:
75 DOSGuard(util::config::ClioConfigDefinition const& config, WhitelistHandlerInterface const& whitelistHandler);
76
84 [[nodiscard]] bool
85 isWhiteListed(std::string_view const ip) const noexcept override;
86
94 [[nodiscard]] bool
95 isOk(std::string const& ip) const noexcept override;
96
102 void
103 increment(std::string const& ip) noexcept override;
104
110 void
111 decrement(std::string const& ip) noexcept override;
112
125 [[maybe_unused]] bool
126 add(std::string const& ip, uint32_t numObjects) noexcept override;
127
139 [[maybe_unused]] bool
140 request(std::string const& ip) noexcept override;
141
145 void
146 clear() noexcept override;
147
148private:
149 [[nodiscard]] static std::unordered_set<std::string>
150 getWhitelist(util::config::ClioConfigDefinition const& config);
151};
152
153} // 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: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:44
A simple denial of service guard used for rate limiting.
Definition DOSGuard.hpp:46
bool isWhiteListed(std::string_view const ip) const noexcept override
Check whether an ip address is in the whitelist or not.
Definition DOSGuard.cpp:49
bool request(std::string const &ip) noexcept override
Adds one request for the given ip address.
Definition DOSGuard.cpp:118
void clear() noexcept override
Instantly clears all fetch counters added by.
Definition DOSGuard.cpp:132
void increment(std::string const &ip) noexcept override
Increment connection count for the given ip address.
Definition DOSGuard.cpp:83
bool isOk(std::string const &ip) const noexcept override
Check whether an ip address is currently rate limited or not.
Definition DOSGuard.cpp:55
void decrement(std::string const &ip) noexcept override
Decrement connection count for the given ip address.
Definition DOSGuard.cpp:92
DOSGuard(util::config::ClioConfigDefinition const &config, WhitelistHandlerInterface const &whitelistHandler)
Constructs a new DOS guard.
Definition DOSGuard.cpp:40
bool add(std::string const &ip, uint32_t numObjects) noexcept override
Adds numObjects of usage for the given ip address.
Definition DOSGuard.cpp:104
Interface for a whitelist handler.
Definition WhitelistHandlerInterface.hpp:29