make bootstrap happen only when we have very few nodes

add strict-connect option
pull/90/head
Jeff Becker 6 years ago
parent 387d05282b
commit 38c9633ca2
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -407,10 +407,11 @@ namespace llarp
llarp_time_t
Now();
private:
void
ExploreNetworkVia(const Key_t& peer);
private:
void
ScheduleCleanupTimer();

@ -176,9 +176,13 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath)
f << "dir=" << basepath << "netdb" << std::endl;
f << std::endl << std::endl;
f << "# bootstrap settings " << std::endl;
f << "[connect]" << std::endl;
f << "bootstrap=" << basepath << "bootstrap.signed" << std::endl;
f << "# bootstrap settings" << std::endl;
f << "[bootstrap]" << std::endl;
f << "# add a bootstrap node's signed identity to the list of nodes we want to bootstrap from" << std::endl;
f << "# if we don't have any peers we connect to this router" << std::endl;
f << "add-node=" << basepath << "bootstrap.signed" << std::endl;
f << "# add another bootstrap node" << std::endl;
f << "#add-node=/path/to/alternative/self.signed" << std::endl;
f << std::endl << std::endl;
}
@ -253,14 +257,14 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
f << "#example-snapp=" << snappExample_fpath << std::endl;
f << std::endl << std::endl;
#endif
f << "# network settings " << std::endl;
f << "[network]" << std::endl;
f << "profiles=" << basepath << "profiles.dat" << std::endl;
// WHAT? Why this ifndef?
#ifndef __linux__
f << "# ";
#endif
f << "# uncomment next line to add router with pubkey to list of routers we connect directly to" << std::endl;
f << "#strict-connect=pubkey" << std::endl;
f << "# uncomment next line to use router with pubkey as an exit node" << std::endl;
f << "#exit-node=pubkey" << std::endl;
// pick ip
std::string ip = llarp::findFreePrivateRange();
/*
@ -313,7 +317,6 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
f << "ifname=" << ifName << std::endl;
f << "ifaddr=" << ip << std::endl;
f << "enabled=true" << std::endl;
return true;
}
@ -349,7 +352,7 @@ extern "C"
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
{"network", conf->impl.network}, {"connect", conf->impl.connect},
{"system", conf->impl.system}, {"bind", conf->impl.iwp_links},
{"bootstrap", conf->impl.bootstrap}, {"system", conf->impl.system},
{"netdb", conf->impl.netdb}, {"api", conf->impl.api},
{"services", conf->impl.services}};
@ -359,6 +362,9 @@ extern "C"
for(const auto item : conf->impl.dns)
iter->visit(iter, "dns", item.first.c_str(), item.second.c_str());
for(const auto item : conf->impl.iwp_links)
iter->visit(iter, "bind", item.first.c_str(), item.second.c_str());
for(const auto &section : sections)
for(const auto &item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),

@ -21,6 +21,7 @@ namespace llarp
section_t system;
section_t api;
section_t lokid;
section_t bootstrap;
bool
Load(const char *fname);

@ -427,9 +427,26 @@ llarp_router::handle_router_ticker(void *user, uint64_t orig, uint64_t left)
self->ScheduleTicker(orig);
}
bool
llarp_router::ConnectionToRouterAllowed(const llarp::RouterID & router) const
{
if(strictConnectPubkeys.size() && strictConnectPubkeys.count(router) == 0)
return false;
else if(IsServiceNode() && whitelistRouters)
return lokinetRouters.count(router) != 0;
else
return true;
}
void
llarp_router::TryEstablishTo(const llarp::RouterID &remote)
{
if(!ConnectionToRouterAllowed(remote))
{
llarp::LogWarn("not connecting to ", remote, " as it's not permitted by config");
return;
}
llarp::RouterContact rc;
if(llarp_nodedb_get_rc(nodedb, remote, rc))
{
@ -528,8 +545,17 @@ llarp_router::Tick()
{
llarp::LogInfo("We need at least ", minRequiredRouters,
" service nodes to build paths but we have ", N);
auto explore = std::max(NumberOfConnectedRouters(), size_t(1));
dht->impl.Explore(explore);
// TODO: only connect to random subset
if(bootstrapRCList.size())
{
for(const auto & rc : bootstrapRCList)
{
llarp_router_try_connect(this, rc, 4);
dht->impl.ExploreNetworkVia(rc.pubkey.data());
}
}
else
llarp::LogError("we have no bootstrap nodes specified");
}
paths.BuildPaths(now);
hiddenServiceContext.Tick(now);
@ -837,7 +863,6 @@ llarp_router::Run()
llarp::LogWarn("Link ", link->Name(), " failed to start");
}
uint64_t delay = ((llarp_randint() % 10) * 500) + 500;
if(IBLinksStarted > 0)
{
// initialize as service node
@ -847,7 +872,6 @@ llarp_router::Run()
Close();
return;
}
delay = llarp_randint() % 50;
}
else
{
@ -884,8 +908,6 @@ llarp_router::Run()
llarp::LogInfo("starting dht context as ", ourPubkey);
llarp_dht_context_start(dht, ourPubkey);
ScheduleTicker(1000);
// delayed connect all
llarp_logic_call_later(logic, {delay, this, &ConnectAll});
}
bool
@ -897,20 +919,6 @@ llarp_router::InitServiceNode()
return exitContext.AddExitEndpoint("default-connectivity", netConfig);
}
void
llarp_router::ConnectAll(void *user, __attribute__((unused)) uint64_t orig,
uint64_t left)
{
if(left)
return;
llarp_router *self = static_cast< llarp_router * >(user);
// connect to all explicit connections in connect block
for(const auto &itr : self->connect)
{
llarp::LogInfo("connecting to node ", itr.first);
self->try_connect(itr.second);
}
}
bool
llarp_router::HasSessionTo(const llarp::RouterID &remote) const
@ -925,6 +933,9 @@ llarp_router::ConnectToRandomRouters(int want)
llarp_router *self = this;
llarp_nodedb_visit_loaded(
self->nodedb, [self, &want](const llarp::RouterContact &other) -> bool {
// check if we really want to
if(!self->ConnectionToRouterAllowed(other.pubkey))
return want > 0;
if(llarp_randint() % 2 == 0
&& !(self->HasSessionTo(other.pubkey)
|| self->HasPendingConnectJob(other.pubkey)))
@ -942,6 +953,7 @@ llarp_router::ConnectToRandomRouters(int want)
bool
llarp_router::ReloadConfig(__attribute__((unused)) const llarp_config *conf)
{
// TODO: implement me
return true;
}
@ -1014,7 +1026,7 @@ llarp_init_router(struct llarp_threadpool *tp, struct llarp_ev_loop *netloop,
router->netloop = netloop;
router->tp = tp;
router->logic = logic;
// TODO: make disk io threadpool count configurable
// TODO: make disk io threadpool count configurable (?)
#ifdef TESTNET
router->disk = tp;
#else
@ -1208,6 +1220,19 @@ namespace llarp
self->routerProfiling.Load(val);
llarp::LogInfo("setting profiles to ", self->routerProfilesFile);
}
else if(StrEq(key, "strict-connect"))
{
llarp::PubKey pk;
if(llarp::HexDecode(val, pk.data(), pk.size()))
{
if(self->strictConnectPubkeys.insert(pk).second)
llarp::LogInfo("added ", pk, " to strict connect list");
else
llarp::LogWarn("duplicate key for strict connect: ", pk);
}
else
llarp::LogError("invalid key for strict-connect: ", val);
}
else
{
self->netConfig.insert(std::make_pair(key, val));
@ -1263,9 +1288,19 @@ namespace llarp
self->netConfig.emplace(std::make_pair("local-dns", val));
}
}
else if(StrEq(section, "connect"))
else if(StrEq(section, "connect") || (StrEq(section, "bootstrap") && StrEq(key, "add-node")))
{
self->connect[key] = val;
self->bootstrapRCList.emplace_back();
auto & rc = self->bootstrapRCList.back();
if(rc.Read(val) && rc.Verify(&self->crypto))
{
llarp::LogInfo("Added bootstrap node ", rc.pubkey);
}
else
{
llarp::LogError("malformed rc file: ", val);
self->bootstrapRCList.pop_back();
}
}
else if(StrEq(section, "router"))
{

@ -40,7 +40,8 @@ struct llarp_router
fs::path transport_keyfile = "transport.key";
// nodes to connect to on startup
std::map< std::string, fs::path > connect;
// DEPRECATED
// std::map< std::string, fs::path > connect;
// long term identity key
fs::path ident_keyfile = "identity.key";
@ -84,7 +85,7 @@ struct llarp_router
byte_t linkmsg_buffer[MAX_LINK_MSG_SIZE];
/// always maintain this many connections to other routers
size_t minConnectedRouters = 5;
size_t minConnectedRouters = 1;
/// hard upperbound limit on the number of router to router connections
size_t maxConnectedRouters = 2000;
@ -105,6 +106,12 @@ struct llarp_router
/// default network config for default network interface
NetConfig_t netConfig;
/// identity keys whitelist of routers we will connect to directly (not for service nodes)
std::set<llarp::PubKey> strictConnectPubkeys;
/// bootstrap RCs
std::list<llarp::RouterContact> bootstrapRCList;
bool
ExitEnabled() const
{
@ -206,15 +213,16 @@ struct llarp_router
void
PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until);
static void
ConnectAll(void *user, uint64_t orig, uint64_t left);
bool
EnsureIdentity();
bool
EnsureEncryptionKey();
bool
ConnectionToRouterAllowed(const llarp::RouterID & router) const;
bool
SaveRC();

@ -57,7 +57,7 @@ namespace llarp
if(!bencode_write_version_entry(buf))
return false;
/* write ai if they exist */
/* write xi if they exist */
if(!bencode_write_bytestring(buf, "x", 1))
return false;
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))

Loading…
Cancel
Save