20#include <xrpld/app/ledger/LedgerMaster.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/overlay/detail/Handshake.h>
23#include <xrpl/basics/base64.h>
24#include <xrpl/beast/core/LexicalCast.h>
25#include <xrpl/beast/rfc2616.h>
26#include <xrpl/protocol/digest.h>
27#include <boost/regex.hpp>
37 boost::beast::http::fields
const& headers,
40 auto const header = headers.find(
"X-Protocol-Ctl");
41 if (header == headers.end())
44 boost::regex rx(feature +
"=([^;\\s]+)");
46 if (boost::regex_search(allFeatures,
match, rx))
53 boost::beast::http::fields
const& headers,
65 boost::beast::http::fields
const& headers,
74 bool ledgerReplayEnabled,
75 bool txReduceRelayEnabled,
76 bool vpReduceRelayEnabled)
81 if (ledgerReplayEnabled)
83 if (txReduceRelayEnabled)
85 if (vpReduceRelayEnabled)
94 bool ledgerReplayEnabled,
95 bool txReduceRelayEnabled,
96 bool vpReduceRelayEnabled)
127 constexpr std::size_t sslMinimumFinishedLength = 12;
129 unsigned char buf[1024];
130 size_t len =
get(ssl, buf,
sizeof(buf));
132 if (len < sslMinimumFinishedLength)
138 SHA512(buf, len, cookie.
data());
145 auto const cookie1 =
hashLastMessage(ssl.native_handle(), SSL_get_finished);
148 JLOG(journal.
error()) <<
"Cookie generation: local setup not complete";
156 JLOG(journal.
error()) <<
"Cookie generation: peer setup not complete";
160 auto const result = (*cookie1 ^ *cookie2);
164 if (result == beast::zero)
166 JLOG(journal.
error())
167 <<
"Cookie generation: identical finished messages";
176 boost::beast::http::fields& h,
202 h.insert(
"Session-Signature",
base64_encode(sig.data(), sig.size()));
211 h.insert(
"Remote-IP", remote_ip.to_string());
213 if (!public_ip.is_unspecified())
214 h.insert(
"Local-IP", public_ip.to_string());
218 h.insert(
"Closed-Ledger",
strHex(cl->info().hash));
219 h.insert(
"Previous-Ledger",
strHex(cl->info().parentHash));
225 boost::beast::http::fields
const& headers,
232 if (
auto const iter = headers.find(
"Server-Domain"); iter != headers.end())
238 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
245 if (networkID && nid != *networkID)
249 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
252 TimeKeeper::duration::rep val;
265 auto const tolerance = 20s;
273 return duration_cast<std::chrono::seconds>(a - b);
274 return -duration_cast<std::chrono::seconds>(b - a);
277 auto const offset = calculateOffset(netTime, ourTime);
279 if (
abs(offset) > tolerance)
284 if (
auto const iter = headers.find(
"Public-Key"); iter != headers.end())
308 auto const iter = headers.find(
"Session-Signature");
310 if (iter == headers.end())
322 if (
auto const iter = headers.find(
"Local-IP"); iter != headers.end())
324 boost::system::error_code ec;
325 auto const local_ip =
326 boost::asio::ip::address::from_string(iter->value(), ec);
333 "Incorrect Local-IP: " + remote.to_string() +
" instead of " +
334 local_ip.to_string());
337 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
339 boost::system::error_code ec;
340 auto const remote_ip =
341 boost::asio::ip::address::from_string(iter->value(), ec);
351 if (remote_ip != public_ip)
353 "Incorrect Remote-IP: " + public_ip.to_string() +
354 " instead of " + remote_ip.to_string());
365 bool ledgerReplayEnabled,
366 bool txReduceRelayEnabled,
370 m.method(boost::beast::http::verb::get);
375 m.insert(
"Connection",
"Upgrade");
376 m.insert(
"Connect-As",
"Peer");
377 m.insert(
"Crawl", crawlPublic ?
"public" :
"private");
383 txReduceRelayEnabled,
384 vpReduceRelayEnabled));
400 resp.result(boost::beast::http::status::switching_protocols);
401 resp.version(req.version());
402 resp.insert(
"Connection",
"Upgrade");
404 resp.insert(
"Connect-As",
"Peer");
406 resp.insert(
"Crawl", crawlPublic ?
"public" :
"private");
416 buildHandshake(resp, sharedValue, networkID, public_ip, remote_ip, app);
A generic endpoint for log messages.
typename Clock::time_point time_point
typename Clock::duration duration
virtual Config & config()=0
virtual TimeKeeper & timeKeeper()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual std::uint64_t instanceID() const =0
Returns a 64-bit instance identifier, generated at startup.
virtual std::pair< PublicKey, SecretKey > const & nodeIdentity()=0
bool VP_REDUCE_RELAY_ENABLE
bool TX_REDUCE_RELAY_ENABLE
std::string SERVER_DOMAIN
std::shared_ptr< Ledger const > getClosedLedger()
An immutable linear range of bytes.
time_point now() const override
Returns the current time, using the server's clock.
Integers of any length that is a multiple of 32-bits.
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
boost::asio::ip::address Address
bool token_in_list(boost::string_ref const &value, boost::string_ref const &token)
Returns true if the specified token exists in the list.
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
std::string const & getFullVersionString()
Full server version string.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::ssl_stream< socket_type > stream_type
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::string makeFeaturesResponseHeader(http_request_type const &headers, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make response header X-Protocol-Ctl value with supported features.
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a secp256k1 signature on the digest of a message.
static constexpr char DELIM_FEATURE[]
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
void buildHandshake(boost::beast::http::fields &h, ripple::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote_ip, Application &app)
Insert fields headers necessary for upgrading the link to the peer protocol.
static constexpr char FEATURE_COMPR[]
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
std::string base64_decode(std::string_view data)
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::IP::Address public_ip, beast::IP::Address remote_ip, uint256 const &sharedValue, std::optional< std::uint32_t > networkID, ProtocolVersion protocol, Application &app)
Make http response.
static constexpr char FEATURE_LEDGER_REPLAY[]
bool isFeatureValue(boost::beast::http::fields const &headers, std::string const &feature, std::string const &value)
Check if a feature's value is equal to the specified value.
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
static std::optional< base_uint< 512 > > hashLastMessage(SSL const *ssl, size_t(*get)(const SSL *, void *, size_t))
Hashes the latest finished message from an SSL stream.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
std::string base64_encode(std::uint8_t const *data, std::size_t len)
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
static constexpr char FEATURE_TXRR[]
std::string to_string(base_uint< Bits, Tag > const &a)
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
boost::beast::http::request< boost::beast::http::empty_body > request_type
PublicKey verifyHandshake(boost::beast::http::fields const &headers, ripple::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
static constexpr char FEATURE_VPRR[]
constexpr Number abs(Number x) noexcept