Merge pull request #2034 from jagerman/fix-router-startup

Fix router startup
pull/2035/head
majestrate 2 years ago committed by GitHub
commit 4c9d076c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -395,6 +395,31 @@ namespace llarp
{
m_Config = std::move(c);
auto& conf = *m_Config;
// Do logging config as early as possible to get the configured log level applied
// Backwards compat: before 0.9.10 we used `type=file` with `file=|-|stdout` for print mode
auto log_type = conf.logging.m_logType;
if (log_type == log::Type::File
&& (conf.logging.m_logFile == "stdout" || conf.logging.m_logFile == "-"
|| conf.logging.m_logFile.empty()))
log_type = log::Type::Print;
if (log::get_level_default() != log::Level::off)
log::reset_level(conf.logging.m_logLevel);
log::clear_sinks();
log::add_sink(log_type, conf.logging.m_logFile);
enableRPCServer = conf.api.m_enableRPCServer;
// re-add rpc log sink if rpc enabled, else free it
if (enableRPCServer and llarp::logRingBuffer)
log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
else
llarp::logRingBuffer = nullptr;
log::debug(logcat, "Configuring router");
whitelistRouters = conf.lokid.whitelistRouters;
if (whitelistRouters)
{
@ -402,33 +427,39 @@ namespace llarp
m_lokidRpcClient = std::make_shared<rpc::LokidRpcClient>(m_lmq, weak_from_this());
}
enableRPCServer = conf.api.m_enableRPCServer;
if (enableRPCServer)
rpcBindAddr = oxenmq::address(conf.api.m_rpcBindAddr);
log::debug(logcat, "Starting RPC server");
if (not StartRpcServer())
throw std::runtime_error("Failed to start rpc server");
if (conf.router.m_workerThreads > 0)
m_lmq->set_general_threads(conf.router.m_workerThreads);
log::debug(logcat, "Starting OMQ server");
m_lmq->start();
_nodedb = std::move(nodedb);
m_isServiceNode = conf.router.m_isRelay;
log::debug(
logcat, m_isServiceNode ? "Running as a relay (service node)" : "Running as a client");
if (whitelistRouters)
{
m_lokidRpcClient->ConnectAsync(lokidRPCAddr);
}
// fetch keys
log::debug(logcat, "Initializing key manager");
if (not m_keyManager->initialize(conf, true, isSNode))
throw std::runtime_error("KeyManager failed to initialize");
log::debug(logcat, "Initializing from configuration");
if (!FromConfig(conf))
throw std::runtime_error("FromConfig() failed");
log::debug(logcat, "Initializing identity");
if (not EnsureIdentity())
throw std::runtime_error("EnsureIdentity() failed");
return true;
@ -601,6 +632,7 @@ namespace llarp
Router::FromConfig(const Config& conf)
{
// Set netid before anything else
log::debug(logcat, "Network ID set to {}", conf.router.m_netId);
if (!conf.router.m_netId.empty() && strcmp(conf.router.m_netId.c_str(), llarp::DEFAULT_NETID))
{
const auto& netid = conf.router.m_netId;
@ -640,16 +672,13 @@ namespace llarp
_ourAddress->setPort(*maybe_port);
else
throw std::runtime_error{"public ip provided without public port"};
log::debug(logcat, "Using {} for our public address", *_ourAddress);
}
else
log::debug(logcat, "No explicit public address given; will auto-detect during link setup");
RouterContact::BlockBogons = conf.router.m_blockBogons;
// Lokid Config
whitelistRouters = conf.lokid.whitelistRouters;
lokidRPCAddr = oxenmq::address(conf.lokid.lokidRPCAddr);
m_isServiceNode = conf.router.m_isRelay;
auto& networkConfig = conf.network;
/// build a set of strictConnectPubkeys (
@ -663,6 +692,7 @@ namespace llarp
throw std::runtime_error(
"Must specify more than one strict-connect router if using strict-connect");
strictConnectPubkeys.insert(val.begin(), val.end());
log::debug(logcat, "{} strict-connect routers configured", val.size());
}
std::vector<fs::path> configRouters = conf.connect.routers;
@ -681,58 +711,51 @@ namespace llarp
}
}
BootstrapList b_list;
bootstrapRCList.clear();
for (const auto& router : configRouters)
{
b_list.AddFromFile(router);
log::debug(logcat, "Loading bootstrap router list from {}", defaultBootstrapFile);
bootstrapRCList.AddFromFile(router);
}
for (const auto& rc : conf.bootstrap.routers)
{
b_list.emplace(rc);
bootstrapRCList.emplace(rc);
}
// in case someone has an old bootstrap file and is trying to use a bootstrap
// that no longer exists
for (auto rc_itr = b_list.begin(); rc_itr != b_list.end();)
{
if (rc_itr->IsObsoleteBootstrap())
b_list.erase(rc_itr);
else
rc_itr++;
}
auto verifyRCs = [&]() {
for (auto& rc : b_list)
auto clearBadRCs = [this]() {
for (auto it = bootstrapRCList.begin(); it != bootstrapRCList.end();)
{
if (rc.IsObsoleteBootstrap())
{
log::warning(logcat, "ignoring obsolete boostrap RC: {}", RouterID(rc.pubkey));
continue;
}
if (not rc.Verify(Now()))
if (it->IsObsoleteBootstrap())
log::warning(logcat, "ignoring obsolete boostrap RC: {}", RouterID{it->pubkey});
else if (not it->Verify(Now()))
log::warning(logcat, "ignoring invalid bootstrap RC: {}", RouterID{it->pubkey});
else
{
log::warning(logcat, "ignoring invalid RC: {}", RouterID(rc.pubkey));
++it;
continue;
}
bootstrapRCList.emplace(std::move(rc));
// we are in one of the above error cases that we warned about:
it = bootstrapRCList.erase(it);
}
};
verifyRCs();
clearBadRCs();
if (bootstrapRCList.empty() and not conf.bootstrap.seednode)
{
auto fallbacks = llarp::load_bootstrap_fallbacks();
if (auto itr = fallbacks.find(_rc.netID.ToString()); itr != fallbacks.end())
{
b_list = itr->second;
verifyRCs();
bootstrapRCList = itr->second;
log::debug(logcat, "loaded {} default fallback bootstrap routers", bootstrapRCList.size());
clearBadRCs();
}
if (bootstrapRCList.empty()
and not conf.bootstrap.seednode) // empty after trying fallback, if set
if (bootstrapRCList.empty() and not conf.bootstrap.seednode)
{
// empty after trying fallback, if set
log::error(
logcat,
"No bootstrap routers were loaded. The default bootstrap file {} does not exist, and "
@ -803,26 +826,6 @@ namespace llarp
hiddenServiceContext().AddEndpoint(conf);
}
// Logging config
// Backwards compat: before 0.9.10 we used `type=file` with `file=|-|stdout` for print mode
auto log_type = conf.logging.m_logType;
if (log_type == log::Type::File
&& (conf.logging.m_logFile == "stdout" || conf.logging.m_logFile == "-"
|| conf.logging.m_logFile.empty()))
log_type = log::Type::Print;
if (log::get_level_default() != log::Level::off)
log::reset_level(conf.logging.m_logLevel);
log::clear_sinks();
log::add_sink(log_type, conf.logging.m_logFile);
// re-add rpc log sink if rpc enabled, else free it
if (enableRPCServer and llarp::logRingBuffer)
log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
else
llarp::logRingBuffer = nullptr;
return true;
}

Loading…
Cancel
Save