2018-10-19 11:41:36 +00:00
|
|
|
#include <llarp/rpc.hpp>
|
|
|
|
|
2018-10-23 11:29:37 +00:00
|
|
|
#include "router.hpp"
|
2018-10-27 12:41:04 +00:00
|
|
|
#ifdef USE_ABYSS
|
|
|
|
#include <libabyss.hpp>
|
|
|
|
#endif
|
2018-10-19 11:41:36 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace rpc
|
|
|
|
{
|
2018-10-27 12:41:04 +00:00
|
|
|
#ifdef USE_ABYSS
|
2018-11-01 12:47:14 +00:00
|
|
|
|
|
|
|
struct CallerHandler : public ::abyss::http::IRPCClientHandler
|
|
|
|
{
|
|
|
|
CallerHandler(::abyss::http::ConnImpl* impl)
|
|
|
|
: ::abyss::http::IRPCClientHandler(impl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CallerHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-07 15:30:22 +00:00
|
|
|
PopulateReqHeaders(__attribute__((unused)) abyss::http::Headers_t& hdr)
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VerifyRouterHandler : public CallerHandler
|
|
|
|
{
|
|
|
|
llarp::PubKey pk;
|
|
|
|
std::function< void(llarp::PubKey, bool) > handler;
|
|
|
|
|
|
|
|
~VerifyRouterHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VerifyRouterHandler(::abyss::http::ConnImpl* impl, const llarp::PubKey& k,
|
|
|
|
std::function< void(llarp::PubKey, bool) > h)
|
|
|
|
: CallerHandler(impl), pk(k), handler(h)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleResponse(__attribute__((unused))
|
|
|
|
const ::abyss::http::RPC_Response& response)
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
handler(pk, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleError()
|
|
|
|
{
|
|
|
|
llarp::LogInfo("failed to verify router ", pk);
|
|
|
|
handler(pk, false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CallerImpl : public ::abyss::http::JSONRPC
|
|
|
|
{
|
|
|
|
llarp_router* router;
|
|
|
|
CallerImpl(llarp_router* r) : ::abyss::http::JSONRPC(), router(r)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start(const std::string& remote)
|
|
|
|
{
|
|
|
|
return RunAsync(router->netloop, remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
abyss::http::IRPCClientHandler*
|
|
|
|
NewConn(PubKey k, std::function< void(llarp::PubKey, bool) > handler,
|
|
|
|
abyss::http::ConnImpl* impl)
|
|
|
|
{
|
|
|
|
return new VerifyRouterHandler(impl, k, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncVerifyRouter(llarp::PubKey pk,
|
|
|
|
std::function< void(llarp::PubKey, bool) > handler)
|
|
|
|
{
|
|
|
|
abyss::json::Value params;
|
|
|
|
params.SetObject();
|
|
|
|
QueueRPC("get_service_node", std::move(params),
|
|
|
|
std::bind(&CallerImpl::NewConn, this, pk, handler,
|
|
|
|
std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
~CallerImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Handler : public ::abyss::httpd::IRPCHandler
|
2018-10-19 11:41:36 +00:00
|
|
|
{
|
|
|
|
llarp_router* router;
|
2018-11-01 12:47:14 +00:00
|
|
|
Handler(::abyss::httpd::ConnImpl* conn, llarp_router* r)
|
|
|
|
: ::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-10-25 18:18:12 +00:00
|
|
|
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 llarp::ILinkSession* session, bool outbound) {
|
|
|
|
abyss::json::Value peer;
|
|
|
|
peer.SetObject();
|
|
|
|
abyss::json::Value ident_val, addr_val;
|
|
|
|
|
|
|
|
auto ident = session->GetPubKey().ToHex();
|
|
|
|
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);
|
|
|
|
});
|
2018-10-25 18:18:12 +00:00
|
|
|
resp.AddMember("result", peers, alloc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:41:36 +00:00
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleJSONRPC(Method_t method,
|
|
|
|
__attribute__((unused)) const Params& params,
|
|
|
|
Response& response)
|
2018-10-19 11:41:36 +00:00
|
|
|
{
|
2018-10-25 18:18:12 +00:00
|
|
|
if(method == "llarp.admin.link.neighboors")
|
|
|
|
{
|
|
|
|
return ListNeighboors(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(llarp_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
|
|
|
{
|
|
|
|
}
|
|
|
|
llarp_router* router;
|
2018-11-01 12:47:14 +00:00
|
|
|
::abyss::httpd::IRPCHandler*
|
2018-11-02 18:02:45 +00:00
|
|
|
CreateHandler(::abyss::httpd::ConnImpl* conn)
|
2018-10-23 11:29:37 +00:00
|
|
|
{
|
|
|
|
return new Handler(conn, router);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ServerImpl
|
|
|
|
{
|
|
|
|
llarp_router* router;
|
|
|
|
ReqHandlerImpl _handler;
|
|
|
|
|
|
|
|
ServerImpl(llarp_router* r) : router(r), _handler(r, 2000)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ServerImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start(const std::string& addr)
|
|
|
|
{
|
2018-10-25 19:06:16 +00:00
|
|
|
uint16_t port = 0;
|
|
|
|
auto idx = addr.find_first_of(':');
|
|
|
|
llarp::Addr netaddr;
|
|
|
|
if(idx != std::string::npos)
|
|
|
|
{
|
|
|
|
port = std::stoi(addr.substr(1 + idx));
|
|
|
|
netaddr = llarp::Addr(addr.substr(0, idx));
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
(const sockaddr*)&saddr);
|
2018-10-23 11:29:37 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-27 12:41:04 +00:00
|
|
|
#else
|
|
|
|
struct ServerImpl
|
|
|
|
{
|
2018-11-07 15:30:22 +00:00
|
|
|
ServerImpl(__attribute__((unused)) llarp_router* r){};
|
|
|
|
|
2018-10-27 13:51:02 +00:00
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
Start(__attribute__((unused)) const std::string& addr)
|
2018-10-27 12:41:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2018-11-01 12:47:14 +00:00
|
|
|
|
|
|
|
struct CallerImpl
|
|
|
|
{
|
2018-11-07 15:30:22 +00:00
|
|
|
CallerImpl(__attribute__((unused)) llarp_router* r)
|
2018-11-01 12:47:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CallerImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Start(const std::string&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Tick()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncVerifyRouter(llarp::PubKey pk,
|
|
|
|
std::function< void(llarp::PubKey, bool) > result)
|
|
|
|
{
|
|
|
|
// always allow routers when not using libabyss
|
|
|
|
result(pk, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-27 12:41:04 +00:00
|
|
|
#endif
|
2018-10-23 11:29:37 +00:00
|
|
|
|
2018-11-01 12:47:14 +00:00
|
|
|
Caller::Caller(llarp_router* r) : m_Impl(new CallerImpl(r))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Caller::~Caller()
|
|
|
|
{
|
|
|
|
delete m_Impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Caller::Start(const std::string& addr)
|
|
|
|
{
|
|
|
|
return m_Impl->Start(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Caller::AsyncVerifyRouter(
|
|
|
|
llarp::PubKey pk, std::function< void(llarp::PubKey, bool) > handler)
|
|
|
|
{
|
|
|
|
m_Impl->AsyncVerifyRouter(pk, handler);
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:41:36 +00:00
|
|
|
Server::Server(llarp_router* r) : m_Impl(new ServerImpl(r))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
delete m_Impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Server::Start(const std::string& addr)
|
|
|
|
{
|
|
|
|
return m_Impl->Start(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rpc
|
|
|
|
} // namespace llarp
|