2019-01-14 21:46:07 +00:00
|
|
|
#include <router/router.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
|
|
|
#include <constants/proto.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <dht/context.hpp>
|
2019-01-19 18:16:40 +00:00
|
|
|
#include <dht/node.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <link/iwp.hpp>
|
2018-12-12 01:32:10 +00:00
|
|
|
#include <link/server.hpp>
|
|
|
|
#include <link/utp.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <messages/link_message.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <rpc/rpc.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/encode.hpp>
|
|
|
|
#include <util/logger.hpp>
|
|
|
|
#include <util/str.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>
|
2019-01-02 17:41:13 +00:00
|
|
|
#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
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
void
|
|
|
|
router_iter_config(llarp_config_iterator *iter, const char *section,
|
|
|
|
const char *key, const char *val);
|
2018-05-30 20:56:47 +00:00
|
|
|
|
|
|
|
struct async_verify_context
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
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
|
2018-09-02 18:25:42 +00:00
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
llarp::RouterContact rc;
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp::ILinkLayer *link;
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router *router;
|
2018-09-02 18:25:42 +00:00
|
|
|
uint16_t triesLeft;
|
|
|
|
TryConnectJob(const llarp::RouterContact &remote, llarp::ILinkLayer *l,
|
2018-12-10 16:26:46 +00:00
|
|
|
uint16_t tries, llarp::Router *r)
|
2018-09-06 11:46:19 +00:00
|
|
|
: rc(remote), link(l), router(r), triesLeft(tries)
|
2018-09-02 18:25:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:56:03 +00:00
|
|
|
~TryConnectJob()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
void
|
|
|
|
Failed()
|
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
llarp::LogInfo("session to ", llarp::RouterID(rc.pubkey), " closed");
|
2018-09-02 18:25:42 +00:00
|
|
|
link->CloseSessionTo(rc.pubkey);
|
2019-01-17 13:56:03 +00:00
|
|
|
// delete this
|
|
|
|
router->pendingEstablishJobs.erase(rc.pubkey);
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Success()
|
|
|
|
{
|
2018-11-20 15:46:26 +00:00
|
|
|
router->FlushOutboundFor(rc.pubkey, link);
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AttemptTimedout()
|
|
|
|
{
|
2018-09-15 11:37:46 +00:00
|
|
|
router->routerProfiling.MarkTimeout(rc.pubkey);
|
|
|
|
if(ShouldRetry())
|
|
|
|
{
|
|
|
|
Attempt();
|
|
|
|
return;
|
|
|
|
}
|
2018-11-21 14:10:02 +00:00
|
|
|
if(!router->IsServiceNode())
|
|
|
|
{
|
|
|
|
if(router->routerProfiling.IsBad(rc.pubkey))
|
2019-01-29 02:16:31 +00:00
|
|
|
{
|
|
|
|
router->nodedb()->Remove(rc.pubkey);
|
|
|
|
}
|
2018-11-21 14:10:02 +00:00
|
|
|
}
|
2018-09-15 11:37:46 +00:00
|
|
|
// delete this
|
2018-09-06 20:31:58 +00:00
|
|
|
router->pendingEstablishJobs.erase(rc.pubkey);
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Attempt()
|
|
|
|
{
|
2018-09-15 11:37:46 +00:00
|
|
|
--triesLeft;
|
2018-11-21 14:56:12 +00:00
|
|
|
if(!link->TryEstablishTo(rc))
|
2019-01-07 16:35:25 +00:00
|
|
|
{
|
2018-11-21 14:56:12 +00:00
|
|
|
llarp::LogError("did not attempt connection to ", rc.pubkey,
|
|
|
|
" and it has ", rc.addrs.size(), " advertised addresses");
|
2019-01-07 16:35:25 +00:00
|
|
|
// delete this
|
|
|
|
router->pendingEstablishJobs.erase(rc.pubkey);
|
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldRetry() const
|
|
|
|
{
|
|
|
|
return triesLeft > 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_try_connecting(void *u)
|
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
TryConnectJob *j = static_cast< TryConnectJob * >(u);
|
2019-01-17 13:56:03 +00:00
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
j->Attempt();
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:18 +00:00
|
|
|
bool
|
|
|
|
llarp_loadServiceNodeIdentityKey(llarp::Crypto *crypto, const fs::path &fpath,
|
|
|
|
llarp::SecretKey &secret)
|
|
|
|
{
|
|
|
|
std::string path = fpath.string();
|
|
|
|
llarp::IdentitySecret ident;
|
|
|
|
|
|
|
|
if(!ident.LoadFromFile(path.c_str()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return crypto->seed_to_secretkey(secret, ident);
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
2019-01-10 12:38:06 +00:00
|
|
|
llarp_findOrCreateIdentity(llarp::Crypto *crypto, const fs::path &path,
|
2019-01-02 01:04:03 +00:00
|
|
|
llarp::SecretKey &secretkey)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
2019-01-10 22:35:38 +00:00
|
|
|
std::string fpath = path.string();
|
|
|
|
llarp::LogDebug("find or create ", fpath);
|
2018-12-10 16:26:46 +00:00
|
|
|
std::error_code ec;
|
|
|
|
if(!fs::exists(path, ec))
|
2018-10-03 11:01:42 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogInfo("generating new identity key");
|
|
|
|
crypto->identity_keygen(secretkey);
|
2019-01-10 22:35:38 +00:00
|
|
|
if(!secretkey.SaveToFile(fpath.c_str()))
|
|
|
|
return false;
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2019-01-10 22:35:38 +00:00
|
|
|
return secretkey.LoadFromFile(fpath.c_str());
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
// C++ ...
|
2018-05-26 18:31:45 +00:00
|
|
|
bool
|
2019-01-03 23:10:32 +00:00
|
|
|
llarp_findOrCreateEncryption(llarp::Crypto *crypto, const fs::path &path,
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::SecretKey &encryption)
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2019-01-10 22:35:38 +00:00
|
|
|
std::string fpath = path.string();
|
2019-01-11 01:42:02 +00:00
|
|
|
llarp::LogDebug("find or create ", fpath);
|
2018-12-10 16:26:46 +00:00
|
|
|
std::error_code ec;
|
|
|
|
if(!fs::exists(path, ec))
|
2018-09-30 12:01:58 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogInfo("generating new encryption key");
|
|
|
|
crypto->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());
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
AbstractRouter::~AbstractRouter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-05 13:45:05 +00:00
|
|
|
bool
|
|
|
|
Router::TryConnectAsync(llarp::RouterContact remote, uint16_t numretries)
|
|
|
|
{
|
|
|
|
// do we already have a pending job for this remote?
|
|
|
|
if(HasPendingConnectJob(remote.pubkey))
|
|
|
|
{
|
|
|
|
llarp::LogDebug("We have pending connect jobs to ", remote.pubkey);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &link : outboundLinks)
|
|
|
|
{
|
|
|
|
if(!link->IsCompatable(remote))
|
|
|
|
continue;
|
2019-01-17 14:02:50 +00:00
|
|
|
std::unique_ptr< TryConnectJob > j = std::make_unique< TryConnectJob >(
|
|
|
|
remote, link.get(), numretries, this);
|
|
|
|
auto itr = pendingEstablishJobs.emplace(remote.pubkey, std::move(j));
|
2019-01-17 13:56:03 +00:00
|
|
|
if(itr.second)
|
|
|
|
{
|
|
|
|
// only try establishing if we inserted a new element
|
|
|
|
TryConnectJob *job = itr.first->second.get();
|
|
|
|
// try establishing async
|
2019-01-29 02:16:31 +00:00
|
|
|
_logic->queue_job({job, &on_try_connecting});
|
2019-01-17 13:56:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
2018-12-17 22:43:16 +00:00
|
|
|
Router::OnSessionEstablished(llarp::RouterContact rc)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2018-12-17 22:43:16 +00:00
|
|
|
async_verify_RC(rc, nullptr);
|
2019-01-29 11:49:02 +00:00
|
|
|
llarp::LogInfo("session with ", rc.pubkey, " established");
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-11-19 12:25:03 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
Router::Router(struct llarp_threadpool *_tp, struct llarp_ev_loop *_netloop,
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp::Logic *l)
|
2018-12-10 16:26:46 +00:00
|
|
|
: ready(false)
|
|
|
|
, netloop(_netloop)
|
|
|
|
, tp(_tp)
|
2019-01-29 02:16:31 +00:00
|
|
|
, _logic(l)
|
|
|
|
, _crypto(std::make_unique< sodium::CryptoLibSodium >())
|
2018-12-10 16:26:46 +00:00
|
|
|
, paths(this)
|
|
|
|
, exitContext(this)
|
2019-01-29 02:16:31 +00:00
|
|
|
, _dht(llarp_dht_context_new(this))
|
2018-12-10 16:26:46 +00:00
|
|
|
, inbound_link_msg_parser(this)
|
|
|
|
, hiddenServiceContext(this)
|
2018-06-20 17:45:44 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
// set rational defaults
|
|
|
|
this->ip4addr.sin_family = AF_INET;
|
|
|
|
this->ip4addr.sin_port = htons(1090);
|
|
|
|
|
|
|
|
#ifdef TESTNET
|
|
|
|
disk = tp;
|
|
|
|
#else
|
|
|
|
disk = llarp_init_threadpool(1, "llarp-diskio");
|
|
|
|
#endif
|
2018-12-24 16:09:05 +00:00
|
|
|
_stopping.store(false);
|
|
|
|
_running.store(false);
|
2018-06-20 17:45:44 +00:00
|
|
|
}
|
2018-09-18 20:56:22 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
Router::~Router()
|
2018-09-19 13:27:15 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp_dht_context_free(_dht);
|
2018-09-19 13:27:15 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2019-02-08 19:43:25 +00:00
|
|
|
void
|
|
|
|
Router::ExtractStatus(util::StatusObject &obj) const
|
|
|
|
{
|
|
|
|
util::StatusObject dhtObj, exitObj, hsObj;
|
|
|
|
_dht->impl.ExtractStatus(dhtObj);
|
|
|
|
obj.PutObject("dht", dhtObj);
|
|
|
|
hiddenServiceContext.ExtractStatus(hsObj);
|
|
|
|
obj.PutObject("services", hsObj);
|
|
|
|
exitContext.ExtractStatus(exitObj);
|
|
|
|
obj.PutObject("exit", exitObj);
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::HandleRecvLinkMessageBuffer(llarp::ILinkSession *session,
|
2019-02-01 01:58:06 +00:00
|
|
|
const llarp_buffer_t &buf)
|
2018-09-18 20:56:22 +00:00
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
if(_stopping)
|
|
|
|
return true;
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!session)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("no link session");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return inbound_link_msg_parser.ProcessFrom(session, buf);
|
2018-10-07 15:29:36 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Router::PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until)
|
2018-10-07 15:29:36 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("persist session to ", remote, " until ", until);
|
|
|
|
m_PersistingSessions[remote] =
|
|
|
|
std::max(until, m_PersistingSessions[remote]);
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
2018-06-26 10:08:51 +00:00
|
|
|
|
2018-12-13 00:03:19 +00:00
|
|
|
bool
|
|
|
|
Router::GetRandomGoodRouter(RouterID &router)
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
auto sz = nodedb()->entries.size();
|
2018-12-13 00:03:19 +00:00
|
|
|
if(sz == 0)
|
|
|
|
return false;
|
2019-01-29 02:16:31 +00:00
|
|
|
auto itr = nodedb()->entries.begin();
|
2018-12-13 00:03:19 +00:00
|
|
|
if(sz > 1)
|
|
|
|
std::advance(itr, randint() % sz);
|
|
|
|
router = itr->first;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
constexpr size_t MaxPendingSendQueueSize = 8;
|
2018-05-26 18:31:45 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::SendToOrQueue(const llarp::RouterID &remote,
|
|
|
|
const llarp::ILinkMessage *msg)
|
2018-06-14 15:10:31 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
2018-11-21 17:46:33 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
if(link->HasSessionTo(remote))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
// no link available
|
|
|
|
|
|
|
|
// this will create an entry in the obmq if it's not already there
|
|
|
|
auto itr = outboundMessageQueue.find(remote);
|
|
|
|
if(itr == outboundMessageQueue.end())
|
|
|
|
{
|
|
|
|
outboundMessageQueue.insert(std::make_pair(remote, MessageQueue()));
|
|
|
|
}
|
|
|
|
// encode
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(linkmsg_buffer);
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!msg->BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// queue buffer
|
|
|
|
auto &q = outboundMessageQueue[remote];
|
|
|
|
|
|
|
|
if(q.size() < MaxPendingSendQueueSize)
|
|
|
|
{
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
q.emplace(buf.sz);
|
|
|
|
memcpy(q.back().data(), buf.base, buf.sz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("tried to queue a message to ", remote,
|
|
|
|
" but the queue is full so we drop it like it's hawt");
|
|
|
|
}
|
|
|
|
llarp::RouterContact remoteRC;
|
|
|
|
// we don't have an open session to that router right now
|
2019-01-29 02:16:31 +00:00
|
|
|
if(nodedb()->Get(remote, remoteRC))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
// try connecting directly as the rc is loaded from disk
|
2019-01-05 13:45:05 +00:00
|
|
|
TryConnectAsync(remoteRC, 10);
|
2018-12-10 16:26:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't have the RC locally so do a dht lookup
|
2019-01-29 02:16:31 +00:00
|
|
|
_dht->impl.LookupRouter(remote,
|
|
|
|
std::bind(&Router::HandleDHTLookupForSendTo, this,
|
|
|
|
remote, std::placeholders::_1));
|
2018-12-10 16:26:46 +00:00
|
|
|
return true;
|
2018-06-14 15:10:31 +00:00
|
|
|
}
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::HandleDHTLookupForSendTo(
|
|
|
|
llarp::RouterID remote,
|
|
|
|
const std::vector< llarp::RouterContact > &results)
|
2018-10-25 18:18:12 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(results.size())
|
|
|
|
{
|
|
|
|
if(whitelistRouters
|
|
|
|
&& lokinetRouters.find(remote) == lokinetRouters.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-01-29 02:16:31 +00:00
|
|
|
if(results[0].Verify(crypto(), Now()))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
nodedb()->Insert(results[0]);
|
2019-01-05 13:45:05 +00:00
|
|
|
TryConnectAsync(results[0], 10);
|
2018-12-19 16:17:41 +00:00
|
|
|
return;
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
DiscardOutboundFor(remote);
|
2018-10-25 18:18:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::ForEachPeer(
|
|
|
|
std::function< void(const llarp::ILinkSession *, bool) > visit) const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : outboundLinks)
|
|
|
|
{
|
|
|
|
link->ForEachSession(
|
|
|
|
[visit](const llarp::ILinkSession *peer) { visit(peer, true); });
|
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
|
|
|
{
|
|
|
|
link->ForEachSession(
|
|
|
|
[visit](const llarp::ILinkSession *peer) { visit(peer, false); });
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
void
|
|
|
|
Router::ForEachPeer(std::function< void(llarp::ILinkSession *) > visit)
|
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
|
|
|
{
|
|
|
|
link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); });
|
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
|
|
|
{
|
|
|
|
link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::try_connect(fs::path rcfile)
|
2018-05-16 18:13:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::RouterContact remote;
|
|
|
|
if(!remote.Read(rcfile.string().c_str()))
|
2018-07-25 01:24:37 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogError("failure to decode or verify of remote RC");
|
|
|
|
return;
|
2018-07-25 01:24:37 +00:00
|
|
|
}
|
2019-01-29 02:16:31 +00:00
|
|
|
if(remote.Verify(crypto(), Now()))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("verified signature");
|
|
|
|
// store into filesystem
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!nodedb()->Insert(remote))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to store");
|
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
if(!TryConnectAsync(remote, 10))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
// or error?
|
|
|
|
llarp::LogWarn("session already made");
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
else
|
|
|
|
llarp::LogError(rcfile, " contains invalid RC");
|
2018-05-16 18:13:18 +00:00
|
|
|
}
|
2018-05-20 17:45:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::EnsureIdentity()
|
|
|
|
{
|
|
|
|
if(!EnsureEncryptionKey())
|
|
|
|
return false;
|
2019-01-21 17:06:31 +00:00
|
|
|
if(usingSNSeed)
|
2019-01-29 02:16:31 +00:00
|
|
|
return llarp_loadServiceNodeIdentityKey(crypto(), ident_keyfile,
|
2019-01-26 15:40:58 +00:00
|
|
|
identity);
|
2019-01-21 17:06:31 +00:00
|
|
|
else
|
2019-01-29 02:16:31 +00:00
|
|
|
return llarp_findOrCreateIdentity(crypto(), ident_keyfile, identity);
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-04-05 14:23:14 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::EnsureEncryptionKey()
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
return llarp_findOrCreateEncryption(crypto(), encryption_keyfile,
|
2019-01-16 21:16:23 +00:00
|
|
|
this->encryption);
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link)
|
2018-05-20 17:45:47 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
inboundLinks.insert(std::move(link));
|
2018-05-20 17:45:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::Configure(struct llarp_config *conf)
|
|
|
|
{
|
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = this;
|
|
|
|
iter.visit = llarp::router_iter_config;
|
|
|
|
llarp_config_iter(conf, &iter);
|
2019-01-05 13:45:05 +00:00
|
|
|
if(!InitOutboundLinks())
|
2018-12-10 16:26:46 +00:00
|
|
|
return false;
|
|
|
|
if(!Ready())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return EnsureIdentity();
|
|
|
|
}
|
2018-11-21 14:10:02 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::Ready()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
return outboundLinks.size() > 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Router::SaveRC()
|
2018-08-18 14:01:21 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("verify RC signature");
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!_rc.Verify(crypto(), Now()))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
rc().Dump< MAX_RC_SIZE >();
|
|
|
|
llarp::LogError("RC is invalid, not saving");
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-09 00:11:33 +00:00
|
|
|
std::string fname = our_rc_file.string();
|
|
|
|
return _rc.Write(fname.c_str());
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
2018-05-29 13:40:26 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::IsServiceNode() const
|
|
|
|
{
|
|
|
|
return inboundLinks.size() > 0;
|
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::Close()
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2018-12-24 16:31:58 +00:00
|
|
|
llarp::LogInfo("closing router");
|
2018-12-24 16:09:05 +00:00
|
|
|
llarp_ev_loop_stop(netloop);
|
2018-12-10 16:26:46 +00:00
|
|
|
inboundLinks.clear();
|
2019-01-05 13:45:05 +00:00
|
|
|
outboundLinks.clear();
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Router::on_verify_client_rc(llarp_async_verify_rc *job)
|
|
|
|
{
|
|
|
|
llarp::async_verify_context *ctx =
|
|
|
|
static_cast< llarp::async_verify_context * >(job->user);
|
|
|
|
auto router = ctx->router;
|
|
|
|
llarp::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
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::on_verify_server_rc(llarp_async_verify_rc *job)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::async_verify_context *ctx =
|
|
|
|
static_cast< llarp::async_verify_context * >(job->user);
|
|
|
|
auto router = ctx->router;
|
|
|
|
llarp::PubKey pk(job->rc.pubkey);
|
|
|
|
if(!job->valid)
|
|
|
|
{
|
|
|
|
if(ctx->establish_job)
|
|
|
|
{
|
|
|
|
// was an outbound attempt
|
|
|
|
ctx->establish_job->Failed();
|
|
|
|
}
|
|
|
|
delete ctx;
|
|
|
|
router->DiscardOutboundFor(pk);
|
|
|
|
router->pendingVerifyRC.erase(pk);
|
2018-09-06 13:16:24 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we're valid, which means it's already been committed to the nodedb
|
2018-09-06 13:16:24 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("rc verified and saved to nodedb");
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
if(router->validRouters.count(pk))
|
|
|
|
{
|
|
|
|
router->validRouters.erase(pk);
|
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::RouterContact rc = job->rc;
|
2018-09-13 16:58:25 +00:00
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
router->validRouters.emplace(pk, rc);
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
// track valid router in dht
|
2019-01-29 02:16:31 +00:00
|
|
|
router->dht()->impl.nodes->PutNode(rc);
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
// mark success in profile
|
|
|
|
router->routerProfiling.MarkSuccess(pk);
|
2018-11-28 14:58:38 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
// this was an outbound establish job
|
|
|
|
if(ctx->establish_job)
|
|
|
|
{
|
|
|
|
ctx->establish_job->Success();
|
|
|
|
}
|
2018-11-28 15:18:18 +00:00
|
|
|
else
|
2018-12-10 16:26:46 +00:00
|
|
|
router->FlushOutboundFor(pk, router->GetLinkWithSessionByPubkey(pk));
|
|
|
|
delete ctx;
|
|
|
|
router->pendingVerifyRC.erase(pk);
|
2018-11-28 15:18:18 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Router::handle_router_ticker(void *user, uint64_t orig, uint64_t left)
|
2018-11-28 15:18:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +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
|
|
|
}
|
|
|
|
|
2018-12-27 14:32:37 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
Router::ParseRoutingMessageBuffer(const llarp_buffer_t &buf,
|
2018-12-28 15:34:41 +00:00
|
|
|
routing::IMessageHandler *h, PathID_t rxid)
|
2018-12-27 14:32:37 +00:00
|
|
|
{
|
|
|
|
return inbound_routing_msg_parser.ParseMessageBuffer(buf, h, rxid, this);
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::ConnectionToRouterAllowed(const llarp::RouterID &router) const
|
2018-11-28 14:58:38 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(strictConnectPubkeys.size() && strictConnectPubkeys.count(router) == 0)
|
|
|
|
return false;
|
|
|
|
else if(IsServiceNode() && whitelistRouters)
|
|
|
|
return lokinetRouters.count(router) != 0;
|
|
|
|
else
|
|
|
|
return true;
|
2018-11-28 14:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::HandleDHTLookupForExplore(
|
|
|
|
llarp::RouterID remote,
|
|
|
|
const std::vector< llarp::RouterContact > &results)
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(results.size() == 0)
|
|
|
|
return;
|
|
|
|
for(const auto &rc : results)
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
if(rc.Verify(crypto(), Now()))
|
|
|
|
nodedb()->Insert(rc);
|
2018-12-10 16:26:46 +00:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(ConnectionToRouterAllowed(remote))
|
|
|
|
{
|
|
|
|
TryEstablishTo(remote);
|
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Router::TryEstablishTo(const llarp::RouterID &remote)
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!ConnectionToRouterAllowed(remote))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("not connecting to ", remote,
|
|
|
|
" as it's not permitted by config");
|
2018-10-07 15:29:36 +00:00
|
|
|
return;
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp::RouterContact rc;
|
2019-01-29 02:16:31 +00:00
|
|
|
if(nodedb()->Get(remote, rc))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
// try connecting async
|
2019-01-05 13:45:05 +00:00
|
|
|
TryConnectAsync(rc, 5);
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
else if(IsServiceNode() || !routerProfiling.IsBad(remote))
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
if(dht()->impl.HasRouterLookup(remote))
|
2018-12-10 16:26:46 +00:00
|
|
|
return;
|
|
|
|
llarp::LogInfo("looking up router ", remote);
|
|
|
|
// dht lookup as we don't know it
|
2019-01-29 02:16:31 +00:00
|
|
|
dht()->impl.LookupRouter(
|
2018-12-10 16:26:46 +00:00
|
|
|
remote,
|
|
|
|
std::bind(&Router::HandleDHTLookupForTryEstablishTo, this, remote,
|
|
|
|
std::placeholders::_1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("not connecting to ", remote, " as it's unreliable");
|
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Router::OnConnectTimeout(ILinkSession *session)
|
2018-11-20 12:13:05 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
auto itr = pendingEstablishJobs.find(session->GetPubKey());
|
2018-12-10 16:26:46 +00:00
|
|
|
if(itr != pendingEstablishJobs.end())
|
|
|
|
{
|
|
|
|
itr->second->AttemptTimedout();
|
|
|
|
}
|
2018-11-20 12:13:05 +00:00
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::HandleDHTLookupForTryEstablishTo(
|
|
|
|
llarp::RouterID remote,
|
|
|
|
const std::vector< llarp::RouterContact > &results)
|
2018-09-15 11:37:46 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(results.size() == 0)
|
|
|
|
{
|
|
|
|
if(!IsServiceNode())
|
|
|
|
routerProfiling.MarkTimeout(remote);
|
|
|
|
}
|
|
|
|
for(const auto &result : results)
|
|
|
|
{
|
|
|
|
if(whitelistRouters
|
|
|
|
&& lokinetRouters.find(result.pubkey) == lokinetRouters.end())
|
|
|
|
continue;
|
2019-01-29 02:16:31 +00:00
|
|
|
nodedb()->Insert(result);
|
2019-01-05 13:45:05 +00:00
|
|
|
TryConnectAsync(result, 10);
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-09-15 11:37:46 +00:00
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
size_t
|
|
|
|
Router::NumberOfConnectedRouters() const
|
2018-09-14 13:43:42 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
return validRouters.size();
|
2018-09-14 13:43:42 +00:00
|
|
|
}
|
2018-06-19 17:11:24 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
bool
|
|
|
|
Router::UpdateOurRC(bool rotateKeys)
|
|
|
|
{
|
|
|
|
llarp::SecretKey nextOnionKey;
|
|
|
|
llarp::RouterContact nextRC = _rc;
|
|
|
|
if(rotateKeys)
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
crypto()->encryption_keygen(nextOnionKey);
|
2019-01-29 13:20:27 +00:00
|
|
|
std::string f = encryption_keyfile.string();
|
|
|
|
if(nextOnionKey.SaveToFile(f.c_str()))
|
|
|
|
{
|
|
|
|
nextRC.enckey = llarp::seckey_topublic(nextOnionKey);
|
2019-02-04 23:18:43 +00:00
|
|
|
encryption = nextOnionKey;
|
2019-01-29 13:20:27 +00:00
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
nextRC.last_updated = Now();
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!nextRC.Sign(crypto(), identity))
|
2018-12-19 16:17:41 +00:00
|
|
|
return false;
|
|
|
|
_rc = nextRC;
|
2019-01-29 12:33:33 +00:00
|
|
|
// propagate RC by renegotiating sessions
|
|
|
|
ForEachPeer([](llarp::ILinkSession *s) {
|
|
|
|
if(s->RenegotiateSession())
|
|
|
|
llarp::LogInfo("renegotiated session");
|
|
|
|
else
|
|
|
|
llarp::LogWarn("failed to renegotiate session");
|
|
|
|
});
|
2019-02-04 23:18:43 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
// TODO: do this async
|
|
|
|
return SaveRC();
|
2018-12-19 17:48:29 +00:00
|
|
|
} // namespace llarp
|
2018-12-19 16:17:41 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Router::CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc)
|
|
|
|
{
|
|
|
|
// missmatch of identity ?
|
|
|
|
if(newrc.pubkey != oldrc.pubkey)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// store it in nodedb async
|
2019-01-29 02:16:31 +00:00
|
|
|
nodedb()->InsertAsync(newrc);
|
2018-12-19 17:48:29 +00:00
|
|
|
// update dht if required
|
2019-01-29 02:16:31 +00:00
|
|
|
if(dht()->impl.nodes->HasNode(dht::Key_t{newrc.pubkey}))
|
2018-12-19 17:48:29 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
dht()->impl.nodes->PutNode(newrc);
|
2018-12-19 17:48:29 +00:00
|
|
|
}
|
2018-12-19 16:17:41 +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
|
|
|
|
Router::ServiceNodeLookupRouterWhenExpired(RouterID router)
|
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
dht()->impl.LookupRouter(router,
|
|
|
|
std::bind(&Router::HandleDHTLookupForExplore, this,
|
|
|
|
router, std::placeholders::_1));
|
2018-12-19 17:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::Tick()
|
2018-06-19 17:11:24 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
// llarp::LogDebug("tick router");
|
|
|
|
auto now = llarp_ev_loop_time_now_ms(netloop);
|
2018-12-19 17:48:29 +00:00
|
|
|
|
|
|
|
if(_rc.ExpiresSoon(now, llarp::randint() % 10000))
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2018-12-19 17:48:29 +00:00
|
|
|
llarp::LogInfo("regenerating RC");
|
2019-01-29 12:33:33 +00:00
|
|
|
if(!UpdateOurRC(false))
|
2018-12-19 16:17:41 +00:00
|
|
|
llarp::LogError("Failed to update our RC");
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:48:29 +00:00
|
|
|
if(IsServiceNode())
|
|
|
|
{
|
|
|
|
// only do this as service node
|
|
|
|
// client endpoints do this on their own
|
2019-01-29 02:16:31 +00:00
|
|
|
nodedb()->visit([&](const RouterContact &rc) -> bool {
|
2018-12-19 17:48:29 +00:00
|
|
|
if(rc.ExpiresSoon(now, llarp::randint() % 10000))
|
|
|
|
ServiceNodeLookupRouterWhenExpired(rc.pubkey);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
paths.TickPaths(now);
|
|
|
|
paths.ExpirePaths(now);
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
auto itr = m_PersistingSessions.begin();
|
|
|
|
while(itr != m_PersistingSessions.end())
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
auto link = GetLinkWithSessionByPubkey(itr->first);
|
|
|
|
if(now < itr->second)
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(link)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("keepalive to ", itr->first);
|
|
|
|
link->KeepAliveSessionTo(itr->first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogDebug("establish to ", itr->first);
|
|
|
|
TryEstablishTo(itr->first);
|
|
|
|
}
|
|
|
|
++itr;
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogInfo("commit to ", itr->first, " expired");
|
|
|
|
itr = m_PersistingSessions.erase(itr);
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-11-20 12:13:05 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
size_t N = nodedb()->num_loaded();
|
2018-12-18 17:37:59 +00:00
|
|
|
if(N < minRequiredRouters)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2018-12-18 17:37:59 +00:00
|
|
|
llarp::LogInfo("We need at least ", minRequiredRouters,
|
2018-12-24 16:31:58 +00:00
|
|
|
" service nodes to build paths but we have ", N,
|
|
|
|
" in nodedb");
|
2018-12-18 17:37:59 +00:00
|
|
|
// TODO: only connect to random subset
|
|
|
|
if(bootstrapRCList.size())
|
2018-11-20 12:13:05 +00:00
|
|
|
{
|
2018-12-18 17:37:59 +00:00
|
|
|
for(const auto &rc : bootstrapRCList)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
TryConnectAsync(rc, 4);
|
2019-01-29 02:16:31 +00:00
|
|
|
dht()->impl.ExploreNetworkVia(dht::Key_t{rc.pubkey});
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-12-18 17:37:59 +00:00
|
|
|
else
|
|
|
|
llarp::LogError("we have no bootstrap nodes specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inboundLinks.size() == 0)
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
paths.BuildPaths(now);
|
|
|
|
hiddenServiceContext.Tick(now);
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
if(NumberOfConnectedRouters() < minConnectedRouters)
|
|
|
|
{
|
|
|
|
ConnectToRandomRouters(minConnectedRouters);
|
|
|
|
}
|
|
|
|
exitContext.Tick(now);
|
|
|
|
if(rpcCaller)
|
|
|
|
rpcCaller->Tick(now);
|
2018-08-22 16:19:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
Router::Sign(llarp::Signature &sig, const llarp_buffer_t &buf) const
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
return crypto()->sign(sig, identity, buf);
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg,
|
|
|
|
llarp::ILinkLayer *selected)
|
2018-08-22 16:19:51 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(linkmsg_buffer);
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
if(!msg->BEncode(&buf))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to encode outbound message, buffer size left: ",
|
|
|
|
llarp_buffer_size_left(buf));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// set size of message
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
llarp::LogDebug("send ", buf.sz, " bytes to ", remote);
|
|
|
|
if(selected)
|
|
|
|
{
|
|
|
|
if(selected->SendTo(remote, buf))
|
|
|
|
return;
|
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : outboundLinks)
|
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
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
|
|
|
{
|
|
|
|
if(link->SendTo(remote, buf))
|
|
|
|
return;
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
llarp::LogWarn("message to ", remote, " was dropped");
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::ScheduleTicker(uint64_t ms)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
ticker_job_id = _logic->call_later({ms, this, &handle_router_ticker});
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Router::SessionClosed(llarp::RouterID remote)
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
__llarp_dht_remove_peer(dht(), remote.data());
|
2018-12-10 16:26:46 +00:00
|
|
|
// remove from valid routers if it's a valid router
|
|
|
|
validRouters.erase(remote);
|
|
|
|
llarp::LogInfo("Session to ", remote, " fully closed");
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
llarp::ILinkLayer *
|
|
|
|
Router::GetLinkWithSessionByPubkey(const llarp::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();
|
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(link->HasSessionTo(pubkey))
|
|
|
|
return link.get();
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
return nullptr;
|
2018-06-06 12:46:26 +00:00
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::FlushOutboundFor(llarp::RouterID remote, llarp::ILinkLayer *chosen)
|
2018-07-03 13:33:37 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("Flush outbound for ", remote);
|
2018-07-03 13:33:37 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
auto itr = outboundMessageQueue.find(remote);
|
|
|
|
if(itr == outboundMessageQueue.end())
|
|
|
|
{
|
|
|
|
pendingEstablishJobs.erase(remote);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!chosen)
|
|
|
|
{
|
|
|
|
DiscardOutboundFor(remote);
|
|
|
|
pendingEstablishJobs.erase(remote);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while(itr->second.size())
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(itr->second.front());
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!chosen->SendTo(remote, buf))
|
2019-01-19 18:16:40 +00:00
|
|
|
llarp::LogWarn("failed to send outbound message to ", remote, " via ",
|
2018-12-10 16:26:46 +00:00
|
|
|
chosen->Name());
|
2018-11-22 15:53:11 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
itr->second.pop();
|
|
|
|
}
|
2018-11-22 15:02:51 +00:00
|
|
|
pendingEstablishJobs.erase(remote);
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::DiscardOutboundFor(const llarp::RouterID &remote)
|
2018-07-25 01:36:22 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
outboundMessageQueue.erase(remote);
|
2018-07-25 01:36:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::GetRandomConnectedRouter(llarp::RouterContact &result) const
|
2018-11-22 15:02:51 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
auto sz = validRouters.size();
|
|
|
|
if(sz)
|
2018-11-22 15:02:51 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
auto itr = validRouters.begin();
|
|
|
|
if(sz > 1)
|
2018-12-11 00:53:11 +00:00
|
|
|
std::advance(itr, llarp::randint() % sz);
|
2018-12-10 16:26:46 +00:00
|
|
|
result = itr->second;
|
|
|
|
return true;
|
2018-11-22 15:02:51 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
return false;
|
2018-11-22 15:02:51 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::async_verify_RC(const llarp::RouterContact &rc,
|
|
|
|
llarp::ILinkLayer *link)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(pendingVerifyRC.count(rc.pubkey))
|
|
|
|
return;
|
|
|
|
if(rc.IsPublicRouter() && whitelistRouters)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end())
|
|
|
|
{
|
|
|
|
llarp::LogInfo(rc.pubkey, " is NOT a valid service node, rejecting");
|
|
|
|
link->CloseSessionTo(rc.pubkey);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-09 12:06:30 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey];
|
|
|
|
llarp::async_verify_context *ctx = new llarp::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;
|
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
job->nodedb = _nodedb;
|
|
|
|
job->logic = _logic;
|
2018-12-10 16:26:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Router::Run(struct llarp_nodedb *nodedb)
|
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
if(_running || _stopping)
|
|
|
|
return false;
|
2019-01-29 02:16:31 +00:00
|
|
|
this->_nodedb = nodedb;
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
if(enableRPCServer)
|
2018-10-09 12:06:30 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
if(rpcBindAddr.empty())
|
|
|
|
{
|
|
|
|
rpcBindAddr = DefaultRPCBindAddr;
|
|
|
|
}
|
|
|
|
rpcServer = std::make_unique< llarp::rpc::Server >(this);
|
|
|
|
while(!rpcServer->Start(rpcBindAddr))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to bind jsonrpc to ", rpcBindAddr);
|
2018-11-08 12:31:50 +00:00
|
|
|
#if defined(ANDROID) || defined(RPI)
|
2018-12-10 16:26:46 +00:00
|
|
|
sleep(1);
|
2018-11-08 12:31:50 +00:00
|
|
|
#else
|
2018-12-10 16:26:46 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
2018-11-08 12:31:50 +00:00
|
|
|
#endif
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
|
|
|
llarp::LogInfo("Bound RPC server to ", rpcBindAddr);
|
2018-10-09 12:06:30 +00:00
|
|
|
}
|
2019-01-28 13:04:45 +00:00
|
|
|
if(whitelistRouters)
|
|
|
|
{
|
2019-02-01 01:58:06 +00:00
|
|
|
rpcCaller = std::make_unique< llarp::rpc::Caller >(this);
|
2019-01-28 13:04:45 +00:00
|
|
|
rpcCaller->SetBasicAuth(lokidRPCUser, lokidRPCPassword);
|
|
|
|
while(!rpcCaller->Start(lokidRPCAddr))
|
|
|
|
{
|
|
|
|
llarp::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
|
|
|
|
}
|
|
|
|
llarp::LogInfo("RPC Caller to ", lokidRPCAddr, " started");
|
|
|
|
}
|
2018-10-09 12:06:30 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp_threadpool_start(tp);
|
|
|
|
llarp_threadpool_start(disk);
|
2018-11-19 16:40:49 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
routerProfiling.Load(routerProfilesFile.c_str());
|
2018-10-12 12:05:03 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Addr publicAddr(this->addrInfo);
|
2018-10-03 11:01:42 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
if(this->publicOverride)
|
2018-07-30 05:58:20 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("public address:port ", publicAddr);
|
2018-07-30 05:58:20 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
llarp::LogInfo("You have ", inboundLinks.size(), " inbound links");
|
2019-01-05 13:45:05 +00:00
|
|
|
|
|
|
|
llarp::AddressInfo ai;
|
2018-12-10 16:26:46 +00:00
|
|
|
for(const auto &link : inboundLinks)
|
2018-06-28 11:29:10 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
if(link->GetOurAddressInfo(ai))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
// override ip and port
|
|
|
|
if(this->publicOverride)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
ai.ip = *publicAddr.addr6();
|
|
|
|
ai.port = publicAddr.port();
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
if(llarp::IsBogon(ai.ip))
|
|
|
|
continue;
|
|
|
|
_rc.addrs.push_back(ai);
|
2018-06-28 11:29:10 +00:00
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
// set public encryption key
|
|
|
|
_rc.enckey = llarp::seckey_topublic(encryption);
|
|
|
|
// set public signing key
|
|
|
|
_rc.pubkey = llarp::seckey_topublic(identity);
|
|
|
|
if(ExitEnabled())
|
2018-09-02 18:25:42 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::nuint32_t a = publicAddr.xtonl();
|
2019-01-28 13:52:18 +00:00
|
|
|
_rc.exits.emplace_back(_rc.pubkey, a);
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogInfo(
|
2018-12-19 16:17:41 +00:00
|
|
|
"Neato tehl33toh, You are a freaking exit relay. w00t!!!!! your "
|
|
|
|
"exit "
|
2018-12-10 16:26:46 +00:00
|
|
|
"is advertised as exiting at ",
|
|
|
|
a);
|
|
|
|
}
|
|
|
|
llarp::LogInfo("Signing rc...");
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!_rc.Sign(crypto(), identity))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to sign rc");
|
|
|
|
return false;
|
2018-06-28 11:29:10 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!SaveRC())
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to save RC");
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
llarp::LogInfo("have ", nodedb->num_loaded(), " routers");
|
2018-09-10 13:43:36 +00:00
|
|
|
|
2019-01-10 22:35:38 +00:00
|
|
|
llarp::LogInfo("starting outbound ", outboundLinks.size(), " links");
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : outboundLinks)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!link->Start(_logic))
|
2019-01-05 13:45:05 +00:00
|
|
|
{
|
2019-01-11 01:42:02 +00:00
|
|
|
llarp::LogWarn("outbound link '", link->Name(), "' failed to start");
|
2019-01-05 13:45:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-06-07 16:22:49 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
int IBLinksStarted = 0;
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
// start links
|
|
|
|
for(const auto &link : inboundLinks)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2019-01-29 02:16:31 +00:00
|
|
|
if(link->Start(_logic))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("Link ", link->Name(), " started");
|
|
|
|
IBLinksStarted++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
llarp::LogWarn("Link ", link->Name(), " failed to start");
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
if(IBLinksStarted > 0)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
// initialize as service node
|
|
|
|
if(!InitServiceNode())
|
|
|
|
{
|
|
|
|
llarp::LogError("Failed to initialize service node");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::RouterID us = pubkey();
|
|
|
|
llarp::LogInfo("initalized service node: ", us);
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
else
|
2018-09-18 20:56:22 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
// we are a client
|
|
|
|
// regenerate keys and resign rc before everything else
|
2019-01-29 02:16:31 +00:00
|
|
|
crypto()->identity_keygen(identity);
|
|
|
|
crypto()->encryption_keygen(encryption);
|
2018-12-10 16:26:46 +00:00
|
|
|
_rc.pubkey = llarp::seckey_topublic(identity);
|
|
|
|
_rc.enckey = llarp::seckey_topublic(encryption);
|
2019-01-29 02:16:31 +00:00
|
|
|
if(!_rc.Sign(crypto(), identity))
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to regenerate keys and sign RC");
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-16 21:16:23 +00:00
|
|
|
|
|
|
|
// don't create default if we already have some defined
|
|
|
|
if(this->ShouldCreateDefaultHiddenService())
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-16 21:16:23 +00:00
|
|
|
// generate default hidden service
|
|
|
|
llarp::LogInfo("setting up default network endpoint");
|
|
|
|
if(!CreateDefaultHiddenService())
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to set up default network endpoint");
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-09-18 20:56:22 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
llarp::LogInfo("starting hidden service context...");
|
|
|
|
if(!hiddenServiceContext.StartAll())
|
2018-10-03 11:01:42 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogError("Failed to start hidden service context");
|
2018-11-26 22:46:22 +00:00
|
|
|
return false;
|
2018-10-03 11:01:42 +00:00
|
|
|
}
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp_dht_context_start(dht(), pubkey());
|
2018-12-10 16:26:46 +00:00
|
|
|
ScheduleTicker(1000);
|
2018-12-24 16:09:05 +00:00
|
|
|
_running.store(true);
|
|
|
|
return _running;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-01-29 02:16:31 +00:00
|
|
|
self->_logic->call_later({200, self, &RouterAfterStopLinks});
|
2018-12-24 16:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Router::StopLinks()
|
|
|
|
{
|
|
|
|
llarp::LogInfo("stopping links");
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &link : outboundLinks)
|
|
|
|
link->Stop();
|
|
|
|
for(const auto &link : inboundLinks)
|
2018-12-24 16:09:05 +00:00
|
|
|
link->Stop();
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 21:16:23 +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;
|
|
|
|
}
|
|
|
|
llarp::LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr,
|
|
|
|
" Enabled: ", enabledOption);
|
|
|
|
// llarp::LogInfo("IfAddr: ", itr->second);
|
|
|
|
// llarp::LogInfo("IfName: ", itr->second);
|
|
|
|
if(enabledOption == "false")
|
|
|
|
{
|
|
|
|
llarp::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
|
|
|
|
if(hiddenServiceContext.hasEndpoints())
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Auto mode detected and we have endpoints");
|
2019-01-17 21:15:40 +00:00
|
|
|
netConfig.emplace("enabled", "false");
|
2019-01-16 21:16:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-17 21:15:40 +00:00
|
|
|
netConfig.emplace("enabled", "true");
|
2019-01-16 21:16:23 +00:00
|
|
|
}
|
|
|
|
// 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 = llarp::findFreePrivateRange();
|
|
|
|
if(defaultIfAddr == "")
|
|
|
|
{
|
|
|
|
llarp::LogError(
|
|
|
|
"Could not find any free lokitun interface names, can't
|
|
|
|
auto set up " "default HS context for client"); defaultIfAddr = "no";
|
|
|
|
netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr));
|
|
|
|
}
|
|
|
|
if(defaultIfName == "auto")
|
|
|
|
{
|
|
|
|
// pick an ifName
|
|
|
|
defaultIfName = llarp::findFreeLokiTunIfName();
|
|
|
|
if(defaultIfName == "")
|
|
|
|
{
|
|
|
|
llarp::LogError(
|
|
|
|
"Could not find any free private ip ranges, can't auto
|
|
|
|
set up " "default HS context for client"); defaultIfName = "no";
|
|
|
|
netConfig.emplace(std::make_pair("defaultIfName", defaultIfName));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
netConfig.emplace(std::make_pair("defaultIfName", defaultIfName));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::Stop()
|
2018-11-26 13:29:45 +00:00
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
if(!_running)
|
|
|
|
return;
|
|
|
|
if(_stopping)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_stopping.store(true);
|
|
|
|
llarp::LogInfo("stopping router");
|
|
|
|
hiddenServiceContext.StopAll();
|
|
|
|
exitContext.Stop();
|
|
|
|
if(rpcServer)
|
|
|
|
rpcServer->Stop();
|
2019-01-29 02:16:31 +00:00
|
|
|
_logic->call_later({200, this, &RouterAfterStopIssued});
|
2018-11-26 13:29:45 +00:00
|
|
|
}
|
2018-08-02 23:30:34 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::HasSessionTo(const llarp::RouterID &remote) const
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
return validRouters.find(remote) != validRouters.end();
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
void
|
|
|
|
Router::ConnectToRandomRouters(int want)
|
|
|
|
{
|
|
|
|
int wanted = want;
|
|
|
|
Router *self = this;
|
2018-12-10 23:29:58 +00:00
|
|
|
|
2019-01-29 02:16:31 +00:00
|
|
|
self->nodedb()->visit(
|
2018-12-10 23:29:58 +00:00
|
|
|
[self, &want](const llarp::RouterContact &other) -> bool {
|
2018-12-10 16:26:46 +00:00
|
|
|
// check if we really want to
|
2018-12-19 17:48:29 +00:00
|
|
|
if(other.ExpiresSoon(self->Now(), 30000))
|
|
|
|
return want > 0;
|
2018-12-10 16:26:46 +00:00
|
|
|
if(!self->ConnectionToRouterAllowed(other.pubkey))
|
|
|
|
return want > 0;
|
2018-12-11 00:53:11 +00:00
|
|
|
if(llarp::randint() % 2 == 0
|
2018-12-10 16:26:46 +00:00
|
|
|
&& !(self->HasSessionTo(other.pubkey)
|
|
|
|
|| self->HasPendingConnectJob(other.pubkey)))
|
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
self->TryConnectAsync(other, 5);
|
2018-12-10 16:26:46 +00:00
|
|
|
--want;
|
|
|
|
}
|
|
|
|
return want > 0;
|
|
|
|
});
|
|
|
|
if(wanted != want)
|
|
|
|
llarp::LogInfo("connecting to ", abs(want - wanted), " out of ", wanted,
|
|
|
|
" random routers");
|
2018-06-07 16:22:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::InitServiceNode()
|
2018-11-26 13:29:45 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogInfo("accepting transit traffic");
|
|
|
|
paths.AllowTransit();
|
2019-01-29 02:16:31 +00:00
|
|
|
llarp_dht_allow_transit(dht());
|
2018-12-10 16:26:46 +00:00
|
|
|
return exitContext.AddExitEndpoint("default-connectivity", netConfig);
|
2018-11-26 13:29:45 +00:00
|
|
|
}
|
2018-06-14 17:35:12 +00:00
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
/// validate a new configuration against an already made and running
|
|
|
|
/// router
|
|
|
|
struct RouterConfigValidator
|
|
|
|
{
|
|
|
|
static void
|
|
|
|
ValidateEntry(llarp_config_iterator *i, const char *section,
|
|
|
|
const char *key, const char *val)
|
|
|
|
{
|
|
|
|
RouterConfigValidator *self =
|
|
|
|
static_cast< RouterConfigValidator * >(i->user);
|
|
|
|
if(self->valid)
|
|
|
|
{
|
|
|
|
if(!self->OnEntry(section, key, val))
|
|
|
|
{
|
|
|
|
llarp::LogError("invalid entry in section [", section, "]: '", key,
|
|
|
|
"'='", val, "'");
|
|
|
|
self->valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Router *router;
|
|
|
|
llarp_config *config;
|
|
|
|
bool valid;
|
|
|
|
RouterConfigValidator(const Router *r, llarp_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()
|
|
|
|
{
|
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = this;
|
|
|
|
iter.visit = &ValidateEntry;
|
|
|
|
llarp_config_iter(config, &iter);
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Router::ValidateConfig(llarp_config *conf) const
|
|
|
|
{
|
|
|
|
RouterConfigValidator validator(this, conf);
|
|
|
|
return validator.Validate();
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
2018-12-24 16:09:05 +00:00
|
|
|
Router::Reconfigure(llarp_config *)
|
2018-06-18 22:05:02 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
// TODO: implement me
|
|
|
|
return true;
|
2018-05-20 17:45:47 +00:00
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-12-10 16:26:46 +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)
|
2018-12-10 16:26:46 +00:00
|
|
|
return true;
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2019-01-05 13:45:05 +00:00
|
|
|
static std::list<
|
|
|
|
std::function< std::unique_ptr< ILinkLayer >(llarp::Router *) > >
|
|
|
|
linkFactories = {llarp::utp::NewServerFromRouter,
|
|
|
|
llarp::iwp::NewServerFromRouter};
|
2018-06-28 11:29:10 +00:00
|
|
|
|
2019-01-05 13:45:05 +00:00
|
|
|
for(const auto &factory : linkFactories)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +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()))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to load ", transport_keyfile);
|
|
|
|
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)
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
2019-01-05 13:45:05 +00:00
|
|
|
if(!link->Configure(netloop, "*", af, 0))
|
|
|
|
continue;
|
|
|
|
outboundLinks.insert(std::move(link));
|
2019-01-10 12:38:06 +00:00
|
|
|
break;
|
2018-12-10 16:26:46 +00:00
|
|
|
}
|
2018-06-21 12:52:45 +00:00
|
|
|
}
|
2019-01-05 13:45:05 +00:00
|
|
|
return outboundLinks.size() > 0;
|
2018-06-21 12:52:45 +00:00
|
|
|
}
|
2018-06-21 13:33:42 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::CreateDefaultHiddenService()
|
|
|
|
{
|
|
|
|
// fallback defaults
|
2019-01-17 21:15:40 +00:00
|
|
|
// 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
|
2018-12-10 16:26:46 +00:00
|
|
|
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"; }},
|
2018-12-10 16:26:46 +00:00
|
|
|
{"local-dns", []() -> std::string { return "127.0.0.1:53"; }},
|
|
|
|
{"upstream-dns", []() -> std::string { return "1.1.1.1:53"; }}};
|
|
|
|
// populate with fallback defaults if values not present
|
|
|
|
auto itr = netConfigDefaults.begin();
|
|
|
|
while(itr != netConfigDefaults.end())
|
2018-06-21 12:52:45 +00:00
|
|
|
{
|
2018-12-19 17:48:29 +00:00
|
|
|
auto found = netConfig.find(itr->first);
|
|
|
|
if(found == netConfig.end() || found->second.empty())
|
2018-12-10 16:26:46 +00:00
|
|
|
{
|
|
|
|
netConfig.emplace(std::make_pair(itr->first, itr->second()));
|
|
|
|
}
|
|
|
|
++itr;
|
2018-06-21 12:52:45 +00:00
|
|
|
}
|
2018-12-10 16:26:46 +00:00
|
|
|
// add endpoint
|
|
|
|
return hiddenServiceContext.AddDefaultEndpoint(netConfig);
|
2018-06-21 12:52:45 +00:00
|
|
|
}
|
2018-08-02 23:30:34 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::HasPendingConnectJob(const llarp::RouterID &remote)
|
2018-06-21 12:52:45 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
return pendingEstablishJobs.find(remote) != pendingEstablishJobs.end();
|
2018-06-21 12:52:45 +00:00
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
bool
|
|
|
|
Router::LoadHiddenServiceConfig(const char *fname)
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::LogDebug("opening hidden service config ", fname);
|
|
|
|
llarp::service::Config conf;
|
|
|
|
if(!conf.Load(fname))
|
2018-07-09 17:32:11 +00:00
|
|
|
return false;
|
2018-12-10 16:26:46 +00:00
|
|
|
for(const auto &config : conf.services)
|
|
|
|
{
|
|
|
|
llarp::service::Config::section_t filteredConfig;
|
|
|
|
mergeHiddenServiceConfig(config.second, filteredConfig.second);
|
|
|
|
filteredConfig.first = config.first;
|
|
|
|
if(!hiddenServiceContext.AddEndpoint(filteredConfig))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-09 17:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
router_iter_config(llarp_config_iterator *iter, const char *section,
|
|
|
|
const char *key, const char *val)
|
2018-05-20 17:45:47 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
llarp::LogDebug(section, " ", key, "=", val);
|
2018-12-10 16:26:46 +00:00
|
|
|
Router *self = static_cast< Router * >(iter->user);
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int af;
|
|
|
|
uint16_t proto;
|
|
|
|
if(StrEq(val, "eth"))
|
|
|
|
{
|
2018-05-29 12:16:08 +00:00
|
|
|
#ifdef AF_LINK
|
2018-05-29 13:40:26 +00:00
|
|
|
af = AF_LINK;
|
2018-05-29 12:16:08 +00:00
|
|
|
#endif
|
|
|
|
#ifdef AF_PACKET
|
2018-05-29 13:40:26 +00:00
|
|
|
af = AF_PACKET;
|
2018-05-29 12:16:08 +00:00
|
|
|
#endif
|
2018-05-22 15:54:19 +00:00
|
|
|
proto = LLARP_ETH_PROTO;
|
|
|
|
}
|
2018-05-20 17:45:47 +00:00
|
|
|
else
|
|
|
|
{
|
2018-05-29 12:21:50 +00:00
|
|
|
// try IPv4 first
|
2018-05-29 12:15:48 +00:00
|
|
|
af = AF_INET;
|
2018-05-22 15:54:19 +00:00
|
|
|
proto = std::atoi(val);
|
2018-05-20 17:45:47 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-06-07 16:22:49 +00:00
|
|
|
if(StrEq(section, "bind"))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-06-07 16:22:49 +00:00
|
|
|
if(!StrEq(key, "*"))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
auto server = llarp::utp::NewServerFromRouter(self);
|
2018-09-03 13:10:56 +00:00
|
|
|
if(!server->EnsureKeys(self->transport_keyfile.string().c_str()))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to ensure keyfile ", self->transport_keyfile);
|
|
|
|
return;
|
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
if(server->Configure(self->netloop, key, af, proto))
|
2018-05-22 18:41:38 +00:00
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
self->AddInboundLink(server);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(af == AF_INET6)
|
|
|
|
{
|
|
|
|
// we failed to configure IPv6
|
|
|
|
// try IPv4
|
|
|
|
llarp::LogInfo("link ", key,
|
|
|
|
" failed to configure IPv6, trying IPv4");
|
|
|
|
af = AF_INET;
|
|
|
|
if(server->Configure(self->netloop, key, af, proto))
|
2018-05-29 12:16:08 +00:00
|
|
|
{
|
2018-09-02 18:25:42 +00:00
|
|
|
self->AddInboundLink(server);
|
2018-05-29 12:16:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-23 14:55:25 +00:00
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp::LogError("Failed to set up curvecp link");
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-02 17:35:51 +00:00
|
|
|
else if(StrEq(section, "network"))
|
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
if(StrEq(key, "profiles"))
|
|
|
|
{
|
|
|
|
self->routerProfilesFile = val;
|
|
|
|
self->routerProfiling.Load(val);
|
|
|
|
llarp::LogInfo("setting profiles to ", self->routerProfilesFile);
|
|
|
|
}
|
2018-11-28 14:58:38 +00:00
|
|
|
else if(StrEq(key, "strict-connect"))
|
|
|
|
{
|
2018-11-28 15:01:17 +00:00
|
|
|
if(self->IsServiceNode())
|
|
|
|
{
|
|
|
|
llarp::LogError("cannot use strict-connect option as service node");
|
|
|
|
return;
|
|
|
|
}
|
2018-12-10 17:22:59 +00:00
|
|
|
llarp::RouterID snode;
|
2018-11-28 14:58:38 +00:00
|
|
|
llarp::PubKey pk;
|
2018-12-10 17:22:59 +00:00
|
|
|
if(pk.FromString(val))
|
2018-11-28 14:58:38 +00:00
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
if(self->strictConnectPubkeys.emplace(pk).second)
|
2018-11-28 14:58:38 +00:00
|
|
|
llarp::LogInfo("added ", pk, " to strict connect list");
|
|
|
|
else
|
|
|
|
llarp::LogWarn("duplicate key for strict connect: ", pk);
|
|
|
|
}
|
2018-12-10 17:22:59 +00:00
|
|
|
else if(snode.FromString(val))
|
|
|
|
{
|
|
|
|
if(self->strictConnectPubkeys.insert(snode).second)
|
|
|
|
llarp::LogInfo("added ", snode, " to strict connect list");
|
|
|
|
else
|
|
|
|
llarp::LogWarn("duplicate key for strict connect: ", snode);
|
|
|
|
}
|
2018-11-28 14:58:38 +00:00
|
|
|
else
|
|
|
|
llarp::LogError("invalid key for strict-connect: ", val);
|
|
|
|
}
|
2018-11-14 18:02:27 +00:00
|
|
|
else
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-26 13:29:45 +00:00
|
|
|
self->netConfig.insert(std::make_pair(key, val));
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
2018-10-02 17:35:51 +00:00
|
|
|
}
|
2018-10-09 12:06:30 +00:00
|
|
|
else if(StrEq(section, "api"))
|
|
|
|
{
|
|
|
|
if(StrEq(key, "enabled"))
|
|
|
|
{
|
|
|
|
self->enableRPCServer = IsTrueValue(val);
|
|
|
|
}
|
|
|
|
if(StrEq(key, "bind"))
|
|
|
|
{
|
|
|
|
self->rpcBindAddr = val;
|
|
|
|
}
|
|
|
|
if(StrEq(key, "authkey"))
|
|
|
|
{
|
|
|
|
// TODO: add pubkey to whitelist
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
else if(StrEq(section, "services"))
|
|
|
|
{
|
|
|
|
if(self->LoadHiddenServiceConfig(val))
|
|
|
|
{
|
|
|
|
llarp::LogInfo("loaded hidden service config for ", key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to load hidden service config for ", key);
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 17:46:33 +00:00
|
|
|
else if(StrEq(section, "lokid"))
|
|
|
|
{
|
2019-01-21 17:06:31 +00:00
|
|
|
if(StrEq(key, "service-node-seed"))
|
|
|
|
{
|
|
|
|
self->usingSNSeed = true;
|
|
|
|
self->ident_keyfile = val;
|
|
|
|
}
|
2018-11-21 17:46:33 +00:00
|
|
|
if(StrEq(key, "enabled"))
|
|
|
|
{
|
|
|
|
self->whitelistRouters = IsTrueValue(val);
|
|
|
|
}
|
|
|
|
if(StrEq(key, "jsonrpc"))
|
|
|
|
{
|
|
|
|
self->lokidRPCAddr = val;
|
|
|
|
}
|
2019-01-28 13:04:45 +00:00
|
|
|
if(StrEq(key, "username"))
|
|
|
|
{
|
|
|
|
self->lokidRPCUser = val;
|
|
|
|
}
|
|
|
|
if(StrEq(key, "password"))
|
|
|
|
{
|
|
|
|
self->lokidRPCPassword = val;
|
|
|
|
}
|
2018-11-21 17:46:33 +00:00
|
|
|
}
|
2018-11-11 13:14:19 +00:00
|
|
|
else if(StrEq(section, "dns"))
|
|
|
|
{
|
|
|
|
if(StrEq(key, "upstream"))
|
|
|
|
{
|
2018-11-11 13:30:54 +00:00
|
|
|
llarp::LogInfo("add upstream resolver ", val);
|
2018-11-26 13:29:45 +00:00
|
|
|
self->netConfig.emplace(std::make_pair("upstream-dns", val));
|
2018-11-11 13:14:19 +00:00
|
|
|
}
|
|
|
|
if(StrEq(key, "bind"))
|
|
|
|
{
|
2018-11-11 13:30:54 +00:00
|
|
|
llarp::LogInfo("set local dns to ", val);
|
2018-11-26 13:29:45 +00:00
|
|
|
self->netConfig.emplace(std::make_pair("local-dns", val));
|
2018-11-11 13:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-02 18:07:07 +00:00
|
|
|
else if(StrEq(section, "connect")
|
|
|
|
|| (StrEq(section, "bootstrap") && StrEq(key, "add-node")))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-02-01 01:58:06 +00:00
|
|
|
// llarp::LogDebug("connect section has ", key, "=", val);
|
2018-11-28 14:58:38 +00:00
|
|
|
self->bootstrapRCList.emplace_back();
|
2018-12-02 18:07:07 +00:00
|
|
|
auto &rc = self->bootstrapRCList.back();
|
2019-01-24 15:13:41 +00:00
|
|
|
if(!rc.Read(val))
|
2018-11-28 14:58:38 +00:00
|
|
|
{
|
2019-01-24 15:14:54 +00:00
|
|
|
llarp::LogWarn("failed to decode bootstrap RC, file='", val,
|
|
|
|
"' rc=", rc);
|
2019-01-24 15:13:41 +00:00
|
|
|
self->bootstrapRCList.pop_back();
|
|
|
|
return;
|
2018-11-28 14:58:38 +00:00
|
|
|
}
|
2019-01-29 02:16:31 +00:00
|
|
|
if(rc.Verify(self->crypto(), self->Now()))
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2019-01-24 15:13:41 +00:00
|
|
|
llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey));
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
2018-11-28 14:58:38 +00:00
|
|
|
else
|
|
|
|
{
|
2019-01-24 15:14:54 +00:00
|
|
|
if(rc.IsExpired(self->Now()))
|
2019-01-24 15:13:41 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Bootstrap node ", RouterID(rc.pubkey),
|
2019-01-24 15:14:54 +00:00
|
|
|
" is too old and needs to be refreshed");
|
2019-01-24 15:13:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("malformed rc file='", val, "' rc=", rc);
|
|
|
|
}
|
2018-11-28 14:58:38 +00:00
|
|
|
self->bootstrapRCList.pop_back();
|
|
|
|
}
|
2018-04-05 14:43:16 +00:00
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
else if(StrEq(section, "router"))
|
|
|
|
{
|
2018-12-21 13:08:01 +00:00
|
|
|
if(StrEq(key, "netid"))
|
|
|
|
{
|
|
|
|
if(strlen(val) <= self->_rc.netID.size())
|
|
|
|
{
|
|
|
|
llarp::LogWarn("!!!! you have manually set netid to be '", val,
|
2019-01-02 22:21:29 +00:00
|
|
|
"' which does not equal '", Version::LLARP_NET_ID,
|
2019-01-24 15:14:54 +00:00
|
|
|
"' you will run as a different network, good luck "
|
|
|
|
"and "
|
|
|
|
"don't forget: something something MUH traffic "
|
|
|
|
"shape "
|
2018-12-21 13:08:01 +00:00
|
|
|
"correlation !!!!");
|
2019-01-05 00:49:06 +00:00
|
|
|
llarp::NetID::DefaultValue() =
|
|
|
|
llarp::NetID(reinterpret_cast< const byte_t * >(strdup(val)));
|
2018-12-28 15:34:41 +00:00
|
|
|
// re set netid in our rc
|
|
|
|
self->_rc.netID = llarp::NetID();
|
2018-12-21 13:08:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
llarp::LogError("invalid netid '", val, "', is too long");
|
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
if(StrEq(key, "nickname"))
|
|
|
|
{
|
2018-09-09 11:23:21 +00:00
|
|
|
self->_rc.SetNick(val);
|
2018-08-30 18:48:43 +00:00
|
|
|
// set logger name here
|
2018-09-09 11:23:21 +00:00
|
|
|
_glog.nodeName = self->rc().Nick();
|
2018-08-02 00:48:43 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
if(StrEq(key, "encryption-privkey"))
|
|
|
|
{
|
|
|
|
self->encryption_keyfile = val;
|
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
if(StrEq(key, "contact-file"))
|
|
|
|
{
|
|
|
|
self->our_rc_file = val;
|
|
|
|
}
|
|
|
|
if(StrEq(key, "transport-privkey"))
|
|
|
|
{
|
|
|
|
self->transport_keyfile = val;
|
|
|
|
}
|
2019-01-29 12:33:33 +00:00
|
|
|
if((StrEq(key, "identity-privkey") || StrEq(key, "ident-privkey"))
|
|
|
|
&& !self->usingSNSeed)
|
2018-05-23 20:37:43 +00:00
|
|
|
{
|
|
|
|
self->ident_keyfile = val;
|
|
|
|
}
|
2019-02-07 14:30:43 +00:00
|
|
|
if(StrEq(key, "public-address") || StrEq(key, "public-ip"))
|
2018-06-28 11:29:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("public ip ", val, " size ", strlen(val));
|
2018-06-29 12:15:15 +00:00
|
|
|
if(strlen(val) < 17)
|
|
|
|
{
|
2018-06-28 11:29:10 +00:00
|
|
|
// assume IPv4
|
2018-10-03 11:01:42 +00:00
|
|
|
// inet_pton(AF_INET, val, &self->ip4addr.sin_addr);
|
2018-06-29 12:15:15 +00:00
|
|
|
// struct sockaddr dest;
|
2018-10-03 11:01:42 +00:00
|
|
|
// sockaddr *dest = (sockaddr *)&self->ip4addr;
|
|
|
|
llarp::Addr a(val);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("setting public ipv4 ", a);
|
2018-06-29 12:15:15 +00:00
|
|
|
self->addrInfo.ip = *a.addr6();
|
2018-06-28 11:59:50 +00:00
|
|
|
self->publicOverride = true;
|
2018-06-28 11:29:10 +00:00
|
|
|
}
|
2018-06-29 12:15:15 +00:00
|
|
|
// llarp::Addr a(val);
|
2018-06-28 11:29:10 +00:00
|
|
|
}
|
|
|
|
if(StrEq(key, "public-port"))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Setting public port ", val);
|
2018-10-09 01:38:25 +00:00
|
|
|
int p = atoi(val);
|
2018-10-12 12:05:03 +00:00
|
|
|
// Not needed to flip upside-down - this is done in llarp::Addr(const
|
|
|
|
// AddressInfo&)
|
2018-10-09 03:33:56 +00:00
|
|
|
self->ip4addr.sin_port = p;
|
|
|
|
self->addrInfo.port = p;
|
2018-06-29 12:15:15 +00:00
|
|
|
self->publicOverride = true;
|
2018-06-28 11:29:10 +00:00
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
}
|
2018-11-14 18:02:27 +00:00
|
|
|
} // namespace llarp
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|