rippled
Loading...
Searching...
No Matches
WSInfoSub.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or 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#ifndef RIPPLE_RPC_WSINFOSUB_H
21#define RIPPLE_RPC_WSINFOSUB_H
22
23#include <xrpld/rpc/InfoSub.h>
24#include <xrpld/rpc/Role.h>
25
26#include <xrpl/beast/net/IPAddressConversion.h>
27#include <xrpl/json/json_writer.h>
28#include <xrpl/server/WSSession.h>
29
30#include <memory>
31#include <string>
32
33namespace ripple {
34
35class WSInfoSub : public InfoSub
36{
40
41public:
43 : InfoSub(source), ws_(ws)
44 {
45 auto const& h = ws->request();
46 if (ipAllowed(
47 beast::IPAddressConversion::from_asio(ws->remote_endpoint())
48 .address(),
49 ws->port().secure_gateway_nets_v4,
50 ws->port().secure_gateway_nets_v6))
51 {
52 auto it = h.find("X-User");
53 if (it != h.end())
54 user_ = it->value();
56 }
57 }
58
60 user() const
61 {
62 return user_;
63 }
64
67 {
68 return fwdfor_;
69 }
70
71 void
72 send(Json::Value const& jv, bool) override
73 {
74 auto sp = ws_.lock();
75 if (!sp)
76 return;
77 boost::beast::multi_buffer sb;
78 Json::stream(jv, [&](void const* data, std::size_t n) {
79 sb.commit(boost::asio::buffer_copy(
80 sb.prepare(n), boost::asio::buffer(data, n)));
81 });
82 auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
83 sp->send(m);
84 }
85};
86
87} // namespace ripple
88
89#endif
Represents a JSON value.
Definition json_value.h:149
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:75
Abstracts the source of subscription data.
Definition InfoSub.h:68
Manages a client's subscription to data feeds.
Definition InfoSub.h:52
std::string_view forwarded_for() const
Definition WSInfoSub.h:66
std::string user_
Definition WSInfoSub.h:38
std::weak_ptr< WSSession > ws_
Definition WSInfoSub.h:37
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition WSInfoSub.h:42
std::string_view user() const
Definition WSInfoSub.h:60
void send(Json::Value const &jv, bool) override
Definition WSInfoSub.h:72
std::string fwdfor_
Definition WSInfoSub.h:39
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:25
std::string_view forwardedFor(http_request_type const &request)
Definition Role.cpp:262
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:46
static IP::Endpoint from_asio(boost::asio::ip::address const &address)