Some maybe-fixes for RouterHive post config cleanup

pull/1245/head
Stephen Shelton 4 years ago
parent 54a7843bc5
commit 526b1320b7
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -207,16 +207,22 @@ namespace llarp
{
(void)params;
constexpr auto DefaultOutboundLinkValue = "1090";
conf.defineOption<std::string>(
"bind", "*", false, false, DefaultOutboundLinkValue, [this](std::string arg) {
m_OutboundLink = std::move(LinkInfoFromINIValues("*", arg));
});
conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) {
LinkInfo info = LinkInfoFromINIValues(name, value);
if (info.port <= 0)
throw std::invalid_argument(stringify("Invalid [bind] port specified on interface", name));
if (name == "*")
m_OutboundLink = std::move(info);
else
m_InboundLinks.emplace_back(std::move(info));
assert(name != "*"); // handled by defineOption("bind", "*", ...) above
m_InboundLinks.emplace_back(std::move(info));
return true;
});
@ -447,6 +453,29 @@ namespace llarp
}
}
bool
Config::LoadDefault(bool isRelay, fs::path dataDir)
{
try
{
ConfigGenParameters params;
params.isRelay = isRelay;
params.defaultDataDir = std::move(dataDir);
ConfigDefinition conf;
initializeConfig(conf, params);
conf.acceptAllOptions();
return true;
}
catch (const std::exception& e)
{
LogError("Error trying to init default config: ", e.what());
return false;
}
}
void
Config::initializeConfig(ConfigDefinition& conf, const ConfigGenParameters& params)
{

@ -189,6 +189,20 @@ namespace llarp
bool
Load(const char* fname, bool isRelay, fs::path defaultDataDir);
/// Load (initialize) a default config.
///
/// This delegates to the ConfigDefinition to generate a default config,
/// as though an empty config were specified.
///
/// If using Config without the intention of loading from file (or string), this is necessary
/// in order to obtain sane defaults.
///
/// @param isRelay determines whether the config will reflect that of a relay or client
/// @param dataDir is a path representing a directory to be used as the data dir
/// @return true on success, false otherwise
bool
LoadDefault(bool isRelay, fs::path dataDir);
std::string
generateBaseClientConfig(fs::path defaultDataDir);

@ -103,7 +103,7 @@ namespace llarp
// must also be done after configure so that netid is properly set if it
// is provided by config
if (!this->LoadDatabase())
return 1;
return 2;
return 0;
}

@ -13,6 +13,7 @@
#include <messages/link_message.hpp>
#include <net/net.hpp>
#include <rpc/rpc.hpp>
#include <stdexcept>
#include <util/buffer.hpp>
#include <util/encode.hpp>
#include <util/logging/file_logger.hpp>
@ -247,21 +248,23 @@ namespace llarp
{
if (nodedb == nullptr)
{
LogError("Attempting to Router::Configure but passed null nodedb pointer");
return false;
throw std::invalid_argument("nodedb cannot be null");
}
_nodedb = nodedb;
if (not m_keyManager->initialize(*conf, true))
return false;
throw std::runtime_error("KeyManager failed to initialize");
if (!FromConfig(conf))
return false;
throw std::runtime_error("FromConfig() failed");
if (!InitOutboundLinks())
return false;
throw std::runtime_error("InitOutboundLinks() failed");
if (not EnsureIdentity())
throw std::runtime_error("EnsureIdentity() failed");
return EnsureIdentity();
return true;
}
/// called in disk worker thread
@ -425,8 +428,7 @@ namespace llarp
const auto& val = conf->network.m_strictConnect;
if (IsServiceNode())
{
llarp::LogError("cannot use strict-connect option as service node");
return false;
throw std::runtime_error("cannot use strict-connect option as service node");
}
llarp::RouterID snode;
llarp::PubKey pk;
@ -488,8 +490,7 @@ namespace llarp
{
if (not BDecodeReadFile(router.c_str(), b_list))
{
LogWarn("failed to read bootstrap list file '", router, "'");
return false;
throw std::runtime_error(stringify("failed to read bootstrap list file '", router, "'"));
}
}
else
@ -497,8 +498,8 @@ namespace llarp
RouterContact rc;
if (not rc.Read(router.c_str()))
{
llarp::LogWarn("failed to decode bootstrap RC, file='", router, "' rc=", rc);
return false;
throw std::runtime_error(
stringify("failed to decode bootstrap RC, file='", router, "' rc=", rc));
}
b_list.insert(rc);
}
@ -556,8 +557,7 @@ namespace llarp
uint16_t port = serverConfig.port;
if (!server->Configure(netloop(), key, af, port))
{
LogError("failed to bind inbound link on ", key, " port ", port);
return false;
throw std::runtime_error(stringify("failed to bind inbound link on ", key, " port ", port));
}
_linkManager.AddLink(std::move(server), true);
}
@ -1235,7 +1235,7 @@ namespace llarp
util::memFn(&AbstractRouter::PumpLL, this));
if (!link)
return false;
throw std::runtime_error("NewOutboundLink() failed to provide a link");
const auto afs = {AF_INET, AF_INET6};
@ -1246,7 +1246,8 @@ namespace llarp
_linkManager.AddLink(std::move(link), false);
return true;
}
return false;
throw std::runtime_error(
stringify("Failed to init AF_INET and AF_INET6 on port ", m_OutboundPort));
}
bool

