add boilerplate for router whitelist via jsonrpc, disabled at the moment

pull/67/head
Jeff Becker 6 years ago
parent 4a9079a7a2
commit 2e3adb48e3
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -40,10 +40,10 @@ namespace llarp
bool
Start(const std::string& remote);
/// async test if a router is valid via jsonrpc
/// test if a router is valid
void
AsyncVerifyRouter(llarp::PubKey pkey,
std::function< void(llarp::PubKey, bool) > handler);
VerifyRouter(llarp::PubKey pkey,
std::function< void(llarp::PubKey, bool) > handler);
private:
CallerImpl* m_Impl;

@ -53,6 +53,9 @@ struct llarp_router
// our router contact
llarp::RouterContact _rc;
/// should we obey the service node whitelist?
bool whitelistRouters = false;
const llarp::RouterContact &
rc() const
{
@ -154,6 +157,11 @@ struct llarp_router
std::unordered_map< llarp::RouterID, llarp_time_t, llarp::RouterID::Hash >
m_PersistingSessions;
// lokinet routers from lokid, maps pubkey to when we think it will expire,
// set to max value right now
std::unordered_map< llarp::PubKey, llarp_time_t, llarp::PubKey::Hash >
lokinetRouters;
// TODO: change me if needed
const std::string defaultUpstreamResolver = "1.1.1.1:53";
std::list< std::string > upstreamResolvers;

@ -22,45 +22,52 @@ namespace llarp
}
void
PopulateReqHeaders(__attribute__((unused)) abyss::http::Headers_t& hdr)
PopulateReqHeaders(abyss::http::Headers_t& hdr)
{
(void)hdr;
// TODO: add http auth
}
};
struct VerifyRouterHandler : public CallerHandler
struct GetServiceNodeListHandler final : public CallerHandler
{
llarp::PubKey pk;
std::function< void(llarp::PubKey, bool) > handler;
using PubkeyList_t = std::vector< llarp::PubKey >;
using Callback_t = std::function< void(const PubkeyList_t&, bool) >;
~VerifyRouterHandler()
~GetServiceNodeListHandler()
{
}
Callback_t handler;
VerifyRouterHandler(::abyss::http::ConnImpl* impl, const llarp::PubKey& k,
std::function< void(llarp::PubKey, bool) > h)
: CallerHandler(impl), pk(k), handler(h)
GetServiceNodeListHandler(::abyss::http::ConnImpl* impl, Callback_t h)
: CallerHandler(impl), handler(h)
{
}
bool
HandleResponse(__attribute__((unused))
const ::abyss::http::RPC_Response& response)
HandleResponse(const ::abyss::http::RPC_Response& response)
{
handler(pk, true);
(void)response;
// TODO: get keys from response
PubkeyList_t keys;
handler(keys, true);
return true;
}
void
HandleError()
{
llarp::LogInfo("failed to verify router ", pk);
handler(pk, false);
handler({}, false);
}
};
struct CallerImpl : public ::abyss::http::JSONRPC
{
llarp_router* router;
llarp_time_t m_NextKeyUpdate;
const llarp_time_t KeyUpdateInterval = 1000 * 60 * 2;
using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t;
CallerImpl(llarp_router* r) : ::abyss::http::JSONRPC(), router(r)
{
}
@ -68,9 +75,26 @@ namespace llarp
void
Tick()
{
llarp_time_t now = router->Now();
if(now >= m_NextKeyUpdate)
{
AsyncUpdatePubkeyList();
m_NextKeyUpdate = now + KeyUpdateInterval;
}
Flush();
}
void
AsyncUpdatePubkeyList()
{
llarp::LogInfo("Updating service node list");
::abyss::json::Value params;
params.SetObject();
QueueRPC("get_all_service_node_keys", std::move(params),
std::bind(&CallerImpl::NewAsyncUpdatePubkeyListConn, this,
std::placeholders::_1));
}
bool
Start(const std::string& remote)
{
@ -78,17 +102,43 @@ namespace llarp
}
abyss::http::IRPCClientHandler*
NewConn(PubKey k, std::function< void(llarp::PubKey, bool) > handler,
abyss::http::ConnImpl* impl)
NewAsyncUpdatePubkeyListConn(abyss::http::ConnImpl* impl)
{
return new VerifyRouterHandler(impl, k, handler);
return new GetServiceNodeListHandler(
impl,
std::bind(&CallerImpl::HandleServiceNodeListUpdated, this,
std::placeholders::_1, std::placeholders::_2));
}
void
AsyncVerifyRouter(llarp::PubKey pk,
std::function< void(llarp::PubKey, bool) > handler)
VerifyRouter(llarp::PubKey pk,
std::function< void(llarp::PubKey, bool) > handler)
{
handler(pk, true);
if(router->whitelistRouters)
{
auto itr = router->lokinetRouters.find(pk);
handler(pk, itr != router->lokinetRouters.end());
}
else
{
handler(pk, true);
}
}
void
HandleServiceNodeListUpdated(const PubkeyList_t& list, bool updated)
{
if(updated)
{
router->lokinetRouters.clear();
for(const auto& pk : list)
router->lokinetRouters.emplace(
std::make_pair(pk, std::numeric_limits< llarp_time_t >::max()));
llarp::LogInfo("updated service node list, we have ",
router->lokinetRouters.size(), " authorized routers");
}
else
llarp::LogError("service node list not updated");
}
~CallerImpl()
@ -256,8 +306,8 @@ namespace llarp
}
void
AsyncVerifyRouter(llarp::PubKey pk,
std::function< void(llarp::PubKey, bool) > result)
VerifyRouter(llarp::PubKey pk,
std::function< void(llarp::PubKey, bool) > result)
{
// always allow routers when not using libabyss
result(pk, true);
@ -282,10 +332,10 @@ namespace llarp
}
void
Caller::AsyncVerifyRouter(
llarp::PubKey pk, std::function< void(llarp::PubKey, bool) > handler)
Caller::VerifyRouter(llarp::PubKey pk,
std::function< void(llarp::PubKey, bool) > handler)
{
m_Impl->AsyncVerifyRouter(pk, handler);
m_Impl->VerifyRouter(pk, handler);
}
Server::Server(llarp_router* r) : m_Impl(new ServerImpl(r))

Loading…
Cancel
Save