lokinet/llarp/rpc/rpc.cpp

447 lines
10 KiB
C++
Raw Normal View History

#include <rpc/rpc.hpp>
#include <router/router.hpp>
2018-10-19 11:41:36 +00:00
#ifdef USE_ABYSS
#include <util/encode.hpp>
#include <libabyss.hpp>
#endif
2018-10-19 11:41:36 +00:00
namespace llarp
{
namespace rpc
{
#ifdef USE_ABYSS
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 ::abyss::json::Value& val) = 0;
bool
HandleResponse(::abyss::http::RPC_Response response)
{
if(!response.IsObject())
{
return HandleJSONResult({});
}
const auto itr = response.FindMember("result");
if(itr == response.MemberEnd())
{
return HandleJSONResult({});
}
if(itr->value.IsObject())
return HandleJSONResult(itr->value);
return false;
}
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
2018-11-21 17:46:33 +00:00
HandleJSONResult(const ::abyss::json::Value& result) override
2018-11-01 12:47:14 +00:00
{
PubkeyList_t keys;
2018-11-21 17:46:33 +00:00
if(!result.IsObject())
{
handler({}, false);
return false;
}
const auto itr = result.FindMember("keys");
if(itr == result.MemberEnd())
{
handler({}, false);
return false;
}
if(!itr->value.IsArray())
{
handler({}, false);
return false;
}
auto key_itr = itr->value.Begin();
while(key_itr != itr->value.End())
{
if(key_itr->IsString())
{
keys.emplace_back();
std::string str = key_itr->GetString();
if(str.size() != Base32DecodeSize(keys.back().size()))
{
keys.pop_back();
}
else if(!Base32Decode(str, keys.back()))
2018-11-21 17:46:33 +00:00
{
keys.pop_back();
}
}
++key_itr;
}
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
{
Router* router;
llarp_time_t m_NextKeyUpdate = 0;
const llarp_time_t KeyUpdateInterval = 1000 * 60 * 2;
using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t;
std::string username;
std::string password;
CallerImpl(Router* 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
SetBasicAuth(const std::string& user, const std::string& passwd)
{
username = user;
password = passwd;
}
void
AsyncUpdatePubkeyList()
{
LogInfo("Updating service node list");
::abyss::json::Value params;
params.SetObject();
QueueRPC("get_all_service_nodes_keys", std::move(params),
std::bind(&CallerImpl::NewAsyncUpdatePubkeyListConn, this,
std::placeholders::_1));
}
2018-11-01 12:47:14 +00:00
bool
Start(const std::string& remote)
{
return RunAsync(router->netloop, remote);
}
abyss::http::IRPCClientHandler*
NewAsyncUpdatePubkeyListConn(abyss::http::ConnImpl* impl)
2018-11-01 12:47:14 +00:00
{
return new GetServiceNodeListHandler(
impl, this,
std::bind(&CallerImpl::HandleServiceNodeListUpdated, this,
std::placeholders::_1, std::placeholders::_2));
2018-11-01 12:47:14 +00:00
}
void
HandleServiceNodeListUpdated(const PubkeyList_t& list, bool updated)
{
if(updated)
{
router->lokinetRouters.clear();
for(const auto& pk : list)
2018-12-20 17:04:23 +00:00
router->lokinetRouters.insert(std::make_pair(
pk.data(), std::numeric_limits< llarp_time_t >::max()));
LogInfo("updated service node list, we have ",
router->lokinetRouters.size(), " authorized routers");
}
else
LogError("service node list not updated");
2018-11-01 12:47:14 +00:00
}
~CallerImpl()
{
}
};
void
CallerHandler::PopulateReqHeaders(abyss::http::Headers_t& hdr)
{
if(m_Parent->username.empty() || m_Parent->password.empty())
return;
std::stringstream ss;
ss << "Basic ";
std::string cred = m_Parent->username + ":" + m_Parent->password;
llarp::Base64Encode(ss, (const byte_t*)cred.c_str(), cred.size());
hdr.emplace("Authorization", ss.str());
}
2018-11-01 12:47:14 +00:00
struct Handler : public ::abyss::httpd::IRPCHandler
2018-10-19 11:41:36 +00:00
{
Router* router;
Handler(::abyss::httpd::ConnImpl* conn, Router* 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
{
}
2018-11-14 18:02:27 +00:00
bool
ListExitLevels(Response& resp) const
{
exit::Context::TrafficStats stats;
2018-11-14 18:02:27 +00:00
router->exitContext.CalculateExitTraffic(stats);
auto& alloc = resp.GetAllocator();
abyss::json::Value exits;
exits.SetArray();
auto itr = stats.begin();
while(itr != stats.end())
{
abyss::json::Value info, ident;
info.SetObject();
ident.SetString(itr->first.ToHex().c_str(), alloc);
info.AddMember("ident", ident, alloc);
info.AddMember("tx", abyss::json::Value(itr->second.first), alloc);
info.AddMember("rx", abyss::json::Value(itr->second.second), alloc);
exits.PushBack(info, alloc);
++itr;
}
resp.AddMember("result", exits, alloc);
return true;
}
bool
ListNeighboors(Response& resp) const
{
auto& alloc = resp.GetAllocator();
2018-10-25 19:06:16 +00:00
abyss::json::Value peers;
peers.SetArray();
router->ForEachPeer([&](const ILinkSession* session, bool outbound) {
abyss::json::Value peer;
peer.SetObject();
abyss::json::Value ident_val, addr_val;
auto ident = RouterID(session->GetPubKey()).ToString();
ident_val.SetString(ident.c_str(), alloc);
auto addr = session->GetRemoteEndpoint().ToString();
addr_val.SetString(addr.c_str(), alloc);
peer.AddMember("addr", addr_val, alloc);
peer.AddMember("ident", ident_val, alloc);
peer.AddMember("outbound", abyss::json::Value(outbound), alloc);
peers.PushBack(peer, alloc);
});
resp.AddMember("result", peers, alloc);
return true;
}
2018-10-19 11:41:36 +00:00
bool
HandleJSONRPC(Method_t method,
__attribute__((unused)) const Params& params,
Response& response)
2018-10-19 11:41:36 +00:00
{
if(method == "llarp.admin.link.neighboors")
{
return ListNeighboors(response);
}
2018-11-14 18:02:27 +00:00
else if(method == "llarp.admin.exit.list")
{
return ListExitLevels(response);
}
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(Router* 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
{
}
Router* 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
{
Router* router;
2018-10-23 11:29:37 +00:00
ReqHandlerImpl _handler;
ServerImpl(Router* 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
}
};
#else
struct ServerImpl
{
ServerImpl(__attribute__((unused)) Router* r){};
bool
Start(__attribute__((unused)) const std::string& addr)
{
return true;
}
void
Stop()
{
}
};
2018-11-01 12:47:14 +00:00
struct CallerImpl
{
CallerImpl(__attribute__((unused)) Router* r)
2018-11-01 12:47:14 +00:00
{
}
~CallerImpl()
{
}
bool
Start(const std::string&)
{
return true;
}
void
Stop()
{
}
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
{
2018-11-21 17:46:33 +00:00
(void)now;
2018-11-01 12:47:14 +00:00
}
void
SetBasicAuth(const std::string&, const std::string&)
{
}
2018-11-01 12:47:14 +00:00
};
#endif
2018-10-23 11:29:37 +00:00
Caller::Caller(Router* r) : m_Impl(new CallerImpl(r))
2018-11-01 12:47:14 +00:00
{
}
Caller::~Caller()
{
delete m_Impl;
}
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::SetBasicAuth(const std::string& user, const std::string& passwd)
{
m_Impl->SetBasicAuth(user, passwd);
}
Server::Server(Router* r) : m_Impl(new ServerImpl(r))
2018-10-19 11:41:36 +00:00
{
}
Server::~Server()
{
delete m_Impl;
}
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