lokinet/llarp/rpc/rpc.cpp

403 lines
9.5 KiB
C++
Raw Normal View History

#include <rpc/rpc.hpp>
#include <router/abstractrouter.hpp>
#include <service/context.hpp>
#include <util/logger.hpp>
#include <router_id.hpp>
#include <exit/context.hpp>
2018-10-19 11:41:36 +00:00
#include <util/encode.hpp>
2019-06-02 21:19:10 +00:00
#include <util/memfn.hpp>
#include <libabyss.hpp>
2018-10-19 11:41:36 +00:00
namespace llarp
{
namespace rpc
{
2018-11-01 12:47:14 +00:00
struct CallerHandler : public ::abyss::http::IRPCClientHandler
{
CallerImpl* m_Parent;
CallerHandler(::abyss::http::ConnImpl* impl, CallerImpl* parent)
: ::abyss::http::IRPCClientHandler(impl), m_Parent(parent)
2018-11-01 12:47:14 +00:00
{
}
~CallerHandler()
{
}
2018-11-21 17:46:33 +00:00
virtual bool
HandleJSONResult(const nlohmann::json& val) = 0;
2018-11-21 17:46:33 +00:00
bool
HandleResponse(::abyss::http::RPC_Response response)
{
if(!response.is_object())
2018-11-21 17:46:33 +00:00
{
return HandleJSONResult({});
}
const auto itr = response.find("result");
if(itr == response.end())
2018-11-21 17:46:33 +00:00
{
return HandleJSONResult({});
}
2019-07-06 17:03:40 +00:00
if(itr.value().is_object())
{
return HandleJSONResult(itr.value());
}
2019-07-06 17:03:40 +00:00
return false;
2018-11-21 17:46:33 +00:00
}
2018-11-01 12:47:14 +00:00
void
PopulateReqHeaders(abyss::http::Headers_t& hdr);
2018-11-01 12:47:14 +00:00
};
struct GetServiceNodeListHandler final : public CallerHandler
2018-11-01 12:47:14 +00:00
{
using PubkeyList_t = std::vector< RouterID >;
using Callback_t = std::function< void(const PubkeyList_t&, bool) >;
2018-11-01 12:47:14 +00:00
~GetServiceNodeListHandler()
2018-11-01 12:47:14 +00:00
{
}
Callback_t handler;
2018-11-01 12:47:14 +00:00
GetServiceNodeListHandler(::abyss::http::ConnImpl* impl,
CallerImpl* parent, Callback_t h)
: CallerHandler(impl, parent), handler(h)
2018-11-01 12:47:14 +00:00
{
}
bool
HandleJSONResult(const nlohmann::json& result) override
2018-11-01 12:47:14 +00:00
{
PubkeyList_t keys;
if(!result.is_object())
2018-11-21 17:46:33 +00:00
{
2019-02-28 16:02:36 +00:00
LogWarn("Invalid result: not an object");
2018-11-21 17:46:33 +00:00
handler({}, false);
return false;
}
const auto itr = result.find("keys");
if(itr == result.end())
2018-11-21 17:46:33 +00:00
{
2019-02-28 16:02:36 +00:00
LogWarn("Invalid result: no keys member");
2018-11-21 17:46:33 +00:00
handler({}, false);
return false;
}
if(!itr.value().is_array())
2018-11-21 17:46:33 +00:00
{
2019-02-28 16:02:36 +00:00
LogWarn("Invalid result: keys is not an array");
2018-11-21 17:46:33 +00:00
handler({}, false);
return false;
}
for(const auto item : itr.value())
2018-11-21 17:46:33 +00:00
{
if(item.is_string())
2018-11-21 17:46:33 +00:00
{
keys.emplace_back();
std::string str = item.get< std::string >();
2019-02-28 16:02:36 +00:00
if(!Base32Decode(str, keys.back()))
2018-11-21 17:46:33 +00:00
{
2019-02-28 16:02:36 +00:00
LogWarn("Invalid key: ", str);
2018-11-21 17:46:33 +00:00
keys.pop_back();
}
}
}
handler(keys, true);
2018-11-01 12:47:14 +00:00
return true;
}
void
2018-11-21 21:59:16 +00:00
HandleError() override
2018-11-01 12:47:14 +00:00
{
handler({}, false);
2018-11-01 12:47:14 +00:00
}
};
struct CallerImpl : public ::abyss::http::JSONRPC
{
AbstractRouter* router;
llarp_time_t m_NextKeyUpdate = 0;
const llarp_time_t KeyUpdateInterval = 5000;
using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t;
CallerImpl(AbstractRouter* r) : ::abyss::http::JSONRPC(), router(r)
2018-11-01 12:47:14 +00:00
{
}
void
2018-11-21 17:46:33 +00:00
Tick(llarp_time_t now)
2018-11-01 12:47:14 +00:00
{
if(now >= m_NextKeyUpdate)
{
AsyncUpdatePubkeyList();
m_NextKeyUpdate = now + KeyUpdateInterval;
}
2018-11-01 12:47:14 +00:00
Flush();
}
void
SetAuth(const std::string& user, const std::string& passwd)
{
username = user;
password = passwd;
}
void
AsyncUpdatePubkeyList()
{
LogInfo("Updating service node list");
QueueRPC("get_all_service_nodes_keys", nlohmann::json::object(),
2019-06-02 21:19:10 +00:00
util::memFn(&CallerImpl::NewAsyncUpdatePubkeyListConn, this));
}
2018-11-01 12:47:14 +00:00
bool
Start(const std::string& remote)
{
return RunAsync(router->netloop(), remote);
2018-11-01 12:47:14 +00:00
}
abyss::http::IRPCClientHandler*
NewAsyncUpdatePubkeyListConn(abyss::http::ConnImpl* impl)
2018-11-01 12:47:14 +00:00
{
return new GetServiceNodeListHandler(
impl, this,
2019-06-02 21:19:10 +00:00
util::memFn(&CallerImpl::HandleServiceNodeListUpdated, this));
2018-11-01 12:47:14 +00:00
}
void
HandleServiceNodeListUpdated(const PubkeyList_t& list, bool updated)
{
if(updated)
{
router->SetRouterWhitelist(list);
}
else
LogError("service node list not updated");
2018-11-01 12:47:14 +00:00
}
~CallerImpl()
{
}
};
void
CallerHandler::PopulateReqHeaders(abyss::http::Headers_t& hdr)
{
hdr.emplace("User-Agent", "lokinet rpc (YOLO)");
}
2018-11-01 12:47:14 +00:00
struct Handler : public ::abyss::httpd::IRPCHandler
2018-10-19 11:41:36 +00:00
{
AbstractRouter* router;
Handler(::abyss::httpd::ConnImpl* conn, AbstractRouter* r)
2018-11-01 12:47:14 +00:00
: ::abyss::httpd::IRPCHandler(conn), router(r)
2018-10-23 11:29:37 +00:00
{
}
2018-10-19 11:41:36 +00:00
2018-10-23 11:29:37 +00:00
~Handler()
2018-10-19 11:41:36 +00:00
{
}
Response
DumpState() const
2019-02-08 19:43:25 +00:00
{
const util::StatusObject dump = router->ExtractStatus();
return dump.get();
2019-02-08 19:43:25 +00:00
}
Response
ListExitLevels() const
2018-11-14 18:02:27 +00:00
{
exit::Context::TrafficStats stats;
router->exitContext().CalculateExitTraffic(stats);
Response resp;
for(const auto& stat : stats)
2018-11-14 18:02:27 +00:00
{
resp.emplace_back(Response{
{"ident", stat.first.ToHex()},
{"tx", stat.second.first},
{"rx", stat.second.second},
});
2018-11-14 18:02:27 +00:00
}
return resp;
2018-11-14 18:02:27 +00:00
}
Response
ListNeighboors() const
{
Response resp = Response::array();
router->ForEachPeer(
[&](const ILinkSession* session, bool outbound) {
resp.emplace_back(
Response{{"ident", RouterID(session->GetPubKey()).ToString()},
{"svcnode", session->GetRemoteRC().IsPublicRouter()},
{"outbound", outbound}});
},
false);
return resp;
}
Response
DumpStatus() const
{
size_t numServices = 0;
size_t numServicesReady = 0;
Response services = Response::array();
auto visitor =
[&](const std::string& name,
const std::shared_ptr< service::Endpoint >& ptr) -> bool {
numServices++;
if(ptr->IsReady())
numServicesReady++;
const Response status{{"ready", ptr->IsReady()},
{"stopped", ptr->IsStopped()},
{"stale", ptr->IntrosetIsStale()}};
services.emplace_back(Response{name, status});
return true;
};
router->hiddenServiceContext().ForEachService(visitor);
const Response resp{{"uptime", router->Uptime()},
{"servicesTotal", numServices},
{"servicesReady", numServicesReady},
{"services", services}};
return resp;
}
absl::optional< Response >
HandleJSONRPC(Method_t method,
__attribute__((unused)) const Params& params)
2018-10-19 11:41:36 +00:00
{
if(method == "llarp.admin.link.neighboors")
{
return ListNeighboors();
}
2019-07-06 17:03:40 +00:00
if(method == "llarp.admin.exit.list")
2018-11-14 18:02:27 +00:00
{
return ListExitLevels();
2018-11-14 18:02:27 +00:00
}
2019-07-06 17:03:40 +00:00
if(method == "llarp.admin.dumpstate")
2019-02-08 19:43:25 +00:00
{
return DumpState();
2019-02-08 19:43:25 +00:00
}
else if(method == "llarp.admin.status")
{
return DumpStatus();
}
2018-10-19 11:41:36 +00:00
return false;
}
};
2018-11-01 12:47:14 +00:00
struct ReqHandlerImpl : public ::abyss::httpd::BaseReqHandler
2018-10-23 11:29:37 +00:00
{
ReqHandlerImpl(AbstractRouter* r, llarp_time_t reqtimeout)
2018-11-01 12:47:14 +00:00
: ::abyss::httpd::BaseReqHandler(reqtimeout), router(r)
2018-10-23 11:29:37 +00:00
{
}
AbstractRouter* router;
2018-11-01 12:47:14 +00:00
::abyss::httpd::IRPCHandler*
CreateHandler(::abyss::httpd::ConnImpl* conn)
2018-10-23 11:29:37 +00:00
{
return new Handler(conn, router);
}
};
struct ServerImpl
{
AbstractRouter* router;
2018-10-23 11:29:37 +00:00
ReqHandlerImpl _handler;
ServerImpl(AbstractRouter* r) : router(r), _handler(r, 2000)
2018-10-23 11:29:37 +00:00
{
}
~ServerImpl()
{
}
void
Stop()
{
_handler.Close();
}
2018-10-23 11:29:37 +00:00
bool
Start(const std::string& addr)
{
2018-10-25 19:06:16 +00:00
uint16_t port = 0;
auto idx = addr.find_first_of(':');
Addr netaddr;
2018-10-25 19:06:16 +00:00
if(idx != std::string::npos)
{
port = std::stoi(addr.substr(1 + idx));
netaddr = Addr(addr.substr(0, idx));
2018-10-25 19:06:16 +00:00
}
sockaddr_in saddr;
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
return _handler.ServeAsync(router->netloop(), router->logic(),
2018-10-25 19:06:16 +00:00
(const sockaddr*)&saddr);
2018-10-23 11:29:37 +00:00
}
};
Caller::Caller(AbstractRouter* r)
: m_Impl(std::make_unique< CallerImpl >(r))
2018-11-01 12:47:14 +00:00
{
}
Caller::~Caller()
{
}
void
Caller::Stop()
{
m_Impl->Stop();
}
2018-11-01 12:47:14 +00:00
bool
Caller::Start(const std::string& addr)
{
return m_Impl->Start(addr);
}
void
2018-11-21 17:46:33 +00:00
Caller::Tick(llarp_time_t now)
2018-11-01 12:47:14 +00:00
{
2018-11-21 17:46:33 +00:00
m_Impl->Tick(now);
2018-11-01 12:47:14 +00:00
}
void
Caller::SetAuth(const std::string& user, const std::string& passwd)
{
m_Impl->SetAuth(user, passwd);
}
Server::Server(AbstractRouter* r)
: m_Impl(std::make_unique< ServerImpl >(r))
2018-10-19 11:41:36 +00:00
{
}
Server::~Server()
{
}
void
Server::Stop()
{
m_Impl->Stop();
}
2018-10-19 11:41:36 +00:00
bool
Server::Start(const std::string& addr)
{
return m_Impl->Start(addr);
}
} // namespace rpc
} // namespace llarp