lokinet/llarp/router/router.cpp

2115 lines
54 KiB
C++
Raw Normal View History

#include <router/router.hpp>
#include <config.hpp>
#include <constants/proto.hpp>
2019-01-13 14:00:50 +00:00
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <dht/context.hpp>
2019-01-19 18:16:40 +00:00
#include <dht/node.hpp>
2019-03-29 15:17:49 +00:00
#include <iwp/iwp.hpp>
2018-12-12 01:32:10 +00:00
#include <link/server.hpp>
#include <messages/link_message.hpp>
#include <net/net.hpp>
#include <rpc/rpc.hpp>
#include <util/buffer.hpp>
#include <util/encode.hpp>
#include <util/logger.hpp>
2019-06-02 21:19:10 +00:00
#include <util/memfn.hpp>
#include <util/file_logger.hpp>
2019-06-13 13:26:34 +00:00
#include <util/json_logger.hpp>
2019-04-11 13:13:29 +00:00
#include <util/logger_syslog.hpp>
2019-03-25 02:08:52 +00:00
#include <util/metrics.hpp>
#include <util/str.hpp>
2019-03-29 14:23:19 +00:00
#include <utp/utp.hpp>
2017-11-28 14:05:31 +00:00
2018-05-20 17:45:47 +00:00
#include <fstream>
2018-10-09 01:38:25 +00:00
#include <cstdlib>
#include <iterator>
2018-11-08 12:31:50 +00:00
#if defined(RPI) || defined(ANDROID)
#include <unistd.h>
#endif
2018-05-20 17:45:47 +00:00
namespace llarp
{
2018-05-30 20:56:47 +00:00
struct async_verify_context
{
Router *router;
2018-09-06 11:46:19 +00:00
TryConnectJob *establish_job;
2018-05-30 20:56:47 +00:00
};
2018-02-01 13:21:00 +00:00
} // namespace llarp
2017-11-28 14:05:31 +00:00
2018-09-06 11:46:19 +00:00
struct TryConnectJob
{
llarp_time_t lastAttempt = 0;
2019-05-14 17:35:01 +00:00
const llarp::RouterContact rc;
2019-05-15 15:54:26 +00:00
llarp::LinkLayer_ptr link;
llarp::Router *router;
uint16_t triesLeft;
2019-05-15 15:54:26 +00:00
TryConnectJob(const llarp::RouterContact &remote, llarp::LinkLayer_ptr l,
uint16_t tries, llarp::Router *r)
2018-09-06 11:46:19 +00:00
: rc(remote), link(l), router(r), triesLeft(tries)
{
}
2019-01-17 13:56:03 +00:00
~TryConnectJob()
{
}
bool
TimeoutReached() const
{
const auto now = router->Now();
return now > lastAttempt && now - lastAttempt > 5000;
}
void
Success()
{
router->routerProfiling().MarkConnectSuccess(rc.pubkey);
2019-05-15 16:15:20 +00:00
router->FlushOutboundFor(rc.pubkey, link.get());
}
2019-05-14 14:27:12 +00:00
/// return true to remove
bool
Timeout()
{
if(ShouldRetry())
{
return Attempt();
}
// discard pending traffic on timeout
router->DiscardOutboundFor(rc.pubkey);
router->routerProfiling().MarkConnectTimeout(rc.pubkey);
if(router->routerProfiling().IsBad(rc.pubkey))
2018-11-21 14:10:02 +00:00
{
if(!router->IsBootstrapNode(rc.pubkey))
router->nodedb()->Remove(rc.pubkey);
2018-11-21 14:10:02 +00:00
}
return true;
}
2019-05-14 14:27:12 +00:00
/// return true to remove
bool
Attempt()
{
--triesLeft;
2019-05-12 21:21:18 +00:00
if(!link)
2019-05-14 14:27:12 +00:00
return true;
if(!link->TryEstablishTo(rc))
return true;
lastAttempt = router->Now();
return false;
}
bool
ShouldRetry() const
{
return triesLeft > 0;
}
};
static void
on_try_connecting(std::shared_ptr< TryConnectJob > j)
{
2019-05-14 14:27:12 +00:00
if(j->Attempt())
j->router->pendingEstablishJobs.erase(j->rc.pubkey);
}
bool
llarp_loadServiceNodeIdentityKey(const fs::path &fpath,
llarp::SecretKey &secret)
{
std::string path = fpath.string();
llarp::IdentitySecret ident;
if(!ident.LoadFromFile(path.c_str()))
return false;
return llarp::CryptoManager::instance()->seed_to_secretkey(secret, ident);
}
2018-08-30 18:48:43 +00:00
bool
llarp_findOrCreateIdentity(const fs::path &path, llarp::SecretKey &secretkey)
{
2019-01-10 22:35:38 +00:00
std::string fpath = path.string();
llarp::LogDebug("find or create ", fpath);
std::error_code ec;
if(!fs::exists(path, ec))
{
llarp::LogInfo("generating new identity key");
llarp::CryptoManager::instance()->identity_keygen(secretkey);
2019-01-10 22:35:38 +00:00
if(!secretkey.SaveToFile(fpath.c_str()))
return false;
}
2019-01-10 22:35:38 +00:00
return secretkey.LoadFromFile(fpath.c_str());
2018-08-14 21:17:18 +00:00
}
// C++ ...
bool
llarp_findOrCreateEncryption(const fs::path &path, llarp::SecretKey &encryption)
{
2019-01-10 22:35:38 +00:00
std::string fpath = path.string();
llarp::LogDebug("find or create ", fpath);
std::error_code ec;
if(!fs::exists(path, ec))
{
llarp::LogInfo("generating new encryption key");
llarp::CryptoManager::instance()->encryption_keygen(encryption);
2019-01-10 22:35:38 +00:00
if(!encryption.SaveToFile(fpath.c_str()))
return false;
2018-06-20 12:34:48 +00:00
}
2019-01-10 22:35:38 +00:00
return encryption.LoadFromFile(fpath.c_str());
}
namespace llarp
{
2019-01-05 13:45:05 +00:00
bool
Router::TryConnectAsync(RouterContact remote, uint16_t numretries)
2019-01-05 13:45:05 +00:00
{
2019-03-25 14:36:51 +00:00
const RouterID us = pubkey();
if(remote.pubkey == us)
return false;
2019-03-26 21:38:53 +00:00
if(!ConnectionToRouterAllowed(remote.pubkey))
return false;
2019-01-05 13:45:05 +00:00
// do we already have a pending job for this remote?
if(HasPendingConnectJob(remote.pubkey))
{
LogDebug("We have pending connect jobs to ", remote.pubkey);
2019-01-05 13:45:05 +00:00
return false;
}
for(auto &link : outboundLinks)
{
if(!link->IsCompatable(remote))
continue;
std::shared_ptr< TryConnectJob > job =
std::make_shared< TryConnectJob >(remote, link, numretries, this);
2019-05-15 15:54:26 +00:00
auto itr = pendingEstablishJobs.emplace(remote.pubkey, job);
2019-01-17 13:56:03 +00:00
if(itr.second)
{
// try establishing async
2019-05-15 15:54:26 +00:00
_logic->queue_func(std::bind(&on_try_connecting, job));
2019-01-17 13:56:03 +00:00
return true;
}
2019-05-14 15:51:56 +00:00
else
{
itr.first->second->Attempt();
}
2019-01-05 13:45:05 +00:00
}
return false;
}
2019-02-27 12:55:26 +00:00
bool
Router::OnSessionEstablished(ILinkSession *s)
{
2019-02-27 12:55:26 +00:00
return async_verify_RC(s->GetRemoteRC());
}
2019-04-08 12:01:52 +00:00
Router::Router(struct llarp_threadpool *_tp, llarp_ev_loop_ptr __netloop,
2019-05-22 16:20:50 +00:00
std::shared_ptr< Logic > l)
: ready(false)
, _netloop(__netloop)
, tp(_tp)
, _logic(l)
, paths(this)
, _exitContext(this)
, disk(1, 1000)
, _dht(llarp_dht_context_new(this))
, inbound_link_msg_parser(this)
2019-02-22 16:21:05 +00:00
, _hiddenServiceContext(this)
2018-06-20 17:45:44 +00:00
{
// set rational defaults
this->ip4addr.sin_family = AF_INET;
this->ip4addr.sin_port = htons(1090);
_stopping.store(false);
_running.store(false);
2018-06-20 17:45:44 +00:00
}
Router::~Router()
2018-09-19 13:27:15 +00:00
{
llarp_dht_context_free(_dht);
2018-09-19 13:27:15 +00:00
}
2019-02-11 17:14:43 +00:00
util::StatusObject
Router::ExtractStatus() const
2019-02-08 19:43:25 +00:00
{
util::StatusObject obj{{"dht", _dht->impl->ExtractStatus()},
2019-02-22 16:21:05 +00:00
{"services", _hiddenServiceContext.ExtractStatus()},
{"exit", _exitContext.ExtractStatus()}};
std::vector< util::StatusObject > ob_links, ib_links;
std::transform(inboundLinks.begin(), inboundLinks.end(),
std::back_inserter(ib_links),
[](const auto &link) -> util::StatusObject {
return link->ExtractStatus();
});
std::transform(outboundLinks.begin(), outboundLinks.end(),
std::back_inserter(ob_links),
[](const auto &link) -> util::StatusObject {
return link->ExtractStatus();
});
obj.Put("links",
util::StatusObject{{"outbound", ob_links}, {"inbound", ib_links}});
2019-02-11 17:14:43 +00:00
return obj;
2019-02-08 19:43:25 +00:00
}
bool
Router::HandleRecvLinkMessageBuffer(ILinkSession *session,
const llarp_buffer_t &buf)
{
if(_stopping)
return true;
if(!session)
{
LogWarn("no link session");
return false;
}
return inbound_link_msg_parser.ProcessFrom(session, buf);
2018-10-07 15:29:36 +00:00
}
void
Router::PersistSessionUntil(const RouterID &remote, llarp_time_t until)
2018-10-07 15:29:36 +00:00
{
m_PersistingSessions[remote] =
std::max(until, m_PersistingSessions[remote]);
2019-05-14 14:36:18 +00:00
LogDebug("persist session to ", remote, " until ",
m_PersistingSessions[remote]);
2018-06-01 14:08:54 +00:00
}
2018-12-13 00:03:19 +00:00
bool
Router::GetRandomGoodRouter(RouterID &router)
{
2019-05-14 14:36:18 +00:00
auto pick_router = [&](auto &collection) -> bool {
const auto sz = collection.size();
auto itr = collection.begin();
if(sz == 0)
return false;
if(sz > 1)
std::advance(itr, randint() % sz);
router = itr->first;
return true;
2019-05-14 14:36:18 +00:00
};
if(whitelistRouters)
{
pick_router(lokinetRouters);
}
absl::ReaderMutexLock l(&nodedb()->access);
2019-05-14 14:36:18 +00:00
return pick_router(nodedb()->entries);
2018-12-13 00:03:19 +00:00
}
2019-04-30 16:07:17 +00:00
void
Router::PumpLL()
{
for(const auto &link : inboundLinks)
{
link->Pump();
}
for(const auto &link : outboundLinks)
{
link->Pump();
}
}
bool
Router::SendToOrQueue(const RouterID &remote, const ILinkMessage *msg)
2018-06-14 15:10:31 +00:00
{
for(const auto &link : inboundLinks)
2018-11-21 17:46:33 +00:00
{
if(link->HasSessionTo(remote))
{
SendTo(remote, msg, link.get());
return true;
}
2018-11-21 17:46:33 +00:00
}
2019-01-05 13:45:05 +00:00
for(const auto &link : outboundLinks)
2018-11-21 17:46:33 +00:00
{
2019-01-05 13:45:05 +00:00
if(link->HasSessionTo(remote))
{
SendTo(remote, msg, link.get());
return true;
}
2018-11-21 17:46:33 +00:00
}
// no link available
// this will create an entry in the outbound mq if it's not already there
auto itr = outboundMessageQueue.find(remote);
if(itr == outboundMessageQueue.end())
{
outboundMessageQueue.emplace(remote, MessageQueue());
}
// encode
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(linkmsg_buffer);
if(!msg->BEncode(&buf))
return false;
// queue buffer
auto &q = outboundMessageQueue[remote];
2019-05-16 18:55:12 +00:00
buf.sz = buf.cur - buf.base;
q.emplace(buf.sz);
memcpy(q.back().data(), buf.base, buf.sz);
RouterContact remoteRC;
// we don't have an open session to that router right now
if(nodedb()->Get(remote, remoteRC))
{
// try connecting directly as the rc is loaded from disk
2019-05-08 12:17:48 +00:00
return TryConnectAsync(remoteRC, 10);
}
// we don't have the RC locally so do a dht lookup
_dht->impl->LookupRouter(remote,
std::bind(&Router::HandleDHTLookupForSendTo, this,
remote, std::placeholders::_1));
return true;
2018-06-14 15:10:31 +00:00
}
2018-06-13 12:58:51 +00:00
void
Router::HandleDHTLookupForSendTo(RouterID remote,
const std::vector< RouterContact > &results)
{
if(results.size())
{
if(whitelistRouters
2019-03-25 20:01:45 +00:00
&& lokinetRouters.find(results[0].pubkey) == lokinetRouters.end())
{
return;
}
if(results[0].Verify(Now()))
{
2019-01-05 13:45:05 +00:00
TryConnectAsync(results[0], 10);
return;
}
}
DiscardOutboundFor(remote);
}
void
2019-04-05 14:58:22 +00:00
Router::ForEachPeer(std::function< void(const ILinkSession *, bool) > visit,
bool randomize) const
{
2019-01-05 13:45:05 +00:00
for(const auto &link : outboundLinks)
{
link->ForEachSession(
2019-04-05 14:58:22 +00:00
[visit](const ILinkSession *peer) { visit(peer, true); }, randomize);
2019-01-05 13:45:05 +00:00
}
for(const auto &link : inboundLinks)
{
link->ForEachSession(
2019-04-05 14:58:22 +00:00
[visit](const ILinkSession *peer) { visit(peer, false); }, randomize);
}
}
void
Router::ForEachPeer(std::function< void(ILinkSession *) > visit)
{
for(const auto &link : outboundLinks)
2019-01-05 13:45:05 +00:00
{
link->ForEachSession([visit](ILinkSession *peer) { visit(peer); });
2019-01-05 13:45:05 +00:00
}
for(const auto &link : inboundLinks)
{
link->ForEachSession([visit](ILinkSession *peer) { visit(peer); });
}
}
void
Router::try_connect(fs::path rcfile)
2018-05-16 18:13:18 +00:00
{
RouterContact remote;
if(!remote.Read(rcfile.string().c_str()))
{
LogError("failure to decode or verify of remote RC");
return;
}
if(remote.Verify(Now()))
{
LogDebug("verified signature");
2019-01-05 13:45:05 +00:00
if(!TryConnectAsync(remote, 10))
{
// or error?
LogWarn("session already made");
}
}
else
LogError(rcfile, " contains invalid RC");
2018-05-16 18:13:18 +00:00
}
2018-05-20 17:45:47 +00:00
bool
Router::EnsureIdentity()
{
if(!EnsureEncryptionKey())
return false;
2019-01-21 17:06:31 +00:00
if(usingSNSeed)
return llarp_loadServiceNodeIdentityKey(ident_keyfile, _identity);
2019-01-21 17:06:31 +00:00
else
return llarp_findOrCreateIdentity(ident_keyfile, _identity);
}
2018-04-05 14:23:14 +00:00
bool
Router::EnsureEncryptionKey()
{
return llarp_findOrCreateEncryption(encryption_keyfile, _encryption);
}
2018-01-29 14:27:24 +00:00
void
2019-05-15 16:15:20 +00:00
Router::AddLink(std::shared_ptr< ILinkLayer > link, bool inbound)
2018-05-20 17:45:47 +00:00
{
if(inbound)
2019-05-15 16:15:20 +00:00
inboundLinks.emplace(link);
else
2019-05-15 16:15:20 +00:00
outboundLinks.emplace(link);
2018-05-20 17:45:47 +00:00
}
bool
Router::Configure(Config *conf)
{
conf->visit(util::memFn(&Router::router_iter_config, this));
2019-01-05 13:45:05 +00:00
if(!InitOutboundLinks())
return false;
if(!Ready())
{
return false;
}
return EnsureIdentity();
}
2018-11-21 14:10:02 +00:00
bool
Router::Ready()
{
2019-01-05 13:45:05 +00:00
return outboundLinks.size() > 0;
}
2019-03-25 15:41:37 +00:00
/// called in disk worker thread
void
Router::HandleSaveRC() const
2019-03-25 15:41:37 +00:00
{
std::string fname = our_rc_file.string();
_rc.Write(fname.c_str());
2019-03-25 15:41:37 +00:00
}
bool
Router::SaveRC()
2018-08-18 14:01:21 +00:00
{
LogDebug("verify RC signature");
if(!_rc.Verify(Now()))
{
2019-05-24 02:01:36 +00:00
Dump< MAX_RC_SIZE >(rc());
LogError("RC is invalid, not saving");
return false;
}
diskworker()->addJob(std::bind(&Router::HandleSaveRC, this));
2019-03-25 15:41:37 +00:00
return true;
2018-06-20 12:34:48 +00:00
}
2018-05-29 13:40:26 +00:00
bool
Router::IsServiceNode() const
{
return inboundLinks.size() > 0;
}
2018-05-30 20:56:47 +00:00
void
Router::Close()
{
LogInfo("closing router");
2019-06-02 21:17:05 +00:00
llarp_ev_loop_stop(_netloop);
inboundLinks.clear();
2019-01-05 13:45:05 +00:00
outboundLinks.clear();
2019-05-18 19:05:48 +00:00
disk.stop();
disk.shutdown();
}
void
Router::on_verify_client_rc(llarp_async_verify_rc *job)
{
async_verify_context *ctx =
static_cast< async_verify_context * >(job->user);
auto router = ctx->router;
const PubKey pk(job->rc.pubkey);
router->FlushOutboundFor(pk, router->GetLinkWithSessionByPubkey(pk));
2018-11-20 15:46:26 +00:00
delete ctx;
2018-11-22 15:02:51 +00:00
router->pendingVerifyRC.erase(pk);
2019-01-17 13:56:03 +00:00
router->pendingEstablishJobs.erase(pk);
2018-05-30 20:56:47 +00:00
}
void
Router::on_verify_server_rc(llarp_async_verify_rc *job)
2018-09-06 11:46:19 +00:00
{
async_verify_context *ctx =
static_cast< async_verify_context * >(job->user);
auto router = ctx->router;
const PubKey pk(job->rc.pubkey);
if(!job->valid)
{
delete ctx;
router->DiscardOutboundFor(pk);
router->pendingVerifyRC.erase(pk);
return;
}
// we're valid, which means it's already been committed to the nodedb
2018-09-06 13:16:24 +00:00
LogDebug("rc verified and saved to nodedb");
2018-06-10 14:05:48 +00:00
if(router->validRouters.count(pk))
{
router->validRouters.erase(pk);
}
2018-05-30 20:56:47 +00:00
const RouterContact rc = job->rc;
router->validRouters.emplace(pk, rc);
2018-05-30 20:56:47 +00:00
// track valid router in dht
router->dht()->impl->Nodes()->PutNode(rc);
2018-05-30 20:56:47 +00:00
// mark success in profile
router->routerProfiling().MarkConnectSuccess(pk);
// this was an outbound establish job
if(ctx->establish_job)
{
ctx->establish_job->Success();
}
2018-11-28 15:18:18 +00:00
else
router->FlushOutboundFor(pk, router->GetLinkWithSessionByPubkey(pk));
delete ctx;
router->pendingVerifyRC.erase(pk);
2018-11-28 15:18:18 +00:00
}
void
Router::handle_router_ticker(void *user, uint64_t orig, uint64_t left)
2018-11-28 15:18:18 +00:00
{
if(left)
return;
Router *self = static_cast< Router * >(user);
self->ticker_job_id = 0;
self->Tick();
self->ScheduleTicker(orig);
2018-11-28 15:18:18 +00:00
}
bool
Router::ParseRoutingMessageBuffer(const llarp_buffer_t &buf,
routing::IMessageHandler *h,
const PathID_t &rxid)
{
return inbound_routing_msg_parser.ParseMessageBuffer(buf, h, rxid, this);
}
bool
Router::ConnectionToRouterAllowed(const RouterID &router) const
{
if(strictConnectPubkeys.size() && strictConnectPubkeys.count(router) == 0)
return false;
else if(IsServiceNode() && whitelistRouters)
2019-03-25 20:04:34 +00:00
return lokinetRouters.find(router) != lokinetRouters.end();
else
return true;
}
void
2019-03-22 14:37:37 +00:00
Router::HandleDHTLookupForExplore(RouterID,
const std::vector< RouterContact > &results)
2018-08-14 21:17:18 +00:00
{
2019-03-22 14:37:37 +00:00
const auto numConnected = NumberOfConnectedRouters();
for(const auto &rc : results)
{
if(!rc.Verify(Now()))
2019-03-22 14:37:37 +00:00
continue;
2019-03-29 15:08:31 +00:00
nodedb()->InsertAsync(rc);
2019-03-22 14:37:37 +00:00
if(ConnectionToRouterAllowed(rc.pubkey)
&& numConnected < minConnectedRouters)
2019-03-29 15:08:31 +00:00
TryConnectAsync(rc, 10);
}
2018-08-14 21:17:18 +00:00
}
void
Router::TryEstablishTo(const RouterID &remote)
2018-08-14 21:17:18 +00:00
{
2019-03-25 14:36:51 +00:00
const RouterID us = pubkey();
if(us == remote)
return;
2019-03-26 21:38:53 +00:00
if(!ConnectionToRouterAllowed(remote))
{
LogWarn("not connecting to ", remote, " as it's not permitted by config");
2018-10-07 15:29:36 +00:00
return;
}
RouterContact rc;
if(nodedb()->Get(remote, rc))
{
// try connecting async
2019-01-05 13:45:05 +00:00
TryConnectAsync(rc, 5);
}
2019-04-16 11:44:55 +00:00
else if(IsServiceNode())
{
if(dht()->impl->HasRouterLookup(remote))
return;
LogInfo("looking up router ", remote);
// dht lookup as we don't know it
dht()->impl->LookupRouter(
remote,
std::bind(&Router::HandleDHTLookupForTryEstablishTo, this, remote,
std::placeholders::_1));
}
else
{
LogWarn("not connecting to ", remote, " as it's unreliable");
}
2018-08-14 21:17:18 +00:00
}
void
Router::OnConnectTimeout(ILinkSession *session)
{
auto itr = pendingEstablishJobs.find(session->GetPubKey());
if(itr != pendingEstablishJobs.end())
{
if(itr->second->Timeout())
pendingEstablishJobs.erase(itr);
}
}
2018-08-14 21:17:18 +00:00
void
Router::HandleDHTLookupForTryEstablishTo(
RouterID remote, const std::vector< RouterContact > &results)
{
if(results.size() == 0)
{
if(!IsServiceNode())
routerProfiling().MarkConnectTimeout(remote);
}
for(const auto &result : results)
{
if(whitelistRouters
&& lokinetRouters.find(result.pubkey) == lokinetRouters.end())
continue;
2019-01-05 13:45:05 +00:00
TryConnectAsync(result, 10);
}
}
2019-05-09 15:36:39 +00:00
size_t
Router::NumberOfRoutersMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const
{
std::set< RouterID > connected;
ForEachPeer([&](const auto *link, bool) {
if(filter(link))
connected.insert(link->GetPubKey());
});
return connected.size();
}
size_t
Router::NumberOfConnectedRouters() const
{
2019-05-09 15:36:39 +00:00
return NumberOfRoutersMatchingFilter([&](const ILinkSession *link) -> bool {
2019-05-11 14:56:11 +00:00
if(!link->IsEstablished())
return false;
2019-05-09 15:36:39 +00:00
const RouterContact rc(link->GetRemoteRC());
return rc.IsPublicRouter() && ConnectionToRouterAllowed(rc.pubkey);
});
}
size_t
Router::NumberOfConnectedClients() const
{
return NumberOfRoutersMatchingFilter([&](const ILinkSession *link) -> bool {
2019-05-11 14:56:11 +00:00
if(!link->IsEstablished())
return false;
2019-05-09 15:36:39 +00:00
const RouterContact rc(link->GetRemoteRC());
return !rc.IsPublicRouter();
});
}
size_t
Router::NumberOfConnectionsMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const
{
size_t sz = 0;
ForEachPeer([&](const auto *link, bool) {
if(filter(link))
++sz;
});
return sz;
}
2018-06-19 17:11:24 +00:00
bool
Router::UpdateOurRC(bool rotateKeys)
{
SecretKey nextOnionKey;
RouterContact nextRC = _rc;
if(rotateKeys)
{
CryptoManager::instance()->encryption_keygen(nextOnionKey);
2019-01-29 13:20:27 +00:00
std::string f = encryption_keyfile.string();
2019-03-25 15:41:37 +00:00
// TODO: use disk worker
2019-01-29 13:20:27 +00:00
if(nextOnionKey.SaveToFile(f.c_str()))
{
nextRC.enckey = seckey_topublic(nextOnionKey);
_encryption = nextOnionKey;
2019-01-29 13:20:27 +00:00
}
}
nextRC.last_updated = Now();
if(!nextRC.Sign(identity()))
return false;
_rc = nextRC;
// propagate RC by renegotiating sessions
ForEachPeer([](ILinkSession *s) {
if(s->RenegotiateSession())
LogInfo("renegotiated session");
else
LogWarn("failed to renegotiate session");
});
2019-02-04 23:18:43 +00:00
return SaveRC();
2019-03-25 15:41:37 +00:00
}
void
Router::router_iter_config(const char *section, const char *key,
const char *val)
{
llarp::LogDebug(section, " ", key, "=", val);
int af;
uint16_t proto = 0;
std::set< std::string > opts;
if(StrEq(val, "eth"))
{
#ifdef AF_LINK
af = AF_LINK;
#endif
#ifdef AF_PACKET
af = AF_PACKET;
#endif
proto = LLARP_ETH_PROTO;
}
else if(StrEq(section, "bind"))
{
// try IPv4 first
af = AF_INET;
std::set< std::string > parsed_opts;
std::string v = val;
std::string::size_type idx;
do
{
idx = v.find_first_of(',');
if(idx != std::string::npos)
{
parsed_opts.insert(v.substr(0, idx));
v = v.substr(idx + 1);
}
else
parsed_opts.insert(v);
} while(idx != std::string::npos);
/// for each option
for(const auto &item : parsed_opts)
{
/// see if it's a number
auto port = std::atoi(item.c_str());
if(port > 0)
{
/// set port
if(proto == 0)
proto = port;
} /// otherwise add to opts
else
opts.insert(item);
}
}
if(StrEq(section, "bind"))
{
if(StrEq(key, "*"))
{
m_OutboundPort = proto;
}
else
{
auto server = llarp::utp::NewServerFromRouter(this);
if(!server->EnsureKeys(transport_keyfile.string().c_str()))
{
llarp::LogError("failed to ensure keyfile ", transport_keyfile);
return;
}
if(server->Configure(netloop(), key, af, proto))
{
AddLink(std::move(server), true);
return;
}
LogError("failed to bind inbound link on ", key, " port ", proto);
}
}
else if(StrEq(section, "network"))
{
if(StrEq(key, "profiling"))
{
if(IsTrueValue(val))
{
routerProfiling().Enable();
LogInfo("router profiling explicitly enabled");
}
else if(IsFalseValue(val))
{
routerProfiling().Disable();
LogInfo("router profiling explicitly disabled");
}
}
if(StrEq(key, "profiles"))
{
routerProfilesFile = val;
routerProfiling().Load(val);
llarp::LogInfo("setting profiles to ", routerProfilesFile);
}
else if(StrEq(key, "strict-connect"))
{
if(IsServiceNode())
{
llarp::LogError("cannot use strict-connect option as service node");
return;
}
llarp::RouterID snode;
llarp::PubKey pk;
if(pk.FromString(val))
{
if(strictConnectPubkeys.emplace(pk).second)
llarp::LogInfo("added ", pk, " to strict connect list");
else
llarp::LogWarn("duplicate key for strict connect: ", pk);
}
else if(snode.FromString(val))
{
if(strictConnectPubkeys.insert(snode).second)
{
llarp::LogInfo("added ", snode, " to strict connect list");
netConfig.emplace(key, val);
}
else
llarp::LogWarn("duplicate key for strict connect: ", snode);
}
else
llarp::LogError("invalid key for strict-connect: ", val);
}
else
{
netConfig.emplace(key, val);
}
}
else if(StrEq(section, "api"))
{
if(StrEq(key, "enabled"))
{
enableRPCServer = IsTrueValue(val);
}
if(StrEq(key, "bind"))
{
rpcBindAddr = val;
}
if(StrEq(key, "authkey"))
{
// TODO: add pubkey to whitelist
}
}
else if(StrEq(section, "services"))
{
if(LoadHiddenServiceConfig(val))
{
llarp::LogInfo("loaded hidden service config for ", key);
}
else
{
llarp::LogWarn("failed to load hidden service config for ", key);
}
}
else if(StrEq(section, "logging"))
{
if(strlen(key) == 0 && strlen(val) == 0)
{
if(m_LogJSON)
{
LogContext::Instance().logStream = std::make_unique< JSONLogStream >(
diskworker(), m_LogFile, 100, m_LogFile != stdout);
}
else if(m_LogFile != stdout)
{
LogContext::Instance().logStream = std::make_unique< FileLogStream >(
diskworker(), m_LogFile, 100, true);
}
}
if(StrEq(key, "type") && StrEq(val, "syslog"))
{
// TODO(despair): write event log syslog class
#if defined(_WIN32)
LogError("syslog not supported on win32");
#else
LogInfo("Switching to syslog");
LogContext::Instance().logStream = std::make_unique< SysLogStream >();
#endif
}
if(StrEq(key, "type") && StrEq(val, "json"))
{
m_LogJSON = true;
}
if(StrEq(key, "file"))
{
LogInfo("open log file: ", val);
FILE *const logfile = ::fopen(val, "a");
if(logfile)
{
m_LogFile = logfile;
LogInfo("will log to file ", val);
}
else if(errno)
{
LogError("could not open log file at '", val, "': ", strerror(errno));
errno = 0;
}
else
{
LogError("failed to open log file at '", val,
"' for an unknown reason, bailing tf out kbai");
::abort();
}
}
}
else if(StrEq(section, "lokid"))
{
if(StrEq(key, "service-node-seed"))
{
usingSNSeed = true;
ident_keyfile = val;
}
if(StrEq(key, "enabled"))
{
whitelistRouters = IsTrueValue(val);
}
if(StrEq(key, "jsonrpc") || StrEq(key, "addr"))
{
lokidRPCAddr = val;
}
if(StrEq(key, "username"))
{
lokidRPCUser = val;
}
if(StrEq(key, "password"))
{
lokidRPCPassword = val;
}
}
else if(StrEq(section, "dns"))
{
if(StrEq(key, "upstream"))
{
llarp::LogInfo("add upstream resolver ", val);
netConfig.emplace("upstream-dns", val);
}
if(StrEq(key, "bind"))
{
llarp::LogInfo("set local dns to ", val);
netConfig.emplace("local-dns", val);
}
}
else if(StrEq(section, "connect")
|| (StrEq(section, "bootstrap") && StrEq(key, "add-node")))
{
// llarp::LogDebug("connect section has ", key, "=", val);
2019-05-09 15:36:39 +00:00
RouterContact rc;
if(!rc.Read(val))
{
llarp::LogWarn("failed to decode bootstrap RC, file='", val,
"' rc=", rc);
;
return;
}
if(rc.Verify(Now()))
{
2019-05-11 20:47:37 +00:00
const auto result = bootstrapRCList.insert(rc);
2019-05-09 15:36:39 +00:00
if(result.second)
llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey));
else
llarp::LogWarn("Duplicate bootstrap node ", RouterID(rc.pubkey));
}
else
{
if(rc.IsExpired(Now()))
{
llarp::LogWarn("Bootstrap node ", RouterID(rc.pubkey),
" is too old and needs to be refreshed");
}
else
{
llarp::LogError("malformed rc file='", val, "' rc=", rc);
}
}
}
else if(StrEq(section, "router"))
{
if(StrEq(key, "netid"))
{
if(strlen(val) <= _rc.netID.size())
{
llarp::LogWarn("!!!! you have manually set netid to be '", val,
"' which does not equal '", Version::LLARP_NET_ID,
"' you will run as a different network, good luck "
"and "
"don't forget: something something MUH traffic "
"shape "
"correlation !!!!");
llarp::NetID::DefaultValue() =
llarp::NetID(reinterpret_cast< const byte_t * >(strdup(val)));
// re set netid in our rc
_rc.netID = llarp::NetID();
}
else
llarp::LogError("invalid netid '", val, "', is too long");
}
if(StrEq(key, "max-connections"))
{
auto ival = atoi(val);
if(ival > 0)
{
maxConnectedRouters = ival;
LogInfo("max connections set to ", maxConnectedRouters);
}
}
if(StrEq(key, "min-connections"))
{
auto ival = atoi(val);
if(ival > 0)
{
minConnectedRouters = ival;
LogInfo("min connections set to ", minConnectedRouters);
}
}
if(StrEq(key, "nickname"))
{
_rc.SetNick(val);
// set logger name here
LogContext::Instance().nodeName = rc().Nick();
}
if(StrEq(key, "encryption-privkey"))
{
encryption_keyfile = val;
}
if(StrEq(key, "contact-file"))
{
our_rc_file = val;
}
if(StrEq(key, "transport-privkey"))
{
transport_keyfile = val;
}
if((StrEq(key, "identity-privkey") || StrEq(key, "ident-privkey"))
&& !usingSNSeed)
{
ident_keyfile = val;
}
if(StrEq(key, "public-address") || StrEq(key, "public-ip"))
{
llarp::LogInfo("public ip ", val, " size ", strlen(val));
if(strlen(val) < 17)
{
// assume IPv4
// inet_pton(AF_INET, val, &ip4addr.sin_addr);
// struct sockaddr dest;
// sockaddr *dest = (sockaddr *)&ip4addr;
llarp::Addr a(val);
llarp::LogInfo("setting public ipv4 ", a);
addrInfo.ip = *a.addr6();
publicOverride = true;
}
// llarp::Addr a(val);
}
if(StrEq(key, "public-port"))
{
llarp::LogInfo("Setting public port ", val);
int p = atoi(val);
// Not needed to flip upside-down - this is done in llarp::Addr(const
// AddressInfo&)
ip4addr.sin_port = p;
addrInfo.port = p;
publicOverride = true;
}
}
}
bool
Router::CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc)
{
// missmatch of identity ?
if(newrc.pubkey != oldrc.pubkey)
return false;
// store it in nodedb async
2019-02-27 12:55:26 +00:00
if(!async_verify_RC(newrc))
return false;
2018-12-19 17:48:29 +00:00
// update dht if required
if(dht()->impl->Nodes()->HasNode(dht::Key_t{newrc.pubkey}))
2018-12-19 17:48:29 +00:00
{
dht()->impl->Nodes()->PutNode(newrc);
2018-12-19 17:48:29 +00:00
}
// update valid routers
{
auto itr = validRouters.find(newrc.pubkey);
if(itr == validRouters.end())
validRouters[newrc.pubkey] = newrc;
else
itr->second = newrc;
}
// TODO: check for other places that need updating the RC
return true;
}
2018-12-19 17:48:29 +00:00
void
2019-06-04 13:19:45 +00:00
Router::LookupRouterWhenExpired(RouterID router)
2018-12-19 17:48:29 +00:00
{
2019-06-04 13:19:45 +00:00
LookupRouter(router,
std::bind(&Router::HandleRouterLookupForExpireUpdate, this,
router, std::placeholders::_1));
}
void
Router::HandleRouterLookupForExpireUpdate(
RouterID router, const std::vector< RouterContact > &result)
{
const auto now = Now();
RouterContact current;
if(nodedb()->Get(router, current))
{
if(current.IsExpired(now))
{
nodedb()->Remove(router);
}
}
2019-06-04 13:29:16 +00:00
if(result.size() == 1 && !result[0].IsExpired(now))
{
LogInfo("storing rc for ", router);
nodedb()->Insert(result[0]);
}
else
{
LogInfo("not storing rc for ", router);
}
2018-12-19 17:48:29 +00:00
}
bool
Router::HasPendingRouterLookup(const RouterID &remote) const
{
if(IsServiceNode())
return dht()->impl->HasRouterLookup(remote);
bool has = false;
_hiddenServiceContext.ForEachService(
[&has, remote](const std::string &,
const std::shared_ptr< service::Endpoint > &ep) -> bool {
has |= ep->HasPendingRouterLookup(remote);
return true;
});
return has;
}
2019-03-31 15:09:59 +00:00
void
2019-05-03 13:15:03 +00:00
Router::LookupRouter(RouterID remote, RouterLookupHandler resultHandler)
2019-03-31 15:09:59 +00:00
{
2019-06-04 13:19:45 +00:00
if(!resultHandler)
{
resultHandler = std::bind(&Router::HandleRouterLookupForExpireUpdate,
this, remote, std::placeholders::_1);
}
2019-03-31 15:09:59 +00:00
if(IsServiceNode())
{
2019-06-04 13:19:45 +00:00
dht()->impl->LookupRouter(remote, resultHandler);
}
else
{
_hiddenServiceContext.ForEachService(
[=](const std::string &,
const std::shared_ptr< service::Endpoint > &ep) -> bool {
return !ep->LookupRouterAnon(remote, resultHandler);
});
2019-03-31 15:09:59 +00:00
}
}
bool
2019-05-09 15:36:39 +00:00
Router::IsBootstrapNode(const RouterID r) const
{
2019-05-09 15:36:39 +00:00
return std::count_if(
bootstrapRCList.begin(), bootstrapRCList.end(),
[r](const RouterContact &rc) -> bool { return rc.pubkey == r; })
> 0;
}
2019-04-05 14:58:22 +00:00
void
Router::Tick()
2018-06-19 17:11:24 +00:00
{
2019-04-23 18:29:42 +00:00
if(_stopping)
return;
// LogDebug("tick router");
auto now = Now();
2018-12-19 17:48:29 +00:00
2019-03-04 17:03:18 +00:00
routerProfiling().Tick();
// try looking up stale routers
nodedb()->VisitInsertedAfter(
[&](const RouterContact &rc) {
if(HasPendingRouterLookup(rc.pubkey))
return;
LookupRouter(rc.pubkey, nullptr);
},
2019-06-10 12:51:45 +00:00
RouterContact::UpdateInterval + now);
std::set< RouterID > removeStale;
// remove stale routers
nodedb()->VisitInsertedAfter(
[&](const RouterContact &rc) { removeStale.insert(rc.pubkey); },
2019-06-10 12:51:45 +00:00
((RouterContact::UpdateInterval * 3) / 2) + now);
nodedb()->RemoveIf([removeStale](const RouterContact &rc) -> bool {
return removeStale.count(rc.pubkey) > 0;
2019-06-04 13:19:45 +00:00
});
2018-12-19 17:48:29 +00:00
if(IsServiceNode())
{
2019-03-31 15:09:59 +00:00
if(_rc.ExpiresSoon(now, randint() % 10000)
|| (now - _rc.last_updated) > rcRegenInterval)
{
LogInfo("regenerating RC");
if(!UpdateOurRC(false))
LogError("Failed to update our RC");
}
2019-05-12 17:54:30 +00:00
/*
2019-05-09 15:36:39 +00:00
// kill nodes that are not allowed by network policy
nodedb()->RemoveIf([&](const RouterContact &rc) -> bool {
2019-05-11 20:53:08 +00:00
if(IsBootstrapNode(rc.pubkey))
return false;
2019-05-09 15:36:39 +00:00
return !ConnectionToRouterAllowed(rc.pubkey);
});
2019-05-12 17:54:30 +00:00
*/
2018-12-19 17:48:29 +00:00
}
else
{
// kill dead nodes if client
nodedb()->RemoveIf([&](const RouterContact &rc) -> bool {
2019-05-09 15:36:39 +00:00
// don't kill first hop nodes
if(strictConnectPubkeys.count(rc.pubkey))
return false;
// don't kill "non-bad" nodes
if(!routerProfiling().IsBad(rc.pubkey))
return false;
routerProfiling().ClearProfile(rc.pubkey);
2019-05-09 15:36:39 +00:00
// don't kill bootstrap nodes
return !IsBootstrapNode(rc.pubkey);
});
}
2019-04-23 16:13:22 +00:00
// expire transit paths
paths.ExpirePaths(now);
{
auto itr = pendingEstablishJobs.begin();
while(itr != pendingEstablishJobs.end())
{
if(itr->second->TimeoutReached() && itr->second->Timeout())
{
LogWarn("failed to connect to ", itr->first);
itr = pendingEstablishJobs.erase(itr);
}
else
++itr;
}
}
2019-02-25 12:46:40 +00:00
2018-08-14 21:17:18 +00:00
{
auto itr = m_PersistingSessions.begin();
while(itr != m_PersistingSessions.end())
2018-08-14 21:17:18 +00:00
{
auto link = GetLinkWithSessionByPubkey(itr->first);
if(now < itr->second)
2018-08-14 21:17:18 +00:00
{
if(link && link->HasSessionTo(itr->first))
{
LogDebug("keepalive to ", itr->first);
link->KeepAliveSessionTo(itr->first);
}
else
{
RouterContact rc;
if(nodedb()->Get(itr->first, rc))
{
if(rc.IsPublicRouter())
{
LogDebug("establish to ", itr->first);
TryConnectAsync(rc, 5);
}
}
}
++itr;
2018-08-14 21:17:18 +00:00
}
else
{
2019-05-09 15:36:39 +00:00
const RouterID r(itr->first);
LogInfo("commit to ", r, " expired");
itr = m_PersistingSessions.erase(itr);
2018-08-14 21:17:18 +00:00
}
}
}
2019-03-29 15:08:31 +00:00
const size_t connected = NumberOfConnectedRouters();
const size_t N = nodedb()->num_loaded();
if(N < minRequiredRouters)
{
LogInfo("We need at least ", minRequiredRouters,
" service nodes to build paths but we have ", N, " in nodedb");
// TODO: only connect to random subset
if(bootstrapRCList.size())
{
for(const auto &rc : bootstrapRCList)
{
dht()->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
}
2019-06-24 19:15:02 +00:00
// explore via every conected peer
ForEachPeer([&](ILinkSession *s) {
if(!s->IsEstablished())
return;
const RouterContact rc = s->GetRemoteRC();
if(rc.IsPublicRouter())
dht()->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
});
2018-08-14 21:17:18 +00:00
}
else
LogError("we have no bootstrap nodes specified");
}
if(connected < minConnectedRouters)
{
size_t dlt = minConnectedRouters - connected;
LogInfo("connecting to ", dlt, " random routers to keep alive");
ConnectToRandomRouters(dlt);
2018-08-14 21:17:18 +00:00
}
if(!IsServiceNode())
2019-04-23 16:13:22 +00:00
{
_hiddenServiceContext.Tick(now);
2019-04-23 16:13:22 +00:00
}
_exitContext.Tick(now);
if(rpcCaller)
rpcCaller->Tick(now);
2019-03-25 15:41:37 +00:00
// save profiles async
if(routerProfiling().ShouldSave(now))
{
diskworker()->addJob(
[&]() { routerProfiling().Save(routerProfilesFile.c_str()); });
2019-03-25 15:41:37 +00:00
}
} // namespace llarp
2018-08-22 16:19:51 +00:00
bool
Router::Sign(Signature &sig, const llarp_buffer_t &buf) const
{
2019-06-13 21:58:17 +00:00
metrics::TimerGuard t("Router", "Sign");
return CryptoManager::instance()->sign(sig, identity(), buf);
}
void
Router::SendTo(RouterID remote, const ILinkMessage *msg, ILinkLayer *selected)
2018-08-22 16:19:51 +00:00
{
2019-06-13 21:58:17 +00:00
metrics::integerTick(msg->Name(), "to", 1, "tx", remote.ToString());
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(linkmsg_buffer);
if(!msg->BEncode(&buf))
{
LogWarn("failed to encode outbound message, buffer size left: ",
buf.size_left());
return;
}
// set size of message
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
LogDebug("send ", buf.sz, " bytes to ", remote);
if(selected)
{
if(selected->SendTo(remote, buf))
return;
}
for(const auto &link : inboundLinks)
2018-06-19 17:11:24 +00:00
{
2019-01-05 13:45:05 +00:00
if(link->SendTo(remote, buf))
return;
2018-06-19 17:11:24 +00:00
}
for(const auto &link : outboundLinks)
2019-01-05 13:45:05 +00:00
{
if(link->SendTo(remote, buf))
return;
2018-06-19 17:11:24 +00:00
}
LogWarn("message to ", remote, " was dropped");
2019-06-28 19:28:59 +00:00
metrics::integerTick(msg->Name(), "to", 1, "drop", remote.ToString());
2018-06-19 17:11:24 +00:00
}
2018-05-30 20:56:47 +00:00
void
Router::ScheduleTicker(uint64_t ms)
2018-06-06 12:46:26 +00:00
{
ticker_job_id = _logic->call_later({ms, this, &handle_router_ticker});
2018-06-06 12:46:26 +00:00
}
void
Router::SessionClosed(RouterID remote)
2018-06-20 12:34:48 +00:00
{
2019-02-25 12:46:40 +00:00
dht::Key_t k(remote);
dht()->impl->Nodes()->DelNode(k);
// remove from valid routers if it's a valid router
validRouters.erase(remote);
LogInfo("Session to ", remote, " fully closed");
2018-06-20 12:34:48 +00:00
}
ILinkLayer *
Router::GetLinkWithSessionByPubkey(const RouterID &pubkey)
2018-06-06 12:46:26 +00:00
{
2019-01-05 13:45:05 +00:00
for(const auto &link : outboundLinks)
{
if(link->HasSessionTo(pubkey))
return link.get();
}
for(const auto &link : inboundLinks)
2018-06-06 12:46:26 +00:00
{
if(link->HasSessionTo(pubkey))
return link.get();
2018-06-06 12:46:26 +00:00
}
return nullptr;
2018-06-06 12:46:26 +00:00
}
2018-05-30 20:56:47 +00:00
void
Router::FlushOutboundFor(RouterID remote, ILinkLayer *chosen)
2018-07-03 13:33:37 +00:00
{
LogDebug("Flush outbound for ", remote);
2018-07-03 13:33:37 +00:00
auto itr = outboundMessageQueue.find(remote);
if(itr == outboundMessageQueue.end())
{
pendingEstablishJobs.erase(remote);
return;
}
// if for some reason we don't provide a link layer pick one that has it
if(!chosen)
{
for(const auto &link : inboundLinks)
{
if(link->HasSessionTo(remote))
{
chosen = link.get();
break;
}
}
for(const auto &link : outboundLinks)
{
if(link->HasSessionTo(remote))
{
chosen = link.get();
break;
}
}
}
while(itr->second.size())
{
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(itr->second.front());
if(!chosen->SendTo(remote, buf))
LogWarn("failed to send queued outbound message to ", remote, " via ",
chosen->Name());
2018-11-22 15:53:11 +00:00
itr->second.pop();
}
2018-11-22 15:02:51 +00:00
pendingEstablishJobs.erase(remote);
outboundMessageQueue.erase(itr);
2018-06-01 14:08:54 +00:00
}
2018-06-13 12:58:51 +00:00
void
Router::DiscardOutboundFor(const RouterID &remote)
{
outboundMessageQueue.erase(remote);
}
bool
Router::GetRandomConnectedRouter(RouterContact &result) const
2018-11-22 15:02:51 +00:00
{
auto sz = validRouters.size();
if(sz)
2018-11-22 15:02:51 +00:00
{
auto itr = validRouters.begin();
if(sz > 1)
std::advance(itr, randint() % sz);
result = itr->second;
return true;
2018-11-22 15:02:51 +00:00
}
return false;
2018-11-22 15:02:51 +00:00
}
2019-02-27 12:55:26 +00:00
bool
2019-05-11 22:15:31 +00:00
Router::async_verify_RC(const RouterContact rc)
2018-10-09 12:06:30 +00:00
{
2019-02-27 12:55:26 +00:00
if(rc.IsPublicRouter() && whitelistRouters && IsServiceNode())
2018-10-09 12:06:30 +00:00
{
2019-03-26 21:38:53 +00:00
if(lokinetRouters.size() == 0)
{
LogError("we have no service nodes in whitelist");
return false;
}
if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end())
{
2019-02-28 05:11:52 +00:00
RouterID sn(rc.pubkey);
LogInfo(sn, " is NOT a valid service node, rejecting");
2019-02-27 12:55:26 +00:00
return false;
}
2018-10-09 12:06:30 +00:00
}
2019-02-27 12:55:26 +00:00
if(pendingVerifyRC.count(rc.pubkey))
return true;
LogInfo("session with ", RouterID(rc.pubkey), " established");
llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey];
async_verify_context *ctx = new async_verify_context();
ctx->router = this;
ctx->establish_job = nullptr;
auto itr = pendingEstablishJobs.find(rc.pubkey);
if(itr != pendingEstablishJobs.end())
ctx->establish_job = itr->second.get();
job->user = ctx;
job->rc = rc;
job->valid = false;
job->hook = nullptr;
job->nodedb = _nodedb;
job->logic = _logic;
job->cryptoworker = tp;
job->diskworker = &disk;
if(rc.IsPublicRouter())
job->hook = &Router::on_verify_server_rc;
else
job->hook = &Router::on_verify_client_rc;
llarp_nodedb_async_verify(job);
2019-02-27 12:55:26 +00:00
return true;
}
void
Router::SetRouterWhitelist(const std::vector< RouterID > &routers)
{
lokinetRouters.clear();
for(const auto &router : routers)
lokinetRouters.emplace(router,
std::numeric_limits< llarp_time_t >::max());
LogInfo("lokinet service node list now has ", lokinetRouters.size(),
" routers");
}
bool
Router::Run(struct llarp_nodedb *nodedb)
{
if(_running || _stopping)
return false;
this->_nodedb = nodedb;
if(enableRPCServer)
2018-10-09 12:06:30 +00:00
{
if(rpcBindAddr.empty())
{
rpcBindAddr = DefaultRPCBindAddr;
}
rpcServer = std::make_unique< rpc::Server >(this);
while(!rpcServer->Start(rpcBindAddr))
{
LogError("failed to bind jsonrpc to ", rpcBindAddr);
2018-11-08 12:31:50 +00:00
#if defined(ANDROID) || defined(RPI)
sleep(1);
2018-11-08 12:31:50 +00:00
#else
std::this_thread::sleep_for(std::chrono::seconds(1));
2018-11-08 12:31:50 +00:00
#endif
}
LogInfo("Bound RPC server to ", rpcBindAddr);
2018-10-09 12:06:30 +00:00
}
if(whitelistRouters)
{
rpcCaller = std::make_unique< rpc::Caller >(this);
rpcCaller->SetAuth(lokidRPCUser, lokidRPCPassword);
while(!rpcCaller->Start(lokidRPCAddr))
{
LogError("failed to start jsonrpc caller to ", lokidRPCAddr);
#if defined(ANDROID) || defined(RPI)
sleep(1);
#else
std::this_thread::sleep_for(std::chrono::seconds(1));
#endif
}
LogInfo("RPC Caller to ", lokidRPCAddr, " started");
}
2018-10-09 12:06:30 +00:00
llarp_threadpool_start(tp);
disk.start();
2018-11-19 16:40:49 +00:00
2019-04-16 16:50:47 +00:00
for(const auto &rc : bootstrapRCList)
2019-05-11 20:47:37 +00:00
{
if(this->nodedb()->Insert(rc))
{
LogInfo("added bootstrap node ", RouterID(rc.pubkey));
}
else
{
LogError("Failed to add bootstrap node ", RouterID(rc.pubkey));
}
}
2019-04-16 16:50:47 +00:00
routerProfiling().Load(routerProfilesFile.c_str());
Addr publicAddr(this->addrInfo);
if(this->publicOverride)
{
LogDebug("public address:port ", publicAddr);
}
LogInfo("You have ", inboundLinks.size(), " inbound links");
2019-01-05 13:45:05 +00:00
2019-04-08 18:21:01 +00:00
// set public signing key
_rc.pubkey = seckey_topublic(identity());
AddressInfo ai;
for(const auto &link : inboundLinks)
{
2019-01-05 13:45:05 +00:00
if(link->GetOurAddressInfo(ai))
{
2019-01-05 13:45:05 +00:00
// override ip and port
if(this->publicOverride)
{
2019-01-05 13:45:05 +00:00
ai.ip = *publicAddr.addr6();
ai.port = publicAddr.port();
}
if(IsBogon(ai.ip))
2019-01-05 13:45:05 +00:00
continue;
_rc.addrs.push_back(ai);
2019-04-08 18:21:01 +00:00
if(ExitEnabled())
{
const llarp::Addr addr(ai);
2019-04-08 18:25:06 +00:00
const nuint32_t a{addr.addr4()->s_addr};
2019-04-08 18:21:01 +00:00
_rc.exits.emplace_back(_rc.pubkey, a);
LogInfo(
"Neato teh l33toh, You are a freaking exit relay. w00t!!!!! your "
"exit "
"is advertised as exiting at ",
a);
}
}
}
// set public encryption key
_rc.enckey = seckey_topublic(encryption());
2019-04-08 18:21:01 +00:00
LogInfo("Signing rc...");
if(!_rc.Sign(identity()))
{
LogError("failed to sign rc");
return false;
}
if(!SaveRC())
{
LogError("failed to save RC");
return false;
}
LogInfo("have ", nodedb->num_loaded(), " routers");
2018-09-10 13:43:36 +00:00
LogInfo("starting outbound ", outboundLinks.size(), " links");
2019-01-05 13:45:05 +00:00
for(const auto &link : outboundLinks)
{
if(!link->Start(_logic))
2019-01-05 13:45:05 +00:00
{
LogWarn("outbound link '", link->Name(), "' failed to start");
2019-01-05 13:45:05 +00:00
return false;
}
}
int IBLinksStarted = 0;
2018-06-10 14:05:48 +00:00
// start links
for(const auto &link : inboundLinks)
2018-06-10 14:05:48 +00:00
{
if(link->Start(_logic))
{
LogDebug("Link ", link->Name(), " started");
IBLinksStarted++;
}
else
LogWarn("Link ", link->Name(), " failed to start");
2018-06-10 14:05:48 +00:00
}
if(IBLinksStarted > 0)
{
// initialize as service node
if(!InitServiceNode())
{
LogError("Failed to initialize service node");
return false;
}
RouterID us = pubkey();
LogInfo("initalized service node: ", us);
if(minConnectedRouters < 6)
minConnectedRouters = 6;
// relays do not use profiling
routerProfiling().Disable();
}
else
{
maxConnectedRouters = minConnectedRouters + 1;
// we are a client
// regenerate keys and resign rc before everything else
CryptoManager::instance()->identity_keygen(_identity);
CryptoManager::instance()->encryption_keygen(_encryption);
_rc.pubkey = seckey_topublic(identity());
_rc.enckey = seckey_topublic(encryption());
if(!_rc.Sign(identity()))
{
LogError("failed to regenerate keys and sign RC");
return false;
}
// don't create default if we already have some defined
if(this->ShouldCreateDefaultHiddenService())
{
// generate default hidden service
LogInfo("setting up default network endpoint");
if(!CreateDefaultHiddenService())
{
LogError("failed to set up default network endpoint");
return false;
}
}
}
LogInfo("starting hidden service context...");
2019-02-22 16:21:05 +00:00
if(!hiddenServiceContext().StartAll())
{
LogError("Failed to start hidden service context");
return false;
}
llarp_dht_context_start(dht(), pubkey());
ScheduleTicker(1000);
_running.store(true);
_startedAt = Now();
return _running;
}
llarp_time_t
Router::Uptime() const
{
const llarp_time_t _now = Now();
if(_startedAt && _now > _startedAt)
return _now - _startedAt;
return 0;
}
static void
RouterAfterStopLinks(void *u, uint64_t, uint64_t)
{
Router *self = static_cast< Router * >(u);
self->Close();
}
static void
RouterAfterStopIssued(void *u, uint64_t, uint64_t)
{
Router *self = static_cast< Router * >(u);
self->StopLinks();
self->_logic->call_later({200, self, &RouterAfterStopLinks});
}
void
Router::StopLinks()
{
LogInfo("stopping links");
2019-01-05 13:45:05 +00:00
for(const auto &link : outboundLinks)
link->Stop();
for(const auto &link : inboundLinks)
link->Stop();
2018-06-10 14:05:48 +00:00
}
bool
Router::ShouldCreateDefaultHiddenService()
{
std::string defaultIfAddr = "auto";
std::string defaultIfName = "auto";
std::string enabledOption = "auto";
auto itr = netConfig.find("defaultIfAddr");
if(itr != netConfig.end())
{
defaultIfAddr = itr->second;
}
itr = netConfig.find("defaultIfName");
if(itr != netConfig.end())
{
defaultIfName = itr->second;
}
itr = netConfig.find("enabled");
if(itr != netConfig.end())
{
enabledOption = itr->second;
}
LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr,
" Enabled: ", enabledOption);
// LogInfo("IfAddr: ", itr->second);
// LogInfo("IfName: ", itr->second);
if(enabledOption == "false")
{
LogInfo("Disabling default hidden service");
return false;
}
else if(enabledOption == "auto")
{
// auto detect if we have any pre-defined endpoints
// no if we have a endpoints
2019-02-22 16:21:05 +00:00
if(hiddenServiceContext().hasEndpoints())
{
LogInfo("Auto mode detected and we have endpoints");
netConfig.emplace("enabled", "false");
return false;
}
netConfig.emplace("enabled", "true");
}
// ev.cpp llarp_ev_add_tun now handles this
/*
// so basically enabled at this point
if(defaultIfName == "auto")
{
// we don't have any endpoints, auto configure settings
// set a default IP range
defaultIfAddr = findFreePrivateRange();
if(defaultIfAddr == "")
{
LogError(
"Could not find any free lokitun interface names, can't
auto set up " "default HS context for client"); defaultIfAddr = "no";
netConfig.emplace("defaultIfAddr", defaultIfAddr);
return false;
}
netConfig.emplace("defaultIfAddr", defaultIfAddr);
}
if(defaultIfName == "auto")
{
// pick an ifName
defaultIfName = findFreeLokiTunIfName();
if(defaultIfName == "")
{
LogError(
"Could not find any free private ip ranges, can't auto
set up " "default HS context for client"); defaultIfName = "no";
netConfig.emplace("defaultIfName", defaultIfName);
return false;
}
netConfig.emplace("defaultIfName", defaultIfName);
}
*/
return true;
}
void
Router::Stop()
2018-11-26 13:29:45 +00:00
{
if(!_running)
return;
if(_stopping)
return;
_stopping.store(true);
LogInfo("stopping router");
2019-02-22 16:21:05 +00:00
hiddenServiceContext().StopAll();
_exitContext.Stop();
if(rpcServer)
rpcServer->Stop();
_logic->call_later({200, this, &RouterAfterStopIssued});
2018-11-26 13:29:45 +00:00
}
2018-08-02 23:30:34 +00:00
bool
Router::HasSessionTo(const RouterID &remote) const
{
for(const auto &link : outboundLinks)
2019-06-05 20:19:53 +00:00
if(link->HasSessionTo(remote))
return true;
for(const auto &link : inboundLinks)
2019-06-05 20:19:53 +00:00
if(link->HasSessionTo(remote))
return true;
return false;
}
void
Router::ConnectToRandomRouters(int want)
{
int wanted = want;
Router *self = this;
self->nodedb()->visit([self, &want](const RouterContact &other) -> bool {
// check if we really want to
if(other.ExpiresSoon(self->Now(), 30000))
return want > 0;
if(!self->ConnectionToRouterAllowed(other.pubkey))
return want > 0;
if(randint() % 2 == 0
&& !(self->HasSessionTo(other.pubkey)
|| self->HasPendingConnectJob(other.pubkey)))
{
2019-05-11 15:10:17 +00:00
if(self->TryConnectAsync(other, 5))
--want;
}
return want > 0;
});
2019-05-08 12:17:48 +00:00
LogInfo("connecting to ", abs(want - wanted), " out of ", wanted,
" random routers");
}
bool
Router::InitServiceNode()
2018-11-26 13:29:45 +00:00
{
LogInfo("accepting transit traffic");
paths.AllowTransit();
llarp_dht_allow_transit(dht());
return _exitContext.AddExitEndpoint("default-connectivity", netConfig);
2018-11-26 13:29:45 +00:00
}
2018-06-14 17:35:12 +00:00
/// validate a new configuration against an already made and running
/// router
struct RouterConfigValidator
{
void
ValidateEntry(const char *section, const char *key, const char *val)
{
if(valid)
{
if(!OnEntry(section, key, val))
{
LogError("invalid entry in section [", section, "]: '", key, "'='",
val, "'");
valid = false;
}
}
}
const Router *router;
Config *config;
bool valid;
RouterConfigValidator(const Router *r, Config *conf)
: router(r), config(conf), valid(true)
{
}
/// checks the (section, key, value) config tuple
/// return false if that entry conflicts
/// with existing configuration in router
bool
OnEntry(const char *, const char *, const char *) const
{
// TODO: implement me
return true;
}
/// do validation
/// return true if this config is valid
/// return false if this config is not valid
bool
Validate()
{
config->visit(util::memFn(&RouterConfigValidator::ValidateEntry, this));
return valid;
}
};
bool
Router::ValidateConfig(Config *conf) const
{
RouterConfigValidator validator(this, conf);
return validator.Validate();
}
bool
Router::Reconfigure(Config *)
2018-06-18 22:05:02 +00:00
{
// TODO: implement me
return true;
2018-05-20 17:45:47 +00:00
}
2018-01-08 13:49:05 +00:00
bool
2019-01-05 13:45:05 +00:00
Router::InitOutboundLinks()
2018-06-18 22:03:50 +00:00
{
2019-01-05 13:45:05 +00:00
if(outboundLinks.size() > 0)
return true;
static std::list< std::function< LinkLayer_ptr(Router *) > > linkFactories =
{utp::NewServerFromRouter, iwp::NewServerFromRouter};
2019-01-05 13:45:05 +00:00
for(const auto &factory : linkFactories)
{
2019-05-15 15:54:26 +00:00
auto link = factory(this);
2019-01-05 13:48:00 +00:00
if(!link)
continue;
2019-01-05 13:45:05 +00:00
if(!link->EnsureKeys(transport_keyfile.string().c_str()))
{
LogError("failed to load ", transport_keyfile);
2019-01-05 13:45:05 +00:00
continue;
}
2018-06-20 17:45:44 +00:00
2019-01-05 13:45:05 +00:00
auto afs = {AF_INET, AF_INET6};
2018-06-20 17:45:44 +00:00
2019-01-05 13:45:05 +00:00
for(auto af : afs)
{
if(!link->Configure(netloop(), "*", af, m_OutboundPort))
2019-01-05 13:45:05 +00:00
continue;
2019-04-25 12:57:26 +00:00
AddLink(std::move(link), false);
2019-01-10 12:38:06 +00:00
break;
}
}
2019-01-05 13:45:05 +00:00
return outboundLinks.size() > 0;
}
2018-06-21 13:33:42 +00:00
bool
Router::CreateDefaultHiddenService()
{
// fallback defaults
// To NeuroScr: why run findFree* here instead of in tun.cpp?
2019-01-29 11:23:10 +00:00
// I think it should be in tun.cpp, better to closer to time of usage
// that way new tun may have grab a range we may have also grabbed here
static const std::unordered_map< std::string,
std::function< std::string(void) > >
netConfigDefaults = {
2019-01-29 11:23:10 +00:00
{"ifname", []() -> std::string { return "auto"; }},
{"ifaddr", []() -> std::string { return "auto"; }},
{"local-dns", []() -> std::string { return "127.0.0.1:53"; }}};
// populate with fallback defaults if values not present
auto itr = netConfigDefaults.begin();
while(itr != netConfigDefaults.end())
{
2018-12-19 17:48:29 +00:00
auto found = netConfig.find(itr->first);
if(found == netConfig.end() || found->second.empty())
{
netConfig.emplace(itr->first, itr->second());
}
++itr;
}
// add endpoint
2019-02-22 16:21:05 +00:00
return hiddenServiceContext().AddDefaultEndpoint(netConfig);
}
2018-08-02 23:30:34 +00:00
bool
Router::HasPendingConnectJob(const RouterID &remote)
{
return pendingEstablishJobs.find(remote) != pendingEstablishJobs.end();
}
2018-01-19 16:51:27 +00:00
bool
Router::LoadHiddenServiceConfig(const char *fname)
2018-07-09 17:32:11 +00:00
{
LogDebug("opening hidden service config ", fname);
service::Config conf;
if(!conf.Load(fname))
2018-07-09 17:32:11 +00:00
return false;
for(const auto &config : conf.services)
{
service::Config::section_t filteredConfig;
mergeHiddenServiceConfig(config.second, filteredConfig.second);
filteredConfig.first = config.first;
2019-02-22 16:21:05 +00:00
if(!hiddenServiceContext().AddEndpoint(filteredConfig))
return false;
}
return true;
2018-07-09 17:32:11 +00:00
}
2018-02-01 13:21:00 +00:00
} // namespace llarp