@ -3,6 +3,7 @@
#include "llarp.h"
#include "llarp.hpp"
#include "util/thread/logic.hpp"
#include "util/str.hpp"
#include "router/abstractrouter.hpp"
#include <chrono>
@ -17,11 +18,20 @@ namespace tooling
const std::shared_ptr<llarp::Config>& config, std::vector<llarp_main*>* routers, bool isRelay)
{
llarp_main* ctx = llarp_main_init_from_config(config->Copy(), isRelay);
if (llarp_main_setup(ctx) == 0)
auto result = llarp_main_setup(ctx);
if (result == 0)
{
llarp::Context::Get(ctx)->InjectHive(this);
routers->push_back(ctx);
}
else
{
throw std::runtime_error(llarp::stringify(
"Failed to add RouterHive ",
(isRelay ? "relay" : "client"),
", llarp_main_setup() returned ",
result));
}
}
void
@ -37,12 +47,12 @@ namespace tooling
}
void
RouterHive::StartRouters(std::vector<llarp_main*>* routers)
RouterHive::StartRouters(std::vector<llarp_main*>* routers, bool isRelay)
{
for (llarp_main* ctx : *routers)
{
routerMainThreads.emplace_back([ctx]() {
llarp_main_run(ctx, llarp_main_runtime_opts{false, false, false});
routerMainThreads.emplace_back([=]() {
llarp_main_run(ctx, llarp_main_runtime_opts{false, false, false, isRelay});
});
std::this_thread::sleep_for(2ms);
}
@ -51,13 +61,13 @@ namespace tooling
void
RouterHive::StartRelays()
{
StartRouters(&relays);
StartRouters(&relays, true);
}
void
RouterHive::StartClients()
{
StartRouters(&clients);
StartRouters(&clients, false);
}
void
@ -202,6 +212,7 @@ namespace tooling
return results;
}
// TODO: DRY -- this smells a lot like RelayConnectedRelays()
std::vector<llarp::RouterContact>
RouterHive::GetRelayRCs()
{
@ -214,7 +225,7 @@ namespace tooling
for (auto relay : relays)
{
auto ctx = llarp::Context::Get(relay);
LogicCall(ctx->logic, [&, i, ctx]() {
LogicCall(ctx->logic, [&]() {
llarp::RouterContact rc = ctx->router->rc();
std::lock_guard<std::mutex> guard{results_lock};
results[i] = std::move(rc);
@ -223,9 +234,10 @@ namespace tooling
i++;
}
while (true)
int numTries = 0;
size_t read_done_count = 0;
while (numTries < 100)
{
size_t read_done_count = 0;
{
std::lock_guard<std::mutex> guard{results_lock};
read_done_count = done_count;
@ -234,7 +246,14 @@ namespace tooling
break;
std::this_thread::sleep_for(100ms);
numTries++;
}
if (read_done_count != relays.size())
{
LogWarn("could not read all relays, last done_count: ", read_done_count);
}
return results;
}

@ -26,7 +26,7 @@ namespace tooling
private:
void
StartRouters(std::vector<llarp_main*>* routers);
StartRouters(std::vector<llarp_main*>* routers, bool isRelay);
void
AddRouter(

@ -27,7 +27,10 @@ namespace llarp
.def_readwrite("lokid", &Config::lokid)
.def_readwrite("bootstrap", &Config::bootstrap)
.def_readwrite("logging", &Config::logging)
.def("LoadFile", &Config::Load);
.def("LoadFile", &Config::Load)
.def("LoadDefault", [](Config& self, bool isRelay, std::string dir) {
return self.LoadDefault(isRelay, dir);
});
py::class_<RouterConfig>(mod, "RouterConfig")
.def(py::init<>())
@ -75,8 +78,15 @@ namespace llarp
py::class_<LinksConfig>(mod, "LinksConfig")
.def(py::init<>())
.def_readwrite("OutboundLink", &LinksConfig::m_OutboundLink)
.def_readwrite("InboundLinks", &LinksConfig::m_InboundLinks)
.def(
"setOutboundLink",
[](LinksConfig& self, std::string interface, int family, uint16_t port) {
LinksConfig::LinkInfo info;
info.interface = std::move(interface);
info.addressFamily = family;
info.port = port;
self.m_OutboundLink = std::move(info);
})
.def(
"addInboundLink",
[](LinksConfig& self, std::string interface, int family, uint16_t port) {

@ -10,27 +10,32 @@ import sys
from argparse import ArgumentParser as ap
import threading
from collections import deque
import traceback
class RouterHive(object):
def __init__(self, n_relays=10, n_clients=10, netid="hive"):
try:
self.endpointName = "pyllarp"
self.tmpdir = "/tmp/lokinet_hive"
self.netid = netid
self.endpointName = "pyllarp"
self.tmpdir = "/tmp/lokinet_hive"
self.netid = netid
self.n_relays = n_relays
self.n_clients = n_clients
self.n_relays = n_relays
self.n_clients = n_clients
self.addrs = []
self.events = deque()
self.addrs = []
self.events = deque()
self.hive = None
self.RCs = []
self.hive = None
self.RCs = []
pyllarp.EnableDebug()
if not self.RemoveTmpDir():
raise RuntimeError("Failed to initialize Router Hive")
pyllarp.EnableDebug()
if not self.RemoveTmpDir():
raise RuntimeError("Failed to initialize Router Hive")
except Exception as error:
print("Exception in __init__: ", error);
def RemoveTmpDir(self):
if self.tmpdir.startswith("/tmp/") and len(self.tmpdir) > 5:
@ -55,6 +60,7 @@ class RouterHive(object):
makedirs("%s/nodedb" % dirname, exist_ok=True)
config = pyllarp.Config()
config.LoadDefault(True, dirname);
port = index + 30000
tunname = "lokihive%d" % index
@ -76,12 +82,16 @@ class RouterHive(object):
config.network.options = {"type": "null"}
config.links.addInboundLink("lo", AF_INET, port);
config.links.setOutboundLink("lo", AF_INET, port + 10000);
config.dns.options = {"local-dns": ("127.3.2.1:%d" % port)}
if index != 1:
config.bootstrap.routers = ["%s/relays/1/rc.signed" % self.tmpdir]
if index != 0:
config.bootstrap.routers = ["%s/relays/0/self.signed" % self.tmpdir]
config.api.enableRPCServer = False
print("adding relay at index %d" % port);
self.hive.AddRelay(config)
@ -90,8 +100,9 @@ class RouterHive(object):
makedirs("%s/nodedb" % dirname, exist_ok=True)
config = pyllarp.Config()
config.LoadDefault(False, dirname);
port = index + 40000
port = index + 50000
tunname = "lokihive%d" % index
config.router.dataDir = dirname
@ -102,9 +113,13 @@ class RouterHive(object):
config.network.routerProfilesFile = "%s/profiles.dat" % dirname
config.network.options = {"type": "null"}
config.links.setOutboundLink("lo", AF_INET, port + 10000);
config.dns.options = {"local-dns": ("127.3.2.1:%d" % port)}
config.bootstrap.routers = ["%s/relays/1/rc.signed" % self.tmpdir]
config.bootstrap.routers = ["%s/relays/0/self.signed" % self.tmpdir]
config.api.enableRPCServer = False
self.hive.AddClient(config)
@ -124,7 +139,7 @@ class RouterHive(object):
def InitFirstRC(self):
print("Starting first router to init its RC for bootstrap")
self.hive = pyllarp.RouterHive()
self.AddRelay(1)
self.AddRelay(0)
self.hive.StartRelays()
print("sleeping 2 sec to give plenty of time to save bootstrap rc")
sleep(2)

Loading…
Cancel
Save