rippled
Loading...
Searching...
No Matches
WSInfoSub.h
1#pragma once
2
3#include <xrpld/rpc/Role.h>
4
5#include <xrpl/beast/net/IPAddressConversion.h>
6#include <xrpl/json/json_writer.h>
7#include <xrpl/server/InfoSub.h>
8#include <xrpl/server/WSSession.h>
9
10#include <memory>
11#include <string>
12
13namespace xrpl {
14
15class WSInfoSub : public InfoSub
16{
20
21public:
22 WSInfoSub(Source& source, std::shared_ptr<WSSession> const& ws) : InfoSub(source), ws_(ws)
23 {
24 auto const& h = ws->request();
25 if (ipAllowed(
26 beast::IPAddressConversion::from_asio(ws->remote_endpoint()).address(),
27 ws->port().secure_gateway_nets_v4,
28 ws->port().secure_gateway_nets_v6))
29 {
30 auto it = h.find("X-User");
31 if (it != h.end())
32 user_ = it->value();
34 }
35 }
36
38 user() const
39 {
40 return user_;
41 }
42
45 {
46 return fwdfor_;
47 }
48
49 void
50 send(Json::Value const& jv, bool) override
51 {
52 auto sp = ws_.lock();
53 if (!sp)
54 return;
55 boost::beast::multi_buffer sb;
56 Json::stream(jv, [&](void const* data, std::size_t n) {
57 sb.commit(boost::asio::buffer_copy(sb.prepare(n), boost::asio::buffer(data, n)));
58 });
59 auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
60 sp->send(m);
61 }
62};
63
64} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:55
Abstracts the source of subscription data.
Definition InfoSub.h:47
Manages a client's subscription to data feeds.
Definition InfoSub.h:31
void send(Json::Value const &jv, bool) override
Definition WSInfoSub.h:50
std::string_view forwarded_for() const
Definition WSInfoSub.h:44
std::string fwdfor_
Definition WSInfoSub.h:19
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition WSInfoSub.h:22
std::weak_ptr< WSSession > ws_
Definition WSInfoSub.h:17
std::string_view user() const
Definition WSInfoSub.h:38
std::string user_
Definition WSInfoSub.h:18
T make_shared(T... args)
void stream(Json::Value const &jv, Write const &write)
Stream compact JSON to the specified function.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string_view forwardedFor(http_request_type const &request)
Definition Role.cpp:228
bool ipAllowed(beast::IP::Address const &remoteIp, std::vector< boost::asio::ip::network_v4 > const &nets4, std::vector< boost::asio::ip::network_v6 > const &nets6)
True if remoteIp is in any of adminIp.
Definition Role.cpp:24
static IP::Endpoint from_asio(boost::asio::ip::address const &address)